Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Operating System  >  Inter Process Communication Using Message Queues & Shared Memory

Inter Process Communication Using Message Queues & Shared Memory | Operating System - Computer Science Engineering (CSE) PDF Download

Inter Process Communication using Message Queues

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.
All processes can exchange information through access to a common system message queue. The sending process places a message (via some (OS) message-passing module) onto a queue which can be read by another process. Each message is given an identification or type so that processes can select the appropriate message. Process must share a common key in order to gain access to the queue in the first place.

Inter Process Communication Using Message Queues & Shared Memory | Operating System - Computer Science Engineering (CSE)

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.

IPC through shared memory

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 document Inter Process Communication Using Message Queues & Shared Memory | Operating System - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Operating System.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
10 videos|99 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

10 videos|99 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Inter Process Communication Using Message Queues & Shared Memory | Operating System - Computer Science Engineering (CSE)

,

pdf

,

Semester Notes

,

Inter Process Communication Using Message Queues & Shared Memory | Operating System - Computer Science Engineering (CSE)

,

ppt

,

shortcuts and tricks

,

Summary

,

Important questions

,

study material

,

past year papers

,

practice quizzes

,

mock tests for examination

,

Viva Questions

,

Inter Process Communication Using Message Queues & Shared Memory | Operating System - Computer Science Engineering (CSE)

,

Exam

,

Sample Paper

,

MCQs

,

Extra Questions

,

Objective type Questions

,

Free

,

video lectures

,

Previous Year Questions with Solutions

;