자식 프로세스에서 부모 프로세스를 죽이는 방법이 있을까요?

nfsp3k의 이미지

멀티 채팅프로그램을 만들고 있습니다.
서버가 있고 여러개의 클라이언트들이 서버에 접속하여 1:1로 채팅을 할 수 있는 프로그램입니다.

접속한 클라이언트간 채팅방이 만들어져
1:1로 채팅을 시작하게 되면,
각각의 클라이언트는 fork를 통해 두 개의 프로세스로 나누어 집니다.

부모 프로세스는 사용자로부터 입력한 내용을 서버로 write하는 역할을,
자식 프로세서는 서버를 통하여 건너온 상대 클라이언트의 메세지를 read하여 화면에 출력하는 역할을 합니다.

여기서 제가 구현하고 싶은 기능은
두 클라이언트 중 어느 한명이라도 quit을 입력하면
둘 다 채팅방에서 나가게 되는 기능을 하고 싶은데요.

quit을 입력한 클라이언트 같은 경우,
부모 프로세스가 자식프로세스를 kill하면서 프로그램을 종료시킬 수가 있는데요.
상대 클라이언트의 경우, 자식이 상대방의 메세지를 확인하기 때문에 부모를 kill 할 수가 없는 상황입니다.

getppid() 함수를 이용하여 부모의 pid를 받아와

t=getppid();
kill(t, SIGQUIT);

하는 식으로 구현하였는데
부모는 죽지 않네요 절대로ㅜ

무림의 고수님들의 조언과 충고 부탁드리겠습니다. 꾸벅

아래는 저의 소스코드입니다.
====================================================================================
quit을 입력하면 정말로 끝낼건지 다시 한번 물어봅니다.
이때도 yes라고 대답하면, 서버에 #12071126#이라는 메세지를 전달합니다.

그리하여 상대 클라이언트는 서버로부터 #12071126#이라는 메세지를 전달받습니다.
====================================================================================

void talk(int x){
char buf[50];
int j,y;
j=fork();
if(j==0){
int t=getppid();
while(1){
y=read(x,buf,50);
buf[y]=0;
if( !strcmp(buf, "#12071126#") ){
printf("\n # The other gets out #\n");
break;
}else
printf("(%d)%s\n", t,buf);
}
kill(getppid(), SIGQUIT);
}else{
while(1){
gets(buf);
if(!strcmp(buf,"quit") || !strcmp(buf,"QUIT")){
printf(" # Really? (1=YES/2=NO) ");
scanf("%d", &y);
if(y==1){
write(x, "#12071126#", 10);
printf("\n");
break;
}
}
write(x, buf, strlen(buf));
}
kill(j, SIGQUIT);
}
}

익명 사용자의 이미지

child: read "quit" -> exit

parent:
while(1)
{
read input ;
send input ;
if(waitpid(-1, NULL, WNOHANG) > 0) break ;
}

gurugio의 이미지

int main(int argc, char **argv)
{
    int pid;
    pid_t t;
    char buf[100];
 
    pid = fork();
    if (pid > 0)
    {
        printf("부모 프로세스 %d : %d\n", getpid(), pid);
 
        gets(buf);
    }
    else if (pid == 0)
    {
        printf("자식 프로세스 %d\n", getpid());
        sleep(1);
        t=getppid();
        kill(t, SIGQUIT);
    }
    else if (pid == -1)
    {
        perror("fork error : ");
        exit(0);
    }
 
    printf("child terminated\n");
    return 0;
}
<code>
 
이렇게 테스트를해봤는데 잘 죽습니다.
다른 코드 어딘가에서 시그널을 처리하지 못하는게 아닐까요?
SIGKILL은 무조건 죽으니까 SIGKILL로 한번 해보시면 어떨까요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.