2010-12-22 7 views
6

저는 운영 체제 과정을위한 과제로 프로그램 모니터를 작성하고 있습니다 (소개는 아주 기본적인 것입니다).프로그램이 신호를 통해 실행을 종료했는지 어떻게 알 수 있습니까?

모니터가해야 할 일 중 하나는 "자연적인 원인"또는 종료 원인 인 신호 코드로 끝나는 경우 모니터하고있는 프로그램의 종료 코드를 표시하는 것입니다.

지금은 아이의 실행이 끝나고 종료 코드가 캡처 될 때까지 기다리는 중입니다.

pid_t id = -1; 
switch (id = fork()) { 
    // Error when forking: 
    case -1: 
     error(-1, "Something went wrong when forking."); 
     exit(-1); 
    // Code for the child process: 
    case 0: 
     // Just launch the program we're asked to: 
     execvp(argv[2], &argv[2]); 
     // If reached here it wasn't possible to launch the process: 
     error(1, "Process could not be launched."); 
     exit(1); 
    // Code for the parent process: 
    default: 
     // Just wait for the child to finish its execution: 
     wait(&return_value); 
} 

error(2) 오류가 발생 할 때마다 바로 코드를 단순화하기 위해, 로거 기능입니다 : 이것은 관련 코드입니다.

내가 가지고있는 프로세스가 서로 다른 진술을 표시하는 방법에 따라 : X가 종료 코드 또는 신호가 수신 될

Process ended: X 

또는

Process terminated with signal X. 

. 신호로 인해 자식 프로세스가 종료되었는지 어떻게 알 수 있습니까? wait(2)에서

답변

5

는 :

WIFSIGNALED(status) 
      returns true if the child process was terminated by a signal. 
    WTERMSIG(status) 
      returns the number of the signal that caused the child process to terminate. 

그래서 당신은 WIFSIGNALED(return_value)를 확인해야하고 그것이 사실 인 경우, WTERMSIG(return_value)을 확인합니다.

+0

감사합니다. 매뉴얼에서 찾았어야했는데 ... 미안해. – Johannes

관련 문제