Jan 24, 2010

Inter Process Communication

Inter-process communication (IPC) is a set of techniques for the exchange of data among multiple threads in one or more processes. Processes may be running on one or more computers connected by a network. IPC techniques are divided into methods for message passing, synchronization, shared memory, and remote procedure calls (RPC).

The method of IPC used may vary based on the bandwidth and latency of communication between the threads, and the type of data being communicated.

IPC mechanisms have many distinct purposes: for example

Data transfer

Sharing data

Event notification

Resource sharing

Process control

There are several reasons for providing an environment that allows process cooperation:

Information sharing

Computation speedup

Modularity

Convenience

IPC Mechanisms

Primitive

Unnamed pipe

Named pipe (FIFO)

System V IPC

Message queues

Shared Memory

Semaphores

Socket Programming

This article deals with the Semaphores.

Overview of Semaphores

Semaphore is a synchronization mechanism, which implements mutual exclusion among processes to avoid race condition to access any shared resource.

An efficient usage of shared memory is not possible without the help of semaphore. Semaphore maintains a counter to implement locking and unlocking. It avoids busy waiting. If a critical section is in use, the calling process will be removed from a run queue and put into a sleep state.

Two types of semaphore exist: binary and counting.

Semaphores are created and stored in kernel and have kernel persistence – Kernel stores in memory a data structure per semaphore set

If a process wants to use the shared object, it will “lock” it by asking the semaphore to decrement the counter. Depending upon the current value of the counter, the semaphore will either be able to carry out this operation, or will have to wait until the operation becomes possible.

Basic Semaphore usage

Two processes, process A and B use a semaphore to synchronize access to a shared resource.

Assume process2 is started after process1 locks semaphore successfully.

1) semop =-ve value, waits till semval is greater than or equal to absolute value of semop; absolute value of semop is subtracted from semval; used for locking a resource.

If the semaphore is deleted by some means, then what happens to this program?

Appropriate error handling is required. In this case, semop () returns an errno of EIDRM, indicating that the semaphore has been removed. If the semaphore was deleted before the semop (), then it returns an EINVAL.

Semaphores cleanup act

Use of SEM_UNDO flag in semop () takes care of cleanup when process terminates.

In the above program, by setting the SEM_UNDO flag in semop (), the kernel updates a

‘semadj’ value. When the program terminates it ‘undoes’ the previous semop () operations thus resetting the semval.

Semaphores Attributes

Per semaphore

semval: current value of semaphore

sempid: pid of process that performed the last semop ()

semncnt: number of processes/threads waiting for semval to increase

semzcnt: number of processes/threads waiting for semval to become zero

semctl() with flag GETVAL or GETALL can be used to retrieve value of semaphore or all semaphores in set Likewise, can be set using SETVAL or SETALL.

semctl () with flags GETPID, GETNCNT, GETZCNT semncnt is incremented whenever there is a process waiting on a semop () with a negative non-zero integer.

semzcnt is incremented whenever there is a process waiting for semval to become zero. Both are decremented when the process acquires the semaphore.