Operating System Tutorial
Operating System Tutorial
Augustine
Department of Computing and Information Technology
COMP 2604 - Semester II, 2022/2023
Lab 5 – Interprocess Communication
Shared Memory
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.
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.
Syntax (shmget())
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
parameter specifies the permissions on the shared segment. On success the shmget() function
returns a valid identifier while on failure it return -1.
Syntax (shmat())
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>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
printf("Enter some data to write to shared memory\n");
read(0,buff,100);
strcpy(shared_memory,buff);
printf("You wrote : %s\n",(char *)shared_memory);
}
//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];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}
Message Queue
A message queue is a linked list of messages stored within the kernel and identified by a
message queue identifier.
New messages are added to the end of a queue by msgsnd(). Every message has a positive
long integer type field, a non-negative length, and the actual data bytes (corresponding to the
length), all of which are specified to msgsnd() when the message is added to a queue.
Messages are fetched from a queue by msgrcv(). We don’t have to fetch the messages in a
first-in, first-out order. Instead, we can fetch messages based on their type field.
int main()
{
key_t key;
int msgid;
return 0;
}
int main()
{
key_t key;
int msgid;
return 0;
}