Operating Systems Question
Consider the following concurrent program:
int count;
void f()
{
for (int j=1; j<=25; j++) {
count = count + 1;
}
}
int main ()
{
count = 0;
parbegin
f(); f();
parend;
cout << count << endl;
}
The statements inside the parbegin/parend block are executed
concurrently.
a) What is the lower bound and upper bound on the final value
for variable count when this concurrent program is
executed? Assume that processes are executed at any
relative speed and that a value can only be incremented
after it has been loaded into a register by a separate
machine instruction. State any other assumptions you make,
if any.
b) Suppose that an arbitrary number of processes, each
executing f(), are permitted to execute concurrently under
the assumptions in part a). What effect will this
modification have on the range of final values for variable
count?
c) Show how you would use semaphores to modify the program
above so that the final value in variable count is always
the upper bound determined in parts a) and b).
d) Is it possible for the program from part c) above to
deadlock? Why or why not?
e) Is it possible for one or more processes to starve in the
program from part c) above? If no, why not? If yes,
discuss ways to avoid the starvation.
f) Suppose that round robin scheduling is used for the
processes in the program for parts a) and b) above (i.e.,
the program without semaphores). The final value in
variable count can be affected by the size of the time
quantum used by the scheduler. Discuss this.
a) lower bound: 25
upper bound: 50
b) lower bound: 25
upper bound: 25 * number of processes
c) semaphore s = 1; // add this line
int count;
void f()
{
for (int j=1; j<=25; j++) {
wait(s); // add this line
count = count + 1;
signal(s); // add this line
}
}
int main ()
{
count = 0;
parbegin
f(); f();
parend;
cout << count << endl;
}
d) Deadlock cannot happen as long as the semaphore is
initialized to 1.
e) Starvation is possible depending on how the semaphore is
implemented. The simplest way to avoid starvation is to
assume that the semaphore implements a FIFO wait queue.
f) Round robin scheduling with a short time quantum could tend
to bias the final value in variable count towards the lower
bound. Round robin scheduling with a larger time quantum
could tend to bias the final value in variable count
towards the upper bound.