2011-05-05 116 views

回答

3

註冊搭上信號:

void termination_handler(int sig) 
{ 
    /* do something */ 
} 

struct sigaction handler; 

handler.sa_handler = termination_handler; 
sigemptyset (&handler.sa_mask); 
handler.sa_flags = SA_RESTART; 

sigaction(SIGTERM, &handler, NULL); 

這裏是一個很好example page

你可以使用舊的風格,但不建議:

void termination_handler() 
{ 
    /* do something */ 
} 

signal(SIGTERM, termination_handler); 
+0

權。這工作正常,我使用「信號」。在事件中,我向特定的PID發送kill命令,因爲該過程如何確定信號是按照我的方式發送的? – JAM 2011-05-05 03:46:30

+1

你幾乎肯定希望'handler.sa_flags = SA_RESTART;'。非重新啓動信號會嚴重破壞不期望得到'EINTR'且不處理它的代碼。 – 2011-05-05 04:04:57

+0

@R ..好點。修改爲反映 – pickypg 2011-05-05 04:20:02