Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Inter process communication


Published on

Published in: Technology, Business
  • Be the first to comment

Inter process communication

  2. 2. What is IPC ? <ul><li>PROCESS : Running program is called a </li></ul><ul><li>process. </li></ul><ul><li>IPC : Exchange of data between two or more </li></ul><ul><li>separate, independent processes </li></ul><ul><li>Operating systems provide facilities for </li></ul><ul><li>inter-process communications( IPC ). </li></ul>
  3. 3. Inter Process Communication
  4. 4. UNICAST AND MULTICAST IPC When communication is from one process to a single other process , the IPC is said to be a unicast. e.g. Socket communication. .When communication is from one process to a group of processes , the IPC is said to be a multicast. e.g. Publish/Subscribe Message model.
  5. 5. Unicast Vs. Multicast
  6. 6. IPC Mechanisms <ul><li>There are various ways in UNIX to share information. </li></ul><ul><li>Here we will discuss three ways. </li></ul><ul><li>Shared memory. </li></ul><ul><li>Semaphore. </li></ul><ul><li>Message queues. </li></ul>
  7. 7. Message Queues <ul><li>A message queue is a queue onto which messages can be placed. </li></ul><ul><li>Message queue can be either public or private . </li></ul><ul><li>If a message queue is public it can be accessed by any process that knows the key of the queue. If it is private then it can be accessed by the creating process or child process of the creator. </li></ul>
  8. 8. Creating a Message Queue <ul><li>To use a message queue it has to be created first. </li></ul><ul><li>Header File : #include<sys/msg.h> </li></ul><ul><li>Function : Int msgget(key_t key, int flag); </li></ul><ul><li>key_t : IPC_PRIVATE, any positive integer. </li></ul><ul><li>Flag : permissions . </li></ul>
  9. 9. Operations on message queue <ul><li>Two operations. </li></ul><ul><li>msgsnd () – sends message to the message </li></ul><ul><li>queue. </li></ul><ul><li>int msgsnd(int msqid,const. void *msgp,size_t msgsz,int msgflag); </li></ul><ul><li>msgrcv() - reads a message from a message </li></ul><ul><li>queue. </li></ul><ul><li>ssize_t msgrcv(int msqid,void *msgp,size_t msgsz,long int msgtyp,int msgflag); </li></ul>
  10. 10. Controlling message queue operations <ul><li>msgctl() – controls the message queue </li></ul><ul><li>operations. </li></ul><ul><li>Int msgctl (int msqid , int cmd , struct msqid_ds *buf); </li></ul><ul><li>We can also remove message queue using msgctl() function if we set the cmd as IPC_RMID . </li></ul>
  11. 11. Shared Memory <ul><li>This is the another mechanism by which processes can communicate with each other. </li></ul><ul><li>In this machanism we declare a section of memory as shared memory. </li></ul><ul><li>This shared memory section is used by communicating processes simultaneously. </li></ul><ul><li>Also we have to synchronise the processes so they can’t alter shared memory simultaneously. </li></ul>
  12. 12. Allocating a Shared Memory <ul><li>A shared memory segment is allocated first. </li></ul><ul><li>Header Files : #include<sys/types.h> </li></ul><ul><li>#include<sys/ipc.h> </li></ul><ul><li>#include<sys/shm.h> </li></ul><ul><li>Shmget() - allocate a shared memory segment. </li></ul><ul><li>Int shmget(key_t key , size_t size , int shmflg); </li></ul>
  13. 13. Attaching and Detaching a shared memory <ul><li>shmat() – attaches the shared memory </li></ul><ul><li>segment. </li></ul><ul><li>Void *shmat(int shmid , const. void *shmaddr. , int shmflg); </li></ul><ul><li>shmdt() – detaches the shared memory </li></ul><ul><li>segment. </li></ul><ul><li>It takes a pointer to the address returned by shmat() ; </li></ul><ul><li>on success it returns 0, on error it returns -1. </li></ul>
  14. 14. Controlling the Shared Memory <ul><li>shmctl() – control the operations on shared </li></ul><ul><li>memory. </li></ul><ul><li>Int shmctl(int shmid , int cmd , struct ds *buf); </li></ul><ul><li>cmd is one of the following </li></ul><ul><li>IPC_STAT </li></ul><ul><li>IPC_SET </li></ul><ul><li>IPC_RMID </li></ul><ul><li>IPC_RMID – deletes the shared memory segment. </li></ul>
  15. 15. Semaphores <ul><li>Semaphores are used to synchronize the processes so that they can’t access critical section simultaneously. </li></ul><ul><li>Semaphores is of two types. </li></ul><ul><li>Binary semaphore. </li></ul><ul><li>General semaphore. </li></ul>
  16. 16. Binary and General Semaphores <ul><li>Binary semaphore : binary semaphore is a variable that can take only the values 0 and 1. </li></ul><ul><li>General semaphore : general semaphore can take any positive value. </li></ul><ul><li>The two functions are associated with two values of binary semaphore. </li></ul><ul><li>wait() and signal(). </li></ul>
  17. 17. Semaphore functions <ul><li>Header File : #include<sys/types.h> </li></ul><ul><li>#include<sys/ipc.h> </li></ul><ul><li>#include<sys/sem.h> </li></ul><ul><li>semget() - The semget function creates a new </li></ul><ul><li>semaphore. </li></ul><ul><li>Int semget(key_t key , int num_sems , int semflag); </li></ul>
  18. 18. Semaphore functions <ul><li>semop() – change the value of the semaphore. </li></ul><ul><li>Int semop(int semid , struct sembuf *semops , size_t num_sem_ops); </li></ul><ul><li>semctl() – allow direct control of semaphore </li></ul><ul><li>information. </li></ul><ul><li>int semctl(int sem_id , int sem_num , int cmd..); </li></ul>
  19. 19. Thank you