IPC--进程间通信五(信号)
![](/assets/blank.gif)
- #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;
- }
![](/assets/blank.gif)
- #include <signal.h>
- int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
![](/assets/blank.gif)
- #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);
- }
![](/assets/blank.gif)
- #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);
- }
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
转载自:https://www.cnblogs.com/thinkinglife/p/5520955.html
IPC--进程间通信五(信号)相关推荐
- Linux系统编程【文件IO、进程、进程间通信、信号、线程、互斥】
linux系统编程 个人通过学习,手打了一份48000字的Linux系统编程的笔记,包含了[文件IO.进程.进程间通信.信号.多线程.互斥]等知识点,并给出了大量的代码案例对每个重要的知识点进行了代码 ...
- Android开发之IPC进程间通信-AIDL介绍及实例解析
一.IPC进程间通信 IPC是进程间通信方法的统称,Linux IPC包括以下方法,Android的进程间通信主要采用是哪些方法呢? 1. 管道(Pipe)及有名管道(named pipe):管道可用 ...
- 音视频:06.linux系统-IPC进程间通信
linux系统-IPC进程间通信 1.进程间通信的方式 2.管道(pipe) 3.共享映射区 4.信号 1.进程间通信的方式 文件,管道,信号.信号量,共享映射区(共享内存),消息队列,管道,套接字( ...
- system V IPC进程间通信机制一网打尽
目录 必备IPCS命令解析 ipcs ipcrm Linux IPC消息队列 msgget msgsnd msgrcv msgctl Linux IPC信号量 理解信号量 semget semop s ...
- Windows Mobile使用Shared Memory(共享内存)进行IPC(进程间通信)的开发
背景 在Unix-like系统进行IPC(Inter-process communication)通信,Shared memory是效率最高的,我称之为IPC的王中王. 简介 本文讲述在Windows ...
- 【Android Binder 系统】一、Binder 系统核心 ( IPC 进程间通信 | RPC 远程调用 )
文章目录 一.Binder 系统两个核心 二.IPC 进程间通信 三.RPC 远程过程调用 一.Binder 系统两个核心 Binder 系统 最重要的两个核心是 IPC 和 RPC ; IPC ( ...
- 进程互斥锁,队列,IPC进程间通信,生产者与消费者,线程,线程对象的属性,先行互斥锁...
进程互斥锁: 让并发变成串行, 牺牲了执行效率, 保证了数据安全.在程序并发执行时,需要修改数据时使用. 队列 队列:先进先出 相当于内存中产生一个队列空间,先进先出, 可以存放多个数据,但数 ...
- YDOOK: Python3 IPC 进程间通信方法分类总结
YDOOK: Python3 IPC 进程间通信方法分类总结 © YDOOK JY Lin 1.管道:pipeline 2.消息队列:msg queue 3.共享内存:share memory 4.信 ...
- Linux IPC进程间通信(三):信号量
系列文章: Linux IPC进程间通信(一):管道 Linux IPC进程间通信(二):共享内存 Linux IPC进程间通信(三):信号量 Linux IPC进程间通信(四):消息队列 文章目录 ...
- linux实验五 信号应用,实验五 进程间通信(中)
一.实验名称:实验五进程间通信(中) 二.实验日期:2014/3/27 三.实验目的: 1. 通过实验理解消息缓冲通信 2. 通过实验理解共享内存通信 3. 了解消息缓冲通信与内存共享通信之间的 ...
最新文章
- python 自动化办公 案例_python自动化工具之pywinauto实例详解
- java 之 学习过程中遇到的大佬博客
- BitLocker Partition Recovery
- 【EXCEL】VLOOKUP函数反向应用
- mysql 值到99999后不增值了_Mysql 增加新数据,若存在则更新的问题
- java 反射 性能_java高性能反射及性能对比
- SolrCloud集群配置
- “睡服”面试官系列第八篇之iterator(建议收藏学习)
- jeecg自定义datagrid查询
- 台式计算机驱动程序未被安装,电脑未能成功安装设备驱动程序怎么办
- (68)Vue-cli 目录与文件
- int与string互转
- 【C++ Primer】第十四章 C++中的代码重用
- Mac系统如何安装php,MAC系统如何快速安装php
- 段落超出,省略...
- case when then end.sql
- DataWorks调度配置最佳实战
- ffmpeg drawtext 背景_8款电视背景墙:电视背景墙这样装,不仅省钱还作用多!效果大不一样!...
- Java中PreparedStatement和Statement区别
- Python PyQt5 教程
热门文章
- 798. 差分矩阵 (二维差分)
- 04737 c++ 自学考试2019版 第五章程序设计题 1
- vscode 用户配置添加 分号、保存格式化、单引号、function格式
- C#LeetCode刷题-极小化极大
- JavaScript数组对象教程–如何使用JS数组方法创建,更新和遍历对象
- docker前后端分离_Docker分离模式介绍
- gatsby_使用TinaCMS + Gatsby编辑Markdown的3种方法
- react可视化_如何使用React和Popmotion.io可视化煎饼算法
- 使用bypy从ubuntu通过命令行上传文件到百度云
- lamp php7.0,LAMP(Ubuntu16+PHP7.0)安装Wordpress