基于命名管道与信号的进程间通信
第1关:命名管道与信号IPC操作考查
(没啥好说的,不会的操作系统考试就靠记呗!!)
- sleep(1); 休眠1秒
open("FIFO",O_RDONLY); (简化记忆哈: /*rd->only 写*/ )
open("FIFO",O_WRONLY); (简化记忆哈: /*wr->only 写*/ )
写入:write(fd,"heool0penEuler",14);(预期输出: sig=13)
#include<stdio.h> #include<string.h> #include<fcntl.h> #include<signal.h> #include<sys/types.h> void handler(int sig) { printf("sig=%d\n",sig); } int main(void) { int j; signal(SIGPIPE,handler);//在reader中止之后写Pipe的时候发送 unlink("FIFO"); mkfifo("FIFO",0644); pid_t pid; pid=fork(); if(pid==0) { /*子进程打开读管道,随后关闭管道*/ int fd; fd = open("FIFO",O_RDONLY); /*rd->only 写*/ /*关闭*/ close(fd); } else { /*父进程打开写通道,休眠1秒,尝试写入*/ int fd; fd = open("FIFO",O_WRONLY); /*wr->only 写*/ int ret; sleep(1); /*休眠1秒*/ /*写入*/ ret = write(fd,"heool0penEuler",14); } }