0% found this document useful (0 votes)
20 views

Operating System Tutorial

Shared memory is the fastest inter-process communication method where the operating system maps a memory segment in the address space of multiple processes so they can read and write to that shared segment. Message queues allow processes to communicate by sending and receiving messages through a message queue identified by a message queue identifier. System calls like shmget(), shmat(), msgget(), msgsnd(), and msgrcv() are used to create, attach, send, and receive messages for these inter-process communication methods. Sample C code demonstrates how to use shared memory and message queues for inter-process communication between two programs.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Operating System Tutorial

Shared memory is the fastest inter-process communication method where the operating system maps a memory segment in the address space of multiple processes so they can read and write to that shared segment. Message queues allow processes to communicate by sending and receiving messages through a message queue identified by a message queue identifier. System calls like shmget(), shmat(), msgget(), msgsnd(), and msgrcv() are used to create, attach, send, and receive messages for these inter-process communication methods. Sample C code demonstrates how to use shared memory and message queues for inter-process communication between two programs.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The University of the West Indies at St.

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.

A new queue is created or an existing queue opened by msgget().

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.

System calls used for message queues:


 ftok(): is use to generate a unique key.
 msgget(): either returns the message queue identifier for a newly created
message queue or returns the identifiers for a queue which exists with the same
key value.
 msgsnd(): Data is placed on to a message queue by calling msgsnd().
 msgrcv(): messages are retrieved from a queue.
 msgctl(): It performs various operations on a queue. Generally it is use to
destroy message queue.

MESSAGE QUEUE FOR WRITER PROCESS


#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;

int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);
// msgget creates a message queue and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;

printf("Write Data : ");


fgets(message.mesg_text,MAX,stdin);

// msgsnd to send message


msgsnd(msgid, &message, sizeof(message), 0);

// display the message


printf("Data send is : %s \n", message.mesg_text);

return 0;
}

MESSAGE QUEUE FOR READER PROCESS


#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;

int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);

// msgget creates a message queue and returns identifier


msgid = msgget(key, 0666 | IPC_CREAT);

// msgrcv to receive message


msgrcv(msgid, &message, sizeof(message), 1, 0);

// display the message


printf("Data Received is : %s \n", message.mesg_text);

// to destroy the message queue


msgctl(msgid, IPC_RMID, NULL);

return 0;
}

You might also like