Today we will use the famous pipe mkfifo to realize a simple version of QQ dialogue function.
Step 1: first create two processes Talka C and talkb c;
Step 2: talk a C and talkb C is responsible for checking whether two pipelines have been created. If not, it is necessary to create pipelines;
Step 3: talk a C open pipe 1 in a write only manner; Open pipe 2 in a read-only manner; Then write and read data circularly
while(1) { Get keyboard entry fgets Write pipeline 1 Read pipeline 2 }
Step 4: talk B C open pipe 1 in a read-only manner; Open pipe 2 in a write only manner; Cyclic read / write data
while(1) { Read pipeline 1 Get keyboard entry fgets Write pipeline }
After completing the above steps, you can write the code. After writing the code, run as follows:
talkA.c. operation results:
talkB. Operation results of C
Through observation, you will find that only when A sends A signal to B, then B can receive the signal sent by A; However, if you continuously send several signals from A to B, B can only receive one signal, and the remaining signals are blocked in it. Only after B sends another signal to A can it receive the signal blocked in the buffer.
terms of settlement:
Add parent-child processes in A and B respectively, put the "read" in A in the parent process and the "write" in A in the child process;
Similarly: put the "read" in B in the parent process and the "write" in A in the child process.
In this way, you can realize the freedom of chat. You don't have to worry that after sending several signals continuously, the other party will only receive one signal.
Next, let's look at two codes:
First look at talk a c
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <sys/stat.h> 5 #include <sys/types.h> 6 #include <stdlib.h> 7 #include <string.h> 8 int main() 9 { 10 int ret=access("fifo1",F_OK); 11 if(ret==-1) 12 { 13 printf("No pipes, creating...."); 14 ret=mkfifo("fifo1",0664); 15 if(ret==-1) 16 { 17 perror("mkfifo"); 18 exit(0); 19 } 20 } 21 ret=access("fifo2",F_OK); 22 if(ret==-1) 23 { 24 printf("No pipes, creating...."); 25 ret=mkfifo("fifo1",0664); 26 if(ret==-1) 27 { 28 perror("mkfifo"); 29 exit(0); 30 } 31 } 32 33 int fd1=open("fifo1",O_WRONLY);//Open pipeline in write only mode 34 if(fd1==-1) 35 { 36 perror("open"); 37 exit(0); 38 } 39 40 int fd2=open("fifo2",O_RDONLY);//Open pipe in read-only mode 41 if(fd2==-1) 42 { 43 perror("open"); 44 exit(0); 45 } 46 pid_t pid=fork(); 47 char str1[128]; 48 if(pid>0) 49 { 50 while(1) 51 { 52 memset(str1,0,128);//Empty the buffer every cycle 53 fgets(str1,128,stdin);//stdin standard input 54 int ret=write(fd1,str1,strlen(str1)); 55 if(ret==-1) 56 { 57 perror("write"); 58 exit(0); 59 } 60 } 61 } 62 if(pid==0) 63 { 64 while(1) 65 { 66 //5. Read pipeline data 67 memset(str1,0,128); 68 int ret1=read(fd2,str1,128); 69 if(ret1<=0) 70 { 71 printf("read failure....,\n"); 72 exit(0); 73 } 74 printf("You say?:%s\n",str1); 75 } 76 } 77 close(fd1); 78 close(fd2); 79 return 0; 80 }
talkB.c
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <sys/stat.h> 5 #include <sys/types.h> 6 #include <stdlib.h> 7 #include <string.h> 8 int main() 9 { 10 int ret=access("fifo1",F_OK); 11 if(ret==-1) 12 { 13 printf("No pipes, creating...."); 14 ret=mkfifo("fifo1",0664); 15 if(ret==-1) 16 { 17 perror("mkfifo"); 18 exit(0); 19 } 20 } 21 ret=access("fifo2",F_OK); 22 if(ret==-1) 23 { 24 printf("No pipes, creating...."); 25 ret=mkfifo("fifo2",0664); 26 if(ret==-1) 27 { 28 perror("mkfifo"); 29 exit(0); 30 } 31 } 32 int fd1=open("fifo1",O_RDONLY);//Open pipe in read-only mode 33 if(fd1==-1) 34 { 35 perror("open"); 36 exit(0); 37 } 38 39 int fd2=open("fifo2",O_WRONLY);//Open pipeline in write only mode 40 if(fd2==-1) 41 { 42 perror("open"); 43 exit(0); 44 } 45 pid_t pid=fork(); 46 char buf[128]; 47 if(pid>0) 48 { 49 while(1) 50 { 51 memset(buf,0,128);//Empty the buffer every cycle 52 int ret=read(fd1,buf,128);//Read operation 53 if(ret<=0) 54 { 55 perror("read"); 56 exit(0); 57 } 58 printf("You say?:%s\n",buf); 59 60 } 61 } 62 if(pid==0) 63 { 64 while(1) 65 { 66 //Write operation 67 memset(buf,0,128); 68 fgets(buf,128,stdin); 69 int ret1=write(fd2,buf,strlen(buf)); 70 if(ret1==-1) 71 { 72 perror("write"); 73 exit(0); 74 } 75 76 } 77 } 78 close(fd1); 79 close(fd2); 80 return 0; 81 }
Try it yourself after you finish writing it. It's great.
Such a simple version of the chat function is done. If you give your girlfriend this gift on Valentine's day, you can't tell how happy the other party is!