Linux进程间通信——信号
- #include <signal.h>
- void (*signal(int sig, void (*func)(int)))(int);
- #include <signal.h>
- #include <stdio.h>
- #include <unistd.h>
- void ouch(int sig)
- {
- printf("\nOUCH! - I got signal %d\n", sig);
- //恢复终端中断信号SIGINT的默认行为
- (void) signal(SIGINT, SIG_DFL);
- }
- int main()
- {
- //改变终端中断信号SIGINT的默认行为,使之执行ouch函数
- //而不是终止程序的执行
- (void) signal(SIGINT, ouch);
- while(1)
- {
- printf("Hello World!\n");
- sleep(1);
- }
- return 0;
- }
- #include <signal.h>
- int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
- #include <unistd.h>
- #include <stdio.h>
- #include <signal.h>
- void ouch(int sig)
- {
- printf("\nOUCH! - I got signal %d\n", sig);
- }
- int main()
- {
- struct sigaction act;
- act.sa_handler = ouch;
- //创建空的信号屏蔽字,即不屏蔽任何信息
- sigemptyset(&act.sa_mask);
- //使sigaction函数重置为默认行为
- act.sa_flags = SA_RESETHAND;
- sigaction(SIGINT, &act, 0);
- while(1)
- {
- printf("Hello World!\n");
- sleep(1);
- }
- return 0;
- }
- #include <sys/types.h>
- #include <signal.h>
- int kill(pid_t pid, int sig);
- #include <unistd.h>
- unsigned int alarm(unsigned int seconds);
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- static int alarm_fired = 0;
- void ouch(int sig)
- {
- alarm_fired = 1;
- }
- int main()
- {
- pid_t pid;
- pid = fork();
- switch(pid)
- {
- case -1:
- perror("fork failed\n");
- exit(1);
- case 0:
- //子进程
- sleep(5);
- //向父进程发送信号
- kill(getppid(), SIGALRM);
- exit(0);
- default:;
- }
- //设置处理函数
- signal(SIGALRM, ouch);
- while(!alarm_fired)
- {
- printf("Hello World!\n");
- sleep(1);
- }
- if(alarm_fired)
- printf("\nI got a signal %d\n", SIGALRM);
- exit(0);
- }
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- static int alarm_fired = 0;
- void ouch(int sig)
- {
- alarm_fired = 1;
- }
- int main()
- {
- //关联信号处理函数
- signal(SIGALRM, ouch);
- //调用alarm函数,5秒后发送信号SIGALRM
- alarm(5);
- //挂起进程
- pause();
- //接收到信号后,恢复正常执行
- if(alarm_fired == 1)
- printf("Receive a signal %d\n", SIGALRM);
- exit(0);
- }
转自网络
转载于:https://www.cnblogs.com/sky0917/p/3864058.html
Linux进程间通信——信号相关推荐
- Linux进程间通信--信号
signal 一.初步理解信号 为了理解信号 ,先从我们最熟悉的场景说起: 1.用户输入命令,在Shell下启动一个前台进程. 2.用户按下Ctrl-C,这个键盘输入产生一个硬件中断. 3.如果CPU ...
- Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存 参考:<linux编程从入门到精通>,<Linux C程序设计大全>,<unix环境高级编程> ...
- Linux进程间通信(二):信号集函数 sigemptyset()、sigprocmask()、sigpending()、sigsuspend()...
我们已经知道,我们可以通过信号来终止进程,也可以通过信号来在进程间进行通信,程序也可以通过指定信号的关联处理函数来改变信号的默认处理方式,也可以屏蔽某些信号,使其不能传递给进程.那么我们应该如何设定我 ...
- Linux进程间通信(管道、消息队列、共享内存、信号、信号量)
目录 Linux进程间通信概述 1.管道 无名管道(pipe) 有名管道(fifo) 2.消息队列(msg) 消息队列的通信原理 消息队列相关api 消息队列收发数据 键值生成 消息队列移除 3.共享 ...
- linux 进程间通信及makefile 无名管道/有名管道/共享内存/信号/消息队列
http://www.voidcn.com/article/p-hxvuiypm-mr.html https://www.cnblogs.com/wuyida/archive/2013/02/03/6 ...
- Linux进程间通信第三讲 信号signal kill
目录 三.信号(signal) 3.1 概念 3.2 信号的处理 3.3 信号的发送 3.4 信号的屏蔽 三.信号(signal) 3.1 概念 信号本质上是一种软件中断 软件触发的中断.和硬件的处理 ...
- linux进程间通信:POSIX 消息队列 ----异步通信
在上一篇中linux进程间通信:POSIX 消息队列我们知道消息队列中在消息个数达到了队列所能承载的上限,就会发生消息的写阻塞. 阻塞式的通信影响系统效率,进程之间在通信收到阻塞时并不能去做其他事情, ...
- linux进程间通信:system V 信号量 生产者和消费者模型编程案例
生产者和消费者模型: 有若干个缓冲区,生产者不断向里填数据,消费者不断从中取数据 两者不冲突的前提: 缓冲区有若干个,且是固定大小,生产者和消费者各有若干个 生产者向缓冲区中填数据前需要判断缓冲区是否 ...
- linux kill信号列表
linux kill信号列表 $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6 ...
最新文章
- ubuntu系统php环境变量设置,Ubuntu系统环境变量详解
- geojson地图_ThingJS的三维城市地图搭建逻辑解析
- 2019春第六周作业Compile Summarize
- 云计算网络基础第八天
- Python基础教程:对象及数字对象与数学运算
- 数组转换成json key-value形式
- 一个AI设计的思考过程(旧文)
- 7.组件连线(贝塞尔曲线)--从零起步实现基于Html5的WEB设计器Jquery插件(含源码)...
- 【转】修改static控件背景颜色和文字颜色
- 大数据之-Hadoop3.x_MapReduce_编程规范---大数据之hadoop3.x工作笔记0086
- SSH远程管理、参数讲解、xshell使用、scp,sftp,ssh命令(ssh两种方式的密钥验证方...
- serialize java_java serialize 浅谈
- Java基础之不一样的方法重载!
- Cadence安装教程(亲测记录)
- 计算机桌面无法新建文件夹,桌面新建的文件、文件夹都无法显示
- python django项目创建步骤
- 游戏测试面试总结(网易雷火、飞鱼科技、冰川网络、完美世界、搜狐畅游)
- WRF4.2安装过程全记录
- CJT长江连接器A2005系列线对板连接器排针排母PCB封装库
- ubuntu软件的卸载