Chapter 4-Process Management
Chapter 4-Process Management
Process Management
Introduction
• An important and fundamental feature in modern operating
systems is concurrent execution of processes/threads.
• This feature is essential for the realization of multiprogramming,
multiprocessing, distributed systems, and client-server model of
computation.
• Concurrency encompasses many design issues including
communication and synchronization among processes,
sharing of and contention for resources.
Principles of Concurrency
• Interleaving and overlapping the execution of
processes.
• Consider two processes P1 and P2 executing
the function echo:
{
input (in, keyboard);
out = in;
output (out, display);
}
...Concurrency (contd.)
• P1 invokes echo, after it inputs into in , gets interrupted (switched).
• P2 invokes echo, inputs into in and completes the execution and exits.
• When P1 returns in is overwritten and gone. Result: first ch is lost and
second ch is written twice.
• This type of situation is even more probable in multiprocessing systems
where real concurrency is realizable thru’ multiple processes executing
on multiple processors.
• Solution: Controlled access to shared resource
• Protect the shared resource : in buffer; “critical resource”
• one process/shared code. “critical region”
Interactions among processes
• In a multi-process application these are the various degrees of
interaction:
1. Competing processes: Processes themselves do not share
anything. But OS has to share the system resources among
these processes “competing” for system resources such as
disk, file or printer.
Co-operating processes : Results of one or more processes may
be needed for another process.
2. Co-operation by sharing : Example: Sharing of an IO buffer.
Concept of critical section. (indirect)
3. Co-operation by communication : Example: typically no data
sharing, but co-ordination thru’ synchronization becomes
essential in certain applications. (direct)
Interactions ...(contd.)
turn = j;
• Shared data:
Semaphore mutex; //initially mutex = 1
• Process Pi:
do {
mutex.wait();
critical section
mutex.signal();
remainder section
} while (1);
Semaphore Implementation
• Define a semaphore as a class:
class Semaphore
{ int value; // semaphore value
ProcessQueue L; // process queue
//operations
wait()
signal()
}
• In addition, two simple utility operations:
• block() suspends the process that invokes it.
• Wakeup() resumes the execution of a blocked process P.
Semantics of wait and signal
• Semaphore operations now defined as
S.wait():
S.value--;
if (S.value < 0) {
add this process to S.L;
block(); // block a
process
}
S.signal():
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(); // wake a process
}
Semaphores for CS
• Dining-Philosophers Problem
Producer/Consumer problem
• Producer • Consumer
repeat repeat
produce item v; while (in <= out) nop;
b[in] = v; w = b[out];
in = in + 1; out = out + 1;
forever; consume w;
forever;
Solution for P/C using Semaphores
• Producer • Consumer
• repeat • repeat
• produce item v; • while (in <= out) nop;
• MUTEX.wait(); • MUTEX.wait();
• b[in] = v; • w = b[out];
• out = out + 1;
• in = in + 1;
• MUTEX.signal();
• MUTEX.signal();
• consume w;
• forever;
• forever;
• Ans: Consumer will busy-
• What if Producer is wait at the while
slow or late? statement.
P/C: improved solution
• Producer • Consumer
repeat repeat
produce item v; AVAIL.wait();
MUTEX.wait(); MUTEX.wait();
b[in] = v; w = b[out];
in = in + 1; out = out + 1;
MUTEX.signal(); MUTEX.signal();
AVAIL.signal(); consume w;
forever; forever;
void putdown(int i) {
state[i] = thinking;
// test left and right neighbors
test((i+4) % 5);
test((i+1) % 5);
}
Dining Philosophers
void test(int i) {
if ( (state[(I + 4) % 5] != eating) &&
(state[i] == hungry) &&
(state[(i + 1) % 5] != eating)) {
state[i] = eating;
self[i].signal();
}
}
Assignment
• Read the following practical problem and write
explanations
• Submission: During next class or lab
A Practical Problem
High in the Andes mountains, there are two circular railway lines. One line is in
Peru, the other in Bolivia. They share a common section of track where the lines
cross a mountain pass that lies on the international border (near Lake Titicaca?).
Unfortunately, the Peruvian and Bolivian trains occasionally collide when simultaneously
entering the common section of track (the mountain pass). The trouble is, alas, that the
drivers of the two trains are both blind and deaf, so they can neither see nor hear each other.
The two drivers agreed on the following method of preventing collisions. They
set up a large bowl at the entrance to the pass. Before entering the pass, a
driver must stop his train, walk over to the bowl, and reach into it to see it it
contains a rock. If the bowl is empty, the driver finds a rock and drops it in the
bowl, indicating that his train is entering the pass; once his train has cleared the
pass, he must walk back to the bowl and remove his rock, indicating that the
pass in no longer being used. Finally, he walks back to the train and continues
down the line.
If a driver arriving at the pass finds a rock in the bowl, he leaves the rock there;
he repeatedly takes a siesta and rechecks the bowl until he finds it empty. Then
he drops a rock in the bowl and drives his train into the pass. A smart graduate
from the University of La Paz (Bolivia) claimed that subversive train schedules
made up by Peruvian officials could block the train forever.
Explain 1
The Bolivian driver just laughed and said that could not be true because it never
happened.
Explain 2
Explain 3
Page 43
Following the crash, the graduate was called in as a consultant to ensure that no
more crashes would occur. He explained that the bowl was being used in the
wrong way. The Bolivian driver must wait at the entry to the pass until the bowl is
empty, drive through the pass and walk back to put a rock in the bowl. The
Peruvian driver must wait at the entry until the bowl contains a rock, drive through
the pass and walk back to remove the rock from the bowl. Sure enough, his
method prevented crashes.
Prior to this arrangement, the Peruvian train ran twice a day and the Bolivian train
ran once a day. The Peruvians were very unhappy with the new arrangement.
Explain 4
The graduate was called in again and was told to prevent crashes while avoiding
the problem of his previous method. He suggested that two bowls be used, one
for each driver. When a driver reaches the entry, he first drops a rock in his bowl,
then checks the other bowl to see if it is empty. If so, he drives his train through
the pass. Stops and walks back to remove his rock. But if he finds a rock in the
other bowl, he goes back to his bowl and removes his rock. Then he takes a
siesta, again drops a rock in his bowl and re-checks the other bowl, and so on,
until he finds the other bowl empty. This method worked fine until late in May,
when the two trains were simultaneously blocked at the entry for many siestas.
Explain 5