CP 226 Tutorial 05
CP 226 Tutorial 05
Reading Tutorial
1. Mutex Locks
2. Semaphores: Include:
Binary semaphore
Counting Semaphores
Semaphores Implementation
Apply Semaphore and Mutex Locks to solve various synchronization problems
3. Synchronization Hardware (Combine semaphore and Synchronization Hardware )
Tutorial Questions
1. Explain why Windows, Linux, and Solaris implement multiple locking mechanisms.
Describe the circumstances under which they use spinlocks, mutex locks, semaphores,
adaptive mutex locks, and condition variables. In each case, explain why the mechanism is
needed.
2. 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.
3. Explain why spinlocks are not appropriate for single-processor systems yet are often used in
multiprocessor systems.
4. Show that, if the wait() and signal() semaphore operations are not executed atomically, then
mutual exclusion may be violated.
5. Illustrate how a binary semaphore can be used to implement mutual exclusion among n
processes.
6. The implementation of mutex locks provided in Section 5.5 suffers from busy waiting.
Describe what changes would be necessary so that a process waiting to acquire a mutex lock
would be blocked and placed into a waiting queue until the lock became available.
7. Assume that a system has multiple processing cores. For each of the following scenarios,
describe which is a better locking mechanism—a spinlock or a mutex lock where waiting
processes sleep while waiting for the lock to become available:
The lock is to be held for a short duration.
The lock is to be held for a long duration.
A thread may be put to sleep while holding the lock.
8. Windows Vista provides a lightweight synchronization tool called slim reader–writer locks.
Whereas most implementations of reader–writer 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.
9. 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.
10. The producer consumer problem (or bounded buffer problem) describes two processes, the
producer and the consumer, which share a common, fixed-size buffer used as a queue.
Producer produces an item and put it into buffer. If buffer is already full then producer will
have to wait for an empty block in buffer. Consumer consumes an item from buffer. If buffer
is already empty then consumer will have to wait for an item in buffer. Apply Semaphore and
mutex locks to ensure synchronization between Producer and Consumer
11. 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.
12. 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.
13. Briefly describe the characteristics of a complete solution to the critical section problem.
14. 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 will return them once finished. 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:
When a process wants to return a number of resources, it calls the increase count() function: