1. INTERPROCESS
COMMUNICATION
(M.SC Computer Science)
13/05/12 GROUP - F 1
2. OUTLINE
WHAT IS IPC
ABOUT RACE CONDITION
CRITICAL REGIONS
MANAGE RACE CONDITION
DINING PHILOSOPHER PROBLEM
MESSAGE PASSING
05/13/12 GROUP I 2
3. What is IPC ?
Processes frequently need to communicate with other
processes.
Generally shared memory is used to establish the
connection among processes.
This memory must be shared carefully to avoid
inconsistency.
13/05/12 GROUP - F 3
4. IPC: Race Condition
Spooler directory
.
. Race condition :-
out= 4
4 abc The situation where two
or more processes are
Process A 5 prog.c reading or writing some
in =6 shared data is called race
6 condition.
Process B
.
.
.
Two processes want to access shared memory at same time
13/05/12 GROUP - F 4
5. IPC: Critical Regions
Critical Region/ Critical Section
The part of the program where the shared memory is
accessed is called the critical region.
conditions to provide mutual exclusion
No two processes will be in critical region
simultaneously.
No process running outside its critical region
may block another process.
No process must wait forever to enter its
critical region.
13/05/12 GROUP - F 5
6. How to Manage Race Condition ?
Mutual Exclusion with Busy Waiting
Strict Alternation
Mutual Exclusion without Busy Waiting
Sleep and wakeup
Semaphore
Message Passing
13/05/12 GROUP - F 6
7. STRICT ALTERNATION
while(TRUE) while(TRUE)
{ {
while(turn!=0) /* loop /; while(turn!=1) /* loop */
critical_region(); critical_region();
turn=1; turn=0;
noncritical_region(); noncritical_region();
} }
Process A Process B
DRAWBACK: wastage of CPU time by Busy waiting
13/05/12 GROUP - F 7
8. Sleep and Wakeup: Producer Consumer Problem
Function performed by Producer :
Repeat forever-------->
. item=produce item()
. while(count=max) sleep()
. insert_item(item)
. count=count+1
. if count=1) then wakeup consumer)
Function produced by Consumer :
repeat forever--------------->
. while(count=0) sleep();
. item=remove item()
. Count-count-1
. if count=max-1) then wakeup producer)
. consume(item)
DRAWBACK : Both may sleep forever.
SOLUTION: USE OF WAKEUP WAITING BIT
13/05/12 GROUP - F 8
9. Semaphores: Producer Consumer Problem
Semaphore
An integer to keep count of wakeups saved for future use
Down (semaphore)
• If value > 0, Decrement it and continue (one stored wakeup is
used)
• If value = 0, Process is put to sleep without completing Down
Up (semaphore)
if one or more processes were sleeping on that semaphore (unable
to
complete down operation), one of them is chosen by the system at
random and allowed to complete its Down
13/05/12 GROUP - F 9
10. Semaphores: Producer Consumer Problem
Function performed by producer:
Repeat forever------>
Generate something to put in buffer
Perform DOWN operation on empty count;
Enter critical region
Put new item in buffer
Leave critical region
Perform UP on full counts
Function performed by consumer :
repeat forever------->
Perform DOWN operation on full.
Enter critical section.
Take item from buffer
Leave critical region
Perform UP operation on empty counts
CONCLUSION: NO BUSY WAITING
13/05/12 GROUP - F 10
11. Readers and writers problem
This model deals with the access to database.
Constraints:
Allows multiple processes to read database simultaneously.
Doesn't allow any other process to access database when a
process writes into the database .
05/13/12 GROUP I 11
12. Application of semaphore in readers and writers
problem
Function performed by reader :
Enter into critical section and increase reader_counter, and check if
it becomes 1 if it is perform down operation on a semaphore db.
Exit the critical section .
Read database
Enter into critical section and decrease reader_counter by 1 .
if it becomes 0 perform UP operation on semaphore db. Exit the
critical section.
Functions performed by writer
Perform DOWN on semaphore db .
Write into database.
Perform UP operation on db.
Exit .
05/13/12 GROUP I 12
13. Dining Philosophers problem in IPC
A problem used for modeling processes competing for access to limited
number of resources .
General incident:
For each philosopher
-> take_fork(left)
-> take_fork(right)
->eat()
-> put_fork(left)
-> put_fork(right)
Problem : Occurrence of DEADLOCK
Solution : Application of SEMAPHORE
13/05/12 GROUP - F 13
14. Application of Semaphore in Dining Philosophers
problem
For each philosopher(i)
->take_fork(i)
...enter critical region
...set semaphore state[i]=hungry
...test whether left or right neighbour is in eating state ,
If not then state[i]=eating , perform UP on semaphore ph(i)
...exit critical region
... perform DOWN operation on semaphore ph(i)
->eat()
->put_fork(i)
....enter critical region
.... state[i]=thinking
RESULT IS NO DEADLOCK , NO STARVATION
13/05/12 GROUP - F 14
15. Message Passing
buffer size=N
Function performed by producer
while(TRUE)
item=produce item() NO SHARED MEMORY IS USED
receive(consumer,&m)
build_message(&m,item)
send(consumer,&m)
End
Function performed by consumer
send N empty slots
while (TRUE)
receive_message(producer,&m)
item=extract_item(&m)
send(producer,&m)
consume(item)
end
13/05/12 GROUP - F 15
16. REFERENCES
OPERATING SYSTEM (Andrew S. Tanenbaum)
DISTRIBUTED OPERATING SYSTEM
( P.K.SINHA)
05/13/12 GROUP I 16