2014-11-24 2 views
0

C 언어에서 두 프로세스를 생성하고 파이프를 통해 연결하는 프로그램을 작성하십시오.SIGUSR1 - C에서 sigaction 제거

첫 번째 자손은 'stdout을 파이프로 리디렉션하고 (공간 분리 된) 난수 쌍 (함수 rand)을 씁니다. 숫자의 출력을 1 초 지연시킵니다.

두 번째 자손은 파이프 출력을 stdin으로 리디렉션하고 stdout을 현재 디렉토리의 out.txt라는 파일로 리디렉션합니다.

상위 프로세스가 5 초 동안 기다린 다음 SIGUSR1을 첫 번째 프로세스 (번호 생성기)로 보냅니다. 이것은 두 프로세스 모두의 올바른 종료를 수행해야합니다. 하위 프로세스가 종료 (대기 함수)하고 자체 종료 될 때까지 대기합니다.

정말 도움이 필요합니다 : 첫 번째 자손은 SIGUSR1 신호 (sigaction 기능)를 처리해야하며 그러한 신호를받는 경우 stderr에 "TERMINATED"문자열을 인쇄하고 종료합니다.

FILE *file; 
file = fopen(NAZEV, "a+"); 

int pipefd[2]; 
pipe(pipefd); 

pid_t pid1; 
int retcode; 
pid1=fork(); 
if(pid1 == 0) // child 1 
{ 
    close(roura[0]); 
    printf("child1...\n"); 
    dup2(roura[1], STDOUT_FILENO); 

    int i = 0; 

    while(i < 6) 
    { 
     i++; 
     int a = rand(); 
     int b = rand(); 
     sleep(1); 
     printf("%d %d\n", a, b); 
    } 
    close(roura[1]); 
    exit(45); 


} 
else if (pid1 < 0) 
{ 
    printf("Fork selhal\n"); 
    exit(2); 
} 
else 
{ 
    pid_t pid2; 
    pid2 = fork(); 
    if (pid2 == 0) //child 2 
    { 
     close(roura[1]); 
     dup2(roura[0], STDIN_FILENO); 

     printf("child2...\n"); 
     int i = 0; 
     while(i < 5) 
     { 
      i++; 
      int c; 
      int d; 
      scanf("%d %d", &c, &d); 
      printf("%d %d\n", c, d); 
      fprintf(file,"%d %d\n", c, d); 
     } 

     printf("child2 end\n"); 
     exit(0); 
    } 
    else if (pid2 < 0) 
    { 
     printf("Fork error\n"); 
     exit(2); 
    }else 
    { 
    sleep(5); 
    kill(pid1, SIGUSR1); 
    wait(&pid1); //wait for child 1 
    wait(&pid2); //wait for child 2 
    printf("parent end\n"); 
    exit(0); 
    } 
} 
exit(0); 

}

+2

이것이 C 인 경우, 왜 C++ 태그가 필요합니까? (C! = C++은 * 많은 * "native"솔루션에 대해) – crashmstr

답변

0

stderr로 출력하고 종료하는 sigusr1에 신호 처리기를 추가하십시오. cygwin에서 컴파일하기 위해 다음을 시도하십시오.

#include <stdio.h> 
    #include <signal.h> 
    #include <stdio.h> 
    #include <stdlib.h> 
    #ifndef STDIN_FILENO 
    # define STDIN_FILENO 0 
    #endif 
    #ifndef STDOUT_FILENO 
    # define STDOUT_FILENO 1 
    #endif 

    void sig_handler(){ 
     fprintf(stderr,"TERMINATED"); 
     exit(0); 
    } 

    void main(int argc, char ** argv){ 
     FILE *file;  
     file = fopen("NAZEV", "a+"); 

     int pipefd[2]; 
     int roura[2] ; 
     pipe(pipefd); 

     pid_t pid1; 
     int retcode; 
     pid1=fork(); 
     if(pid1 == 0) // child 1 
     { 
      close(roura[0]); 
      printf("child1...\n"); 
      dup2(roura[1], STDOUT_FILENO); 
      if (signal(SIGUSR1, sig_handler) == SIG_ERR){ 
       printf("\ncan't catch SIGUSR1\n"); 
       exit(13); 
      }  
      int i = 0; 

      while(i < 6) 
      { 
       i++; 
       int a = rand(); 
       int b = rand(); 
       sleep(1); 
       printf("%d %d\n", a, b); 
      } 
      close(roura[1]); 
      exit(45); 


     } 
     else if (pid1 < 0) 
     { 
      printf("Fork selhal\n"); 
      exit(2); 
     } 
     else 
     { 
      pid_t pid2; 
      pid2 = fork(); 
      if (pid2 == 0) //child 2 
      { 
       close(roura[1]); 
       dup2(roura[0], STDIN_FILENO); 

       printf("child2...\n"); 
       int i = 0; 
       while(i < 5) 
       { 
        i++; 
        int c; 
        int d; 
        scanf("%d %d", &c, &d); 
        printf("%d %d\n", c, d); 
        fprintf(file,"%d %d\n", c, d); 
       } 

       printf("child2 end\n"); 
       exit(0); 
      } 
      else if (pid2 < 0) 
      { 
       printf("Fork error\n"); 
       exit(2); 
      }else 
      { 
      sleep(5); 
      kill(pid1, SIGUSR1); 
      wait(&pid1); //wait for child 1 
      wait(&pid2); //wait for child 2 
      printf("parent end\n"); 
      exit(0); 
      } 
     } 
     exit(0); 
    } 
0

당신은 당신이 기본 동작을 재정의하려는 경우 sigaction를 사용하여 신호 처리기를 등록해야합니다. SIGUSR1의 경우, 기본 조치는 프로세스를 종료하는 것입니다.

+0

어떻게 알지는 못합니다 –

+0

이것은 [sigaction (2)'매뉴얼] (http://linux.die.net/man/2)에 설명되어 있습니다./sigaction). 터미널에서'man sigaction '을 입력하여 그것을 볼 수 있습니다. – user3426575

관련 문제