fork에서 signal 처리 질문입니다.

craftwar의 이미지

fork로 자식 프로세스들을 생성한 뒤에
거기서 나오는 pid값들을 따로 저장해 두고, 자식들은 일단 pause()로 대기 하고 있다가
부모가 signal을 주면 그때부터 수행하다가, 다시 부모가 signal을 주면 또 대기하고....

이런 코드를 짜고 싶은데요.

#include <stdio.h>
#include <signal.h>
 
void sigcatch(int sig){
    signal(sig,SIG_IGN);
}
 
void sigcatch2(int sig){
    signal(sig, SIG_IGN);
    pause();
    printf("pause success\n");
}
 
main(){
    int pid,fork(), wait();
    int i;
    void exit();
 
    signal(SIGALRM, sigcatch);
    signal(SIGUSR1, sigcatch2);
    if((pid=fork()==0)){    //child
        pause();            //자식은 생성되자 마자 pause()를 걸어서 대기 시켰습니다.
 
        for(i=0;i<7;i++){
         printf("child : %d\n",i);
            sleep(1);
            if(i==4)                   //1초에 숫자 하나씩 출력하다가 4를 출력하고나서 다시 pause
                pause();
        }
        exit(1);
    }
    printf("Waiting for Child\n");
    for(i=0;i<3;i++){
        printf("parent : %d\n",i);
        sleep(1);
    }
    kill(pid,SIGALRM);
    printf("kill SIGALRM\n");
    sleep(7);
 
    kill(pid,SIGALRM);
    printf("kill SIGUSR1\n");
 
 
    kill(pid,SIGALRM);
    wait(NULL);
 
    printf("wait child\n");
}

소스코드는 위와 같은데요.

출력 결과는

Waiting for Child
parent : 0
parent : 1
parent : 2
kill SIGALRM
child : 0
child : 1
child : 2
child : 3
child : 4
kill SIGALRM

저기서 끝이 납니다.

4까지 출력 하고 자식이 pause를 했으니, 부모에서 다시 SIGALRM을 주면
응당 5,6,7 까지 나오고 프로그램이 종료되야 하는데,

저렇게 SIGALRM을 줬다는 메세지만 뜨고 계속 멈춰 있습니다.

fork와 signal 부분은 이해하기 너무너무 어려운것 같습니다 ㅠㅠ

소스코드 길어 보이지만 별 내용 없으니 한번만 도와주세요.

bacon의 이미지

sigcatch 에서 signal(sig,SIG_IGN);한후에 복구를 하지 않았네요. 두번째 시그널 부터는 먹통이 되겠네요. sigcatch에서 빠져나오기 전에 signal (sig, sigcatch)하시면 될듯... sigcatch2에서도 필요하다면 같은 작업을 하시고요.

craftwar의 이미지

감사합니다!
저도 언젠가 답변을 달 수 있는 실력이 되길 바라며! ㅋㅋ

SoulreaveR의 이미지

signal 보다는 sigaction을 추천합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.