Code--linux process communication (based on shared memory)

Source: Internet
Author: User

1. Communication of affinity process, parent write sub-read


thought analysis:1 First we need to create a shared memory.

2) The fork function is used to create a parent-child process. After the fork function is created, two processes are executed independently of each other.

3) The parent process finishes writing the content. At the same time, when you want to ensure that the child process exits, the shared memory is deleted.

4) The child process is finished reading the content.

Effect Show:




Code Show:

 #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ipc.h> #include <sys/shm.h> #include <errno.h>int main () {//parent-child process operation shared memory//create shared memory first write to shared memory        Child processes read INT flag to shared memory; Flag=shmget (ipc_private,4096,0600|        Ipc_creat);        Create a shared memory then return the identifier char buf[]={"I am Your Father\n"};        Char s[123];           if (fork ()!=0) {//The parent process finishes writing Char *f to the shared memory;           f= (char *) Shmat (flag,null,0);//connection of the parent process and shared memory returns the pointer to the first byte of the//memory memset (F, ' n ', 4096);//This time can operate F           strncpy (F, "I am You Father", 16);//write Content printf ("Parent%d write buf is%s\n", Getpid (), f);        Wait (NULL);//waits for child process Shmctl (flag,ipc_rmid,0);//delete shared memory exit (0);           } else {char *fp; Sleep (3);//Let the parent process have time to write fp= (char *) Shmat (flag,null,0);//The child process is connected to it and then returned to the character pointer printf ("Children PID is%d,read         BUF is%s\n ", Getpid (), FP);  Exit (0); }}

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------

2. Implement non-affinity communication. (Two processes change the value of shared memory)


thought analysis:1 First we need to create a shared memory.

2) Two processes to take a lock to access the value of shared memory.


Effect Show:









Code Show:

#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h>//writing process Then, to infer that the current in-memory lock is open and closed, the struct t{        int user_now;//defines a lock        int val;}; int main () {        int flag;        Flag=shmget ((key_t) 1234,4096,0600| Ipc_creat);        struct T *tt;        tt= (struct t*) Shmat (flag,null,0);//Get memory        tt->user_now=0;        while (1)        {          if (tt->user_now==0)                {//Assuming 0 is locked then set the value                 tt->val=1;                 printf ("I am Write, the value is%d\n", tt->val);                                        tt->user_now=1;//plus lock.

} sleep (1); } Shmdt (void *) TT); return 0;}

Write Process 2:
<pre name= "code" class= "OBJC" > #include <stdio.h> #include <stdlib.h> #include <string.h># Include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h>//write process constantly write and then infer that the current in-memory lock is open and closed state struct t{        int user_now;//define a lock        int Val;}; int main () {        int flag;        Flag=shmget ((key_t) 1234,4096,0600| Ipc_creat);        struct T *tt;        tt= (struct t*) Shmat (flag,null,0);//Get memory        tt->user_now=0;        while (1)        {          if (tt->user_now==1)                {//Assuming 0 is locked then set                 tt->val=2;                 printf ("I am Write2, the value is%d\n", tt->val);                               tt->user_now=0;//plus lock.                }          Sleep (1);        }        Shmdt (void *) TT);        Shmctl (flag,ipc_rmid,0);        return 0;}



--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------

3. Inter-Program dialogue (AB process can achieve dialogue only can achieve a process continuously write B process continuously read)

thought analysis:1 First we need to create a shared memory.

2) Establish two processes, a, B.

A process finishes accepting input from the keyboard and then placing it in shared memory.

3) The B process takes out the shared memory data. And then show it.

Effect Show:

                 

Code Show:

A process

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h >//write process constantly write and then infer that the current in-memory lock is open and closed state struct t{        int user_now;//define a lock        char buf[1024];}; int main () {        int flag;        Flag=shmget ((key_t) 1234,4096,0600| Ipc_creat);        struct T *tt;        tt= (struct t*) Shmat (flag,null,0);//Get memory        tt->user_now=0;        while (1)        {          if (tt->user_now==0)                {//Assuming 0 locks open then set the value                 read (stdin_fileno,tt->buf,1024);                 Place the keyboard input in the buf of shared memory                 tt->user_now=1;                }        memset (tt->buf,0,sizeof (TT->BUF));         Sleep (1);        }        Shmdt (void *) TT);        return 0;} <strong></strong>

b Process

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h >//write process constantly write and then infer that the current in-memory lock is open and closed state struct t{        int user_now;//define a lock        char buf[1024];}; int main () {        int flag;        Flag=shmget ((key_t) 1234,4096,0600| Ipc_creat);        struct T *tt;        tt= (struct t*) Shmat (flag,null,0);//Get memory        tt->user_now=0;        while (1)        {          if (tt->user_now==1)                {//Assuming 0 locks open then set the value of                 write (Stdout_fileno,tt->buf,strlen (tt- >BUF));                 memset (tt->buf,0,sizeof (TT->BUF));                 tt->user_now=0;                }          Sleep (1);        }        Shmdt (void *) TT);        Shmctl (flag,ipc_rmid,0);        return 0;}





Code--linux process communication (based on shared 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.