信号与命名管道

来源:互联网 发布:李逵打鱼版本源码 编辑:程序博客网 时间:2024/05/19 09:49
信号 (用整形值表示)
信号 (用整形值表示)define SIGHUP      1        /* Hangup (POSIX).  */ 34 *#define SIGINT      2   /* Interrupt (ANSI).  */ 终端 中断 两个系统调用 kill signal crtl+c  35 #define SIGQUIT     3   /* Quit (POSIX).  */ 36 #define SIGILL      4   /* Illegal instruction (ANSI).  */ 37 #define SIGTRAP     5   /* Trace trap (POSIX).  */ 38 #define SIGABRT     6   /* Abort (ANSI).  */ 39 #define SIGIOT      6   /* IOT trap (4.2 BSD).  */ 40 #define SIGBUS      7   /* BUS error (4.2 BSD).  */ 41 #define SIGFPE      8   /* Floating-point exception (ANSI).  */ 42 #define SIGKILL     9   /* Kill, unblockable (POSIX).  */ 43 #define SIGUSR1     10  /* User-defined signal 1 (POSIX).  */ 44 #define SIGSEGV     11  /* Segmentation violation (ANSI).  */ 45 #define SIGUSR2     12  /* User-defined signal 2 (POSIX).  */ 46 #define SIGPIPE     13  /* Broken pipe (POSIX).  */ 47 #define SIGALRM     14  /* Alarm clock (POSIX).  */ 48 *#define SIGTERM     15  /* Termination (ANSI).  */ 49 #define SIGSTKFLT   16  /* Stack fault.  */ 50 #define SIGCLD      SIGCHLD /* Same as SIGCHLD (System V).  */ 51 #define SIGCHLD     17  /* Child status has changed (POSIX).  */ 52 #define SIGCONT     18  /* Continue (POSIX).  */ 53 *#define SIGSTOP     19  /* Stop, unblockable (POSIX).  */ 54 #define SIGTSTP     20  /* Keyboard stop (POSIX).  */                                                        产生信号的原因 一般不是好事情给进程发送信号进程收到信号后会发出响应内核可以给进程发送信号信号由系统提供进程可以给进程发送信号 SIGINT (终端中断)调用signal 改变进程的响应方式 signal是个函数调用 程序响应有三种方式 默认signal(SIGINT,SIG_DFL); 忽略signal(SIGINT,SIG_IGN); 自定义signal(SIGINT,fun); #include <stdio.h>#include <stdlib.h>#include <signal.h>void fun(int sig) //不掉用默认的处理方式 系统内核调用该函数ctrl +终端中断{printf("sig=%d",sig); //内核调用会将该处理方式停止自爱上次执行的地方signal(SIGINT,SIG_DFL);//和内核重新约定信号处理方式}int main(){signal(SIGINT,fun);和内核约定while(1) { printf("man\n");  sleep(1);}return 0;17 }~    kill给进程发送一个信号让进程结束#include <stdio.h>//用信号让他结束#include <stdlib.h>#include <signal.h>#include <string.h>#include <unistd.h>#include <assert.h>//crtl + c  void fun(int sig){printf("sig=%d\n",sig);wait();                 //这个是系统调用 在子函数结束的时候进行处理wait()signal(SIGINT,SIG_DFL);}int main(){signal(SIGCHLD,fun);int n=0;char *p=NULL;pid_t pid=fork();assert(pid!=-1);         //检验fork是否成功if(pid==0){n=3;p="child";}else{n=5;p="parent";}int i;for(i=0;i<n;i++){printf("%s,%d\n",p,getpid());sleep(1);}return 0;}kill不允许忽略9号信号(kill)// #define SIGKILL     9   /* Kill, unblockable (POSIX).  */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <assert.h>#include <string.h>#include<signal.h>int main(int argc,char* argv[]){if(argc!=2&&argc!=3){printf("mykill arg error\n");}int id =0;int sign=0;if(argc==3){sscanf(argv[2],"%d",&sign);}if(argc ==2){sscanf(argv[1],"%d",&id);}if(kill(id ,SIGKILL)==-1){printf("kill erro");}return 0;}管道文件(管道文件位于内存中)  1 #include <stdio.h>  2 #include <stdlib.h>  3 #include <fcntl.h>  4 #include <string.h>  5 #include <assert.h>  6 int main()  7 {  8     int fd=open("fifo",O_RDONLY);  9     assert(fd!=-1); 10     printf("fd=%d",fd); 11     char buff[128]={0}; 12     gets(buff); 13     read(fd,buff,128); 14     printf("%s",buff); 15     return 0; 16 }~                                                                            ~    命名管道的用法(两个进程间的通信)通过管道文件                                                                       ~   #include <stdlib.h>  3 #include <fcntl.h>  4 #include <string.h>  5 #include <assert.h>  6 int main()  7 {  8     char buff[128]={0};  9     int fd=open("fifo",O_WRONLY); 10     assert(fd!=-1); 11     printf("fd=%d\n",fd); 12     while(1) 13     { 14         memset(buff,0,127); 15         gets(buff); 16         if(strcmp(buff,"end")==0) 17         { 18             break; 19         } 20         write(fd,buff,127); 21     } 22     close(fd); 23     return 0; 24 }1 #include <stdio.h>2 #include <stdlib.h>3 #include <fcntl.h>4 #include <string.h>5 #include <assert.h>6 int main()7 {8         char buff[128]={0};9         int fd=open("fifo",O_RDONLY);10         assert(fd!=-1);11         printf("fd=%d\n",fd);12         while(read(fd,buff,127)!=0)13         {14             printf("%s\n",buff);sleep(1);15         }16         close(fd);17 18     return 0;19 }~                                                                            ~                                                                            ~          


                                                        



~                                                                                                              
~          
0 0
原创粉丝点击