Advertisement

Inter-Process Communication in operating system

Inter-Process Communication or IPC

Processes communicate with each other so,   the issues related to the communication of processes is called Inter-Process Communication or IPC.

Race Condition

In some operating systems, multiple processes use shared resources (Memory, storage or printer etc) for reading and writing.
A race condition is a situation where two or more processes are reading or writing some shared data and final result depends on who runs exactly when.


An operating system is providing printing services, “Printer System Process ” picks a print job from SPOOLer. Each job gets a job number, and this is done by the SPOOLer using two global variables “In” and “Out”.
Two processes, A & B need printing services, both of these processes have their own
local variables named “next -fee-slot”.

q  Process A found from “In” that slot 7 is free, so it recorded this number in its
variable i.e. next free slot = 7.
q  Process A consumed its time and schedule now gives CPU to Process B.
q  Process B has some need so it also recorded next free slot = 7. Process B submitted the job and added 1 in its local variable and placed the value in global variable i.e. now “In” = 8.
q  Process A comes back for execution and starts from where it was proceeded out. As this time it has 7 in its local variable so it will submit the job in slot number 7, overwriting the job submitted by process B and will again overwrite the global variable too i.e. “In = 8”.

Mutual Exclusion

Mutual Exclusion forces all processes to avoid the use of a shared variable if in use by one process. In order to avoid race conditions, there should be a way that if one process is using a shared variable etc then other processes should not be allowed to use the same shared variable.    If a process is using a shared variable then other processes will be excluded from using same -shared variable and this is called the Mutual Exclusion.

Critical Section

The problem occurred in the above example because process B started using the shared variable before process A was finished with it. So, the part of a program where shared memory is accessed is called the Critical Section. If we arrange that two processes will not be in their critical section at the same time, then we can avoid Race Condition.


Various techniques are used for achieving Mutual Exclusion so that when one process is busy updating shared memory in its critical region, no other process can enter its region and cause problems.


Disabling Interrupts

Each process is allowed to disable all the Interrupts immediately after it has entered the critical section and re-enable the interrupts just before exiting the critical section.
The problem here is that if a process disable interrupts and due to any reason, didn’t
enable them, then it will cause a complete halt of the system.

Lock Variables

Here we have a single shared lock variable and its initial value is set to 0. 0 means Resource is available. The process sets it to 1 and enters the critical section. Another process wants to use the resource but lock variables value is 1 so the process will have to wait for the value to be 0.
The problem here is that if a process reads the lock as 0 but before setting it to 1. CPU scheduled another process that sets lock to 1. So when the first process runs again, it will also set the lock to 1 and two processes will be in their critical regions at the same time.

The Producer-Consumer or Bounded Buffer Problem

operating system concepts solutions
Two processes share a common buffer. One is a producer, which puts information into the buffer, and the other one is the consumer that takes it out.
The producer wants to put a new item in the Buffer, but it is already full, then the producer should go to sleep and awakened by the consumer on removing one or more items. Similarly, the consumer wants to take items but the buffer is empty, so it goes to sleep until the producer puts something in the buffer and wakes it up.
To check the number of items in the buffer we need a variable, Count. If the maximum number of items, the buffer can hold is N, the producer will first test if count is N, if it is, the producer will get to sleep, if it is not, the producer will add an item and increments counter.
Similarly, consumer tests count to see if it is 0. If it is, it goes to sleep, if it is not 0, remove an item and decrement the counter.
Problem here is that if buffer is empty and consumer finds the count to 0, at that time if scheduler starts running the producer, producer enters the item in the buffer, increments count and sends a wakeup call to consumer. Consumer was not sleeping so the wake-up signal is lost. Similarly, when consumer runs again, it read previously that count is 0 although count now is 1, it will sleep, as it knows that the count is 0. Like this producer will fill up the buffer and will also go to sleep and both will sleep forever.


Semaphore

A Semaphore ‘S’ is an integer variable that other initialization is accessed only
through two standard atomic operations i.e. wait and signal.

So, when Semaphore = 0 it means ‘No wakeups’ i.e. no job is pending and When Semaphore > = then it means ‘Pending wakeups’ i.e. jobs are pending

Operations of Semaphore
A Semaphore can have two operations that are called

i)         Wait or
Sleep or
Down
ii)        Signal or
Wakeup or
Up

Wait Operation

q  tests semaphore for value > 0.
q  if true, then decrement the value and continue the processing.
q  (Decrements means one process has consumed one wakeup turn).
q  if = 0 then process is put to sleep.

Signal Operation

q  Increment the value of the semaphore.

q  If more than one process is sleeping then pick one randomly.