进程间通信系列 之 概述与对比 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. "); 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. "); sleep(1); } }else{ for(i = 0;i < 5;i ++) { printf("Father loop. "); 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. "); } exit(EXIT_SUCCESS); } int main() { int count = 0; int n = 0; signal(SIGALRM,handler); n = alarm(10); printf("n = %d. ",n); sleep(2); n = alarm(5); printf("n = %d. ",n); while(1) { printf("count = %d. ", ++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. "); alarm(10); }else{ kill(getppid(),SIGKILL); usleep(500); exit(EXIT_SUCCESS); } flag = 1; break; case SIGINT: printf("The alarm is cancel. "); 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. ",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] = ' '; printf("Read %d bytes : %s. ",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. ",fifo_name,strerror(errno)); return -1; } signal(SIGINT,SIG_IGN); while(1) { getchar(); printf(">"); fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-1] = ' '; 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]. ",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. "); alarm(10); }else{ kill(getppid(),SIGKILL); usleep(500); exit(EXIT_SUCCESS); } flag = 1; break; case SIGINT: printf("The alarm is cancel. "); 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. ",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] = ' '; printf("Read %d bytes : %s. ",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. ",fifo_name,strerror(errno)); return -1; } signal(SIGINT,SIG_IGN); while(1) { getchar(); printf(">"); fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-1] = ' '; 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]. ",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>