19
INTER PROCESS COMMUNICATION By: MOHD TOUSIF

Inter process communication

Embed Size (px)

Citation preview

Page 1: Inter process communication

INTER PROCESS COMMUNICATION

By: MOHD TOUSIF

Page 2: Inter process communication

What is IPC ?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).

Page 3: Inter process communication

Inter Process CommunicationInter Process Communication

P r o c es s 1 P r o c es s 2

d ata

s en d er r ec e iv er

Page 4: Inter process communication

UNICAST AND MULTICAST IPCUNICAST 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.

Page 5: Inter process communication

Unicast Vs. Multicast Unicast Vs. Multicast

P 2

P 1 P 1

P 2 P 3 P 4...

uni c as t m ul t i c as t

mm m m

Page 6: Inter process communication

IPC Mechanisms

• There are various ways in UNIX to share information.

• Here we will discuss three ways.

Shared memory. Semaphore. Message queues.

Page 7: Inter process communication

Message QueuesMessage 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.

Page 8: Inter process communication

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 .

Page 9: Inter process communication

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);

Page 10: Inter process communication

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.

Page 11: Inter process communication

Shared MemoryShared 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.

Page 12: Inter process communication

Allocating a Shared MemoryAllocating 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);

Page 13: Inter process communication

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.

Page 14: Inter process communication

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_RMIDIPC_RMID – deletes the shared memory segment.

Page 15: Inter process communication

SemaphoresSemaphores

• 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.

Page 16: Inter process communication

Binary and General SemaphoresBinary 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().

Page 17: Inter process communication

Semaphore functionsSemaphore 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);

Page 18: Inter process communication

Semaphore functionsSemaphore 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..);

Page 19: Inter process communication

Thank youThank you