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 run independently of each other.

3) The parent process finishes writing the content. Also, ensure that the shared memory is deleted after the child process exits.

4) The sub-process finishes 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) {//parent process completes write Char *f to 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 you 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 modify 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 shared memory value.


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 determine that the current in-memory lock is open and closed state 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)                {///If 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 to determine 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)                {///If 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 can only achieve a process of continuous writing 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 puts it in shared memory.

3) The B process takes out the shared memory data and then displays 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 to determine 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)                {///If 0 is locked 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 to determine 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)                {///If 0 is locked then set the value                 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;}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.