Inter process communication using Shared Memory

Explanation:

One of the simplest interprocess communication methods is using shared memory. Shared memory allows two or more processes to access the same memory as if they all called malloc and were returned pointers to the same actual memory. When one process changes the memory, all the other processes see the modification.

 Shared memory is the fastest form of interprocess communication because all processes share the same piece of memory. Access to this shared memory is as fast as accessing a process’s nonshared memory, and it does not require a system call or entry to the kernel. It also avoids copying data unnecessarily. Because the kernel does not synchronize accesses to shared memory, you must provide your own synchronization. For example, a process should not read from the memory until after data is written there, and two processes must not write to the same memory location at the same time.A common strategy to avoid these race conditions is to use semaphores, which are discussed in the next section. Our illustrative programs, though, show just a single process accessing the memory, to focus on the shared memory mechanism and to avoid cluttering the sample code with synchronization logic.

 There are three flag values include while dealing with shared memory, these flags are supplied as the third parameter of the function

 int segment_id = shmget (shm_key, getpagesize (), IPC_CREAT | S_IRUSR | S_IWUSER);

IPC_CREAT—This flag indicates that a new segment should be created.This permits creating a new segment while specifying a key value.

IPC_EXCL—This flag, which is always used with IPC_CREAT, causes shmget to fail if a segment key is specified that already exists.Therefore, it arranges for the calling process to have an “exclusive” segment. If this flag is not given and the key of an existing segment is used, shmget returns the existing segment instead of creating a new one.

Mode flags—This value is made of 9 bits indicating permissions granted to owner, group, and world to control access to the segment. Execution bits are ignored. An easy way to specify permissions is to use the constants defined in <sys/stat.h> and documented in the section 2 stat man page.1 For example, S_IRUSR and S_IWUSR specify read and write permissions for the owner of the shared memory segment, and S_IROTH and S_IWOTH specify read and write permissions for others.

Program:

 

#include <stdio.h>

#include <sys/shm.h>

#include <sys/stat.h>

int main ()

{

int segment_id;

char* shared_memory;

struct shmid_ds shmbuffer;

int segment_size;

const int shared_segment_size = 0x6400;

/* Allocate a shared memory segment. */

segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

/* Attach the shared memory segment. */


shared_memory = (char*) shmat (segment_id, 0, 0);

printf ("shared memory attached at address %p\n", shared_memory);


/* Determine the segment’s size. */

shmctl (segment_id, IPC_STAT, &shmbuffer);

segment_size = shmbuffer.shm_segsz;

printf ("segment size: %d\n", segment_size);


/* Write a string to the shared memory segment. */

sprintf (shared_memory, "Hello, world.");


/* Detach the shared memory segment. */

shmdt (shared_memory);


/* Reattach the shared memory segment, at a different address. */

shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0);

printf ("shared memory reattached at address %p\n", shared_memory);


/* Print out the string from shared memory. */

printf ("%s\n", shared_memory);


/* Detach the shared memory segment. */

shmdt (shared_memory);


/* Deallocate the shared memory segment. */

shmctl (segment_id, IPC_RMID, 0);

return 0;

}

 

Observation

The prorgam can be compiled by

$ gcc -o sharedmem sharedmem.c

Comments

Popular Posts