Experiment 5
Experiment 5
5
Aim: Write a program to implement interprocess communication using shared memory
(SHM) in Linux.
Theory: Shared Memory is the fastest inter-process communication (IPC) method. The
operating system maps a memory segment in the address space of several processes so that
those processes can read and write in that memory segment. The overview is as shown below:
Two functions: shmget() and shmat() are used for IPC using shared memory. shmget() function
is used to create the shared memory segment while shmat() function is used to attach the shared
segment with the address space of the process.
Shared Memory
Syntax (shmget()):
#include <sys/ipc.h>
#include <sys/shm.h>
The first parameter specifies the unique number (called key) identifying the shared segment.
The second parameter is the size of the shared segment e.g. 1024 bytes or 2048 bytes. The third
Syntax (shmat()):
#include <sys/types.h>
#include <sys/shm.h>
shmat() is used to attach the created shared segment with the address space of the calling
process. The first parameter here is the identifier which shmget() function returns on success.
The second parameter is the address where to attach it to the calling process. A NULL value
of second parameter means that the system will automatically choose a suitable address. The
third parameter is ‘0’ if the second parameter is NULL, otherwise, the value is specified by
SHM_RND.
We will write two program for IPC using shared memory. Program 1 will create the shared
segment, attach to it and then write some content into it. Then Program 2 will attach itself to
the shared segment and read the value written by Program 1.
//Program 1: This program creates a shared memory segment, attaches itself to it and then
writes some content into the shared memory segment.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
int main()
int i;
void *shared_memory;
char buff[100];
int shmid;
Output:
shmget() function creates a segment with key 2345, size 1024 bytes and read and write
permissions for all users. It returns the identifier of the segment which gets store in shmid. This
identifier is used in shmat() to attach the shared segment to the address space of the process.
NULL in shmat() means that the OS will itself attach the shared segment at a suitable address
of this process.
Then some data is read from the user using read() system call and it is finally written to the
shared segment using strcpy() function.
//Program 2: This program attaches itself to the shared memory segment created in Program
1. Finally, it reads the content of the shared memory
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
int i;
void *shared_memory;
char buff[100];
How it works?
shmget() here generates the identifier of the same segment as created in Program 1. Remember
to give the same key value. The only change is, do not write IPC_CREAT as the shared memory
segment is already created. Next, shmat() attaches the shared segment to the current process.
After that, the data is printed from the shared segment. In the output, you will see that it is the
same data that you have written while executing the Program 1.
Output:
Conclusion: Thus we have studied that Shared memory is the fastest form of interprocess
communication.