Chapter 5
•
Look at the diagram; The issues are:
•
Communication among processes
•
Sharing resources
•
Synchronization of multiple processes
•
Allocation of processor time
•
Multiple applications
–
Multiprogramming to share the processor time
•
Structured applications
–
Applications can be designed as a set of concurrent processes
•
Operating-system structure
–
Operating system is also a set of processes or threads
•
Sharing global resources including variables (read/write dependencies)
•
Management of allocation of resources such as I/O devices
•
Programming errors difficult to locate because of unpredictable behaviors
void echo()
{ char chin,chout; chin = getchar(); chout = chin; putchar(chout);
}
This is a global procedure used by processes P1 and P2 which share chin as a common variable
.
Process P1 Process P2
.
chin = getchar(); .
.
(Interrupt) chin = getchar();
.
.
chout = chin; chout = chin; putchar(chout); .
putchar(chout);
.
•
Only one process should be allowed to use this procedure at any given time
•
Process P1 interrupted while inside procedure echo
•
Process P2 blocked from using echo so this problem cannot occur!!
•
PROTECT GLOBAL VARIABLES BY
CONTROLLING THE CODE
•
Keep track of active processes (ch4)
•
Allocate and deallocate resources
–
Processor time (scheduling)
– Memory (Virtual Memory Mgmt.)
–
Files (File System)
– I/O devices (I/O Mgmt.)
• Protect data and resources ( Protection )
•
Result of process must be independent of the speed of execution of other concurrent processes
•
Processes unaware of each other (not designed to work together; competing )
•
Processes indirectly aware of each other
(Share access to an object; cooperating )
•
Process directly aware of each other
(designed to work together and use IPC or other ways to communicate )
•
Each process unaware and (its results are) unaffected by other processes
•
However, the one who waits for a resource slows down; (starvation?)
•
Non-sharable resources such as printers need to be allocated in mutual exclusion state (deadlock?) (critical section)
•
Processes know that there are other processes out there
•
Writing must be mutually exclusive
•
Critical sections are used to provide data integrity
•
Deadlock, starvation may occur
• Messages are passed without sharing any resource
– 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
•
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
•
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
Providing Software Solution for Mutual
Exclusion: Dekker’s Algorithm
•
No support from OS except to serialize shared resource access
•
Busy Waiting
– Use a global variable called “turn”
– 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
•
Designed to be able to pass execution control back and forth between themselves
•
Inadequate to support concurrent processing
•
Processes may suffer from slowing down as well as crashing all processes
• Each process can examine the other’s status with a boolean array flag[] 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
•
This method does not guarantee mutual exclusion and it is not crash proof
•
Each process can check the flags and then proceed to enter the critical section at the same time
• Set flag to enter critical section before checking 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. Mutual Exclusion is guaranteed!!
•
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
•
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.
•
It is possible for each process to set its flag, check other processes, and reset its flag. This scenario is called
“livelock”and it will not last very long so it is not deadlock. It is undesirable but with random waiting delay, this cycle can be broken
•
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. If “turn” indicates this process’s turn, it can enter the critical section
• Both “turn” and “flag” provide a working solution!!
• Uniprocessor
–
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 because interrupts are being disabled
•
Multiprocessing
• disabling interrupts on one processor will not guarantee mutual exclusion
•
Special Machine Instructions
–
Performed in a single instruction cycle
– Not subject to interference from other instructions
– Reading and writing
–
Reading and testing
•
Test and Set Instruction (Atomic) boolean testset (int i) { if (i == 0) { i = 1; return true;
} else { return false;
}
}
• Exchange Instruction (Access to memory blocked for others) void exchange(int register, int memory) { int temp; temp = memory; memory = register; register = temp;
}
•
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, each defined by its own variable
•
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 it is interrupted in favor of a higher priority process, the higher priority process will obtain the processor and may wait for the critical region
•
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
•
Semaphore is a variable that has an integer value
•
It may be initialized to a nonnegative number
•
Wait operation decrements the semaphore value and if the value becomes negative, the process is suspended
•
Signal operation increments semaphore value and if it is still negative, a process waiting is unblocked
•
Check out the binary semaphores
• What is a weak semaphore anyway?
•
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 (No overlap allowed in buffer access)
producer: while (true) {
/* produce item v */ b[in] = v; in++;
}
consumer: while (true) { while (in <= out)
/*do nothing */; w = b[out]; out++;
/* consume item w */
}
•
Producer performs wait on a semaphore before writing to the buffer
•
If the buffer is being accessed by another producer or consumer, the producer will be blocked
• Consumer performs wait on a second semaphore that blocks it if the buffer is empty
•
Thus the producer also has to perform signal on the second semaphore if it just put a value in an empty buffer. Why?
• TO WAKE UP THE CONSUMER
• See the program and discussion in the textbook p223
producer: while (true) {
/* produce item v */ while ((in + 1) % n == out)
/* do nothing */; b[in] = v; in = (in + 1) % n
}
consumer: while (true) { while (in == out)
/* do nothing */; w = b[out]; out = (out + 1) % n;
/* consume item w */
}
•
But the barber shop has only three chairs for customers so only three barbers are available
•
The waiting area has the capacity of four customers
•
Only 20 customers can be accommodated within the shop at any time
•
When a barber is free, the longest waiting customer on the sofa moves to the chair
•
The longest standing customer then sits on the sofa
•
Payment is accepted one at a time
•
Barbers cut hair, accept payment and sleep if no customers are around
•
/* program barbershop1 */
• semaphore max_capacity = 20;
• semaphore sofa = 4;
• semaphore barber_chair = 3;
• semaphore coord = 3;
• semaphore cust_ready = 0, finished = 0, leave_b_chair = 0, payment= 0, receipt =
0;
• 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_chair();
• signal(leave_b_chair);
• pay();
• signal(payment);
• wait(receipt);
• exit_shop();
• signal(max_capacity)
• }
• void barber()
•
{
• while (true)
•
{
• wait(cust_ready);
• wait(coord);
• cut_hair();
• signal(coord);
• signal(finished);
• wait(leave_b_chair);
• signal(barber_chair);
• }
•
}
• void cashier()
•
{
• while (true)
•
{
• wait(payment);
• wait(coord);
• accept_pay();
• signal(coord);
• signal(receipt);
• }
•
}
•
Shop and Sofa Capacity
•
Barber Chair capacity
•
Putting exactly one customer in a chair
•
Holding customer in chair
•
Forcing customer to pay before leaving
•
Switching barber to become a cashier
•
Speed not taken into account; see discussion starting on page 232
•
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
–
Wait and signal different from semaphores; a signal may be lost if no one waiting
•
Enforce mutual exclusion
•
Exchange information send (destination, message) receive (source, message)
•
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
–
Tight synchronization
•
Non-blocking send, blocking receive
–
Sender continues processing such as sending messages as quickly as possible
–
Receiver is blocked until the requested message arrives (such as a server process)
•
Non-blocking send, non-blocking receive
–
Neither party is required to wait
•
Non-blocking send is dangerous!!
•
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
•
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
–
Sender and receiver are decoupled!!
–
1-N, N-1, 1-1 and N-N messages possible
•
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
•
Read Section 5.7