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

Inter process communication

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