Chapter 2.3: Interprocess Communication
Chapter 2.3: Interprocess Communication
3 : Interprocess Communication
Process concept Process scheduling Interprocess communication Deadlocks Threads
2.3-1
Progress in time..
Producer p1 1 p2 2 p3 3 p4 4
Buffer Consumer 1 c1
3 instead of 2! 2 c2
Both processes are started at the same time and consumer uses some old value initially
Ceng 334 - Operating Systems
2.3-3
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
2.3-4
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
2.3-5
2.3-6
Postponement
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
2.3-9
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
Example
Producer
produce If lock = 1 loop until lock = 0 lock=1 put in buffer lock=0
Consumer
If lock = 1 loop until lock = 0 lock=1 get from buffer lock=0 consume
Ceng 334 - Operating Systems
2.3-12
2.3-13
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
busy waiting is used starvation is possible deadlock is possible (especially with priorities)
Disadvantages
2.3-15
2.3-17
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
Semaphore Operations
Wait(s)
If s > 0
Signal(s)
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
CS
Wait(mutex) Put in buffer Signal(mutex)
Another Example
Three processes all share a resource on which
one draws an A one draws a B one draws a C
Process A
think(); draw_A();
Process B
think(); draw_B();
Process C
think(); draw_C();
2.3-22
think();
draw_A(); draw_B(); signal(b); draw_C(); think();
signal(c);
Ceng 334 - Operating Systems
2.3-23
Message Passing
Provides synchronization and information
exchange
Message Operations:
send(destination, &message)
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