信号与命名管道
来源:互联网 发布:李逵打鱼版本源码 编辑:程序博客网 时间: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
- 信号与命名管道
- Linux信号和命名管道
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- 匿名管道 与 命名管道
- 匿名管道与命名管道
- 信号与管道
- 管道与信号
- 管道与信号
- 管道与信号
- 命名管道与Mailslot
- Unix管道与命名FIFO
- 匿名管道 与 命名管道/FIFO管道 的特点
- (命名)管道,信号量,消息队列,共享内存,信号,socket
- 《Android源码设计模式解析与实战》读书笔记(五)——工厂方法模式
- 杭电2149 Public Sale 巴什博弈
- 用java如何解析(任意)一句话中的词
- org.testng.TestNGException: Cannot find class in classpath
- lintcode/leetcode由易至难第3题:选择排序
- 信号与命名管道
- 文件转化时可用FileUtils
- SpringMVC与Struts2区别与比较总结
- linux audio 子系统(转)
- ubuntu16.04下xfce4下快速显示桌面的快捷键设置
- java中system.out.prin()打印格式化
- Matlab入门教程
- Nginx 内置变量,细化规则,真实IP获取及限制连接请求
- word技巧总结