3 chapter 5. theme of os design? management of processes and threads multiprogramming...
TRANSCRIPT
![Page 1: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/1.jpg)
3
Chapter 5
![Page 2: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/2.jpg)
Theme of OS Design?
Management of
processes and threads
Multiprogramming
Multiprocessing
Distributed processing
![Page 3: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/3.jpg)
Currency
• Communication among processes
• Sharing resources
• Synchronization of multiple processes
• Allocation of processor time
![Page 4: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/4.jpg)
Concurrency in different contexts
• Multiple applications– Multiprogramming – time sharing
• Structured application– Application can be a set of concurrent
processes
• Operating-system structure– Operating system is a set of processes or
threads
Unit of concurrency : process / thread
![Page 5: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/5.jpg)
Chapter 5
• What is mutual exclusion?
• How to implement mutual exclusion
• Busy waiting? Software/Hardware
• Semaphore, monitor, massage passing
![Page 6: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/6.jpg)
Difficulties with Concurrency
• Sharing global resources, global variables
• Management of allocation of resources– Optimally?, deadlock?
• Programming errors difficult to locate
Relative speed of execution can not be predicted
Other process, interrupt handling, scheduling
![Page 7: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/7.jpg)
A Simple Example
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
//uniprocessorBlock from entering
![Page 8: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/8.jpg)
A Simple Example
Process P1 Process P2. .in = getchar(); .. in = getchar();chout = chin; chout = chin;putchar(chout); .. putchar(chout);. .//in processor A & processor B
Control access to the shared resource.
![Page 9: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/9.jpg)
Operating System Concernsissues
• Keep track of active processes • Allocate and deallocate resources
– Processor time– Memory– Files– I/O devices
• Protect data and resources –chapter 15• Result of process must be independent of the
speed of execution of other concurrent processes – this chapter
PCB
![Page 10: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/10.jpg)
Process Interaction
• Processes unaware of each other –multiprograming of multiplr independent process – competition for disk, file or printer
• Processes indirectly aware of each other –not by the PID, but by sharing some object such as I/O buffer
• Process directly aware of each other –by PID and can communicate each other - cooperation
![Page 11: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/11.jpg)
![Page 12: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/12.jpg)
Competition Among Processes for Resources
• Mutual Exclusion (non shareable resource)
– Critical sections – portion of program
– Only one program at a time is allowed in its critical section
• Example only one process at a time is allowed to send command to the printer
• Deadlock
• Starvation
Processes : P1, P2
Resources: R1, R2
P1 hold R1 and waiting R2
P2 hold R2 and waiting R1
Processes : P1, P2, P3
Resources: R
P1 hold R and P2, P3 are waiting
P3 – P1 – P3 ….
![Page 13: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/13.jpg)
Mutual exclusion mechanism in abstract terms
Const int n = // number of processes;
void P(int i){
while (true) {
entercritical(i);
//critical section;
exitcritical(i);
}
}
Void main()
{
parbegin(P(R1), P(R2), …,P(Rn))
}
![Page 14: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/14.jpg)
Cooperation Among Processes by Sharing
• Aware each other with shared data
• Writing must be mutually exclusive-• data coherence is required
• Critical sections are used to provide data integrity
![Page 15: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/15.jpg)
In a bookkeeping application a = b
P1:
a = a + 1;
b = b + 1;
P2:
b = 2 * b;
a = a * 2;
Start w/ a = b = 1
A = 4 , b = 3 -> cs is important!
![Page 16: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/16.jpg)
Cooperation Among Processes by Communication
• Communication – A way of synchronization or coordination
• Messages are passes – no sharing– Mutual exclusion is not a control requirement
• Possible to have deadlock– Each process waiting for a message from the other
process
• Possible to have starvation– Two processes sending message to each other
while another process waits for a message
![Page 17: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/17.jpg)
Requirements for Mutual Exclusion
• Only one process at a time is allowed in the critical section for a resource
• A process that halts in its non-critical section must do so without interfering with other processes
• No deadlock or starvation
![Page 18: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/18.jpg)
Requirements for Mutual Exclusion
• A process must not be delayed access to a critical section when there is no other process using it
• No assumptions are made about relative process speeds or number of processes
• A process remains inside its critical section for a finite time only
![Page 19: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/19.jpg)
Ways
• Each process takes responsibility– Software approach– High processing overhead, error prone
• Special purpose machine instn
– Hardware approach
– Special purpose
• O/S or PL supports
![Page 20: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/20.jpg)
First Attempt
• Global memory - turn
• Busy Waiting– Process is always checking to see if it can
enter the critical section– Process can do nothing productive until it
gets permission to enter its critical section
![Page 21: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/21.jpg)
//process 0
…
While (turn != 0)
//do nothing;
//critical section
turn = 1
//process 1
…
While (turn != 1)
//do nothing;
//critical section
turn = 0
Drawback ; Speed is dictated by slower process
One fails inside or outside cs
![Page 22: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/22.jpg)
Coroutine
• Designed to be able to pass execution control back and forth between themselves
• Inadequate to support concurrent processing
![Page 23: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/23.jpg)
Second Attempt
• Each process can examine the other’s status but cannot alter it
• When a process wants to enter the critical section is checks the other processes first
• If no other process is in the critical section, it sets its status for the critical section
• Each process can check the flags and then proceed to enter the critical section at the same time
![Page 24: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/24.jpg)
Boolean flag[2] = false, false
//process 0
…
While (flag[1])
//do nothing;
flag[0] = true;
//critical section
Flag[0] = false;
//process 1
…
While (flag[0])
//do nothing;
flag[1] = true;
//critical section
Flag[1] = false;
![Page 25: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/25.jpg)
drawbacks
• If processes fails inside its cs
• It does not guarantee mutual exclusion– P0 finds flag[1] set to false.– P1 finds flag[0] set to false.– P0 set flag[0] and enter cs– P1 set flag[1] and enter cs
![Page 26: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/26.jpg)
Third attempt
//process 0
…
Flag[0] = true
While (flag[1])
//do nothing;
//critical section
Flag[0] = false;
//process 1
…
Flag[1] = true
While (flag[0])
//do nothing;
//critical section
Flag[1] = false;
![Page 27: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/27.jpg)
Third Attempt
• Set flag to enter critical section before check other processes
• If another process is in the critical section when the flag is set, the process is blocked until the other process releases the critical section
• Deadlock is possible when two process set their flags to enter the critical section. Now each process must wait for the other process to release the critical section
![Page 28: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/28.jpg)
Fourth Attempt
• A process sets its flag to indicate its desire to enter its critical section but is prepared to reset the flag
• Other processes are checked. If they are in the critical region, the flag is reset and later set to indicate desire to enter the critical region. This is repeated until the process can enter the critical region.
![Page 29: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/29.jpg)
4th attempt
//process 0
…
Flag[0] = true
While (flag[1]){
flag[0] = false;
Delay;
Flag[0] = true};
//critical section
Flag[0] = false;
//process 1
…
Flag[1] = true
While (flag[0]){
flag[0] = false;
Delay;
Flag[0] = true};
//critical section
Flag[1] = false;
![Page 30: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/30.jpg)
Fourth Attempt
• It is possible for each process to set their flag, check other processes, and reset their flags. This scenario will not last very long so it is not deadlock. It is undesirable
![Page 31: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/31.jpg)
livelock
• P0 sets flag[0] to true• P1 sets flag[1] to true• P0 checks flag[1]• P1 checks flag[0]• P0 sets flag[0] to false• P1 sets flag[1] to false• P0 sets flag[0] to true• P1 sets flag[1] to true
![Page 32: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/32.jpg)
Correct Solution
• Each process gets a turn at the critical section
• If a process wants the critical section, it sets its flag and may have to wait for its turn
• Set flag and check other’s flag, if set consults turn -> eventually get its turn
![Page 33: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/33.jpg)
Boolean flag 2;
Int turn;
Void P0(){
while (true) {
flag[0] = true;
while(flag[1])
if (turn == 1){
flag[0] = false;
while (turn == 1)
//do nothing;
flag[0] = true; }
//cs
turn = 1;
flag[0] = false
}}
Void P1(){
while (true) {
flag[1] = true;
while(flag[0])
if (turn == 1){
flag[1] = false;
while (turn == 0)
//do nothing;
flag[1] = true; }
//cs
turn = 0;
flag[1] = false
}}
Void main(){
Flag[0] = false;
Flag[1] = false;
Turn = 1;
Parbegin(P0,P1)
}
Fig 5.3 Dekker’s algorithm
![Page 34: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/34.jpg)
Boolean flag 2;
Int turn;
Void P0(){
while (true) {
flag[0] = true;
turn = 1
while(flag[1] && turn == 1)
//do nothing;
//cs;
flag[0] = false;
}
}
Void P1(){
while (true) {
flag[1] = true;
turn = 0
while(flag[1] && turn == 1)
//do nothing;
//cs;
flag[1] = false;
}
}
Fig 5.3 Peterson’s algorithm
Void main(){
Flag[0] = false;
Flag[1] = false;
Parbegin(P0,P1)
}
![Page 35: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/35.jpg)
Mutual Exclusion:Hardware Support
• Interrupt Disabling– A process runs until it invokes an operating-
system service or until it is interrupted– Disabling interrupts guarantees mutual
exclusion– Processor is limited in its ability to
interleave programs– Multiprocessing
• disabling interrupts on one processor will not guarantee mutual exclusion
![Page 36: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/36.jpg)
Mutual Exclusion:Hardware Support
• Special Machine Instructions– Performed in a single instruction cycle– Not subject to interference from other
instructions– Reading and writing– Reading and testing
![Page 37: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/37.jpg)
Mutual Exclusion:Hardware Support
• Test and Set Instructionboolean testset (int i) {
if (i == 0) {i = 1;return true;
}else {
return false;}
}
![Page 38: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/38.jpg)
Mutual Exclusion:Hardware Support
• Exchange Instruction
void exchange(int register, int memory) {
int temp;
temp = memory;
memory = register;
register = temp;
}
![Page 39: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/39.jpg)
Const int = n //number of processes
int bolt;
void P(int i)
{ while (true)
{ while(!testset(bolt))
//do nothing;
//critical section;
bolt = 0;
}
}
void main()
{
bolt = 0;
parbegin(P(1),P(2), …,P(n));
}
![Page 40: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/40.jpg)
Const int = n //number of processes
int bolt;
void P(int i)
{ int keyi;
while (true)
{ keyi = 1;
while(keyi != 0)
exchange(keyi,bolt);
//critical section;
exchange(keyi,bolt);
bolt = 0;
}
}
void main()
{
bolt = 0;
parbegin(P(1),P(2), …,P(n));
}
![Page 41: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/41.jpg)
Mutual Exclusion Machine Instructions
• Advantages– Applicable to any number of processes on
either a single processor or multiple processors sharing main memory
– It is simple and therefore easy to verify– It can be used to support multiple critical
sections
![Page 42: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/42.jpg)
Mutual Exclusion Machine Instructions
• Disadvantages– Busy-waiting consumes processor time– Starvation is possible when a process leaves
a critical section and more than one process is waiting.
– Deadlock• If a low priority process has the critical region
and a higher priority process needs, the higher priority process will obtain the processor to wait for the critical region
![Page 43: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/43.jpg)
O.S / PL
• Semaphore
• Monitors
• Message passing
![Page 44: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/44.jpg)
Semaphores
• Special variable called a semaphore is used for signaling
• If a process is waiting for a signal, it is suspended until that signal is sent
• Wait and signal operations cannot be interrupted
• Queue is used to hold processes waiting on the semaphore
![Page 45: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/45.jpg)
Semaphores
• Semaphore is a variable that has an integer value– May be initialized to a nonnegative number– Wait operation decrements the semaphore
value– Signal operation increments semaphore
value– signal(s): P, wait(s): V
![Page 46: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/46.jpg)
Const int n //number of processes
Semaphore s = 1;
Void P(int I)
{
while(true)
{
wait(s);
//cs;
signal(s);
}
}
Void main()
{
Parbegin(P(1),P(2), …P(n))
}
![Page 47: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/47.jpg)
1
0
A
Wait(s)
B
B
B
Wait(s)
C
C
Wait(s)
signal(s)
![Page 48: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/48.jpg)
Producer/Consumer Problem
• One or more producers are generating data and placing these in a buffer
• A single consumer is taking items out of the buffer one at time
• Only one producer or consumer may access the buffer at any one time
• Power & pitfalls of semaphore!
![Page 49: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/49.jpg)
Producer
producer:
while (true) {
/* produce item v */
b[in] = v;
in++;
}
![Page 50: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/50.jpg)
Consumer
consumer:while (true) { while (in <= out)
/*do nothing */;w = b[out];out++; /* consume item w */
}
![Page 51: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/51.jpg)
Void producer()
{
while(true)
{
Produce();
waitB(s);
Append();
N++
If(n==1) signalB(delay);
signalB(s);
}
}
Void consumer()
{
waitB(delay);
while(true)
{
waitB(s);
take();
N--
signalB(s);
consume()
if(n == 0) waitB(delay);
}
}
![Page 52: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/52.jpg)
![Page 53: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/53.jpg)
Void producer()
{
while(true)
{
Produce();
waitB(s);
Append();
N++
If(n==1) signalB(delay);
signalB(s);
}
}
Void consumer()
{
waitB(delay);
while(true)
{
waitB(s);
take();
N--
signalB(s);
consume()
if(n == 0) waitB(delay);
}
}
Int m;
M = n;
If (m ==0) waitB(delay)
![Page 54: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/54.jpg)
Infinite Buffer
![Page 55: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/55.jpg)
![Page 56: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/56.jpg)
Producer with Circular Buffer
producer:
while (true) {
/* produce item v */
while ((in + 1) % n == out) /* do nothing */;
b[in] = v;
in = (in + 1) % n
}
![Page 57: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/57.jpg)
Consumer with Circular Buffer
consumer:while (true) {while (in == out)
/* do nothing */;w = b[out];out = (out + 1) % n;/* consume item w */
}
![Page 58: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/58.jpg)
Barbershop Problem
![Page 59: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/59.jpg)
Barbershop
• Shop and sofa capacity
• Barber chair capacity
• Customers are in barber chair
• Holding customers in barber chair
• Limiting one customer/barber chair
• Paying and receiving
• Coordinating barber and cashier function
![Page 60: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/60.jpg)
![Page 61: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/61.jpg)
Void cashier()
{
while (true)
{
wait(payment);
wait(coord);
accept_pay();
signal (coord);
signal(receipt);
}
}
Semaphore max_capacity = 20;
Semaphore sofa = 4;
Semaphore barber_chair = 3;
Semaphore coord = 3;
Semaphore cust_ready = 0;
Semaphore finished = 0;
Semaphore leave_b_chair = 0;
Semaphore payment = 0;
Semaphore receipt = 0;
![Page 62: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/62.jpg)
Void barber()
{
While(true)
{
wait(cust_ready);
wait(coord);
cut_hair();
signal(coord);
signal(finished);
wait(leave_b_chair);
signal(barber_chair);
}
}
Void customer ()
{
wait(max_capacity);
enter_shop();
wait(sofa);
sit_on_sofa();
wait(barber_chair);
get_up_from_sofa();
signal_sofa();
sit_in_barber_chair();
signal(cust_ready);
wait(finished);
leave_barber_chaor();
signal(leave_b_chair);
pay();
signal(payment)
wait(recipt)
exit_shop();
signal(max_capacity);
}
![Page 63: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/63.jpg)
Void main()
{
parbegin(customer, …50times, barber, barber, barber, cashier);
}
![Page 64: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/64.jpg)
Monitors
• Monitor is a software module
• Chief characteristics– Local data variables are accessible only by
the monitor– Process enters monitor by invoking one of
its procedures– Only one process may be executing in the
monitor at a time
![Page 65: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/65.jpg)
![Page 66: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/66.jpg)
Monitor boundedbuffer;
Char buffer[N];
Int nextin, nextout;
Int count;
Int notfull, notempty;
void append(char x)
{
if (count) == N)
cwait(notfull);
buffer[nextin] = x;
nextin = (nextin++)%N
count++;
csignal(notempty)
}
void take(char x)
{
if (count == 0)
cwait(notempty);
x = buffer(nextout];
nextout = nextout++)%N
count++;
csignal(notfull);
}
{
Nextin = nextout = count =0;
}
![Page 67: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/67.jpg)
Void producer()
Char x;
{
while (true)
{
produce(x);
append(x);
}
}
Void consumer()
Char x;
{
while(true)
{
take(x);
consume(x)
}
}
Parbegin(producer, consumer)
![Page 68: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/68.jpg)
Message Passing
• Enforce mutual exclusion
• Exchange information
send (destination, message)
receive (source, message)
![Page 69: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/69.jpg)
Synchronization
• Sender and receiver may or may not be blocking (waiting for message)
• Blocking send, blocking receive– Both sender and receiver are blocked until
message is delivered– Called a rendezvous
![Page 70: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/70.jpg)
Synchronization
• Nonblocking send, blocking receive– Sender continues processing such as
sending messages as quickly as possible– Receiver is blocked until the requested
message arrives
• Nonblocking send, nonblocking receive– Neither party is required to wait
![Page 71: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/71.jpg)
Addressing
• Direct addressing– send primitive includes a specific identifier
of the destination process– receive primitive could know ahead of time
which process a message is expected– receive primitive could use source
parameter to return a value when the receive operation has been performed
![Page 72: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/72.jpg)
Addressing
• Indirect addressing– messages are sent to a shared data structure
consisting of queues– queues are called mailboxes– one process sends a message to the mailbox
and the other process picks up the message from the mailbox
![Page 73: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/73.jpg)
![Page 74: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/74.jpg)
Message Format
![Page 75: 3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing](https://reader035.vdocument.in/reader035/viewer/2022062301/56649f265503460f94c3db5a/html5/thumbnails/75.jpg)
Readers/Writers Problem
• Any number of readers may simultaneously read the file
• Only one writer at a time may write to the file
• If a writer is writing to the file, no reader may read it