Inter-process communication ipc-memory sharing

Source: Internet
Author: User

Function:

(1) int shmget (key_t key, int size, int shmflg), open or use a piece of shared memory.

(2) void *shmat (int shmid, const void *shmaddr, int shmflg), which connects the shared memory pointed to by the parameter shmid to the current process. when using a shared memory, you need to use Shmat to reach the connection.

(3) int shmdt (const void *SHMADDR) to disassociate the shared memory previously connected with Shmat from the current process. The parameter shmaddr is the address of the shared memory returned by Shmat. after you have completed the use of shared memory, you need to use SHMDT to unblock the connection.

(4) int shmctl (int shmid, int cmd, struct shmid_ds *buf), controls the operation of memory. When CMD is Ipc_rmid, delete the shared memory referred to by Shmid.

Instance:

file: shmshare.c#include<stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/ ipc.h> #include <sys/shm.h>int main (int argc, char* argv[]) {         int shmid = shmget (ipc_private, 1024, ipc_creat |  0666);        if  (shmid < 0)          {                 perror ("Shmget");                 exit (exit_failure);        }         printf ("create shared memory ok, size = 1024,  shmid = %d\n ",  shmid);         char* buff  =  (char*) Shmat (Shmid, null, 0);        if  ((int) buff == -1)          {                 perror ("Shmat");                 exit (exit_failure);        }         memset (buff, 0, 1024);         char temp[1024] =  ""; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", &NBSP;TEMP);         strncpy (buff, temp, 1024);         shmctl (shmid, ipc_rmid, null);         return 0;}

file: shmshare2.c#include<stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/ ipc.h> #include <sys/shm.h> #include <assert.h>int main (int argc, char* argv[]) {        assert (argc == 2);         int shmid = atoi (argv[1]);         char* buff =  (char*) Shmat (shmid, null, 0);         if  ((int) buff == -1)         {                 perror ("Shmat");                 exit (EXIT_FAILURE);         }        while (1)        &Nbsp; {                if   (buff[0])                  {                         printf ("buff:%s\n",  buff);                          break;                }  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SHMDT (Buff);         return 0;}

$GCC-wall-o shmshare shmshare.c$./shmsharecreate shared memory OK, size = 1024x768, Shmid = 229377<wait for input>

$GCC-wall-o shmshare2 shmshare2.c$./shmshare2 229377print <wait for input>

Summary: shared memory is the most efficient of various communication methods, but there are some problems, such as multi-process, multi-threaded access to shared memory synchronization problem. all kinds of communication way Datong small easy, the principle is similar, are provided by the system to provide the means of communication support. From Message Queuing, semaphores can be seen, this series of POSIX IPC is similar in the way, see the Man manual can learn more usage.

Inter-process communication ipc-memory sharing

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.