IPC——信号
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()
- {
- printf("Hello World!\n");
- sleep();
- }
- return ;
- }

- #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, );
- while()
- {
- printf("Hello World!\n");
- sleep();
- }
- return ;
- }
- #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 = ;
- void ouch(int sig)
- {
- alarm_fired = ;
- }
- int main()
- {
- pid_t pid;
- pid = fork();
- switch(pid)
- {
- case -:
- perror("fork failed\n");
- exit();
- case :
- //子进程
- sleep();
- //向父进程发送信号
- kill(getppid(), SIGALRM);
- exit();
- default:;
- }
- //设置处理函数
- signal(SIGALRM, ouch);
- while(!alarm_fired)
- {
- printf("Hello World!\n");
- sleep();
- }
- if(alarm_fired)
- printf("\nI got a signal %d\n", SIGALRM);
- exit();
- }

- #include <unistd.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- static int alarm_fired = ;
- void ouch(int sig)
- {
- alarm_fired = ;
- }
- int main()
- {
- //关联信号处理函数
- signal(SIGALRM, ouch);
- //调用alarm函数,5秒后发送信号SIGALRM
- alarm();
- //挂起进程
- pause();
- //接收到信号后,恢复正常执行
- if(alarm_fired == )
- printf("Receive a signal %d\n", SIGALRM);
- exit();
- }

Linux进程间通信——信号集函数
- #include <unistd.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <stdio.h>
- void handler(int sig)
- {
- printf("Handle the signal %d\n", sig);
- }
- int main()
- {
- sigset_t sigset;//用于记录屏蔽字
- sigset_t ign;//用于记录被阻塞的信号集
- struct sigaction act;
- //清空信号集
- sigemptyset(&sigset);
- sigemptyset(&ign);
- //向信号集中添加信号SIGINT
- sigaddset(&sigset, SIGINT);
- //设置处理函数和信号集
- act.sa_handler = handler;
- sigemptyset(&act.sa_mask);
- act.sa_flags = ;
- sigaction(SIGINT, &act, );
- printf("Wait the signal SIGINT...\n");
- pause();//挂起进程,等待信号
- //设置进程屏蔽字,在本例中为屏蔽SIGINT
- sigprocmask(SIG_SETMASK, &sigset, );
- printf("Please press Ctrl+c in 10 seconds...\n");
- sleep();
- //测试SIGINT是否被屏蔽
- sigpending(&ign);
- if(sigismember(&ign, SIGINT))
- printf("The SIGINT signal has ignored\n");
- //在信号集中删除信号SIGINT
- sigdelset(&sigset, SIGINT);
- printf("Wait the signal SIGINT...\n");
- //将进程的屏蔽字重新设置,即取消对SIGINT的屏蔽
- //并挂起进程
- sigsuspend(&sigset);
- printf("The app will exit in 5 seconds!\n");
- sleep();
- exit();
- }

IPC——信号的更多相关文章
- liunx中的进程与线程
1. 进程和线程 进程和线程是程序运行时状态,是动态变化的,进程和线程的管理操作(比如,创建,销毁等)都是有内核来实现的. Linux中的进程于Windows相比是很轻量级的,而且不严格区分进程和线程 ...
- Linux 2.4调度系统分析--转
http://www.ibm.com/developerworks/cn/linux/kernel/l-k24sch/index.html 杨沙洲 (pubb@163.net)国防科技大学计算机学院 ...
- linux下使用nmon工具对服务器性能进行检测
1.nmon工具介绍: nmon工具是linux系统下可以对服务器及系统性能进行监测,CPU信息.CPU占用.内存使用.网卡使用等.最大的好处是此工具会将结果以列表的形式或者是模拟图形化的方式展示,不 ...
- 【读书笔记】《Linux内核设计与实现》进程管理与进程调度
大学跟老师做嵌入式项目,写过I2C的设备驱动,但对Linux内核的了解也仅限于此.Android系统许多导致root的漏洞都是内核中的,研究起来很有趣,但看相关的分析文章总感觉隔着一层窗户纸,不能完全 ...
- 《Linux内核设计与实现》第三章学习笔记
第三章 进程管理 姓名:王玮怡 学号:20135116 一.进程 1.进程的含义 进程是处于执行期的程序以及相关资源的总称,程序本身并不是进程,实际上就是正在执行的代码的实时结果.Linux内核通 ...
- 《linux内核设计与实现》第三章
1.进程 进程就是正在执行的程序代码的实时结果,不仅包含可执行代码,还包括其他资源,比如:打开的文件,挂起的信号,内核内部数据结构,处理器状态,一个或多个具有内存映射的内存地址空间及一个或多个执行线程 ...
- 《Linux内核设计与实现》 第三章学习笔记
一.进程 1.进程就是处于执行期的程序(目标码存放在某种存储介质上).但进程并不仅仅局限于一段可执行程序代码,通常进程还要包含其他资源.执行线程,简称线程(thread),是在进程中活动的对象. 2. ...
- Linux内核设计与实现 第三章
1. 进程和线程 进程和线程是程序运行时状态,是动态变化的,进程和线程的管理操作都是由内核来实现的. Linux中的进程于Windows相比是很轻量级的,而且不严格区分进程和线程,线程不过是一种特殊的 ...
- (笔记)Linux内核学习(二)之进程
一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是线程而不是进程.对 ...
随机推荐
- Fedora20 和ubuntu 14.04 chrome标签中文乱码
作为两个流行的桌面发行版本,Fedora和ubuntu最新版本都存在chrome标签中文乱码问题. 下面是解决办法,都来自百度贴吧. 1.ubuntu 系列: 解决办法就是: 编辑/etc/fonts ...
- erlang observer工具
1.服务器安装wxWidgets,之前需要装gtk+库 2.客户端安装otp_win64_17.5.exe 3.快捷方式点属性,在D:\erl6.4\bin\werl.exe后面加上参数 -setco ...
- Hadoop的分布模式安装
1.确定集群的结构 IP(主机名) 角色 192.168.1.220(hadoop0) NameNode.JobTracker 192.168.1.221(hadoop1) SecondaryNa ...
- 阿里云开放服务oss的api
http://imgs-storage.cdn.aliyuncs.com/help/oss/OSS_API_20131015.pdf?spm=5176.383663.5.23.JQjiIK&f ...
- T-SQL 变量
T-SQL变量 变量的种类: 在T-SQL中,变量按生存范围可以分为全局变量(Global Variable)和局部变量(Local Variable) 1.全局变量是由系统定义的,在整个SQL Se ...
- VISA资源名称控件
NI-VISA能自动检测端口.通过前面板上的VISA资源名称控件或VISA查找资源函数可查看端口列表.在任何平台上,NI-VISA支持的最大串口数量为256,串口的默认数量取决于操作系统. VISA资 ...
- Codeforces 467C. George and Job (dp)
题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...
- 配置Synergy(Server : XP, client: Win7)
此文只是为了Mark一下配置方法,以防以后重装系统的时候,忘记. 首先,因为我的Server机器是XP,所以要求两台机器,都是安装的x86的版本,而不能是x64的版本. 我用的版本是1.4.11, ...
- Mysql自增字段
1.关键字 auto_increment 2.自增用法 例: CREATE TABLE animals ( id mediumint not null auto_increment, name cha ...
- keycode按键对照表
功能场景,鼠标在某区域内,比如多个条件的搜索框内,按下enter键让其具有,点击 [确定/搜索]按钮的功能.在编辑的区域内,点击enter键,让其有 [保存]按钮的功能.则可这样:$("#s ...