linux下实现两人、三人无序对话功能
对于两个程序之间的进行交互式
本程序主要通过父进程创建两个子进程,通过管道来实现,和两人无序对话的功能一样。只要逻辑清晰,并不难。共需要pipe(有名管道)六根,功能为用于读、写,为了使逻辑清晰,方便讨论,以下1、2、3分别代表程序1、2、3之间的管道,分别对程序之间的管道进项讨论分析:
A | B | C | |||
1-2 | write | 1-2 | read | 1-3 | read |
1-3 | write | 2-1 | write | 3-1 | write |
2-1 | read | 2-3 | write | 2-3 | read |
3-1 | read | 3-2 | read | 3-2 | write |
- A:第一人
1 /*============================================ 2 > Copyright (C) 2014 All rights reserved. 3 > FileName:1.c 4 > author:donald 5 > date:2014/08/22/ 20:28:53 6 > details: 7 ==============================================*/ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <sys/types.h> 13 #include <sys/stat.h> 14 #include <fcntl.h> 15 #define N 1024 16 int main(int argc, const char *argv[])//12,13,21,31 17 { 18 if(mkfifo(argv[1],0666) == -1 || mkfifo(argv[2],0666) == -1){ 19 perror("mkfifo"); 20 exit(1); 21 } 22 23 int fd_12,fd_13,fd_21,fd_31; 24 char buf[N]; 25 fd_12 = open(argv[1],O_WRONLY); 26 fd_13 = open(argv[2],O_WRONLY); 27 fd_21 = open(argv[3],O_RDONLY); 28 fd_31 = open(argv[4],O_RDONLY); 29 30 printf("open sucess\n"); 31 32 if(fork() == 0){//21 r 33 close(fd_13); 34 close(fd_12); 35 close(fd_31); 36 while(memset(buf,0,N),read(fd_21,buf,N) != 0){ 37 printf("from 2:"); 38 write(1,buf,strlen(buf)); 39 } 40 close(fd_21); 41 exit(1); 42 } 43 if(fork() == 0){//31 r 44 close(fd_13); 45 close(fd_12); 46 close(fd_21); 47 while(memset(buf,0,N),read(fd_31,buf,N) != 0){ 48 printf("from 3:"); 49 write(1,buf,strlen(buf)); 50 } 51 close(fd_31); 52 exit(1); 53 } 54 55 //12 13 w 56 close(fd_21); 57 close(fd_31); 58 while(memset(buf,0,N),fgets(buf,N,stdin) != NULL){ 59 write(fd_13,buf,strlen(buf)); 60 write(fd_12,buf,strlen(buf)); 61 } 62 close(fd_13); 63 close(fd_12); 64 wait(NULL); 65 wait(NULL); 66 67 unlink(argv[1]); 68 unlink(argv[2]); 69 printf("program 1 over\n"); 70 return 0; 71 }
- B:第二人
1 /*============================================ 2 > Copyright (C) 2014 All rights reserved. 3 > FileName:2.c 4 > author:donald 5 > date:2014/08/22/ 20:29:02 6 > details: 7 ==============================================*/ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <sys/types.h> 13 #include <sys/stat.h> 14 #include <fcntl.h> 15 #define N 1024 16 int main(int argc, const char *argv[]) 17 { 18 19 if(mkfifo(argv[2],0666) == -1 || mkfifo(argv[3],0666) == -1){ 20 perror("mkfifo"); 21 exit(1); 22 } 23 24 int fd_12,fd_21,fd_23,fd_32; 25 char buf[N]; 26 fd_12 = open(argv[1],O_RDONLY); 27 fd_21 = open(argv[2],O_WRONLY); 28 fd_23 = open(argv[3],O_WRONLY); 29 fd_32 = open(argv[4],O_RDONLY); 30 31 printf("open success\n"); 32 33 if(fork() == 0){//12 r 34 close(fd_32); 35 close(fd_21); 36 close(fd_23); 37 while(memset(buf,0,N),read(fd_12,buf,N) != 0){ 38 printf("from 1:"); 39 write(1,buf,strlen(buf)); 40 } 41 close(fd_12); 42 exit(1); 43 } 44 45 if(fork() == 0){//32 r 46 close(fd_12); 47 close(fd_21); 48 close(fd_23); 49 while(memset(buf,0,N),read(fd_32,buf,N) != 0){ 50 printf("from 3:"); 51 write(1,buf,strlen(buf)); 52 } 53 close(fd_32); 54 exit(1); 55 } 56 57 //21 23 w 58 close(fd_12); 59 close(fd_32); 60 while(memset(buf,0,N),fgets(buf,N,stdin) != NULL){ 61 write(fd_21,buf,strlen(buf)); 62 write(fd_23,buf,strlen(buf)); 63 } 64 close(fd_21); 65 close(fd_23); 66 wait(NULL); 67 wait(NULL); 68 69 unlink(argv[2]); 70 unlink(argv[3]); 71 printf("program 2 over\n"); 72 return 0; 73 }
- C:第三人
1 /*============================================ 2 > Copyright (C) 2014 All rights reserved. 3 > FileName:3.c 4 > author:donald 5 > date:2014/08/22/ 20:29:13 6 > details: 7 ==============================================*/ 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <sys/types.h> 12 #include <sys/stat.h> 13 #include <fcntl.h> 14 #define N 1024 15 int main(int argc, const char *argv[]) 16 { 17 18 if(mkfifo(argv[2],0666) == -1 || mkfifo(argv[4],0666) == -1){ 19 perror("mkfifo"); 20 exit(1); 21 } 22 23 int fd_13,fd_31,fd_23,fd_32; 24 char buf[N]; 25 fd_13 = open(argv[1],O_RDONLY); 26 fd_31 = open(argv[2],O_WRONLY); 27 fd_23 = open(argv[3],O_RDONLY); 28 fd_32 = open(argv[4],O_WRONLY); 29 30 printf("open success\n"); 31 32 if(fork() == 0){//13 r 33 close(fd_31); 34 close(fd_23); 35 close(fd_32); 36 while(memset(buf,0,N),read(fd_13,buf,N) != 0){ 37 printf("from 1:"); 38 write(1,buf,strlen(buf)); 39 } 40 close(fd_13); 41 exit(1); 42 } 43 if(fork() == 0){//23 r 44 close(fd_13); 45 close(fd_31); 46 close(fd_32); 47 while(memset(buf,0,N),read(fd_23,buf,N) != 0){ 48 printf("from 2:"); 49 write(1,buf,strlen(buf)); 50 } 51 close(fd_23); 52 exit(1); 53 } 54 55 //31 32 w 56 close(fd_13); 57 close(fd_23); 58 while(memset(buf,0,N),fgets(buf,N,stdin) != NULL){ 59 write(fd_31,buf,strlen(buf)); 60 write(fd_32,buf,strlen(buf)); 61 } 62 close(fd_31); 63 close(fd_32); 64 wait(NULL); 65 wait(NULL); 66 67 unlink(argv[2]); 68 unlink(argv[4]); 69 printf("program 3 over\n"); 70 return 0; 71 }
PS:在命令行参数中还需注意的,假设1.c、2.c、3.c的可执行文件为A.out、B.out、C.out。命令行参数分别为:(其实和表格里分析的一样)
- ./A.out 12.fifo 13.fifo 21.fifo 31.fifo
- ./B.out 12.fifo 21.fifo 23.fifo 32.fifo
- ./C.out 13.fifo 31.fifo 23.fifo 32.fifo
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。