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);
- }

Linux进程间通信——使用信号的更多相关文章
- [转]Linux进程间通信——使用信号
转载于:http://blog.csdn.net/ljianhui/article/details/10128731 经典!!! Linux进程间通信——使用信号 一.什么是信号 用过 ...
- 练习--LINUX进程间通信之信号SIGNAL
同样的,信号也不要太迷信可靠信号及不及靠信号,实时或非实时信号. 但必须要了解这些信号之间的差异,函数升级及参数,才能熟练运用. ~~~~~~~~~~~~~~~~ 信号本质 信号是在软件层次上对中断机 ...
- Linux进程间通信(三) - 信号
什么是信号 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.在软件层次上是对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是进程间通 ...
- Linux进程间通信方式--信号,管道,消息队列,信号量,共享内存
1.概述 通信方法 无法介于内核态与用户态的原因 管道(不包括命名管道) 局限于父子进程间的通信. 消息队列 在硬.软中断中无法无阻塞地接收数据. 信号量 无法介于内核态和用户态使用. 内存共享 需要 ...
- linux进程间通信之信号
1.wait()函数 原型:pid_t wait(int *status) 子进程退出时,它向父进程发送一个SIGCHLD信号,默认情况是总是忽略SIGCHLD信号,此时进程状态一直保留在内存中,因 ...
- Linux进程间通信——使用信号量
这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:Linux进程间通信——使用信号.下面 ...
- Linux进程间通信--使用信号量【转】
本文转载自:http://blog.csdn.net/ljianhui/article/details/10243617 这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号 ...
- Linux进程间通信——使用信号量(转)
这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:Linux进程间通信——使用信号.下面 ...
- Linux进程间通信——使用信号量【转】
本文转载自:http://blog.csdn.net/ljianhui/article/details/10243617 这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号 ...
随机推荐
- 在线程中建立Form遇到的问题
一个项目由很多Form组成,默认情况下在启动程序时,这些form都会被建立,这会黑屏很长时间,一种方法是用到Form时再建立,结果又发现如果Form设计复杂,建立的过程也会超过1秒以上,于是想到用线程 ...
- redis的安装与配置
官网 http://redis.io/download 管理工具 http://docs.redisdesktop.com/en/latest/quick-start/ https://redisde ...
- winform datagridview 添加行号。
private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { private ...
- TCP Keepalive HOWTO
TCP Keepalive HOWTO Fabio Busatto <fabio.busatto@sikurezza.org> 2007-05-04 Revision History Re ...
- 基于jeasyui的遮罩扩展[修复链式bug]
说明和使用方法看下面代码,直接复制下面代码保存为js文件,引用即可. 遮罩效果从datagrid中提取,针对jquery进行优化. 下载地址(附Demo):http://pan.baidu.com/s ...
- LeeCode(Database)-Customers Who Never Order
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...
- |,&,<<,>>运算符
<< 位移运算符(>>相反了) /* * 题目: 2 << 3 = 10000 = 16 * 解答: 2向左移动三位,就变成了10000 * 十进制 二进制 * 2 ...
- telnet如何操作Memcached缓存系统?
4.(1)telnet操作Memcached 许多语言都实现了连接memcached的客户端,其中以Perl.PHP为主.仅仅memcached网站上列出的语言就有:• Perl • PHP • Py ...
- strcpy与memcpy的区别
strcpy和memcpy的区别 strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制 ...
- [KMP求最小循环节][HDU1358][Period]
题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...