进程间通信系列 之 信号综合实例
进程间通信系列 之 概述与对比 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 100 void 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 100 void 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>
posted on 2013-11-13 20:27 YoungerChina 阅读(242) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人