Linux process communication (shared memory area)

Source: Internet
Author: User

Shared memory is a chunk of memory (segment) that is set aside by the kernel for the purpose of exchanging information between multiple processes. If the permissions of a segment are set appropriately, each process that accesses the memory of that segment can image it into its own private address space. If one process updates the data in the segment, other processes will see the update immediately. A segment created by one process, or read and written by another process. Each process puts its own image of the shared memory into its own address space.
Create a shared memory area int shmget (key_t key,size_t size,int shm-flg), parameter key can be ipc_private (ipc_private means that the system assigns a key), It can also be a keyword returned by the Ftok function. Argument size Specifies the size of the segment. Parameter Flgs-eight binary number,0666, converted to binary after the rw-rw-rw-Shmget successfully returned the segment identifier, failed to return 1(typically does not create a failure unless the system does not have memory) .
//Create shared Memory#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/ipc.h>#include<sys/shm.h>intMainintArgChar*args[]) {    intShmid=shmget (Ipc_private,sizeof(Char)*1024x768,0666); if(shmid==-1) {printf ("error\n"); return-1; } printf ("Create shared memory successfully! Memory segment identifier is%d\n", Shmid); return 0;}
Execution  on the command line shows that a chunk of shared memory has been created successfully. The Nattch field shows the number of processes that have been attached to this memory area. 

additional shared memory area void * Shmat (int shmid,void * shmaddr,int  SHMFLG); int shmdt (constvoid-m display). Always set the parameter shmaddr to 0,0 to indicate that the system automatically creates a memory segment within the attached process and maps to the shared memory area. Of course, you can also specify a piece of memory that is attached to the process, but it requires the user to allocate memory to malloc, and pass the address to the SHMADDR parameter, which is more troublesome and is usually automatically assigned by the system. The parameter shmflg can be shm_rdonly, which means that the additional segment is read-only. Shmat successfully returned the address of the segment that was appended, failed to return -1, and set errno. The function shmdt is to detach the segment attached to SHMADDR from the address space of the calling process, which must be returned by Shmat (that is, releasing the attached shared memory from within its own process, emphasizing that it is not the shared memory that freed the system). Note: The size of the additional shared memory area in the process must be as large as the system shared memory, for easy mapping (two blocks of memory are not as large how to synchronize data?). )。 

Linux process Communication (shared memory area)

Related Article

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.