A进程
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <unistd.h>
8 int main(int argc, const char *argv[])
9 {
10 if(mkfifo("./fifo",0664)<0)
11 {
12 if(errno!=17)
13 {
14 perror("mkfifo");
15 return -1;
16 }
17 }
18 if(mkfifo("./fifo2",0664)<0)
19 {
20 if(errno!=17)
21 {
22 perror("mkfifo");
23 return -1;
24 }
25 }
26
27 int fd=open("./fifo",O_RDONLY);
28 if(fd<0)
29 {
30 perror("open");
31 return -1;
32 }
33 int fp=open("./fifo2",O_WRONLY);
34 if(fp<0)
35 {
36 perror("open");
37 return -1;
38 }
39 printf("open success\n");
40
41 ssize_t res=0;
42 ssize_t bes=0;
43 char str[20]="";
44 char buf[20]="";
45 while(1)
46 {
47 printf("A进程写入:");
48 bzero(str,sizeof(str));
49 fgets(str,sizeof(str),stdin);
50 str[strlen(str)-1]='\0';
51 res=write(fp,str,sizeof(str));
52 if(res<0)
53 {
54 perror("write");
55 return -1;
56 }
57 if(strcasecmp(str,"quit")==0)
58 break;
59 // printf("A进程写入%ld数据\n",res);
60
61 bzero(buf,sizeof(buf));
62 bes=read(fd,buf,sizeof(buf));
63 if(bes<0)
64 {
65 perror("read");
66 return -1;
67 }
68 else if(bes==0)
69 {
70 printf("B进程退出\n");
71 break;
72 }
73 if(strcasecmp(buf,"quit")==0)
74 break;
75 printf("A进程接收%ld数据:%s\n",bes,buf);
76 }
77
78 return 0;
79 }
B进程
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <unistd.h>
8 int main(int argc, const char *argv[])
9 {
10
11 if(mkfifo("./fifo",0664)<0)
12 {
13 if(errno!=17)
14 {
15 perror("mkfifo");
16 return -1;
17 }
18 }
19 if(mkfifo("./fifo2",0664)<0)
20 {
21 if(errno!=17)
22 {
23 perror("mkfifo");
24 return -1;
25 }
26 }
27
28 int fp=open("./fifo",O_WRONLY);
29 if(fp<0)
30 {
31 perror("open");
32 return -1;
33 }
34 int fd=open("./fifo2",O_RDONLY);
35 if(fd<0)
36 {
37 perror("open");
38 return -1;
39 }
40 printf("open success\n");
41
42 ssize_t res=0;
43 ssize_t bes=0;
44 char str[20]="";
45 char buf[20]="";
46 while(1)
47 {
48 res=read(fd,str,sizeof(str));
49 if(res<0)
50 {
51 perror("read");
52 return -1;
53 }
54 else if(res==0)
55 {
56 printf("A进程退出\n");
57 break;
58 }
59 if(strcasecmp(str,"quit")==0)
60 break;
61
62 printf("B进程读取%ld数据:%s\n",res,str);
63
64 printf("B进程写入:");
65 bzero(buf,sizeof(buf));
66 fgets(buf,sizeof(buf),stdin);
67 buf[strlen(buf)-1]='\0';
68 bes=write(fp,buf,sizeof(buf));
69 if(bes<0)
70 {
71 perror("write");
72 return -1;
73 }
74 if(strcasecmp(buf,"quit")==0)
75 break;
76 // printf("B进程写入%ld数据\n",bes);
77 }
78 return 0;
79 }
捕捉信号
1 #include <stdio.h>
2 #include <signal.h>
3 #include <unistd.h>
4 typedef void (*sighandler_t)(int);
5
6
7 void handler2(int sig)
8 {
9 printf("2号ctrl+c信号,%d\n",sig);
10 }
11
12 void handler2t(int sig)
13 {
14 printf("第二次捕捉2号ctrl+c信号,%d\n",sig);
15 }
16
17 void handler3(int sig)
18 {
19 printf("3号ctrl+\\信号,%d\n",sig);
20 }
21
22
23
24
25
26 int main(int argc, const char *argv[])
27 {
28 printf("%p ,%p ,%p\n",handler2,handler2t,handler3);
29 sighandler_t s2 =signal(2,handler2);
30 if(SIG_ERR==s2)
31 {
32 perror("signal");
33 return -1;
34 }
35 printf("%p\n",s2);
36
37
38 sighandler_t s2t =signal(2,handler2t);
39 if(SIG_ERR==s2t)
40 {
41 perror("signal");
42 return -1;
43 }
44 printf("%p\n",s2t);
45
46 sighandler_t s3 =signal(3,handler3);
47 if(SIG_ERR==s3)
48 {
49 perror("signal");
50 return -1;
51 }
52 printf("%p\n",s3);
53
54 while(1)
55 {
56 printf("ddd\n");
57 sleep(1);
58 }
59
60 return 0;
61 }