sigrecv.c

来自「linux父子进程间关系」· C语言 代码 · 共 61 行

C
61
字号
#include <stdio.h>#include <signal.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#define M 2void sighdlr(int signum);int main(){       pid_t a;    signal(SIGUSR1, sighdlr);    signal(SIGUSR2, sighdlr);    a=getpid();    printf("My pid is %d.\n",a);    while(1);    return 0;}void sighdlr(int signum){    int fd0,fd1;    int success;	     char s[M];    switch (signum)     {       case SIGUSR1:           printf("Recived SIGUSR1\n");           exit(0);       break;       case SIGUSR2:           printf("Recived SIGUSR2\n");	   if ((fd0 = open("temp.txt", O_RDONLY)) < 0)               {  	            write(STDERR_FILENO, "open file Error!\n",sizeof("open file Error!\n"));  	            exit(1);               }           mkfifo("test.txt",0777);           if ((fd1 = open("test.txt", O_WRONLY)) < 0)               {                    write(STDERR_FILENO, "open fifo file Error!\n",sizeof("open file Error!\n"));                    exit(1);               }           success=read(fd0,s,M);           while(success>0)           {               write(fd1,s,strlen(s));               success=read(fd0,s,M);           }           close(fd0);           close(fd1);       break;       default:           break;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?