Inter Process Communication:
The
popular IPC techniques are Shared Memory and Message Queues.
Shared memory:
We know that to communicate
between two or more processes, we use shared memory but before using the shared
memory what needs to be done with the system calls,
•
Create the shared memory segment or use an
already created shared memory segment (shmget())
• Detach the
process from the already attached shared memory segment (shmdt())
•
Control operations on the shared memory segment (shmctl())
Inter Process
Communication through shared memory is a concept where two or more process can
access the common memory. And communication is done via this shared memory
where changes made by one process can be viewed by another process.
The problem with pipes, fifo
and message queue – is that for two process to exchange information. The
information has to go through the kernel.
•
Server reads from the input file.
•
The server writes this data in a message using either a pipe, fifo
or message queue.
•
The client reads the data from the IPC channel, again requiring
the data to be copied from kernel's IPC buffer to the client's buffer.
•
Finally the data is copied from the client's buffer.
A total of four
copies of data are required (2 read and 2 write). So, shared memory provides a
way by letting two or more processes share a memory segment. With Shared Memory
the data is only copied twice – from input file into shared memory and from
shared memory to the output file.
SYSTEM CALLS USED ARE:
●
ftok(): is use to generate a unique
key.
●
shmget(): int shmget(key_t,
size_tsize, intshmflg); upon successful completion, shmget() returns an
identifier for the shared memory segment.
●
shmat(): Before you can use a shared
memory segment, you have to attach yourself to it using shmat(). void
*shmat(int shmid , void *shmaddr , int shmflg);
●
shmid is shared memory id. shmaddr specifies specific address to
use but we should set it to zero and OS will automatically choose the address.
●
shmdt(): When you're done with the shared
memory segment, your program should detach itself from it using shmdt(). int
shmdt(void *shmaddr);
●
shmctl(): when you detach from shared
memory, it is not destroyed. So, to destroy shmctl() is used. shmctl(int
shmid,IPC_RMID,NULL); The second argument, cmd, is the command to perform the
required control operation on the shared memory segment.
Valid
values for cmd are -
●
IPC_STAT - Copies the information of the
current values of each member of struct shmid_ds to the passed structure
pointed by buf. This command requires read permission to the sharedmemory
segment.
●
IPC_SET - Sets the user ID, group ID of
the owner, permissions, etc. pointed to by structure buf.
●
IPC_RMID – Marks the segment to be
destroyed. The segment is destroyed only after the last process has detached
it. IPC_INFO - Returns the
information about the shared memory limits and parameters in the structure
pointed by buf.
●
SHM_INFO – Returns a shm_info
structure containing information about the consumed system resources by the
shared memory.
Program:1- Shared memory implementation using readers writers problem.
Writer process:
