Practice Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

2 Chapter 1 Introduction

1.4 Keeping in mind the various definitions of operating system, consider


whether the operating system should include applications such as Web

1
browsers and mail programs. Argue both that it should and that it should
not, and support your answers.
CHAPTER Answer:
An argument in favor of including popular applications with the
operating system is that if the application is embedded within the
operating system, it is likely to be better able to take advantage of
features in the kernel and therefore have performance advantages

Introduction over an application that runs outside of the kernel. Arguments against
embedding applications within the operating system typically dominate
however: (1) the applications are applications - and not part of an
operating system, (2) any performance benefits of running within the
kernel are offset by security vulnerabilities, (3) it leads to a bloated
operating system.
Practice Exercises 1.5 How does the distinction between kernel mode and user mode function
as a rudimentary form of protection (security) system?
1.1 What are the three main purposes of an operating system? Answer:
Answer: The distinction between kernel mode and user mode provides a rudi-
The three main puropses are: mentary form of protection in the following manner. Certain instructions
could be executed only when the CPU is in kernel mode. Similarly, hard-
• To provide an environment for a computer user to execute programs ware devices could be accessed only when the program is executing in
on computer hardware in a convenient and efficient manner. kernel mode. Control over when interrupts could be enabled or disabled
is also possible only when the CPU is in kernel mode. Consequently, the
• To allocate the separate resources of the computer as needed to CPU has very limited capability when executing in user mode, thereby
solve the problem given. The allocation process should be as fair enforcing protection of critical resources.
and efficient as possible.
1.6 Which of the following instructions should be privileged?
• As a control program it serves two major functions: (1) supervision
of the execution of user programs to prevent errors and improper use a. Set value of timer.
of the computer, and (2) management of the operation and control
b. Read the clock.
of I/O devices.
c. Clear memory.
1.2 We have stressed the need for an operating system to make efficient use
d. Issue a trap instruction.
of the computing hardware. When is it appropriate for the operating
system to forsake this principle and to “waste” resources? Why is such e. Turn off interrupts.
a system not really wasteful?
f. Modify entries in device-status table.
Answer:
Single-user systems should maximize use of the system for the user. A g. Switch from user to kernel mode.
GUI might “waste” CPU cycles, but it optimizes the user’s interaction
h. Access I/O device.
with the system.
1.3 What is the main difficulty that a programmer must overcome in writing Answer:
an operating system for a real-time environment? The following operations need to be privileged: Set value of timer, clear
Answer: memory, turn off interrupts, modify entries in device-status table, access
I/O device. The rest can be performed in user mode.
The main difficulty is keeping the operating system within the fixed time
constraints of a real-time system. If the system does not complete a task 1.7 Some early computers protected the operating system by placing it in
in a certain time frame, it may cause a breakdown of the entire system it a memory partition that could not be modified by either the user job
is running. Therefore when writing an operating system for a real-time or the operating system itself. Describe two difficulties that you think
system, the writer must be sure that his scheduling schemes don’t allow could arise with such a scheme.
response time to exceed the time constraint. Answer:
1
Practice Exercises 3 4 Chapter 1 Introduction

The data required by the operating system (passwords, access controls, retain data as well), and (b) the cache is affordable, because faster storage
accounting information, and so on) would have to be stored in or passed tends to be more expensive.
through unprotected memory and thus be accessible to unauthorized
1.11 Distinguish between the client–server and peer-to-peer models of
users.
distributed systems.
1.8 Some CPUs provide for more than two modes of operation. What are Answer:
two possible uses of these multiple modes? The client-server model firmly distinguishes the roles of the client and
Answer: server. Under this model, the client requests services that are provided
Although most systems only distinguish between user and kernel by the server. The peer-to-peer model doesn’t have such strict roles. In
modes, some CPUs have supported multiple modes. Multiple modes fact, all nodes in the system are considered peers and thus may act as
could be used to provide a finer-grained security policy. For example, either clients or servers—or both. A node may request a service from
rather than distinguishing between just user and kernel mode, you another peer, or the node may in fact provide such a service to other
could distinguish between different types of user mode. Perhaps users peers in the system.
belonging to the same group could execute each other’s code. The For example, let’s consider a system of nodes that share cooking
machine would go into a specified mode when one of these users was recipes. Under the client-server model, all recipes are stored with the
running code. When the machine was in this mode, a member of the server. If a client wishes to access a recipe, it must request the recipe from
group could run code belonging to anyone else in the group. the specified server. Using the peer-to-peer model, a peer node could ask
Another possibility would be to provide different distinctions within other peer nodes for the specified recipe. The node (or perhaps nodes)
kernel code. For example, a specific mode could allow USB device drivers with the requested recipe could provide it to the requesting node. Notice
to run. This would mean that USB devices could be serviced without how each peer may act as both a client (it may request recipes) and as a
having to switch to kernel mode, thereby essentially allowing USB device server (it may provide recipes).
drivers to run in a quasi-user/kernel mode.
1.9 Timers could be used to compute the current time. Provide a short
description of how this could be accomplished.
Answer:
A program could use the following approach to compute the current
time using timer interrupts. The program could set a timer for some
time in the future and go to sleep. When it is awakened by the interrupt,
it could update its local state, which it is using to keep track of the
number of interrupts it has received thus far. It could then repeat this
process of continually setting timer interrupts and updating its local
state when the interrupts are actually raised.
1.10 Give two reasons why caches are useful. What problems do they solve?
What problems do they cause? If a cache can be made as large as the
device for which it is caching (for instance, a cache as large as a disk),
why not make it that large and eliminate the device?
Answer:
Caches are useful when two or more components need to exchange
data, and the components perform transfers at differing speeds. Caches
solve the transfer problem by providing a buffer of intermediate speed
between the components. If the fast device finds the data it needs in the
cache, it need not wait for the slower device. The data in the cache must
be kept consistent with the data in the components. If a component has
a data value change, and the datum is also in the cache, the cache must
also be updated. This is especially a problem on multiprocessor systems
where more than one process may be accessing a datum. A component
may be eliminated by an equal-sized cache, but only if: (a) the cache
and the component have equivalent state-saving capacity (that is, if the
component retains its data when electricity is removed, the cache must
6 Chapter 2 Operating-System Structures

• Free-space management.

2
• Storage allocation.
Operating- CHAPTER
• Disk scheduling.

System
2.5 What is the purpose of the command interpreter? Why is it usually
separate from the kernel?
Answer:
It reads commands from the user or from a file of commands and executes

Structures them, usually by turning them into one or more system calls. It is usually
not part of the kernel since the command interpreter is subject to changes.
2.6 What system calls have to be executed by a command interpreter or shell
in order to start a new process?
Answer:
In Unix systems, a fork system call followed by an exec system call need
Practice Exercises to be performed to start a new process. The fork call clones the currently
executing process, while the exec call overlays a new process based on a
2.1 What is the purpose of system calls? different executable over the calling process.
Answer:
System calls allow user-level processes to request services of the operat- 2.7 What is the purpose of system programs?
ing system. Answer:
System programs can be thought of as bundles of useful system calls.
2.2 What are the five major activities of an operating system with regard to They provide basic functionality to users so that users do not need to
process management? write their own programs to solve common problems.
Answer:
The five major activities are: 2.8 What is the main advantage of the layered approach to system design?
What are the disadvantages of using the layered approach?
a. The creation and deletion of both user and system processes Answer:
As in all cases of modular design, designing an operating system in
b. The suspension and resumption of processes a modular way has several advantages. The system is easier to debug
c. The provision of mechanisms for process synchronization and modify because changes affect only limited sections of the system
rather than touching all sections of the operating system. Information
d. The provision of mechanisms for process communication is kept only where it is needed and is accessible only within a defined
e. The provision of mechanisms for deadlock handling and restricted area, so any bugs affecting that data must be limited to a
specific module or layer.
2.3 What are the three major activities of an operating system with regard
to memory management? 2.9 List five services provided by an operating system, and explain how each
Answer: creates convenience for users. In which cases would it be impossible for
The three major activities are: user-level programs to provide these services? Explain your answer.
Answer:
a. Keep track of which parts of memory are currently being used and The five services are:
by whom.
b. Decide which processes are to be loaded into memory when a. Program execution. The operating system loads the contents (or
memory space becomes available. sections) of a file into memory and begins its execution. A user-level
program could not be trusted to properly allocate CPU time.
c. Allocate and deallocate memory space as needed.
b. I/O operations. Disks, tapes, serial lines, and other devices must be
2.4 What are the three major activities of an operating system with regard communicated with at a very low level. The user need only specify
to secondary-storage management? the device and the operation to perform on it, while the system
Answer: converts that request into device- or controller-specific commands.
The three major activities are: User-level programs cannot be trusted to access only devices they
5
Practice Exercises 7 8 Chapter 2 Operating-System Structures

should have access to and to access them only when they are certain locations of the hard disk to be recognized during system startup.
otherwise unused. Boot managers often provide the user with a selection of systems to boot
into; boot managers are also typically designed to boot into a default
c. File-system manipulation. There are many details in file creation,
operating system if no choice is selected by the user.
deletion, allocation, and naming that users should not have to
perform. Blocks of disk space are used by files and must be tracked.
Deleting a file requires removing the name file information and
freeing the allocated blocks. Protections must also be checked
to assure proper file access. User programs could neither ensure
adherence to protection methods nor be trusted to allocate only
free blocks and deallocate blocks on file deletion.
d. Communications. Message passing between systems requires
messages to be turned into packets of information, sent to the
network controller, transmitted across a communications medium,
and reassembled by the destination system. Packet ordering and
data correction must take place. Again, user programs might not
coordinate access to the network device, or they might receive
packets destined for other processes.
e. Error detection. Error detection occurs at both the hardware and
software levels. At the hardware level, all data transfers must be
inspected to ensure that data have not been corrupted in transit. All
data on media must be checked to be sure they have not changed
since they were written to the media. At the software level, media
must be checked for data consistency; for instance, whether the
number of allocated and unallocated blocks of storage match the
total number on the device. There, errors are frequently process-
independent (for instance, the corruption of data on a disk), so there
must be a global program (the operating system) that handles all
types of errors. Also, by having errors processed by the operating
system, processes need not contain code to catch and correct all the
errors possible on a system.

2.10 Why do some systems store the operating system in firmware, while
others store it on disk?
Answer:
For certain devices, such as handheld PDAs and cellular telephones, a
disk with a file system may be not be available for the device. In this
situation, the operating system must be stored in firmware.
2.11 How could a system be designed to allow a choice of operating systems
from which to boot? What would the bootstrap program need to do?
Answer:
Consider a system that would like to run both Windows XP and three
different distributions of Linux (e.g., RedHat, Debian, and Mandrake).
Each operating system will be stored on disk. During system boot-up, a
special program (which we will call the boot manager) will determine
which operating system to boot into. This means that rather initially
booting to an operating system, the boot manager will first run during
system startup. It is this boot manager that is responsible for determining
which system to boot into. Typically boot managers must be stored at
10 Chapter 3 Processes

b. Heap

3
c. Shared memory segments
Answer:
CHAPTER Only the shared memory segments are shared between the parent
process and the newly forked child process. Copies of the stack and
the heap are made for the newly created process.
3.6 With respect to the RPC mechanism, consider the “exactly once” semantic.
Does the algorithm for implementing this semantic execute correctly
Processes even if the ACK message back to the client is lost due to a network
problem? Describe the sequence of messages and discuss whether
“exactly once” is still preserved.
Answer:
The “exactly once” semantics ensure that a remore procedure will
be executed exactly once and only once. The general algorithm for
Practice Exercises ensuring this combines an acknowledgment (ACK) scheme combined
with timestamps (or some other incremental counter that allows the
3.1 Using the program shown in Figure 3.30, explain what the output will server to distinguish between duplicate messages).
be at Line A. The general strategy is for the client to send the RPC to the server
Answer: along with a timestamp. The client will also start a timeout clock. The
The result is still 5 as the child updates its copy of value. When control client will then wait for one of two occurrences: (1) it will receive an ACK
returns to the parent, its value remains at 5. from the server indicating that the remote procedure was performed,
or (2) it will time out. If the client times out, it assumes the server was
3.2 Including the initial parent process, how many processes are created by unable to perform the remote procedure so the client invokes the RPC a
the program shown in Figure 3.31? second time, sending a later timestamp. The client may not receive the
Answer: ACK for one of two reasons: (1) the original RPC was never received by
There are 8 processes created. the server, or (2) the RPC was correctly received —and performed —by
3.3 Original versions of Apple’s mobile iOS operating system provided no the server but the ACK was lost. In situation (1), the use of ACKs allows
means of concurrent processing. Discuss three major complications that the server ultimately to receive and perform the RPC. In situation (2),
concurrent processing adds to an operating system. the server will receive a duplicate RPC and it will use the timestamp to
Answer: FILL identify it as a duplicate so as not to perform the RPC a second time. It
is important to note that the server must send a second ACK back to the
3.4 The Sun UltraSPARC processor has multiple register sets. Describe what client to inform the client the RPC has been performed.
happens when a context switch occurs if the new context is already
loaded into one of the register sets. What happens if the new context is 3.7 Assume that a distributed system is susceptible to server failure.
in memory rather than in a register set and all the register sets are in What mechanisms would be required to guarantee the “exactly once”
use? semantics for execution of RPCs?
Answer: Answer:
The CPU current-register-set pointer is changed to point to the set The server should keep track in stable storage (such as a disk log)
containing the new context, which takes very little time. If the context is information regarding what RPC operations were received, whether
in memory, one of the contexts in a register set must be chosen and be they were successfully performed, and the results associated with the
moved to memory, and the new context must be loaded from memory operations. When a server crash takes place and a RPC message is
into the set. This process takes a little more time than on systems with received, the server can check whether the RPC had been previously
one set of registers, depending on how a replacement victim is selected. performed and therefore guarantee “exactly once” semanctics for the
execution of RPCs.
3.5 When a process creates a new process using the fork() operation, which
of the following state is shared between the parent process and the child
process?

a. Stack
9
12 Chapter 4 Threads

4.4 What resources are used when a thread is created? How do they differ
from those used when a process is created?

4
Answer:
Because a thread is smaller than a process, thread creation typically
CHAPTER uses fewer resources than process creation. Creating a process requires
allocating a process control block (PCB), a rather large data structure.
The PCB includes a memory map, list of open files, and environment
variables. Allocating and managing the memory map is typically the
most time-consuming activity. Creating either a user or kernel thread

Threads 4.5
involves allocating a small data structure to hold a register set, stack,
and priority.
Assume that an operating system maps user-level threads to the kernel
using the many-to-many model and that the mapping is done through
LWPs. Furthermore, the system allows developers to create real-time
threads for use in real-time systems. Is it necessary to bind a real-time
Practice Exercises thread to an LWP? Explain.
Answer:
4.1 Provide three programming examples in which multithreading provides Yes. Timing is crucial to real-time applications. If a thread is marked as
better performance than a single-threaded solution. real-time but is not bound to an LWP, the thread may have to wait to
Answer: be attached to an LWP before running. Consider if a real-time thread is
running (is attached to an LWP) and then proceeds to block (i.e. must
a. A Web server that services each request in a separate thread. perform I/O, has been preempted by a higher-priority real-time thread,
is waiting for a mutual exclusion lock, etc.) While the real-time thread is
b. A parallelized application such as matrix multiplication where
blocked, the LWP it was attached to has been assigned to another thread.
different parts of the matrix may be worked on in parallel.
When the real-time thread has been scheduled to run again, it must first
c. An interactive GUI program such as a debugger where a thread is wait to be attached to an LWP. By binding an LWP to a real-time thread
used to monitor user input, another thread represents the running you are ensuring the thread will be able to run with minimal delay once
application, and a third thread monitors performance. it is scheduled.

4.2 What are two differences between user-level threads and kernel-level
threads? Under what circumstances is one type better than the other?
Answer:

a. User-level threads are unknown by the kernel, whereas the kernel


is aware of kernel threads.
b. On systems using either M:1 or M:N mapping, user threads are
scheduled by the thread library and the kernel schedules kernel
threads.
c. Kernel threads need not be associated with a process whereas every
user thread belongs to a process. Kernel threads are generally
more expensive to maintain than user threads as they must be
represented with a kernel data structure.

4.3 Describe the actions taken by a kernel to context-switch between kernel-


level threads.
Answer:
Context switching between kernel threads typically requires saving the
value of the CPU registers from the thread being switched out and
restoring the CPU registers of the new thread being scheduled.
11
14 Chapter 5 Process Synchronization

Answer:
Busy waiting means that a process is waiting for a condition to be satisfied

5
in a tight loop without relinquishing the processor. Alternatively, a
process could wait by relinquishing the processor, and block on a
CHAPTER condition and wait to be awakened at some appropriate time in the
future. Busy waiting can be avoided but incurs the overhead associated

Process with putting a process to sleep and having to wake it up when the
appropriate program state is reached.

Synchronization
5.4 Explain why spinlocks are not appropriate for single-processor systems
yet are often used in multiprocessor systems.
Answer:
Spinlocks are not appropriate for single-processor systems because the
condition that would break a process out of the spinlock can be obtained
only by executing a different process. If the process is not relinquishing
the processor, other processes do not get the opportunity to set the
Practice Exercises program condition required for the first process to make progress. In a
multiprocessor system, other processes execute on other processors and
5.1 In Section 5.4, we mentioned that disabling interrupts frequently can thereby modify the program state in order to release the first process
affect the system’s clock. Explain why this can occur and how such from the spinlock.
effects can be minimized. 5.5 Show that, if the wait() and signal() semaphore operations are not
Answer: executed atomically, then mutual exclusion may be violated.
The system clock is updated at every clock interrupt. If interrupts were Answer:
disabled —particularly for a long period of time —it is possible the A wait operation atomically decrements the value associated with a
system clock could easily lose the correct time. The system clock is semaphore. If two wait operations are executed on a semaphore when
also used for scheduling purposes. For example, the time quantum for a its value is 1, if the two operations are not performed atomically, then it is
process is expressed as a number of clock ticks. At every clock interrupt, possible that both operations might proceed to decrement the semaphore
the scheduler determines if the time quantum for the currently running value, thereby violating mutual exclusion.
process has expired. If clock interrupts were disabled, the scheduler
could not accurately assign time quantums. This effect can be minimized 5.6 Illustrate how a binary semaphore can be used to implement mutual
by disabling clock interrupts for only very short periods. exclusion among n processes.
Answer:
5.2 Explain why Windows, Linux, and Solaris implement multiple locking The n processes share a semaphore, mutex, initialized to 1. Each process
mechanisms. Describe the circumstances under which they use spin- Pi is organized as follows:
locks, mutex locks, semaphores, adaptive mutex locks, and condition
variables. In each case, explain why the mechanism is needed. do {
Answer: wait(mutex);
These operating systems provide different locking mechanisms depend-
ing on the application developers’ needs. Spinlocks are useful for /* critical section */
multiprocessor systems where a thread can run in a busy-loop (for a
short period of time) rather than incurring the overhead of being put in signal(mutex);
a sleep queue. Mutexes are useful for locking resources. Solaris 2 uses
adaptive mutexes, meaning that the mutex is implemented with a spin /* remainder section */
lock on multiprocessor machines. Semaphores and condition variables } while (true);
are more appropriate tools for synchronization when a resource must
be held for a long period of time, since spinning is inefficient for a long
duration.
5.3 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.

13
16 Chapter 6 CPU Scheduling

that two shorter processes would arrive soon. Compute what the
average turnaround time will be if the CPU is left idle for the first

6
1 unit and then SJF scheduling is used. Remember that processes
P1 and P2 are waiting during this idle time, so their waiting time
CHAPTER may increase. This algorithm could be known as future-knowledge
scheduling.
Answer:
a. 10.53

CPU Scheduling b.
c.
9.53
6.86
Remember that turnaround time is finishing time minus arrival time, so
you have to subtract the arrival times to compute the turnaround times.
FCFS is 11 if you forget to subtract arrival time.
Practice Exercises
6.4 What advantage is there in having different time-quantum sizes at
6.1 A CPU-scheduling algorithm determines an order for the execution different levels of a multilevel queueing system?
of its scheduled processes. Given n processes to be scheduled on one Answer:
processor, how many different schedules are possible? Give a formula Processes that need more frequent servicing, for instance, interactive
in terms of n. processes such as editors, can be in a queue with a small time quantum.
Answer: Processes with no need for frequent servicing can be in a queue with
n! (n factorial = n × n – 1 × n – 2 × ... × 2 × 1). a larger quantum, requiring fewer context switches to complete the
processing, and thus making more efficient use of the computer.
6.2 Explain the difference between preemptive and nonpreemptive schedul-
ing. 6.5 Many CPU-scheduling algorithms are parameterized. For example, the
Answer: RR algorithm requires a parameter to indicate the time slice. Multilevel
Preemptive scheduling allows a process to be interrupted in the midst of feedback queues require parameters to define the number of queues,
its execution, taking the CPU away and allocating it to another process. the scheduling algorithms for each queue, the criteria used to move
Nonpreemptive scheduling ensures that a process relinquishes control processes between queues, and so on.
of the CPU only when it finishes with its current CPU burst. These algorithms are thus really sets of algorithms (for example, the
set of RR algorithms for all time slices, and so on). One set of algorithms
6.3 Suppose that the following processes arrive for execution at the times may include another (for example, the FCFS algorithm is the RR algorithm
indicated. Each process will run for the amount of time listed. In with an infinite time quantum). What (if any) relation holds between the
answering the questions, use nonpreemptive scheduling, and base all following pairs of algorithm sets?
decisions on the information you have at the time the decision must be
made. a. Priority and SJF

Process Arrival Time Burst Time b. Multilevel feedback queues and FCFS
P1 0.0 8 c. Priority and FCFS
P2 0.4 4 d. RR and SJF
P3 1.0 1
Answer:
a. What is the average turnaround time for these processes with the
FCFS scheduling algorithm?
a. The shortest job has the highest priority.

b. What is the average turnaround time for these processes with the b. The lowest level of MLFQ is FCFS.
SJF scheduling algorithm? c. FCFS gives the highest priority to the job having been in existence
c. The SJF algorithm is supposed to improve performance, but notice the longest.
that we chose to run process P1 at time 0 because we did not know d. None.
15
Practice Exercises 17

6.6 Suppose that a scheduling algorithm (at the level of short-term CPU
scheduling) favors those processes that have used the least processor
time in the recent past. Why will this algorithm favor I/O-bound
programs and yet not permanently starve CPU-bound programs?
Answer:
It will favor the I/O-bound programs because of the relatively short CPU
burst request by them; however, the CPU-bound programs will not starve
because the I/O-bound programs will relinquish the CPU relatively often
to do their I/O.
6.7 Distinguish between PCS and SCS scheduling.
Answer:
PCS scheduling is done local to the process. It is how the thread library
schedules threads onto available LWPs. SCS scheduling is the situation
where the operating system schedules kernel threads. On systems using
either many-to-one or many-to-many, the two scheduling models are
fundamentally different. On systems using one-to-one, PCS and SCS are
the same.
6.8 Assume that an operating system maps user-level threads to the kernel
using the many-to-many model and that the mapping is done through
the use of LWPs. Furthermore, the system allows program developers to
create real-time threads. Is it necessary to bind a real-time thread to an
LWP?
Answer:
Yes, otherwise a user thread may have to compete for an available LWP
prior to being actually scheduled. By binding the user thread to an LWP,
there is no latency while waiting for an available LWP; the real-time user
thread can be scheduled immediately.
6.9 The traditional UNIX scheduler enforces an inverse relationship between
priority numbers and priorities: the higher the number, the lower the
priority. The scheduler recalculates process priorities once per second
using the following function:
Priority = (recent CPU usage / 2) + base
where base = 60 and recent CPU usage refers to a value indicating how
often a process has used the CPU since priorities were last recalculated.
Assume that recent CPU usage for process P1 is 40, for process P2 is 18,
and for process P3 is 10. What will be the new priorities for these three
processes when priorities are recalculated? Based on this information,
does the traditional UNIX scheduler raise or lower the relative priority
of a CPU-bound process?
Answer:
The priorities assigned to the processes are 80, 69, and 65 respectively.
The scheduler lowers the relative priority of CPU-bound processes.
20 Chapter 7 Deadlocks

allows process P0 to complete, which would free a total of nine resources,


thereby allowing process P2 to complete as well.

7
CHAPTER
7.3 Consider the following snapshot of a system:

P0
P1
P2
Allocation
ABCD
0012
1000
1354
Max
ABCD
0012
1750
2356
Available
ABCD
1520

Deadlocks P3
P4
0632
0014
0652
0656
Answer the following questions using the banker’s algorithm:
a. What is the content of the matrix Need?
b. Is the system in a safe state?
Practice Exercises
c. If a request from process P1 arrives for (0,4,2,0), can the request be
7.1 List three examples of deadlocks that are not related to a computer- granted immediately?
system environment. Answer:
Answer: a. The values of Need for processes P0 through P4 respectively are (0,
0, 0, 0), (0, 7, 5, 0), (1, 0, 0, 2), (0, 0, 2, 0), and (0, 6, 4, 2).
• Two cars crossing a single-lane bridge from opposite directions.
b. The system is in a safe state? Yes. With Available being equal to
• A person going down a ladder while another person is climbing up (1, 5, 2, 0), either process P0 or P3 could run. Once process P3 runs,
the ladder.
it releases its resources, which allow all other existing processes to
• Two trains traveling toward each other on the same track. run.
7.2 Suppose that a system is in an unsafe state. Show that it is possible for c. The request can be granted immediately? This results in the value
the processes to complete their execution without entering a deadlock of Available being (1, 1, 0, 0). One ordering of processes that can
state. finish is P0 , P2 , P3 , P1 , and P4 .
Answer: 7.4 A possible method for preventing deadlocks is to have a single, higher-
An unsafe state may not necessarily lead to deadlock, it just means that order resource that must be requested before any other resource. For
we cannot guarantee that deadlock will not occur. Thus, it is possible example, if multiple threads attempt to access the synchronization
that a system in an unsafe state may still allow all processes to complete objects A · · · E, deadlock is possible. (Such synchronization objects may
without deadlock occurring. Consider the situation where a system has include mutexes, semaphores, condition variables, and the like.) We can
12 resources allocated among processes P0 , P1 , and P2 . The resources are prevent the deadlock by adding a sixth object F . Whenever a thread
allocated according to the following policy: wants to acquire the synchronization lock for any object A · · · E, it must
first acquire the lock for object F . This solution is known as containment:
Max Current Need the locks for objects A · · · E are contained within the lock for object F .
P0 10 5 5 Compare this scheme with the circular-wait scheme of Section 7.4.4.
P1 4 2 2 Answer:
P2 9 3 6 This is probably not a good solution because it yields too large a scope.
It is better to define a locking policy with as narrow a scope as possible.
Currently there are two resources available. This system is in an 7.5 Prove that the safety algorithm presented in Section 7.5.3 requires an
unsafe state as process P1 could complete, thereby freeing a total of order of m × n2 operations.
four resources. But we cannot guarantee that processes P0 and P2 can Answer:
complete. However, it is possible that a process may release resources Figure 7.1 provides Java code that implement the safety algorithm of
before requesting any further. For example, process P2 could release a the banker’s algorithm (the complete implementation of the banker’s
resource, thereby increasing the total number of resources to five. This algorithm is available with the source code download).
19
Practice Exercises 21 22 Chapter 7 Deadlocks

for (int i = 0; i < n; i++) { 7.7 Can a system detect that some of its processes are starving? If you answer
// first find a thread that can finish “yes,” explain how it can. If you answer “no,” explain how the system
for (int j = 0; j < n; j++) { can deal with the starvation problem.
if (!finish[j]) { Answer:
boolean temp = true; Starvation is a difficult topic to define as it may mean different things
for (int k = 0; k < m; k++) { for different systems. For the purposes of this question, we will define
if (need[j][k] > work[k]) starvation as the situation whereby a process must wait beyond a
temp = false; reasonable period of time —perhaps indefinitely—before receiving a
} requested resource. One way of detecting starvation would be to first
identify a period of time — T —that is considered unreasonable. When a
if (temp) { // if this thread can finish process requests a resource, a timer is started. If the elapsed time exceeds
finish[j] = true; T, then the process is considered to be starved.
for (int x = 0; x < m; x++) One strategy for dealing with starvation would be to adopt a policy
work[x] += work[j][x]; where resources are assigned only to the process that has been waiting
} the longest. For example, if process Pa has been waiting longer for
} resource X than process Pb , the request from process Pb would be
} deferred until process Pa ’s request has been satisfied.
} Another strategy would be less strict than what was just mentioned. In
Figure 7.1 Banker’s algorithm safety algorithm. this scenario, a resource might be granted to a process that has waited less
than another process, providing that the other process is not starving.
However, if another process is considered to be starving, its request
would be satisfied first.
As can be seen, the nested outer loops—both of which loop through n
times—provide the n2 performance. Within these outer loops are two 7.8 Consider the following resource-allocation policy. Requests for and
sequential inner loops which loop m times. The big-oh of this algorithm releases of resources are allowed at any time. If a request for resources
is therefore O(m × n2 ). cannot be satisfied because the resources are not available, then we check
any processes that are blocked waiting for resources. If a blocked process
7.6 Consider a computer system that runs 5,000 jobs per month with no has the desired resources, then these resources are taken away from it
deadlock-prevention or deadlock-avoidance scheme. Deadlocks occur and are given to the requesting process. The vector of resources for which
about twice per month, and the operator must terminate and rerun about the blocked process is waiting is increased to include the resources that
10 jobs per deadlock. Each job is worth about $2 (in CPU time), and the were taken away.
jobs terminated tend to be about half-done when they are aborted. For example, consider a system with three resource types and the
A systems programmer has estimated that a deadlock-avoidance vector Available initialized to (4,2,2). If process P0 asks for (2,2,1), it gets
algorithm (like the banker’s algorithm) could be installed in the system them. If P1 asks for (1,0,1), it gets them. Then, if P0 asks for (0,0,1), it
with an increase in the average execution time per job of about 10 percent. is blocked (resource not available). If P2 now asks for (2,0,0), it gets the
Since the machine currently has 30-percent idle time, all 5,000 jobs per available one (1,0,0) and one that was allocated to P0 (since P0 is blocked).
month could still be run, although turnaround time would increase by P0 ’s Allocation vector goes down to (1,2,1), and its Need vector goes up
about 20 percent on average. to (1,0,1).

a. What are the arguments for installing the deadlock-avoidance a. Can deadlock occur? If you answer “yes,” give an example. If you
algorithm? answer “no,” specify which necessary condition cannot occur.
b. What are the arguments against installing the deadlock-avoidance b. Can indefinite blocking occur? Explain your answer.
algorithm?
Answer:
Answer:
An argument for installing deadlock avoidance in the system is that a. Deadlock cannot occur because preemption exists.
we could ensure deadlock would never occur. In addition, despite the
increase in turnaround time, all 5,000 jobs could still run. b. Yes. A process may never acquire all the resources it needs if they
An argument against installing deadlock avoidance software is that are continuously preempted by a series of requests such as those
deadlocks occur infrequently and they cost little when they do occur. of process C.
Practice Exercises 23

7.9 Suppose that you have coded the deadlock-avoidance safety algorithm
and now have been asked to implement the deadlock-detection algo-
rithm. Can you do so by simply using the safety algorithm code and
redefining Max[i] = Waiting[i] + Allocation[i], where Waiting[i] is a
vector specifying the resources for which process i is waiting and
Allocation[i] is as defined in Section 7.5? Explain your answer.
Answer:
Yes. The Max vector represents the maximum request a process may
make. When calculating the safety algorithm we use the Need matrix,
which represents Max — Allocation. Another way to think of this is Max
= Need + Allocation. According to the question, the Waiting matrix fulfills
a role similar to the Need matrix, therefore Max = Waiting + Allocation.
7.10 Is it possible to have a deadlock involving only one single-threaded
process? Explain your answer.
Answer:
No. This follows directly from the hold-and-wait condition.
26 Chapter 8 Main Memory

8.4 Consider a logical address space of 64 pages of 1024 words each, mapped
onto a physical memory of 32 frames.

8
CHAPTER
a.
b.
How many bits are there in the logical address?
How many bits are there in the physical address?
Answer:
a. Logical address: 16 bits

Main Memory 8.5


b. Physical address: 15 bits
What is the effect of allowing two entries in a page table to point to the
same page frame in memory? Explain how this effect could be used to
decrease the amount of time needed to copy a large amount of memory
from one place to another. What effect would updating some byte on the
one page have on the other page?
Practice Exercises Answer:
By allowing two entries in a page table to point to the same page frame
8.1 Name two differences between logical and physical addresses. in memory, users can share code and data. If the code is reentrant, much
Answer: memory space can be saved through the shared use of large programs
A logical address does not refer to an actual existing address; rather, such as text editors, compilers, and database systems. “Copying” large
it refers to an abstract address in an abstract address space. Contrast amounts of memory could be effected by having different page tables
this with a physical address that refers to an actual physical address in point to the same memory location.
memory. A logical address is generated by the CPU and is translated into However, sharing of nonreentrant code or data means that any user
a physical address by the memory management unit(MMU). Therefore, having access to the code can modify it and these modifications would
physical addresses are generated by the MMU. be reflected in the other user’s “copy.”

8.2 Consider a system in which a program can be separated into two 8.6 Describe a mechanism by which one segment could belong to the address
parts: code and data. The CPU knows whether it wants an instruction space of two different processes.
(instruction fetch) or data (data fetch or store). Therefore, two base– Answer:
limit register pairs are provided: one for instructions and one for data. Since segment tables are a collection of base–limit registers, segments
The instruction base–limit register pair is automatically read-only, so can be shared when entries in the segment table of two different jobs
programs can be shared among different users. Discuss the advantages point to the same physical location. The two segment tables must have
and disadvantages of this scheme. identical base pointers, and the shared segment number must be the
Answer: same in the two processes.
The major advantage of this scheme is that it is an effective mechanism 8.7 Sharing segments among processes without requiring that they have the
for code and data sharing. For example, only one copy of an editor or same segment number is possible in a dynamically linked segmentation
a compiler needs to be kept in memory, and this code can be shared system.
by all processes needing access to the editor or compiler code. Another
advantage is protection of code against erroneous modification. The a. Define a system that allows static linking and sharing of segments
only disadvantage is that the code and data must be separated, which is without requiring that the segment numbers be the same.
usually adhered to in a compiler-generated code. b. Describe a paging scheme that allows pages to be shared without
8.3 Why are page sizes always powers of 2? requiring that the page numbers be the same.
Answer: Answer:
Recall that paging is implemented by breaking up an address into a page Both of these problems reduce to a program being able to reference
and offset number. It is most efficient to break the address into X page both its own code and its data without knowing the segment or page
bits and Y offset bits, rather than perform arithmetic on the address number associated with the address. MULTICS solved this problem by
to calculate the page number and offset. Because each bit position associating four registers with each process. One register had the address
represents a power of 2, splitting an address between bits results in of the current program segment, another had a base address for the stack,
a page size that is a power of 2. another had a base address for the global data, and so on. The idea is
25
Practice Exercises 27

that all references have to be indirect through a register that maps to


the current segment or page number. By changing these registers, the
same code can execute for different processes without the same page or
segment numbers.
8.8 In the IBM/370, memory protection is provided through the use of keys.
A key is a 4-bit quantity. Each 2K block of memory has a key (the storage
key) associated with it. The CPU also has a key (the protection key)
associated with it. A store operation is allowed only if both keys are
equal, or if either is zero. Which of the following memory-management
schemes could be used successfully with this hardware?
a. Bare machine
b. Single-user system
c. Multiprogramming with a fixed number of processes
d. Multiprogramming with a variable number of processes
e. Paging
f. Segmentation
Answer:
a. Protection not necessary, set system key to 0.
b. Set system key to 0 when in supervisor mode.
c. Region sizes must be fixed in increments of 2k bytes, allocate key
with memory blocks.
d. Same as above.
e. Frame sizes must be in increments of 2k bytes, allocate key with
pages.
f. Segment sizes must be in increments of 2k bytes, allocate key with
segments.

You might also like