Although the previous program can implement simple communication, there are still deficiencies
1. when one of the interruptions exits by ctdl + C, the other interruption will be out of control. So what can be used to solve this problem? The semaphore is used, when the process intercepts the CTRL + C signal, it sends the USR signal to both parent and child processes. The child process exits and is killed by the parent process. Then, the parent process exits.
Client:
# Include <stdio. h> # include <stdlib. h> # include <sys/STAT. h> # include <sys/types. h> # include <fcntl. h> # include <string. h> # include <unistd. h> # include <sys/Wait. h> void stop () {kill (getpid (), 10); kill (getppid (), 12);} void childstop () {puts ("sub-process exited "); exit (0) ;}void fatherstop () {puts ("parent process exited"); exit (0) ;}int main () {pid_t PID; int RfD, WFD; char Buf [1024]; int Len; umask (0); signal (SIGINT, stop); pid = fork (); If (pid> 0) {signal (10, fatherstop); While (1) {WFD = open ("rdfifo", o_rdwr); If (WFD =-1) {perror ("client write wrong ");} memset (BUF, 0,100); // clear the following bufprintf ("client:"); fgets (BUF, 1024, stdin) during each use ); // Buf [strlen (BUF)-1] = '\ 0'; If (strcmp (BUF, "quit") = 0) {close (RFD ); unlink ("rdfifo"); wait (null); exit (0);} write (WFD, Buf, strlen (BUF);} else {signal (10, childstop); While (1) {While (RfD = open ("wrfifo", o_rdonly) =-1); memset (BUF ); // clear the following buflen = read (RfD, Buf, 1024) each time you use it; if (LEN =-1) {perror ("client read wrong ");} else {Buf [Len] = '\ 0'; printf ("server: % s", Buf) ;}} exit (0 );}
Server:
# Include <stdio. h> # include <stdlib. h> # include <sys/STAT. h> # include <sys/types. h> # include <fcntl. h> # include <string. h> # include <unistd. h> # include <sys/Wait. h> void stop () {kill (getpid (), 10); kill (getppid (), 12);} void childstop () {puts ("sub-process exited "); exit (0);} void fatherstop () {puts ("parent process exited"); exit (0);} int main (INT argc, char ** argv) {pid_t PID; int WFD, RFD; char Buf [1024]; int Len; umask (0); signal (SIGINT, stop); // signal (sigin T, sig_ign); // you need to set the signal pid = fork () when the CTRL + C signal is encountered. If (pid = 0) {signal (10, childstop); While (1) {While (RfD = open ("rdfifo", o_rdonly) =-1) {} memset (BUF, 0,100); // clear the following buflen = read (RfD, Buf, 1024) each time you use it; if (LEN =-1) {perror ("server write wrong"); exit (1) ;}else {// Buf [Len] = '\ 0'; printf ("client: % s ", buf) ;}} else if (pid> 0) {signal (10, fatherstop); While (1) {WFD = open ("wrfifo", o_rdwr ); if (WFD =-1) {perror ("Open Pipe Wrfifo wrong! "); Exit (1);} memset (BUF, 0,100); // clear the following Buf printf (" server: "); fgets (BUF, 1024, stdin); // get from standard input // Buf [strlen (BUF)-1] = '\ 0'; If (strcmp (BUF, "Q") = 0) {close (WFD); unlink ("wrfifo"); exit (0);} write (WFD, Buf, strlen (BUF) ;}} exit (0 );}
In this way, force exit will not be affected.