Code--QQ Chat implementation (based on the Linux pipeline Signal sharing memory)

Source: Internet
Author: User
Tags readable stdin strcmp

One: Description of tasks

             a b Two processes communicate through pipelines, like they used to chat with each other, and then a process receives data each time through a1 process display (a new process for displaying a The information received), a a1 The data transfer between the use of shared memory, corresponding to a b1 process. Used to display the information received by the b process.

For A,B process, exit with CTRL + C exit, when the corresponding signal is received. The process can be cleaned by the signal processing function, and exit exits the process after cleanup . (A1,B1, manual shutdown is possible). interface diagrams such as the following.


Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

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;//set viewing time is one second char buf[128]; while (1) {Fd_zero (&readset);//Initialize Fd_set (stdin_fileno,&readset);//Join the Monitoring object Fd_set (F2 , &readset); Select (Maxfd,&readset,null,null,&check);//Returns a readable monitor object if (Fd_isset (Stdin_fileno,&readset)) {//hypothesis 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)) {//Monitor to pipe 2 to 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);//Join the Monitoring Object Fd_set (F1,&readset); Select (Maxfd,&readset,null,null,&check);//Returns a readable monitor object if (Fd_isset (Stdin_fileno,&readset)) {//hypothesis           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 Pipeline 2} if (Fd_isset (F1,&readset)) {///monitored to be able to read MEMS in Pipe 2           ET (buf,0,sizeof (BUF));          Read (f1,buf,sizeof (BUF));//reads strcpy (P-&GT;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





Code--QQ Chat implementation (based on the Linux pipeline Signal sharing memory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.