#include "../csapp.h"
volatile int counter = 0;
void handler(int sig)
{
counter++;
sleep(1); //do some work in the handler
return;
}
int main()
{
int i;
Signal(SIGUSR2, handler);
if(Fork() == 0) {
for(i = 0; i < 10; i++){
Kill(getppid(), SIGUSR2);
printf("sent SIGUSR2 to parent \n");
}
exit(0);
}
Wait(NULL);
printf("counter = %d\n", counter);
exit(0);
}
$ ./counterprob
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
sent SIGUSR2 to parent
counter = 2
答案似乎是不能的,原因是信號是不排隊的,只能同時存在一個pending的信號,一個正在處理,後來的都會被cancel 掉
程序執行圖:
SIGUSR2 SIGUSR2 SIGUSR2 SIGUSR2 SIGUSR2
| | | | |
being handled Pending Canceld Canceld Canceld
need 1 sec