20 Oct 2014

What is shared memory segment placing data complete example

Allocating A Shared Memory Segment

A shared memory segment first needs to be allocated (created), using the shmget() system call. This call gets a key for the segment (like the keys used in msgget() and semget()), the desired segment size, and flags to denote access permissions and whether to create this page if it does not exist yet. shmget() returns an identifier that can be later used to access the memory segment. Here is how to use this call: 
/* this variable is used to hold the returned segment identifier. */
int shm_id;
/* allocate a shared memory segment with size of 2048 bytes,      */
/* accessible only to the current user.                             */
shm_id = shmget(100, 2048, IPC_CREAT | IPC_EXCL | 0600);
if (shm_id == -1) {
    perror("shmget: ");

If several processes try to allocate a segment using the same ID, they will all get an identifier for the same page, unless they defined 
IPC_EXCL in the flags to shmget(). In that case, the call will succeed only if the page did not exist before.

How To Become Expert In Linux/Unix

No comments:

Post a Comment