Follow me on Linkedin

InterProcess Communication

InterProcess Communication

Interprocess communication (IPC) is the transfer of data among processes.
   five types of interprocess communication:
       1)Shared memory permits processes to communicate by simply reading and
          writing to a specified memory location.
       2)Mapped memory is similar to shared memory, except that it is associated with a
          file in the filesystem.
       3)Pipes permit sequential communication from one process to a related process.
       4)FIFOs are similar to pipes, except that unrelated processes can communicate
          because the pipe is given a name in the filesystem.
       5)Sockets support communication between unrelated processes even on different
          computers.

Shared Memory:

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.

The Memory Model:

To use a shared memory segment, one process must allocate the segment.Then each
process desiring to access the segment must attach the segment. After finishing its use
of the segment, each process detaches the segment. At some point, one process must
deallocate the segment.
Allocating a new shared memory segment causes virtual memory pages to be created.
Because all processes desire to access the same shared segment, only one process
should allocate a new shared segment. Allocating an existing segment does not create
new pages, but it does return an identifier for the existing pages.To permit a process
to use the shared memory segment, a process attaches it, which adds entries mapping
from its virtual memory to the segment’s shared pages.When finished with the segment,
these mapping entries are removed.When no more processes want to access
these shared memory segments, exactly one process must deallocate the virtual
memory pages.All shared memory segments are allocated as integral multiples of the system’s page
size, which is the number of bytes in a page of memory.

Allocation:

A process allocates a shared memory segment using shmget (“SHared Memory
GET”).

Name
shmget - allocates a shared memory segment
Synopsis
#include <sys/ipc.h>
#include <sys/shm.h>

int shmget(key_t key, size_t size, int shmflg);
Explanation:
Its first parameter is an integer key that specifies which segment to create.
Unrelated processes can access the same shared segment by specifying the same key
value.conflict. Using the special constant IPC_PRIVATE as the key value guarantees
that a brand new memory segment is created.
Its second parameter specifies the number of bytes in the segment. Because segments
are allocated using pages, the number of actually allocated bytes is rounded up
to an integral multiple of the page size.
The third parameter is the bitwise or of flag values that specify options to shmget.
The FLAG VALUES include these:
<-->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. 
<-->Mode flags—This value is made of 9 bits indicating permissions granted to
      owner, group, and world to control access to the segment.
      An easy way to specify permissions is to use the constants defined in <sys/stat.h>
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.
Attachment and Detachment:

int shmat ( int shmid, char *shmaddr, int shmflg);

To make the shared memory segment available, a process must use shmat,“SHared
Memory ATtach.”
Pass it the shared memory segment identifier SHMID returned byshmget.
The second argument is a pointer that specifies where in your process’s address
space you want to map the shared memory; If shmaddr is NULL, the system chooses a 
suitable (unused) address at which to attach the segment.
The third argument is a flag, which can include the following:
<--> SHM_RND indicates that the address specified for the second parameter should be
rounded down to a multiple of the page size. If you don’t specify this flag, you
must page-align the second argument to shmat yourself.
<--> SHM_RDONLY indicates that the segment will be only read, not written.
Debugging:
The ipcs command provides information on interprocess communication facilities,
including shared segments. Use the -m flag to obtain information about shared
memory. For example, this code illustrates that one shared memory segment,
numbered 1627649, is in use:
% ipcs -m
------ Shared Memory Segments --------
key                   shmid        owner           perms          bytes         nattch status
0x00000000     1627649     user             640              25600       0

0 comments:

Post a Comment

a