Linux_c Development (5-4) interprocess communication _ Shared memory communication

Source: Internet
Author: User

Shared memory

Shared Memory is a portion of physical memory that is shared by multiple processes. Shared memory is the quickest way to share data between processes, where a process writes data to shared memory, and all processes that share that memory can immediately see the contents.

The shared memory implementation is divided into two steps
1. Create shared memory and use the Shmget function.
2. Map the shared memory and map the created shared memory to the specific process space, using the Shmat function.

int shmget(key_t key,int size,int shmflg)

Key flag for Shared memory: O/ipc_private. When the value of key is Ipc/private, the function shmget () Creates a new shared memory, if the value of key is 0, and the IPC_PRIVATE flag is set in the parameter SHMFLG. A new shared memory is also created.
return Value: returns the shared memory identifier if successful, or 1 if it fails.

Mapping
int shmat(int shmat,char *shmaddr,int flag)

Parameters:
? Shmid: shared storage identifier returned by the Shmget () function
? flag: determines in what way to determine the address of the map (typically 0)
return Value:
If successful, returns the shared memory map to the address in the process, or 1 if it fails.
When a process does not need to share memory, it needs to be detached from the process address space.

int shmdt(char *shmaddr)

Header file: shm_com.h

#define TEXT_SZ 2048struct shared_use_st{    int written_by_you;    char some_text[TEXT_SZ];};

Program 1 for reading the data in shared memory
shm1.c

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include "shm_com.h"/* * Program Entry * */intMainvoid){intrunning=1;void*shared_memory= (void*)0;structShared_use_st *shared_stuff;intShmid;/ * Create shared memory * /Shmid=shmget ((key_t)1234,sizeof(structShared_use_st),0666| Ipc_creat);if(shmid==-1)    {fprintf(stderr,"Shmget failed\n");Exit(exit_failure); }/ * Map Shared memory * /Shared_memory=shmat (Shmid, (void*)0,0);if(shared_memory== (void*)-1)    {fprintf(stderr,"Shmat failed\n");Exit(exit_failure); }printf("Memory attached at%x\n",(int) shared_memory);/ * Let the struct pointer point to this shared memory * /Shared_stuff= (structShared_use_st *) Shared_memory;/* Control read/write order */shared_stuff->written_by_you=0;/ * Loops the data from shared memory until "end" is read * /     while(running) {if(shared_stuff->written_by_you) {printf("You wrote:%s", Shared_stuff->some_text); Sleep1);//Read process sleep a second, and will cause the write process to sleep a second, so do read it and then writeshared_stuff->written_by_you=0;if(strncmp(Shared_stuff->some_text,"End",3)==0) {running=0;//End Loop}       }    }/ * Delete Shared memory * /    if(SHMDT (shared_memory) ==-1)    {fprintf(stderr,"Shmdt failed\n");Exit(exit_failure); }Exit(exit_success);}

Program 2, create shared memory, share data

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include "shm_com.h"/* * Program Entry * */intMainvoid){intrunning=1;void*shared_memory= (void*)0;structShared_use_st *shared_stuff;CharBuffer[bufsiz];intShmid;/ * Create shared memory * /Shmid=shmget ((key_t)1234,sizeof(structShared_use_st),0666| Ipc_creat);if(shmid==-1)    {fprintf(stderr,"Shmget failed\n");Exit(exit_failure); }/ * Map Shared memory * /Shared_memory=shmat (Shmid, (void*)0,0);if(shared_memory== (void*)-1)    {fprintf(stderr,"Shmat failed\n");Exit(exit_failure); }printf("Memory attached at%x\n",(int) shared_memory);/ * Let the struct pointer point to this shared memory * /Shared_stuff= (structShared_use_st *) Shared_memory;/ * Writes the data to the shared memory until the "End" is written * /     while(running) { while(shared_stuff->written_by_you==1) {Sleep (1);//Wait until the reading process is finished and then write            printf("Waiting for client...\n"); }printf("Ener some text:"); Fgets (Buffer,bufsiz,stdin);strncpy(SHARED_STUFF-&GT;SOME_TEXT,BUFFER,TEXT_SZ); shared_stuff->written_by_you=1;if(strncmp(Buffer,"End",3)==0) {running=0;//End Loop}    }/ * Delete Shared memory * /    if(SHMDT (shared_memory) ==-1)    {fprintf(stderr,"Shmdt failed\n");Exit(exit_failure); }Exit(exit_success);}

Shared Memory Communication test results:

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

Linux_c Development (5-4) interprocess communication _ Shared memory communication

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.