进程间通信系列 之 信号综合实例
来源:互联网 发布:电脑无法连接有线网络 编辑:程序博客网 时间:2024/05/18 20:49
进程间通信系列 之 概述与对比 http://blog.csdn.net/younger_china/article/details/15808685
进程间通信系列 之 共享内存及其实例 http://blog.csdn.net/younger_china/article/details/15961557
进程间通信系列 之 共享内存简单实例 http://blog.csdn.net/younger_china/article/details/15991081
进程间通信系列 之 信号(理论) http://blog.csdn.net/younger_china/article/details/15976961
进程间通信系列 之 信号实例 http://blog.csdn.net/younger_china/article/details/15968715
进程间通信系列 之 信号综合实例 http://blog.csdn.net/younger_china/article/details/15980485
进程间通信系列 之 命名管道FIFO及其应用实例 http://blog.csdn.net/younger_china/article/details/15808531
进程间通信系列 之 管道(客户端和服务端通信) http://blog.csdn.net/younger_china/article/details/15809281
进程间通信系列 之 信号量详解及编程实例 http://blog.csdn.net/younger_china/article/details/15808531
进程间通信系列 之 消息队列函数及其范例 http://blog.csdn.net/younger_china/article/details/15503871
进程间通信系列 之 消息队列应用实例 http://blog.csdn.net/younger_china/article/details/15808501
进程间通信系列 之 socket套接字及其实例 http://blog.csdn.net/younger_china/article/details/15809163
进程间通信系列 之 socket套接字实例 http://blog.csdn.net/younger_china/article/details/15809207
原文地址:进程间通信--信号(进程间通信唯一的异步方式) 作者:Deem_passion
一、信号的介绍
B.常用信号的含义
#include <stdio.h>#include <sys/types.h>#include <signal.h>#include <stdlib.h>int main(){ int pid; if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); }else if(pid == 0){ while(1); }else{ int signum; while(scanf("%d",&signum) == 1) { kill(pid,signum); system("ps -aux | grep a.out"); } } return 0;}
运行结果如下:
B.捕捉一个信号
其原型:
<span style="font-size:12px;">#include <stdio.h>#include <signal.h>#include <unistd.h>#include <stdlib.h>void child_exit_handler(int signum){ if(signum == SIGCHLD) { printf("Child exit.\n"); wait(NULL); }}int main(){ int pid; int i = 0; //想内核注册,处理 SIGCHLD信号的方式 signal(SIGCHLD,child_exit_handler); if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); }else if(pid == 0){ for(i = 0;i < 5;i ++) { printf("child loop.\n"); sleep(1); } }else{ for(i = 0;i < 5;i ++) { printf("Father loop.\n"); sleep(2); } } exit(EXIT_SUCCESS);}</span>
<span style="font-size:12px;">#include <stdio.h>#include <signal.h>#include <stdlib.h>void handler(int signum){ if(signum == SIGALRM) { printf("Recv SIGALARM.\n"); } exit(EXIT_SUCCESS);}int main(){ int count = 0; int n = 0; signal(SIGALRM,handler); n = alarm(10); printf("n = %d.\n",n); sleep(2); n = alarm(5); printf("n = %d.\n",n); while(1) { printf("count = %d.\n", ++count); sleep(1); } return 0;}</span>
<span style="font-size:12px;">#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <string.h>#include <fcntl.h>#define MAX 100void signal_handler(int signum){ static int flag = 0; switch(signum) { case SIGALRM: if(flag == 0) { printf("The people is leaving,the system is closed in 10 seconds \ and you can input 'ctrl + c' cancel.\n"); alarm(10); }else{ kill(getppid(),SIGKILL); usleep(500); exit(EXIT_SUCCESS); } flag = 1; break; case SIGINT: printf("The alarm is cancel.\n"); alarm(0); break; }}int child_recv_fifo(char *fifo_name){ int n,fd; char buf[MAX]; if((fd = open(fifo_name,O_RDONLY)) < 0) { fprintf(stderr,"fail to open %s : %s.\n",fifo_name,strerror(errno)); return -1; } signal(SIGALRM,signal_handler); signal(SIGINT,signal_handler); alarm(15);//璁剧疆瀹氭椂鍣? while(1) { n = read(fd,buf,sizeof(buf)); buf[n] = '\0'; printf("Read %d bytes : %s.\n",n,buf); if(strncmp(buf,"quit",4) == 0 || n == 0) { kill(getppid(),SIGKILL); usleep(500); exit(EXIT_SUCCESS); } alarm(15); } return 0;}int father_send_fifo(char *fifo_name,int pid){ int n,fd; char buf[MAX]; if((fd = open(fifo_name,O_WRONLY)) < 0) { fprintf(stderr,"Fail to open %s : %s.\n",fifo_name,strerror(errno)); return -1; } signal(SIGINT,SIG_IGN); while(1) { getchar(); printf(">"); fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-1] = '\0'; write(fd,buf,strlen(buf)); if(strncmp(buf,"quit",4) == 0) { kill(pid,SIGKILL); usleep(500); exit(EXIT_SUCCESS); } } return 0;}int main(int argc,char *argv[]){ int pid; if(argc < 3) { fprintf(stderr,"usage %s argv[1].\n",argv[0]); exit(EXIT_FAILURE); } if(mkfifo(argv[1],0666) < 0 && errno != EEXIST) { perror("Fail to mkfifo"); exit(EXIT_FAILURE); } if(mkfifo(argv[2],0666) < 0 && errno != EEXIST) { perror("Fail to mkfifo"); exit(EXIT_FAILURE); } if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); }else if(pid == 0){ child_recv_fifo(argv[2]); }else{ father_send_fifo(argv[1],pid); } exit(EXIT_SUCCESS);}</span>
client B
<span style="font-size:12px;">#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <string.h>#include <fcntl.h>#define MAX 100void signal_handler(int signum){ static int flag = 0; switch(signum) { case SIGALRM: if(flag == 0) { printf("The people is leaving,the system is closed in 10 seconds \ and you can input 'ctrl + c' cancel.\n"); alarm(10); }else{ kill(getppid(),SIGKILL); usleep(500); exit(EXIT_SUCCESS); } flag = 1; break; case SIGINT: printf("The alarm is cancel.\n"); alarm(0); break; }}int child_recv_fifo(char *fifo_name){ int n,fd; char buf[MAX]; if((fd = open(fifo_name,O_RDONLY)) < 0) { fprintf(stderr,"fail to open %s : %s.\n",fifo_name,strerror(errno)); return -1; } signal(SIGALRM,signal_handler); signal(SIGINT,signal_handler); alarm(15);//璁剧疆瀹氭椂鍣? while(1) { n = read(fd,buf,sizeof(buf)); buf[n] = '\0'; printf("Read %d bytes : %s.\n",n,buf); if(strncmp(buf,"quit",4) == 0 || n == 0) { kill(getppid(),SIGKILL); usleep(500); exit(EXIT_SUCCESS); } alarm(15); } return 0;}int father_send_fifo(char *fifo_name,int pid){ int n,fd; char buf[MAX]; if((fd = open(fifo_name,O_WRONLY)) < 0) { fprintf(stderr,"Fail to open %s : %s.\n",fifo_name,strerror(errno)); return -1; } signal(SIGINT,SIG_IGN); while(1) { getchar(); printf(">"); fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-1] = '\0'; write(fd,buf,strlen(buf)); if(strncmp(buf,"quit",4) == 0) { kill(pid,SIGKILL); usleep(500); exit(EXIT_SUCCESS); } } return 0;}int main(int argc,char *argv[]){ int pid; if(argc < 3) { fprintf(stderr,"usage %s argv[1].\n",argv[0]); exit(EXIT_FAILURE); } if(mkfifo(argv[1],0666) < 0 && errno != EEXIST) { perror("Fail to mkfifo"); exit(EXIT_FAILURE); } if(mkfifo(argv[2],0666) < 0 && errno != EEXIST) { perror("Fail to mkfifo"); exit(EXIT_FAILURE); } if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); }else if(pid == 0){ child_recv_fifo(argv[1]); }else{ father_send_fifo(argv[2],pid); } exit(EXIT_SUCCESS);}</span>
- 进程间通信系列 之 信号综合实例
- 进程间通信系列 之 信号实例
- 进程间通信系列 之 信号(理论)
- 进程间通信之信号
- 进程间的通信之信号通信
- 进程通信之信号
- 进程通信之信号
- 进程之信号通信
- 进程通信之信号
- 进程通信之信号
- 进程间通信系列 之 消息队列应用实例
- 进程间通信系列 之 信号量详解及编程实例
- 进程间通信系列 之 socket套接字及其实例
- 进程间通信系列 之 socket套接字实例
- 进程间通信系列 之 共享内存及其实例
- 进程间通信系列 之 共享内存简单实例
- 进程通信之信号通信
- Linux进程间通信之信号
- 工作笔记
- OC语法之Copy
- 【线程高新】——【面试题】子线程运行10次,主线程运行100次,轮换运行50次
- myeclipse不编译 10种解决办法
- 黑马程序员_毕向东_Java基础视频教程学习笔记(二)
- 进程间通信系列 之 信号综合实例
- 二叉查找树的插入,删除,查找
- mac下Finder寻找文件,显示隐藏文件,快速前往文件夹
- 23-静态方法和非静态方法
- 泯灭Web App可用性的七个开发错误
- 波导强势回归,能否重夺“战斗机”称号?
- Linux下查看一个文件的类型
- Virtual-Key Codes
- 1统计输入的字符串的字符个数