OpSy 03 CH 23

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Chapter 2.

3: Interprocess
Communication
Chapter 2.3 : Interprocess
Communication

• Process concept 
• Process scheduling 
• Interprocess communication
• Deadlocks
• Threads

Ceng 334 - Operating Systems 2.3-1

1
Producer Consomer Problem
Producer - Consumer Problem
Producer Process Consumer Process

Produce Get from buffer


Put in buffer Consume

BUFFER

• Buffer is shared (ie., it is a shared variable)


Ceng 334 - Operating Systems 2.3-2

2
Progressing time
Progress in time…..
Producer
p1 1 p2 2 p3 3 p4 4

Buffer
3 instead of 2!
Consumer
1 c1 2 c2

t
• Both processes are started at the same time
and consumer uses some old value initially
Ceng 334 - Operating Systems 2.3-3

3
A Race Condition
A Race Condition

• Because of the timing and which process


starts first
• There is a chance that different executions
may end up with different results

Ceng 334 - Operating Systems 2.3-4

4
Critical Section
Critical Sections
• Critical Section
– A section of code in which the process accesses
and modifies shared variables

• Mutual Exclusion
– A method of preventing for ensuring that one
(or a specified number) of processes are in a
critical section

Ceng 334 - Operating Systems 2.3-5

5
Why Interprocess Communication?
Why Processes Need to
Communicate?

• To synchronize their executions

• To exchange data and information

Ceng 334 - Operating Systems 2.3-6

6
Rules to Interprocess Comm.
Rules to Form Critical Sections

1. No two processes may be simultaneously


inside their CS (mutual exclusion)
2. No assumptions are made about relative
process speeds or number of CPUs
3. A process outside a CS should not block
other processes
4. No process should wait forever before
entering its CS
Ceng 334 - Operating Systems 2.3-7

7
Starvation
Mutual Exclusion Problem :
Starvation
• Also known as Indefinite Postponement
• Definition
– Indefinitely delaying the scheduling of a process in
favour of other processes
• Cause
– Usually a bias in a systems scheduling policies (a bad
scheduling algorithm)
• Solution
– Implement some form of aging
Ceng 334 - Operating Systems 2.3-8

8
Deadlocks
Another Problem : Deadlocks
– Two (or more) processes are blocked waiting
for an event that will never occur

– Generally, A waits for B to do something and B


is waiting for A

– Both are not doing anything so both events


never occur

Ceng 334 - Operating Systems 2.3-9

9
Implementig Mutual Exclusion
How to Implement Mutual
Exclusion
• Three possibilities
– Application: programmer builds some method
into the program
– Hardware: special h/w instructions provided
to implement ME
– OS: provides some services that can
be used by the programmer
• All schemes rely on some code for
– enter_critical_section, and
– exit_critical_section
• These "functions" enclose the critical
section
Ceng 334 - Operating Systems 2.3-10

10
Application Mutual Exclusion
Application Mutual Exclusion
• Application Mutual Exclusion is
– implemented by the programmer
– hard to get correct, and
– very inefficient
• All rely on some form of busy waiting
(process tests a condition, say a flag, and
loops while the condition remains the same)

Ceng 334 - Operating Systems 2.3-11

11
Test and Set
Hardware ME :
Test and Set Instruction
• Perform an indivisible x:=r and r:=1
• x is a local variable
• r is a global register set to 0 initially

• repeat (test&set(x)) until x = 0;


< critical section >
r:= 0;

Ceng 334 - Operating Systems 2.3-13

12
Harware Mutal Exculison
Hardware ME :
Exchange Instruction
• Exchange: swap the values of x and r
• x is a local variable
• r is a global register set to 1 initially

• x:= 0;
repeat exchange(r, x) until x = 1;
< critical section >
exchange(r, x);

Note: r:= 0 and x:= 1 when the process is


in CS

Ceng 334 - Operating Systems 2.3-14

13
Characteristics
Hardware ME Characteristics
• Advantages
– can be used by a single or multiple processes
(with shared memory)
– simple and therefore easy to verify
– can support multiple critical sections

• Disadvantages
– busy waiting is used
– starvation is possible
– deadlock is possible (especially with priorities)

Ceng 334 - Operating Systems 2.3-15

14
Disabling Interrups
Another Hardware ME :
Disabling Interrupts
• On a single CPU only one process is
executed
• Concurrency is achieved by interleaving
execution (usually done using interrupts)
• If you disable interrupts then you can be
sure only one process will ever execute
• One process can lock a system or degrade
performance greatly
Ceng 334 - Operating Systems 2.3-16

15
Mutual Exculusion Trough OS.
Mutual Exclusion Through OS

• Semaphores
• Message passing

Ceng 334 - Operating Systems 2.3-17

16
Semaphores
Semaphores

• Major advance incorporated into many


modern operating systems (Unix, OS/2)

• A semaphore is
– a non-negative integer
– that has two indivisible, valid operations

Ceng 334 - Operating Systems 2.3-18

17
Semaphore Oparations
Semaphore Operations
• Wait(s)
If s > 0 then s:= s - 1
else block this process
• Signal(s)
If there is a blocked process on this
semaphore then wake it up
else s:= s + 1
Ceng 334 - Operating Systems 2.3-19

18
More on Semaphores
More on Semaphores
• The other valid operation is initialisation
• Two types of semaphores
– binary semaphores can only be 0 or 1
– counting semaphores can be any non-negative
integer
• Semaphores are an OS service implemented
using one of the methods shown already
– usually by disabling interrupts for a very short
time
Ceng 334 - Operating Systems 2.3-20

19
P-C Problem;Solution by Semephore
Producer - Consumer Problem:
Solution by Semaphores

Produce Wait(mutex)
CS Get from buffer
Wait(mutex) Signal(mutex)
Put in buffer
Signal(mutex) Consume

• Initially semaphore mutex is 1


Ceng 334 - Operating Systems 2.3-21

20
More Examples
Another Example
• Three processes all share a resource on which
– one draws an A
– one draws a B
– one draws a C
• Implement a form of synchronization so that the output
appears ABC

Process A Process B Process C

think(); think(); think();

draw_A(); draw_B(); draw_C();

Ceng 334 - Operating Systems 2.3-22

21
• Semaphore b = 0, c = 0;

Process A Process B Process C

wait(b);
think(); wait(c);
think();
draw_A(); think();
draw_B();
signal(b); draw_C();
signal(c);

Ceng 334 - Operating Systems 2.3-23

22
Message Passing
Message Passing

• Provides synchronization and information


exchange
• Message Operations:
– send(destination, &message)
– receive (source, &message)

Ceng 334 - Operating Systems 2.3-24

23
P-C Problem: Using Messages
Producer - Consumer Problem
Using Messages
#define N 100 /*number of message slots*/

producer( )
{int item; message m;
while (TRUE) {
produce_item(&item);
receive(consumer,&m);
build_message(&m, item);
send(consumer,&m);
}}
Ceng 334 - Operating Systems 2.3-25

24
Consumer( )
{int item; message m;
for (i=0; i<N; i++) send(producer,&m);
while (TRUE) {
receive(producer,&m);
extract_item(&m,&item);
send(producer,&m);
consume_item(item);
}}
Ceng 334 - Operating Systems 2.3-26

25

You might also like