- 论坛徽章:
- 1
|
可以用sigaction
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <signal.h>
- typedef void (*SIGFUNC)(int, siginfo_t *, void *);
- int Signal(int signum, SIGFUNC func)
- {
- struct sigaction newact, oldact;
- newact.sa_handler = NULL;
- newact.sa_sigaction = func;
- sigemptyset(&newact.sa_mask);
- newact.sa_flags = SA_SIGINFO;
- sigaction(signum, &newact, &oldact);
- return 0;
- }
- void on_kill(int signum, siginfo_t *sig_info, void *arg)
- {
- printf("process id:%d\n", sig_info->si_pid);
- }
- int main()
- {
- pid_t pid;
- Signal(SIGCHLD, on_kill);
- if((pid = fork()) == 0)
- {
- printf("child process id:%d!\n", getpid());
- } else if(pid<0)
- {
- printf("error\n");
- }
- else
- {
- while(1) sleep(1);
- }
- }
复制代码 |
|