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进程间通信-信号
前言:之前说看<C++ Primer >暂时搁浅一下,迷上公司大神写的代码,想要明白,主要是socket.进程间通信! 知道进程间通信:信号.信号量.管道.消息队列.共享内存(共享存储), ...
- Linux进程间通信—信号
三.信号(Signal) 信号是Unix系统中使用的最古老的进程间通信的方法之一.操作系统通过信号来通知某一进程发生了某一种预定好的事件:接收到信号的进程可以选择不同的方式处理该信号,一是可以采用默认 ...
- Linux进程间通信——信号
一.认识信号 信号(Signals)是Unix.类Unix以及其他POSIX兼容的操作系统中进程间通讯的一种有限制的方式.它是一种异步的通知机制,用来提醒进程一个事件已经发生.当一个信号发送给一个进程 ...
- Linux进程间通信-信号
1.什么是信号信号是Linux系统响应某些条件而产生的一个事件,接收到该信号的进程会执行相应的操作. 2.信号的产生1)由硬件产生,如从键盘输入Ctrl+C可以终止当前进程2)由其他进程发送,如可在s ...
- Linux 进程间通信 信号(signal)
1. 概念: 1)信号是在软件层次上对中断机制的一种模拟,是一种异步通信方式 2)信号可以直接进行用户空间进程和内核进程之间的交互,内核进程也可以利用它来通知用户空间进程发生了哪些系统事件. 3)如果 ...
- Linux进程间通信(一): 信号 signal()、sigaction()
一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...
- [转]Linux进程间通信——使用信号
转载于:http://blog.csdn.net/ljianhui/article/details/10128731 经典!!! Linux进程间通信——使用信号 一.什么是信号 用过 ...
- Linux进程间通信——使用信号
一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...
- Linux进程间通信(五):信号量 semget()、semop()、semctl()
这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:Linux进程间通信 -- 信号.下面 ...
随机推荐
- Yslow-23条规则编辑
1. 减少HTTP请求次数 合并图片.CSS.JS,改进首次访问用户等待时间. 2. 使用CDN 就近缓存==>智能路由==>负载均衡==>WSA全站动态加速 3. 避免空的 ...
- NOI十连测 第四测 T2
思路:线段树套可持久化treap,可持久化treap我还是第一次听说.. 改题的时候没看数据范围..乱开数组T_T #include<algorithm> #include<cstd ...
- (?:pattern) (?=pattern) (?!pattern)
(pattern) 匹配 pattern 并获取这一匹配.所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0- ...
- 开源欣赏wordpress之文章新增页面如何实现。
本地网址http://localhost/wordpress/wp-admin/post-new.php 进而找到post-new.php页面. 进入之后, require_once( dirname ...
- FNN模糊神经网络——信息系统客户服务感知评价
案例描述 信息系统是否真正减轻业务人员的日常工作量提高工作效率?如何从提供“被动”服务转变为根据客户感知提供“主动”服务,真正实现电网企业对信息系统服务的有效管理?如何构建一套适合企业的信息系统客户服 ...
- Word Search II 解答
Question Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
- Best Time to Buy and Sell Stock III 解答
Question Say you have an array for which the ith element is the price of a given stock on day i. Des ...
- LeeCode(Database)-Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- Mod_python: The Long Story
mod_python: the long story - Grisha Trubetskoy Mod_python: The Long Story Oct 25th, 2013 | Comments ...
- Java语言程序设计(基础篇) 第八章 多维数组
第八章 多维数组 8.2 二维数组的基础知识 二维数组中的元素通过行和列的下标来访问. 8.2.1 声明二维数组变量并创建二维数组 下面是二维数组的语法: 数据类型[][] 数组名; int[][] ...