Linux中的信号处理
来源:互联网 发布:二手车的价格怎么算法 编辑:程序博客网 时间:2024/04/28 03:02
突然想起使用sigaction注册信号处理函数时,信号处理函数类型有二个:
- struct sigaction
- {
- void (*sa_handler)(int);
- void (*sa_sigaction)(int, siginfo_t *, void *);
- sigset_t sa_mask;
- int sa_flags;
- void (*sa_restorer)(void);
- };
通过对上面的sa_flags赋值为SA_SIGINFO,让系统使用多参数的信号处理函数。在处理函数中,由于传进来一个siginfo_t的参数,我们可以通过它获取到哪个进程传进来的消息。
今天试了下,有点问题,在产生信号时,应当调用注册的信号处理函数,但此时,报出段错误,去除注册信号处理函数那行,一切OK。所以断定系统在调用信号处理函数时发生错误。网上查找相关资料也没有看到原因,故此记下来,继续寻找答案。(注:如果使用sa_handler,完全没有问题)
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <errno.h>
- #include <string.h>
- #include <stdio.h>
- #include <sys/types.h>
- void child_process_exit_handle(int signo,siginfo_t *info, void *context);
- int main(int argc, char **argv)
- {
- struct sigaction new_action , old_action;
- memset(&new_action,0,sizeof(new_action));
- memset(&old_action,0,sizeof(old_action));
- new_action.sa_sigaction = child_process_exit_handle;
- sigemptyset(&new_action.sa_mask);
- new_action.sa_flags = SA_SIGINFO;
- sigaction(SIGCHLD,&new_action,&old_action);
- puts("output before vfork!");
- pid_t child_pid = fork();
- if(child_pid < 0)
- {
- printf(strerror(errno));
- return -1;
- }
- else if(child_pid == 0)
- {
- printf("In the Child Process!/n");
- _exit(0);
- }
- printf("Do nothing!/n");
- int ch = 0x00;
- while(ch = getchar(),ch != 'q'){}
- sigaction(SIGCHLD,&old_action,&new_action);
- return 1;
- }
- void child_process_exit_handle(int signo, siginfo_t *info, void *context)
- {
- int state;
- printf("child process %d exit!/n",info->si_pid);
- pid_t pid = waitpid(info->si_pid,&state,0);
- if(pid < 0 )
- printf("Wait process exit failured!/n");
- }
- Linux中的信号处理
- linux内核中的信号机制--信号处理
- Linux中的信号处理原理
- linux中的信号处理机制
- Linux中的信号处理函数
- linux系统中的信号处理
- linux 信号&信号处理
- linux 信号&信号处理
- linux 信号&信号处理
- linux中的信号处理(signal和alarm)
- linux中的信号处理(signal和alarm)
- linux中的信号处理(signal和alarm)
- linux中的信号处理(signal和alarm)
- linux中的信号处理(signal和alarm)
- linux中的信号处理(signal和alarm)
- Linux 多线程应用中的信号处理
- linux中的信号处理(signal和alarm)
- linux中的信号处理(signal和alarm)
- 简单转换宽窄字符
- JAVA
- RCP基础总结
- autohotkey源文件乱码解决方法
- Google C++ Style
- Linux中的信号处理
- 实习感悟(一)
- 清朝如何选秀女
- 能在水上“散步”的动物
- 动态链接库与静态链接库
- Barry生活日记(四)
- Coding Help 编码帮助系统
- Linux的Shell编程之shell基本语法
- 关于>&2、2>&1等重定向