• Home
  • Textbooks
  • Operating System Concepts Essentials
  • Process Synchronization

Operating System Concepts Essentials

Abraham Silberschatz, Peter B. Galvin, Greg Gagne

Chapter 6

Process Synchronization - all with Video Answers

Educators


Chapter Questions

Problem 1

In Section 6.4, we mentioned that disabling interrupts frequently can affect the system's clock. Explain why this can occur and how such effects can be minimized.

Check back soon!
04:18

Problem 2

The Cigarette-Smokers Problem. Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches. One of the smoker processes has paper, another has tobacco, and the third has matches. The agent has an infinite supply of all three materials. The agent places two of the ingredients on the table. The smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent on completion. The agent then puts out another two of the three ingredients, and the cycle repeats. Write a program to synchronize the agent and the smokers using Java synchronization.

Daniel Caproni
Daniel Caproni
Numerade Educator

Problem 3

Explain why Solaris, Windows XP, and Linux implement multiple locking mechanisms. Describe the circumstances under which they use spinlocks, mutexes, semaphores, adaptive mutexes, and condition variables. In each case, explain why the mechanism is needed.

Check back soon!
00:54

Problem 4

List three examples of deadlocks that are not related to a computersystem environment.

Adam Conner
Adam Conner
Numerade Educator

Problem 5

Is it possible to have a deadlock involving only a single process? Explain your answer.

Check back soon!
01:48

Problem 6

Race conditions are possible in many computer systems. Consider a banking system with two functions: deposit (amount) and withdraw (amount). These two functions are passed the amount that is to be deposited or withdrawn from a bank account. Assume a shared bank account exists between a husband and wife and concurrently the husband calls the withdraw () function and the wife calls deposit(). Describe how a race condition is possible and what might be done to prevent the race condition from occurring.

Amit Srivastava
Amit Srivastava
Numerade Educator

Problem 7

The first known correct software solution to the critical-section problem for two processes was developed by Dekker. The two processes, $P_0$ and $P_1$, share the following variables:
boolean flag $[2] ; / *$ initially false */
int turn;
boolean flag[2]; /* initially false */
int turn;
do {
flag[i] = TRUE;
while (flag[j]) {
if (turn == j) {
flag[i] = false;
while (turn == j)
; // do nothing
flag[i] = TRUE;
}
}
// critical section
turn = j;
flag[i] = FALSE;
// remainder section
} while (TRUE);
The structure of process $P_i$ ( $i=0$ or 1 ) is shown in Figure 6.26 ; the other process is $P_j(j==1$ or 0$)$. Prove that the algorithm satisfies all three requirements for the critical-section problem.

Check back soon!
05:28

Problem 8

The first known correct software solution to the critical-section problem for $n$ processes with a lower bound on waiting of $n-1$ turns was presented by Eisenberg and McGuire. The processes share the following variables:
enum pstate $\{$ idle, want_in, in_cs $\}$
pstate flag[n];
int turn;
enum pstate $\{$ idle, want_in, in_cs\};
pstate flag[n];
int turn;
All the elements of flag are initially idle; the initial value of turn is immaterial (between 0 and $\mathrm{n}-1$ ). The structure of process $P_i$ is shown in Figure 6.27. Prove that the algorithm satisfies all three requirements for the critical-section problem.

Samriddhi Singh
Samriddhi Singh
Numerade Educator
01:22

Problem 9

What is the meaning of the term busy waiting? What other kinds of waiting are there in an operating system? Can busy waiting be avoided altogether? Explain your answer.

Hubert Agamasu
Hubert Agamasu
Numerade Educator
01:29

Problem 10

Explain why spinlocks are not appropriate for single-processor systems yet are often used in multiprocessor systems.

Adam Conner
Adam Conner
Numerade Educator
01:29

Problem 11

Explain why implementing synchronization primitives by disabling interrupts is not appropriate in a single-processor system if the synchronization primitives are to be used in user-level programs.

Adam Conner
Adam Conner
Numerade Educator
01:29

Problem 12

Explain why interrupts are not appropriate for implementing synchronization primitives in multiprocessor systems.
do {
while (TRUE) {
flag[i] = want in;
j = turn;
while (j != i) {
if (flag[j] != idle) {
j = turn;
else
j = (j + 1) % n;
}
flag[i] = in cs;
j = 0;
while ( (j < n) && (j == i || flag[j] != in cs))
j++;
if ( (j >= n) && (turn == i || flag[turn] == idle))
break;
}
// critical section
j = (turn + 1) % n;
while (flag[j] == idle)
j = (j + 1) % n;
turn = j;
flag[i] = idle;
// remainder section
} while (TRUE);

Adam Conner
Adam Conner
Numerade Educator
02:01

Problem 13

Describe two kernel data structures in which race conditions are possible. Be sure to include a description of how a race condition can occur.

Rodger Claar
Rodger Claar
Numerade Educator

Problem 14

Describe how the $\operatorname{Swap}()$ instruction can be used to provide mutual exclusion that satisfies the bounded-waiting requirement.

Check back soon!

Problem 15

Servers can be designed to limit the number of open connections. For example, a server may wish to have only $N$ socket connections at any point in time. As soon as $N$ connections are made, the server will not accept another incoming connection until an existing connection is released. Explain how semaphores can be used by a server to limit the number of concurrent connections.

Check back soon!
01:39

Problem 16

Show that, if the wait () and signal () semaphore operations are not executed atomically, then mutual exclusion may be violated.

James Kiss
James Kiss
Numerade Educator

Problem 17

Windows Vista provides a new lightweight synchronization tool called slim reader-writer locks. Whereas most implementations of readerwriter locks favor either readers or writers, or perhaps order waiting threads using a FIFO policy, slim reader-writer locks favor neither readers nor writers, nor are waiting threads ordered in a FIFO queue. Explain the benefits of providing such a synchronization tool.

Check back soon!

Problem 18

Show how to implement the wait () and signal() semaphore operations in multiprocessor environments using the TestAndSet () instruction. The solution should exhibit minimal busy waiting.

Check back soon!

Problem 19

Exercise 4.17 requires the parent thread to wait for the child thread to finish its execution before printing out the computed values. If we let the parent thread access the Fibonacci numbers as soon as they have been computed by the child thread - rather than waiting for the child thread to terminate - explain what changes would be necessary to the solution for this exercise? Implement your modified solution.

Check back soon!

Problem 20

Demonstrate that monitors and semaphores are equivalent insofar as they can be used to implement the same types of synchronization problems.

Check back soon!

Problem 21

Write a bounded-buffer monitor in which the buffers (portions) are embedded within the monitor itself.

Check back soon!
01:19

Problem 22

The strict mutual exclusion within a monitor makes the bounded-buffer monitor of Exercise 6.21 mainly suitable for small portions.
a. Explain why this is true.
b. Design a new scheme that is suitable for larger portions.

Manisha Sarker
Manisha Sarker
Numerade Educator
02:47

Problem 23

Discuss the tradeoff between fairness and throughput of operations in the readers-writers problem. Propose a method for solving the readers-writers problem without causing starvation.

Dharmendra Jain
Dharmendra Jain
Numerade Educator

Problem 24

How does the signal () operation associated with monitors differ from the corresponding operation defined for semaphores?

Check back soon!
01:40

Problem 25

Suppose the signal () statement can appear only as the last statement in a monitor procedure. Suggest how the implementation described in Section 6.7 can be simplified in this situation.

Manik Pulyani
Manik Pulyani
Numerade Educator

Problem 26

Consider a system consisting of processes $P_1, P_2, \ldots, P_n$, each of which has a unique priority number. Write a monitor that allocates three identical line printers to these processes, using the priority numbers for deciding the order of allocation.

Check back soon!
01:37

Problem 27

A file is to be shared among different processes, each of which has a unique number. The file can be accessed simultaneously by several processes, subject to the following constraint: The sum of all unique numbers associated with all the processes currently accessing the file must be less than $n$. Write a monitor to coordinate access to the file.

James Kiss
James Kiss
Numerade Educator
01:38

Problem 28

When a signal is performed on a condition inside a monitor, the signaling process can either continue its execution or transfer control to the process that is signaled. How would the solution to the preceding exercise differ with these two different ways in which signaling can be performed?

VS
Vivek Singh
Numerade Educator

Problem 29

Suppose we replace the wait() and signal () operations of monitors with a single construct await (B), where B is a general Boolean expression that causes the process executing it to wait until B becomes true.
a. Write a monitor using this scheme to implement the readerswriters problem.
b. Explain why, in general, this construct cannot be implemented efficiently.
c. What restrictions need to be put on the await statement so that it can be implemented efficiently?

Check back soon!
02:21

Problem 30

Write a monitor that implements an alarm clock that enables a calling program to delay itself for a specified number of time units (ticks). You may assume the existence of a real hardware clock that invokes a procedure tick in your monitor at regular intervals.

Darren Wilson
Darren Wilson
Numerade Educator
01:29

Problem 31

Why do Solaris, Linux, and Windows use spinlocks as a synchronization mechanism only on multiprocessor systems and not on single-processor systems?

Adam Conner
Adam Conner
Numerade Educator

Problem 32

Assume that a finite number of resources of a single resource type must be managed. Processes may ask for a number of these resources and - once finished - will return them. As an example, many commercial software packages provide a given number of licenses, indicating the number of applications that may run concurrently. When the application is started, the license count is decremented. When the application is terminated, the license count is incremented. If all licenses are in use, requests to start the application are denied. Such requests will only be granted when an existing license holder terminates the application and a license is returned.
The following program segment is used to manage a finite number of instances of an available resource. The maximum number of resources and the number of available resources are declared as follows:
$$
\begin{aligned}
& \text { \#define MAX_RESOURCES } 5 \\
& \text { int available_resources = MAX_RESOURCES; }
\end{aligned}
$$
When a process wishes to obtain a number of resources, it invokes the decrease_count () function:
/* decrease available_resources by count resources */
/* return 0 if sufficient resources available, */
$/ *$ otherwise return $-1 * /$
int decrease_count (int count) \{
if (available_resources < count)
return -1 ;
else \{
available_resources $-=$ count;
return 0 ;
\} ?
/* decrease available_resources by count resources */
$/ *$ return 0 if sufficient resources available, */
/* otherwise return $-1 * /$
int decrease_count (int count) \{
if (available_resources $<$ count)
return -1 ;
else \{
available_resources $=$ count;
return 0 ;
\}
\}
When a process wants to return a number of resources, it calls the increase_count () function:
/* increase available_resources by count */
int increase_count(int count) \{
available_resources $+=$ count;
return 0 ;
/* increase available_resources by count */
int increase_count (int count) \{
available_resources $+=$ count;
return 0 ;
\}
The preceding program segment produces a race condition. Do the following:
a. Identify the data involved in the race condition.
b. Identify the location (or locations) in the code where the race condition occurs.
c. Using a semaphore, fix the race condition. It is OK to modify the decrease_count () function so that the calling process is blocked until sufficient resources are available.

Check back soon!
01:14

Problem 33

The decrease_count () function in the previous exercise currently returns 0 if sufficient resources are available and -1 otherwise. This leads to awkward programming for a process that wishes to obtain a number of resources:
while (decrease_count $($ count) $==-1$ )
Rewrite the resource-manager code segment using a monitor and condition variables so that the decrease_count () function suspends the process until sufficient resources are available. This will allow a process to invoke decrease_count () by simply calling
decrease_count(count);
The process will return from this function call only when sufficient resources are available.
Figure 6.28 (FIGURE CAN'T COPY)

James Kiss
James Kiss
Numerade Educator
01:39

Problem 34

Consider the traffic deadlock depicted in Figure 6.28.
a. Show that the four necessary conditions for deadlock hold in this example.
b. State a simple rule for avoiding deadlocks in this system.

James Kiss
James Kiss
Numerade Educator

Problem 35

Consider the deadlock situation that can occur in the diningphilosophers problem when the philosophers obtain the chopsticks one at a time. Discuss how the four necessary conditions for deadlock hold in this setting. Discuss how deadlocks could be avoided by eliminating any one of the four necessary conditions.

Check back soon!

Problem 36

The Sleeping-Barber Problem. A barbershop consists of a waiting room with $n$ chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.

Check back soon!