Tom Kelliher, CS42
Sept. 25, 1996
to
.
Suppose the following jobs arrive for processing at the times indicated and run with the specified CPU bursts (at the end of a burst a process waits for one time unit on a resource). Assume that a just-created job enters the ready queue after any job entering the ready queue from the wait queue.

Calculate the average turnaround time for each of the scheduling disciplines listed:
Don't forget the ``bubble'' cycles (where no process is runnable), if required.
Recall cooperating processes --- they affect or are affected by other processes through access to shared variables.
E.g., threads.
Producer/Consumer (again).
const int N = 10;
int in = 0, out = 0, count = 0;
data buffer[N];
producer()
{
data next;
while (1)
{
produce in next;
while (counter == n)
;
buffer[in] = next;
in = ++in % N;
++counter;
}
}
consumer()
{
data next;
while (1)
{
while (counter == 0)
;
next = buffer[out];
out = ++out % N;
--counter;
consume next;
}
}
What variables are being shared?
What is the nature of the sharing?
Critical variables; critical sections.
Cases where cooperating processes need not synchronize to share resources:
What does atomic mean?
Characteristics of ``trouble situations:''
The overlapping portion of each process, where the shared variables are being accessed.
Necessary conditions for a solution to the c.s. problem:
is executing in one of its critical
sections, no
,
, is executing in its critical sections.
Assumptions:
(for n processes,
)
Pi:
do {
mutexbegin; /* CS entry */
CSi;
mutexend; /* CS exit */
non-CS
} while (!done);
mutexbegin()
{
disable_ints;
}
mutexend()
{
enable_ints;
}
Problems with this approach:
int TAS(int& val)
{
int temp;
temp = val; // Body performed atomically.
val = 1;
return temp;
}
int FAI(int& val)
{
return val++; // Performed atomically.
}
FAI solution to CS problem for n processes:
int nextTicket = 0, serving = 0;
mutexbegin()
{
int myTicket;
myTicket = FAI(nextTicket);
while (myTicket != serving)
;
}
mutexend()
{
++serving;
}
Correctness proof:
Assumption: no process ``plays'' with control variables.