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内核学习(二)之进程
一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是线程而不是进程.对 ...
随机推荐
- 详解WPF Blend工具中的复合路径功能 ( 含路径标记语法 )
写此文章的目的是为了简单分析一下 Blend工具中提供的"复合路径"功能.有人在我的博文中留言问我复合路径的问题. 稍微琢磨一下,觉得应该是对的.因此贴出来和大家分享.有不对的说 ...
- asp.net mvc下文件上传
典型的文件上传表单 <form action="/File" enctype="multipart/form-data" method="pos ...
- JavaEE5 Tutorial_Jsp,EL
Jsp的各种元素在转化为servlet时处理是不一样的:指令,控制web容器如何处理页面脚本,被插入到生成的servlet里EL表达式,作为参数传递到解析器get/set Property,变成方 ...
- [POJ] #1008# Maya Calendar : 字符处理/同余问题
一. 题目 Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74085 Accepted: 2 ...
- Principles of good RESTful API Design 好的 RESTful API 设计
UPDATE: This post has been expanded upon and converted into an eBook. Good API design is hard! An AP ...
- 前端复习-01-dom操作包括ie和现代浏览器处理相关
一.事件流事件流描述的是从页面中接受事件的顺序.IE的事件流是事件冒泡流,而Netscape的事件流是事件捕获流1.事件冒泡事件冒泡,即事件最开始由最具体的元素(文档中嵌套层次最深的那个节点)接收,然 ...
- gcc -D
[gcc -D] -D name Predefine name as a macro, with definition 1. 通常debug和release版的区别就在于是否有DEBUG宏,DEBUG ...
- Linux查找文件夹名
@(编程) find / -type d -name filename type的类型 -type c File is of type c: b block (buffered) special c ...
- System.Data.SqlTypes.SqlNullValueException: 数据为空。不能对空值调用此方法或
有可能读出的数据为NULL,可以这样改: 方法一:while (reader.Read()){ for (int i = 0; i < 7; i++) { if (reader.IsDBNull ...
- Objective-C 学习记录--toches、Motion/Size/Rect/Point/CGFloat/protocol
- (void)touchesBegan touchesEnd touchesCancelled touchesMoved //代表的是手指在屏幕上的动作,开始 结束 取消 移动 //还有就是代表摇动 ...