1、要求实现AB进程对话
a.A进程发送一句话给B进程,B进程接收后打印
b.B进程再恢复一句话给A进程,A进程接收后打印
c.重复1.2步骤,当收到quit后,要结束AB进程
d.提示:两根管道
程序代码
1 #include<stdio.h>
2 #include<sys/types.h>
3 #include<sys/stat.h>
4 #include<errno.h>
5 #include<fcntl.h>
6 #include<unistd.h>
7 #include<string.h>
8
9 int main(int argc, const char *argv[])
10 {
11 //创建有名管道1
12 if(mkfifo("./myfifo",0664) < 0)
13 {
14 //文件已经存在的错误是一个合法的错误,需要排除,代码允许正常运行
15 if(17 != errno)
16 {
17 perror("mkfifo");
18 return -1;
19 }
20 }
21 //创建有名管道2
22 if(mkfifo("./myfifo1",0664) < 0)
23 {
24 //文件已经存在的错误是一个合法的错误,需要排除,代码允许正常运行
25 if(17 != errno)
26 {
27 perror("mkfifo");
28 return -1;
29 }
30 }
31 printf("myfifo create cusess\n");
32
33 //有名管道存在,则直接写管道
34 int fd_w = open("./myfifo",O_WRONLY);
35 if(fd_w < 0)
36 {
37 perror("open");
38 return -1;
39 }
40
41 //有名管道存在,则直接读管道
42 int fd_r = open("./myfifo1",O_RDONLY);
43 if(fd_r < 0)
44 {
45 perror("open");
46 return -1;
47 }
48
49
50 printf("open fifo sucess\n");
51
52 char buf[128]="";
53 while(1)
54 {
55 bzero(buf,sizeof(buf));
56
57 fgets(buf,sizeof(buf),stdin);
58 buf[strlen(buf)-1] = 0;
59
60 write(fd_w,buf,sizeof(buf));
61 if(strcasecmp(buf,"quit") == 0)
62 {
63 break;
64 }
65 bzero(buf,sizeof(buf));
66 read(fd_r,buf,sizeof(buf));
67 printf("%s\n",buf);
68 if(strcasecmp(buf,"quit") == 0)
69 {
70 printf("对端进程退出\n");
71 break;
72 }
73
74
75 }
76
77 close(fd_w);
78 close(fd_r);
79 return 0;
80 }
~
进程2
1 #include<stdio.h>
2 #include<sys/types.h>
3 #include<sys/stat.h>
4 #include<errno.h>
5 #include<fcntl.h>
6 #include<unistd.h>
7 #include<string.h>
8 int main(int argc, const char *argv[])
9 {
10 //创建有名管道1
11 if(mkfifo("./myfifo",0664) < 0)
12 {
13 //文件已经存在的错误是一个合法的错误,需要排除,代码允许正常运行
14 if(17 != errno)
15 {
16 perror("mkfifo");
17 return -1;
18 }
19 }
20
21 //创建有名管道1
22 if(mkfifo("./myfifo1",0664) < 0)
23 {
24 //文件已经存在的错误是一个合法的错误,需要排除,代码允许>
25 if(17 != errno)
26 {
27 perror("mkfifo");
28 return -1;
29 }
30 }
31
32 printf("myfifo1 create cusess\n");
33
34 //有名管道存在,则直接读写管道
35 int fd_r = open("./myfifo",O_RDONLY);
36 if(fd_r < 0)
37 {
38 perror("open");
39 return -1;
40 }
41
42 int fd_w = open("./myfifo1",O_WRONLY);
43 if(fd_w < 0)
44 {
45 perror("open");
46 return -1;
47 }
48
49
50 printf("open fifo sucess\n");
51
52 char buf[128]="";
53
54 while(1)
55 {
56 bzero(buf,sizeof(buf));
57 read(fd_r,buf,sizeof(buf));
58 printf("%s\n",buf);
59 if(strcasecmp(buf,"quit") == 0)
60 {
61 printf("对端进程退出\n");
62 break;
63 }
64 bzero(buf,sizeof(buf));
65 fgets(buf,sizeof(buf),stdin);
66 buf[strlen(buf)-1] = 0;
67
68 write(fd_w,buf,sizeof(buf));
69 if(strcasecmp(buf,"quit") == 0)
70 {
71 break;
72 }
73 }
74
75 close(fd_w);
76 close(fd_r);
77 return 0;
78 }
~
~
~
运行结果
程序1:
ubuntu@ubuntu:作业$ ./r
myfifo1 create cusess
open fifo sucess
qwe
ert
asd
dn
quit
对端进程退出
程序2:
ubuntu@ubuntu:作业$ ./w
myfifo create cusess
open fifo sucess
qwe
ert
asd
dn
quit
2、附加题:在第1题的基础上,能够实现随时收发
程序代码
进程1:
1 #include<stdio.h>
2 #include<sys/types.h>
3 #include<sys/stat.h>
4 #include<errno.h>
5 #include<fcntl.h>
6 #include<unistd.h>
7 #include<string.h>
8 #include<pthread.h>
9
10
11 //子线程写信息
12 void *callBack(void *arv)
13 {
14 int fd_w = open("./myfifo1",O_WRONLY);
15 if(fd_w < 0)
16 {
17 perror("open");
18 return NULL;
19 }
20 char buf[128]="";
21
22 while(1)
23 {
24 bzero(buf,sizeof(buf));
25 fgets(buf,sizeof(buf),stdin);
26 buf[strlen(buf)-1] = 0;
27
28 write(fd_w,buf,sizeof(buf));
29 if(strcasecmp(buf,"quit") == 0)
30 {
31 break;
32 }
33
34 }
35 close(fd_w);
36 pthread_exit(NULL);
37 }
38
39
40
41 int main(int argc, const char *argv[])
42 {
43
44
45 //创建有名管道1
46 if(mkfifo("./myfifo",0664) < 0)
47 {
48 //文件已经存在的错误是一个合法的错误,需要排除,代码允许正常运行
49 if(17 != errno)
50 {
51 perror("mkfifo");
52 return -1;
53 }
54 }
55
56 //创建有名管道1
57 if(mkfifo("./myfifo1",0664) < 0)
58 {
59 //文件已经存在的错误是一个合法的错误,需要排除,代码允许>
60 if(17 != errno)
61 {
62 perror("mkfifo");
63 return -1;
64 }
65 }
66 printf("myfifo1 create cusess\n");
67 printf("myfifo create cusess\n");
68
69 //父线程读信息
70 //有名管道存在,则直接读管道
71 int fd_r = open("./myfifo",O_RDONLY);
72 if(fd_r < 0)
73 {
74 perror("open");
75 return -1;
76 }
77 printf("open fifo sucess__RD__\n");
78
79
80
81 pthread_t pid;
82 pthread_create(&pid,NULL,callBack,&fd_r);
83
84 char buf[128]="";
85
86 while(1)
87 {
88 bzero(buf,sizeof(buf));
89 read(fd_r,buf,sizeof(buf));
90 printf("%s\n",buf);
91 if(strcasecmp(buf,"quit") == 0)
92 {
93 printf("对端进程退出\n");
94 break;
95 }
96 }
97
98 close(fd_r);
99
100 pthread_join(pid,NULL);
101 return 0;
102 }
进程2
1 #include<stdio.h>
2 #include<sys/types.h>
3 #include<sys/stat.h>
4 #include<errno.h>
5 #include<fcntl.h>
6 #include<unistd.h>
7 #include<string.h>
8 #include<pthread.h>
9
10
11 //子线程写信息
12 void *callBack(void *arv)
13 {
14 //有名管道存在,则直接写管道
15 int fd_w = open("./myfifo",O_WRONLY);
16 if(fd_w < 0)
17 {
18 perror("open");
19 return NULL;
20 }
21 char buf[128]="";
22
23 while(1)
24 {
25 bzero(buf,sizeof(buf));
26
27 fgets(buf,sizeof(buf),stdin);
28 buf[strlen(buf)-1] = 0;
29
30 write(fd_w,buf,sizeof(buf));
31 if(strcasecmp(buf,"quit") == 0)
32 {
33 break;
34 }
35 }
36 close(fd_w);
37 pthread_exit(NULL);
38 }
39
40
41
42 int main(int argc, const char *argv[])
43 {
44 //创建有名管道1
45 if(mkfifo("./myfifo",0664) < 0)
46 {
47 //文件已经存在的错误是一个合法的错误,需要排除,代码允许正常运行
48 if(17 != errno)
49 {
50 perror("mkfifo");
51 return -1;
52 }
53 }
54 //创建有名管道2
55 if(mkfifo("./myfifo1",0664) < 0)
56 {
57 //文件已经存在的错误是一个合法的错误,需要排除,代码允许正常运行
58 if(17 != errno)
59 {
60 perror("mkfifo");
61 return -1;
62 }
63 }
64
65 printf("myfifo create cusess\n");
66 printf("myfifo1 create cusess\n");
67
68
69 pthread_t pid;
70 pthread_create(&pid,NULL,callBack,NULL);
71
72 //父线程读信息
73 //有名管道存在,则直接读管道
74 int fd_r = open("./myfifo1",O_RDONLY);
75 if(fd_r < 0)
76 {
77 perror("open");
78 return -1;
79 }
80
81
82 printf("open fifo sucess\n");
83
84 char buf[128]="";
85 while(1)
86 {
87
88 bzero(buf,sizeof(buf));
89 read(fd_r,buf,sizeof(buf));
90 printf("%s\n",buf);
91 if(strcasecmp(buf,"quit") == 0)
92 {
93 printf("对端进程退出\n");
94 break;
95 }
96
97 }
98
99 close(fd_r);
100 pthread_join(pid,NULL);
101 return 0;
102 }
~
运行结果
进程1:
ubuntu@ubuntu:作业$ ./w
myfifo create cusess
myfifo1 create cusess
open fifo sucess
asd
qwe
dfgh
adsf
asdf
quit
对端进程退出
quit
ubuntu@ubuntu:作业$
进程2
ubuntu@ubuntu:作业$ ./r
myfifo1 create cusess
myfifo create cusess
open fifo sucess__RD__
asd
qwe
dfgh
adsf
asdf
quit
quit
对端进程退出
3、抓捕2 3 20 号信号
1 #include<stdio.h>
2 #include<signal.h>
3 typedef void (*sighandler_t)(int);
4
5 void callBack(int sig)
6 {
7 printf("抓捕2号进程成功\n");
8 }
9 void callBack3(int sig)
10 {
11 printf("抓捕3号进程成功\n");
12 }
13 void callBack20(int sig)
14 {
15 printf("抓捕20号进程成功\n");
16
17 }
18 int main(int argc, const char *argv[])
19 {
20
21 sighandler_t sig = signal(2,callBack);
22 if(SIG_ERR==sig)
23 {
24 perror("signal");
25 return -1;
26 }
27 sig = signal(3,callBack3);
28 if(SIG_ERR==sig)
29 {
30 perror("signal");
31 return -1;
32 }
33 sig = signal(20,callBack20);
34 if(SIG_ERR==sig)
35 {
36 perror("signal");
37 return -1;
38 }
39
40 while(1)
41
42 sleep(1);
43 return 0;
44 }
运行结果
ubuntu@ubuntu:作业$ ./a.out
^C抓捕2号进程成功
^\抓捕3号进程成功
^Z抓捕20号进程成功