首页 > 代码库 > linux下实现两人、三人无序对话功能

linux下实现两人、三人无序对话功能

 对于两个程序之间的进行交互式

 

本程序主要通过父进程创建两个子进程,通过管道来实现,和两人无序对话的功能一样。只要逻辑清晰,并不难。共需要pipe(有名管道)六根,功能为用于读、写,为了使逻辑清晰,方便讨论,以下1、2、3分别代表程序1、2、3之间的管道,分别对程序之间的管道进项讨论分析:  

ABC
1-2write1-2read1-3read
1-3write2-1write3-1write
2-1read2-3write2-3read
3-1read3-2read3-2write
 以上表格表示的具体含义我在这里举例说明一下。eg:对于A(聊天猪头)而言共有四根管道与其相连,两根用于读,另外两根用于写,1-2管道代表A、B之间的管道A需要进行写操作,而B进行读操作。 
  • 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 102416 int main(int argc, const char *argv[])//12,13,21,3117 {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  r33         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  r44         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  w56     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 }
1.c
  • 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 102416 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  r34         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  r46         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  w58     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 }
2.c
  • 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 102415 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  r33         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  r44         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  w56     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 }
3.c

 PS:在命令行参数中还需注意的,假设1.c、2.c、3.c的可执行文件为A.out、B.out、C.out。命令行参数分别为:(其实和表格里分析的一样)

            1. ./A.out   12.fifo  13.fifo  21.fifo   31.fifo
            2. ./B.out   12.fifo  21.fifo  23.fifo   32.fifo
            3. ./C.out   13.fifo  31.fifo  23.fifo   32.fifo

 

linux下实现两人、三人无序对话功能