Embedded Linux inter-process communication (eight)--shared memory

Source: Internet
Author: User

embedded Linux Inter-process communication (eight)--shared memory one, shared memory

Shared memory allows two or more processes to share a given area of memory, which does not require replication between different processes, and is the fastest way to communicate between processes. Using shared memory The only thing to note is the simultaneous access to a given store between multiple processes, but the shared memory itself does not provide a synchronization mechanism, typically using semaphores to synchronize access to shared memory.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/DF/wKioL1d-_kLhX-qPAABWE8PpGO4866.png "title=" Picture 1.png "alt=" Wkiol1d-_klhx-qpaabwe8ppgo4866.png "/>

Shared memory programming Process: Create shared memory, map shared memory, use shared memory, Undo map operations, delete shared memory

1. Create shared Memory

#include <sys/ipc.h>

#include <sys/shm.h>

int Shmget (key_t key, size_t size, int shmflg);

Key: not 0 integers

size : Specify the amount of memory that needs to be shared in bytes

SHMFLG : Permission Flags , can be associated with ipc_creat do or operate

When the Shmget function succeeds, it returns a Shared memory identifier (non-negative integer) associated with the key. The call failed to return -1.

2. Mapping Shared Memory

#include <sys/types.h>

#include <sys/shm.h>

void *shmat (int shmid, const void *shmaddr, int shmflg);

shm_id is The shared memory identity returned by the Shmget function.

SHM_ADDR Specifies that the shared memory is connected to the address location in the current process, which is usually empty, indicating that the system chooses the address of the shared memory.

SHM_FLG is a set of flag bits, typically 0.

When the call succeeds, returns a pointer to the first byte of shared memory if the call fails to return -1.

3. Using Shared memory

shared memory can be manipulated using an IO function without a cache

4. Undo the mapped shared memory operation

#include <sys/types.h>

#include <sys/shm.h>

int SHMDT (const void *SHMADDR);

detach shared memory from the current process , shmaddr is The address pointer returned by the SHMAT function, returns 0 upon successful invocation , and 1 on Failure .

5. Delete Shared memory

#include <sys/ipc.h>

#include <sys/shm.h>

int shmctl (int shmid, int cmd, struct shmid_ds *buf);

shm_id is The shared memory identifier returned by the Shmget function.

Command is the action to take, there are three kinds of actions:

Ipc_stat: Sets the data in the SHMID_DS structure to the current associated value of shared memory, which overwrites the value of Shmid_ds with the current associated value of shared memory .

Ipc_set: If the process has sufficient permissions, set the current association value of shared memory to the value given in the SHMID_DS structure

Ipc_rmid: Deleting shared memory Segments

BUF is a struct pointer that points to the structure of shared memory mode and access rights.

the SHMID_DS structure includes at least the following members:

struct SHMID_DS

{

uid_t Shm_perm.uid;

uid_t Shm_perm.gid;

mode_t Shm_perm.mode;

};

second, shared memory programming example

To use shared memory for interprocess communication program instances:

Shmdata.h file (defines the data structure for shared memory):

#ifndef shmdata_h#define shmdata_h #define data_size typedef struct SHM{INT flag;//non 0, readable, 0, writable unsigned char data[data _size];//data}share_memory; #endif

SHMREAD.C:

#include  <unistd.h>   #include  <stdlib.h>   #include  <stdio.h >   #include  <string.h> #include  <sys/shm.h>   #include  <sys /types.h> #include  <sys/ipc.h> #include   "Shmdata.h"  int main (int argc,  CHAR&NBSP;**ARGV) {Int shmid;void *shm = null;share_memory *shmdata;key_t key  = ftok ("./shmread.c",  ' K '); Shmid = shmget (Key, sizeof (share_memory),  0666| Ipc_creat), if (shmid == -1) {fprintf (stderr,  "shmget failed.\n"); exit (-1);} Shm = shmat (shmid, 0, 0); if (shm ==  (void *)-1) {fprintf (stderr, ) Shmat  failed.\n "); exit (-1);} fprintf (stdout,  "sharememory at 0x%x\n",  shm);shmdata =  (share_memory *) SHM ; Shmdata->flag = 1;int read = 1;while (Read) {if (Shmdata->flag) {printf ("Share  memorY data:%s\n ",  shmdata->data); shmdata->flag = 0;} else{printf ("waiting...\n");} Sleep (1);} if (SHMDT (SHM)  == -1) {fprintf (stderr,  "shmdt failed.\n"); exit (-1);} if (Shmctl (shmid, ipc_rmid, 0)  == -1) {fprintf (stderr,  "shmdt failed.\n"); exit (-1) ;} return 0;}

shmwrite.c:

#include  <unistd.h>   #include  <stdlib.h>   #include  <stdio.h >   #include  <sys/shm.h>   #include  <string.h> #include  <sys /types.h> #include  <sys/ipc.h> #include   "Shmdata.h"  int main (int argc,  CHAR&NBSP;**ARGV) {int shmid;void *shm = null;share_memory * shmdata;key_t  Key = ftok ("./shmread.c",  ' K '); Shmid = shmget (key, sizeof (share_memory),  0666| ipc_creat);char buffer[128] =  "Hello wolrd", if (shmid == -1) {fprintf (stderr,   "shmget failed.\n"); exit (-1);} Shm = shmat (shmid, 0, 0); if (shm ==  (void *)-1) {fprintf (stderr, ) Shmat  failed.\n "); exit (-1);} fprintf (stdout,  "sharememory at 0x%x\n",  shm);shmdata =  (share_memory *) SHM ; Int read = 1;while (Read) {if (SHMDATA-&GT;FLag == 0) {printf ("writing...\n"); strncpy (shmdata->data, buffer,sizeof (buffer));shmdata-> flag = 1;} else{printf ("waiting...\n");} Sleep (1);} if (SHMDT (SHM)  == -1) {fprintf (stderr,  "shmdt failed.\n"); exit (-1);} return 0;}

This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Embedded Linux inter-process communication (eight)--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.