code in execution, like a process but without as much state (esp without an address space)
policy - what to do (e.g., give a class of users
a specific priority)
mechanism - how to carry out the policy (e.g. support different priorities for different processes)
The ability to run more than one process at a time
(though not simultaneously) and to have the operating system switch
between tasks without the task voluntarily giving up the prcessor.
User program could change the timer interrupt and prevent the OS from getting control of the processor back.
Virtualize the interrupt vector. Processes can change
the virtual interrupt, but the hardware timer interrupt is still
controlled by the kernel.
If there is more than one instance of a resource, it is possible for a circular waiting condition to be broken when a third party (not involved in the circular wait) releases a resource.
maintain a list of all processes waiting for each
resource (make it a queue to ensure resources are handed out FIFO).
For each process, maintain a list of resources held.
inLoop[1..nproc] of Boolean;
for each process p {
inloop = false;
while (p <> null) {
if inLoop[p] then return true
else
inLoop[p] = true;
p = waitOn[p];
}
}
}
while not done {
if request is acquire
if requested resource is free
requested.resource.holder = requesting
process;
else
enqueue(request.resource, requesting
process);
waitingOn[requesting process] = requested.resource.holder;
else if request is release
request.holder = dequeue(resource);
Below is a skeleton program for you to build upon by supplying code for the boxes and perhaps introducing more variables. You are also welcome to disregard this skeleton and come up with something else.
nr_active:
integer initialized to 0. /* number of reads currently executing
*/
nr_wait:
integer initialized to 0. /* number of readers currently waiting
*/
r_sem:
semaphore initialized to 0. /* readers wait here */
nw_active:
integer initialized to 0. /* number of writes currently executing
*/
nw_wait:
integer initialized to 0. /* number of writers currently waiting
*/
Readers execute this code:
while (1) {
P(mutex);
if (nw_active + nw_waiting == 0)
nr_active++;
V(r_sem);
else
nr_waiting++;
V(mutex);
P(r_sem);
Read operation;
P(mutex);
nr_active--;
if (nr_active == 0) and (nw_waiting
!= 0)
while (nw_waiting)
nw_active++;
nw_waiting--;
v(w_sem);
V(mutex);
}
w_sem:
semaphore initialized to 0. /* writers wait here */
mutex: semaphore initialized to 0.
/* protect shared object */
Writers execute this code:
while (1) {
P(mutex);
if (nr_active + nr_waiting == 0)
nw_active++;
V(w_sem);
else
nw_waiting++;
V(mutex);
P(w_sem);
Write operation;
P(mutex);
nw_active--;
if (nw_active == 0) and (nr_waiting
!= 0)
while (nr_waiting)
nr_active++;
nr_waiting--;
v(r_sem);
V(mutex);
}