One: Task description
A,Btwo processes communicate through the pipeline, like they used to chat with one another, and thenAeach time the process receives data throughA1process Display (a new process for displayingAreceiving information),Aand theA1shared memory is used for data transfer betweenB1process, for displayingBthe information that the process received. ForA,Bprocess, when exiting withCtrl + Cwhen the corresponding signal is received, the process can clean up the resource through the signal processing function.Exitexits the process. (A1,B1, you can manually close it). The interface diagram is as follows.
two : Code show
A process
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/shm.h> #include < sys/sem.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/time.h> #include <fcntl.h > #include <sys/types.h> #include <sys/stat.h> #include <signal.h>int shm_id;struct t{Char buf[1 28];}; struct T *p;void hanle (int sig) {SHMDT (P); Shmctl (shm_id,ipc_rmid,null);//delete shared memory printf ("delete ok........\n"); Exit (0);} int main () {//a process signal (SIGINT,HANLE);//capture CTRL + C signal Shm_id=shmget ((key_t) 1234,4096,0600| Ipc_creat);//Create a shared memory//struct T *p; p= (struct t*) Shmat (shm_id,null,0);//Get the memory pointer structure int F1; F1=open ("1.fifo", o_wronly);//Open write-end int F2 to 1; F2=open ("2.fifo", o_rdonly);//to 2 open read-end fd_set Readset; Defines the readable set int maxfd; Maxfd=stdin_fileno > F2? stdin_fileno+1:f2+1; struct Timeval check; check.tv_sec=1;//Setting the viewing time is one second Char buf[128]; while (1) {Fd_zero (&readset);//Initialize Fd_set (stdin_fileno,&readset);//Add Monitoring object Fd_set (F2 , &readset); Select (Maxfd,&readset,null,null,&check);//Returns a readable monitor object if (Fd_isset (Stdin_fileno,&readset)) {//IF Monitoring to the 1 pipeline has standard input so get to the data written into the pipeline memset (buf,0,sizeof (BUF)); Fgets (Buf,sizeof (BUF), stdin); Buf[strlen (BUF) -1]= ' + '; Write (f1,buf,sizeof (BUF));//written to pipe 1} if (Fd_isset (F2,&readset)) {//monitored into Pipe 2 can be read memset (buf,0,sizeof (BUF)); Read (f2,buf,sizeof (BUF));//reads from pipeline 2 to buf strcpy (P->BUF,BUF);//The BUF read in pipeline 2 to the shared memory printf ("From 2:%s\ n ", buf); } if (strcmp (buf, "Bye") ==0) {break; }} exit (0);}
b Process
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/shm.h> #include < sys/sem.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/time.h> #include <fcntl.h > #include <sys/types.h> #include <sys/stat.h>int shm_id;struct t{char buf[128];}; struct T *p;void hanle (int sig) {SHMDT (P); Shmctl (shm_id,ipc_rmid,null);//delete shared memory printf ("delete ok........\n"); Exit (0);} int main () {//b process signal (SIGINT,HANLE); Shm_id=shmget ((key_t) 1235,4096,0600| Ipc_creat);//struct T *p; p= (struct t*) Shmat (shm_id,null,0); int F1; F1=open ("1.fifo", o_rdonly);//to 1 open read-end int f2; F2=open ("2.fifo", o_wronly);//to 2 open write-end fd_set Readset; Defines the readable set int maxfd; Maxfd=stdin_fileno > F1? stdin_fileno+1:f1+1; struct Timeval check; check.tv_sec=1;//set viewing time is one second char buf[128]; while (1) {Fd_zero (& readset);//Initialize Fd_set (stdin_fileno,&readset);//Add Monitoring Object Fd_set (F1,&readset); Select (Maxfd,&readset,null,null,&check);//Returns a readable monitor object if (Fd_isset (Stdin_fileno,&readset)) {//IF Monitoring to the 1 pipeline has standard input so get to the data written into the pipeline memset (buf,0,sizeof (BUF)); Fgets (Buf,sizeof (BUF), stdin); Buf[strlen (BUF) -1]= ' + '; Write (f2,buf,sizeof (BUF));//written to pipe 2} if (Fd_isset (F1,&readset)) {//Monitor to Pipe 2 can read MEMS ET (buf,0,sizeof (BUF)); Read (f1,buf,sizeof (BUF));//reads strcpy (P->BUF,BUF) from pipe 1;//places the read-out characters into the shared memory printf ("From 1:%s\n", buf); } if (strcmp (buf, "Bye") ==0) {break; }} exit (0);}
A1 Process
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/shm.h> #include < String.h>int shm_id;struct t{ char buf[128];}; struct T *p;void hanle (int sig) { SHMDT (p); Shmctl (shm_id,ipc_rmid,null);//delete shared memory printf ("delete ok........\n"); Exit (0);} int main () { shm_id=shmget (key_t) 1234,4096,0600| Ipc_creat); Acquired shared memory// struct T *p; p= (struct t*) Shmat (shm_id,null,0);//This also got char s[128]={0}; while (1) { if (strcmp (s,p->buf)!=0) {//string is not the same then print out strcpy (S,P->BUF); printf ("From B :%s\n", p->buf); } } Exit (0);}
B1 process
<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/shm.h> #include <string.h>int shm_id;struct t{ char buf[128];}; struct T *p;void hanle (int sig) { SHMDT (p); Shmctl (shm_id,ipc_rmid,null);//delete shared memory printf ("delete ok........\n"); Exit (0);} int main () { shm_id=shmget (key_t) 1235,4096,0600| Ipc_creat); Acquired shared memory// struct T *p; p= (struct t*) Shmat (shm_id,null,0);//This also got char s[128]={0}; while (1) { if (strcmp (s,p->buf)!=0) {//string is not the same then print out strcpy (S,P->BUF); printf ("From A :%s\n", p->buf); } } Exit (0);} </span><span style= "FONT-SIZE:18PX;" ></span>
Three: Results show
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Code--QQ Chat implementation (based on the Linux pipeline Signal sharing memory)