Uploaded by Sami VP

Abraham Silberschatz Operating System Concepts Ch 2-8 Solution

advertisement
•
2.1 What is the purpose of system calls?
Ans: System calls allow user-level processes to request services of the operating system.
•
2.2 What is the purpose of the command interpreter? Why is it usually separate from the kernel?
Ans: It reads commands from the user or from a file of commands and executes 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. An user should
be able to develop a new command interpreter using the system-call interface provided by the operating system. The
command interpreter allows an user to create and manage processes and also determine ways by which they communicate
(such as through pipes and files). As all of this functionality could be accessed by an user-level program using the system
calls, it should be possible for the user to develop a new command-line interpreter.
•
2.3 What system calls have to be executed by a command interpreter or shell in order to start a new process on a
UNIX system?
Ans: : In Unix systems, a fork system call followed by an exec system call need 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 different
executable over the calling process.
•
2.4 What is the purpose of system programs?
Ans: System programs can be thought of as bundles of useful system calls. They provide basic functionality to users so
that users do not need to write their own programs to solve common problems.
•
2.5 What is the main advantage of the layered approach to system design? What are the disadvantages of the
layered approach?
Ans: As in all cases of modular design, designing an operating system in a modular way has several advantages. The
system is easier to debug and modify because changes affect only limited sections of the system rather than touching
all sections of the operating system. Information is kept only where it is needed and is accessible only within a
defined and restricted area, so any bugs affecting that data must be limited to a specific module or layer.
•
2.6 List five services provided by an operating system, and explain how each creates convenience for users. In which
cases would it be impossible for user-level programs to provide these services? Explain your answer.
Ans: The five services are:
a. Program execution. The operating system loads the contents (or sections) of a file into memory and begins its
execution. A user-level program could not be trusted to properly allocate CPU time.
b. I/O operations. Disks, tapes, serial lines, and other devices must be communicated with at a very low level. The
user need only specify the device and the operation to perform on it, while the system converts that request into
device- or controller-specific commands. User-level programs cannot be trusted to access only devices they should
have access to and to access them only when they are otherwise unused.
c. File-system manipulation. There are many details in file creation, 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.7 Why do some systems store the operating system in firmware, while others store it on disk?
Ans: 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.8 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?
Ans: 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
certain locations of the hard disk to be recognized during system startup. 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 operating system
if no choice is selected by the user.
•
2.9 The services and functions provided by an operating system can be divided into two main categories. Briefly
describe the two categories, and discuss how they differ.
Ans: One class of services provided by an operating system is to enforce protection between different processes
running concurrently in the system. Processes are allowed to access only those memory locations that are associated
with their address spaces. Also, processes are not allowed to corrupt files associated with other users. A process is
also not allowed to access devices directly without operating system intervention. The second class of services
provided by an operating system is to provide new functionality that is not supported directly by the underlying
hardware. Virtual memory and file systems are two such examples of new services provided by an operating system.
•
2.10 Describe three general methods for passing parameters to the operating system.
Ans: a. Pass parameters in registers b. Registers pass starting addresses of blocks of parameters c. Parameters can be
placed, or pushed, onto the stack by the program, and popped off the stack by the operating system.
•
2.11 Describe how you could obtain a statistical profile of the amount of time a program spends executing different
sections of its code. Discuss the importance of obtaining such a statistical profile.
Ans: One could issue periodic timer interrupts and monitor what instructions or what sections of code are currently
executing when the interrupts are delivered. A statistical profile of which pieces of code were active should be
consistent with the time spent by the program in different sections of its code. Once such a statistical profile has been
obtained, the programmer could optimize those sections of code that are consuming more of the CPU resources.
•
2.12 What are the advantages and disadvantages of using the same system call interface for manipulating both files
and devices?
Ans: Each device can be accessed as though it was a file in the file system. Since most of the kernel deals with devices
through this file interface, it is relatively easy to add a new device driver by implementing the hardware-specific code
to support this abstract file interface. Therefore, this benefits the development of both user program code, which can
be written to access devices and files in the same manner, and device driver code, which can be written to support a
well-defined API. The disadvantage with using the same interface is that it might be difficult to capture the
functionality of certain devices within the context of the file access API, thereby either resulting in a loss of
functionality or a loss of performance. Some of this could be overcome by the use of ioctl operation that provides a
general purpose interface for processes to invoke operations on devices.
•
2.13 Would it be possible for the user to develop a new command interpreter using the system-call interface
provided by the operating system?
Ans: It reads commands from the user or from a file of commands and executes 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. An user should be able to develop a new command interpreter using
the system-call interface provided by the operating system. The command interpreter allows an user to
create and manage processes and also determine ways by which they communicate (such as through
pipes and files). As all of this functionality could be accessed by an user-level program using the
system calls, so it should be possible for the user to develop a new command-line interpreter.
• 2.14 Describe why Android uses ahead-of-time (AOT) rather than just-in-time
(JIT) compilation.
Ans: Google has replaced the just-in-time (JIT) compiler in Android with an ahead-oftime (AOT) compiler that translates bytecode to native machine code during installation. ... Google's
implementation of ART adjusts JIT's space for speed trade-off while keeping the hardware flexibility
benefits of JIT compilation
• 2.15 What are the two models of inter process communication? What are the
strengths and weaknesses of the two approaches?
Ans: The two models of inter process communication are message passing model and the shared-memory model. The
message-passing model controls the
• 2.16 Contrast and compare an application programming interface (API) and
an application binary interface (ABI).
Ans:
•
2.17 Why is the separation of mechanism and policy desirable?
Ans: Mechanism and policy must be separate to ensure that systems are easy to modify. No two system installations are
the same, so each installation may want to tune the operating system to suit its needs. With mechanism and policy
separate, the policy may be changed at will while the mechanism stays unchanged. This arrangement provides a more
flexible system.
•
2.18 It is sometimes difficult to achieve a layered approach if two components of the operating system are
dependent on each other. Identify a scenario in which it is unclear how to layer two system components that require
tight coupling of their functionalities.
Ans: The virtual memory subsystem and the storage subsystem are typically tightly-coupled and requires careful design in a
layered system due to the following interactions. Many systems allow files to be mapped into the virtual memory space of
an executing process. On the other hand, the virtual memory subsystem typically uses the storage system to provide the
backing store for pages that do not currently reside in memory. Also, updates to the filesystem are sometimes buffered
in physical memory before it is flushed to disk, thereby requiring careful coordination of the usage of memory between the
virtual memory subsystem and the filesystem.
•
2.19 What is the main advantage of the microkernel approach to system design? How do user programs and system
services interact in a microkernel architecture? What are the disadvantages of using the microkernel approach?
Ans: Benefits typically include the following (a) adding a new service does not require modifying the kernel, (b) it is more
secure as more operations are done in user mode than in kernel mode, and (c) a simpler kernel design and functionality
typically results in a more reliable operating system. User programs and system services interact in a microkernel
architecture by using inter process communication mechanisms such as messaging. These messages are conveyed by the
operating system. The primary disadvantage of the microkernel architecture are the overheads associated with inter
process communication and the frequent use of the operating system’s messaging functions in order to
enable the user process and the system service to interact with each other.
•
2.20 What are the advantages of using loadable kernel modules?
Ans: It is difficult to predict what features an operating system will need when it is being designed. The advantage of
using loadable kernel modules is that functionality can be added to and removed from the kernel while it is running.
There is no need to either recompile or reboot the kernel.
•
2.21 How are iOS and Android similar? How are they different?
Ans: Both are based on existing kernels (Linux and Mac OS X). • Both have architecture that uses software stacks. •
Both provide frameworks for developers. Differences • iOS is closed-source, and Android is open-source. • iOS
applications are developed in Objective-C, Android in Java. • Android uses a virtual machine, and iOS executes code
natively
•
2.22 Explain why Java programs running on Android systems do not use the standard Java API and virtual machine.
Ans: It is because the standard API and virtual machine are designed for desktop and server systems, not mobile
devices. Google developed a separate API and virtual machine for mobile devices
•
2.23 The experimental Synthesis operating system has an assembler incorporated in the kernel. To optimize system
call performance, the kernel assembles routines within kernel space to minimize the path that the system call must
take through the kernel. This approach is the antithesis of the layered approach, in which the path through the kernel
is extended to make building the operating system easier. Discuss the pros and cons of the Synthesis approach to
kernel design and system-performance optimization.
Ans: Synthesis is impressive due to the performance it achieves through on-the-fly compilation. Unfortunately, it is
difficult to debug problems within the kernel due to the fluidity of the code. Also, such compilation is system specific,
making Synthesis difficult to port (a new compiler must be written for each architecture).
3.1 Using the program shown in Figure 3.30, explain what the output will
be at LINE A.
The result is still 5 as the child updates its copy of value. When control returns to the parent, its value
remains at 5.
3.2 Including the initial parent process, how many processes are created
by the program shown in Figure 3.31?
There are 16 processes created.
3.3 Original versions of Apple’s mobile iOS operating system provided no
means of concurrent processing. Discuss three major complications that
concurrent processing adds to an operating system.
FILL
3.4 Some computer systems provide multiple register sets. Describe what
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 in
memory rather than in a register set and all the register sets are in use?
The CPU current-register-set pointer is changed to point to the set containing the new context, which
takes very little time. If the context is in memory, one of the contexts in a register set must be chosen
and be moved to memory, and the new context must be loaded from memory into the set. This
process takes a little more time than on systems with one set of registers, depending on how a
replacement victim is selected.
3.5 When a process creates a new process using the fork() operation, which
of the following states is shared between the parent process and the child
process?
a. Stack
b. Heap
c. Shared memory segments
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 Consider the “exactly once "semantic with respect to the RPC
mechanism. Does the algorithm for implementing this semantic execute
correctly even if the ACK message sent back to the client is lost due to a
network problem? Describe the sequence of messages, and discuss whether
“exactly once” is still preserved.
The “exactly once” semantics ensure that a remore procedure will be executed exactly once and only
once. The general algorithm for ensuring this combines an acknowledgment (ACK) scheme combined
with timestamps (or some other incremental counter that allows the server to distinguish between
duplicate messages). The general strategy is for the client to send the RPC to the server along with a
timestamp. The client will also start a timeout clock. The client will then wait for one of two
occurrences: (1) it will receive an ACK 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 unable to perform
the remote procedure so the client invokes the RPC a second time, sending a later timestamp. The
client may not receive the ACK for one of two reasons: (1) the original RPC was never received by the
server, or (2) the RPC was correctly received—and performed—by the server but the ACK was lost. In
situation (1), the use of ACKs allows the server ultimately to receive and perform the RPC. In situation
(2), the server will receive a duplicate RPC and it will use the timestamp to 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 client to inform the client the RPC has been performed.
3.7 Assume that a distributed system is susceptible to server failure.
What mechanisms would be required to guarantee the “exactly once” semantic
for execution of RPCs?
The server should keep track in stable storage (such as a disk log) information regarding what RPC
operations were received, whether they were successfully performed, and the results associated with
the operations. When a server crash takes place and a RPC message is received, the server can check
whether the RPC had been previously performed and therefore guarantee “exactly once” semanctics
for the execution of RPCs.
3.8 Describe the actions taken by a kernel to context-switch between
processes.
In general, the operating system must save the state of the currently running process and restore the
state of the process scheduled to be run next. Saving the state of a process typically includes the
values of all the CPU registers in addition to memory allocation. Context switches must also perform
many architecture-specific operations, including flushing data and instruction caches.
3.8 Describe the differences among short-term, medium-term, and longterm
scheduling.
a. Short-term (CPU scheduler)—selects from jobs in memory those jobs that are ready to execute and
allocates the CPU to them.
b. Medium-term—used especially with time-sharing systems as an intermediate scheduling level. A
swapping scheme is implemented to remove partially run programs from memory and reinstate them
later to continue where they left off.
c. Long-term (job scheduler)—determines which jobs are brought into memory for processing.
The primary difference is in the frequency of their execution. The shortterm must select a new
process quite often. Long-term is used much less often since it handles placing jobs in the system and
may wait a while for a job to finish before it admits another one.
3.9 Construct a process tree similar to Figure 3.7. To obtain process
information for the UNIX or Linux system, use the command ps -ael. Use the
command man ps to get more information about the ps command. The task
manager on Windows systems does not provide the parent process ID, but the
process monitor tool, available from technet.microsoft.com, provides a
process-tree tool.
Answer: Results will vary widely.
3.10 Explain the role of the init (or systemd) process on UNIX and Linux
systems in regard to process termination.
When a process is terminated, it briefly moves to the zombie state and remains in that state until the
parent invokes a call to wait(). When this occurs, the process id as well as entry in the process table
are both released. However,if a parent does not invoke wait(), the child process remains a zombie as
long as the parent remains alive. Once the parent process terminates, the initprocess becomes the
new parent of the zombie. Periodically, the init process calls wait() which ultimately releases the pid
and entry in the process table of the zombie process
3.11 Including the initial parent process, how many processes are created
by the program shown in Figure 3.32?
16 processes are created. The program online includes printf() statements to better understand how
many processes have been created.
3.12 Explain the circumstances under which the line of code marked
printf("LINE J") in Figure 3.33 will be reached.
The call to exec() replaces the address space of the process with the program specified as the
parameter to exec(). If the call to exec() succeeds, the new program is now running and control from
the call to exec() never returns. In this scenario, the line printf("Line J"); would never be performed.
However, if an error occurs in the call to exec(), the function returns control and therefor the line
printf("Line J"); would be performed.
3.13 Using the program in Figure 3.34, identify the values of pid at
lines A, B, C, and D. (Assume that the actual pids of the parent and child
are 2600 and 2603, respectively.)
Answer: A = 0, B = 2603, C = 2603, D = 2600
3.14 Give an example of a situation in which ordinary pipes are more
suitable than named pipes and an example of a situation in which named
pipes are more suitable than ordinary pipes.
Simple communication works well with ordinary pipes. For example, assume we have a process that
counts characters in a file. An ordinary pipe can be used where the producer writes the file to the
pipe and the consumer reads the files and counts the number of characters in the file. Next, for an
example where named pipes are more suitable, consider the situation where several processes may
write messages to a log. When processes wish to write a message to the log, they write it to the
named pipe. A server reads the messages from the named pipe and writes them to the log file.
3.15 Consider the RPC mechanism. Describe the undesirable consequences
that could arise from not enforcing either the “at most once” or “exactly
once” semantic. Describe possible uses for a mechanism that has neither of
these guarantees.
If an RPC mechanism cannot support either the “at most once” or “at least once” semantics, then the
RPC server cannot guarantee that a remote procedure will not be invoked multiple occurrences.
Consider if a remote procedure were withdrawing money from a bank account on a system that did
not support these semantics. It is possible that a single invocation of the remote procedure might
lead to multiple withdrawals on the server. For a system to support either of these semantics
generally requires the server maintain some form of client state such as the timestamp described in
the text. If a system were unable to support either of these semantics, then such a system could only
safely provide remote procedures that do not alter data or provide time-sensitive results. Using our
bank account as an example, we certainly require “at most once” or “at least once” semantics for
performing a withdrawal (or deposit!). However, an inquiry into an account balance or other account
information such as name, address, etc. does not require these semantics.
3.16 Using the program shown in Figure 3.35, explain what the output will
be at lines X and Y.
Because the child is a copy of the parent, any changes the child makes will occur in its copy of the
data and won’t be reflected in the parent. As a result, the values output by the child at line X are 0, -1,
-4, -9, -16. The values output by the parent at line Y are 0, 1, 2, 3, 4
3.17 What are the benefits and the disadvantages of each of the following?
Consider both the system level and the programmer level.
a. Synchronous and asynchronous communication
b. Automatic and explicit buffering
c. Send by copy and send by reference
d. Fixed-sized and variable-sized messages
a. Synchronous and asynchronous communication—A benefit of synchronous communication is that
it allows a rendezvous between the sender and receiver. A disadvantage of a blocking send is that a
rendezvous may not be required and the message could be delivered asynchronously. As a result,
message-passing systems often provide both forms of synchronization.
b. Automatic and explicit buffering—Automatic buffering provides a queue with indefinite length,
thus ensuring the sender will never have to block while waiting to copy a message. There are no
specifications on how automatic buffering will be provided; one scheme may reserve sufficiently large
memory where much of the memory is wasted. Explicit buffering specifies how large the buffer is. In
this situation, the sender may be blocked while waiting for available space in the queue. However, it is
less likely that memory will be wasted with explicit buffering.
c. Send by copy and send by reference—Send by copy does not allow the receiver to alter the state of
the parameter; send by reference does allow it. A benefit of send by reference is that it allows the
programmer to write a distributed version of a centralized application. Java’s RMI provides both;
however, passing a parameter by reference requires declaring the parameter as a remote object as
well.
d. Fixed-sized and variable-sized messages—The implications of this are mostly related to buffering
issues; with fixed-size messages, a buffer with a specific size can hold a known number of messages.
The number of variable-sized messages that can be held by such a buffer is unknown. Consider how
Windows 2000 handles this situation: with fixed-sized messages (anything < 256 bytes), the messages
are copied from the address space of the sender to the address space of the receiving process. Larger
messages (i.e. variable-sized messages) use shared memory to pass the message.
CHAPTER 4 : THREADS
1) Provide two programming examples in which multithreading does not provide
better performance than a single-threaded solution.
Any kind of sequential program is not a good candidate to be threaded. An example of this is a
program that calculates an individual tax return. (2) Another example is a
"shell" program such as the C-shell or Korn shell. Such a program must closely monitor its own
working space such as open files, environment variables, and current working directory.
2) Describe the actions taken by a thread library to context switch between userlevel threads.
Context switching between user threads is quite similar to switching between kernel threads,
although it is dependent on the threads library and how it maps user threads to kernel threads. In
general, context switching between user threads involves taking a user thread out of its LWP and
replacing it with another thread. This act typically involves saving and restoring the state of the
registers.
3) Under what circumstances does a multithreaded solution using multiple kernel
threads provide better performance than a single-threaded solution on a singleprocessor system?
When a kernel thread suffers a page fault, another kernel thread can be switched in to use the
interleaving time in a useful manner. A single-threaded process, on the other hand, will not be
capable of performing useful work when a page fault takes place. Therefore, in scenarios where a
program might suffer from frequent page faults or has to wait for other system events, a
multithreaded solution would perform better even on a single-processor system.
4) which of the following components of program state are shared across threads in
a multithreaded process?
a. register values
b. heap memory
c. global variables
d. stack memory
The threads of a multithreaded process share heap memory and global variables. Each thread has its
separate set of register values and a separate stack.
Or
A heap and global variables are shared among the threads of a multi-threaded process. Every thread
has its separate set of register values and a separate sack.
Heap memory and global variables are shared across thread.
5) can a multithreaded solution using multiple user-level threads achieve better
performance on a multiprocessor system than on a single-processor system ?
explain
A multithreaded system comprising of multiple user level threads cannot make use of the different
processors in a multiprocessor system simultaneously. The operating system sees only a single
process and will not schedule the different threads of the process on separate processors.
Consequently, there is no performance benefit associated with executing multiple user-level threads
on a multiprocessor system.
6) is it possible to have concurrency but not parallelism? Explain.
Though it is not possible to have parallelism without concurrency, it is possible to have concurrency
but not parallelism.
7) Describe how Linux, Windows XP and Solaris treat thread processes.
Linux does not distinguish between processes and threads. Instead, Linux treats both in the same
way, allowing a task to be more akin to a process or a thread depending on the set of flags passed to
the clone() system call. However, many operating systems, such as Windows XP and Solaris, treat
processes and threads differently. Typically, such systems use a notation wherein the data structure
for a process contains pointers to the separate threads belonging to the process.
8) A system with two dual-core processors has four processors available for
scheduling. A CPU-intensive application is running on this system. All input is
performed at program start-up, when a single file must
be opened. Similarly, all output is performed just before the program termina tes,
when the program results must be written to a single file. Between startup and
termination, the program is entirely CPUbound.
Your task is to improve the performance of this application by multithreading it. The
application runs on a system that uses the one-to-one threading model (each user
thread maps to a kernel thread).
• How many threads will you create to perform the input and output? Explain .
• How many threads will you create for the CPU-intensive portion of the application?
Explain.
1) It only makes sense to create as many threads as there are blocking system calls, as the threads will
be spent blocking. Creating additional threads provides no benefit. Thus, it makes sense to create a
single thread for input and a single thread for output.
2) Four. There should be as many threads as there are processing cores. Fewer would be a waste of
processing resources, and any number > 4 would be unable to run.
9) Consider the following code segment:
pid t pid;
pid = fork();
if (pid == 0) { / child process /
fork();
thread create( . . .);
}
fork();
a. How many unique processes are created?
b. How many unique threads are created?
a) 6 processes
b) 2 threads
main
/\
13
/|
2 thr
||
thr 3
|
3
10) Consider a multiprocessor system and a multithreaded program written using the
many-to-many threading model. Let the number of user-level threads in the program
be greater than the number of processors in the system. Discuss the performance
implications of the number of kernel threads allocated to the program is greater than
the number of processors but less than the number of user level threads .
When there are more kernel threads than processors, a blocked kernel thread could be swapped out
in favor of another kernel thread that is ready to execute, thereby increasing the utilization of the
multiprocessor system.
11) Consider a multicore system and a multithreaded program written using the
many-to-many threading model. Let the number of user-level threads in the program
be greater than the number of processing cores in the system. Discuss the
performance implications of the following scenarios.
a. The number of kernel threads allocated to the program is less than the number of
processing cores.
b. The number of kernel threads allocated to the program is equal to the number of
processing cores.
c. The number of kernel threads allocated to the program is greater than the number
of processing cores but less than the number of user-level threads.
A) When the number of kernel threads is less than the number of processors, then some of the
processors would remain idle since the scheduler maps only kernel threads to processors and not
user-level threads to processors.
B) When the number of kernel threads is exactly equal to the number of processors, then it is possible
that all of the processors might be utilized simultaneously. However, when a kernel thread blocks
inside the kernel (due to a page fault or while invoking system calls), the corresponding processor
would remain idle.
C) When there are more kernel threads than processors, a blocked kernel thread could be swapped
out in favor of another kernel thread that is ready to execute, thereby increasing the utilization of the
multiprocessor system.
12 ) Using Amdahl’s Law, calculate the speedup gain of an application :



40 percent parallel with (a) eight processing cross and (b) sixteen processing
cores
67 percent parallel with (a) two processing cores and (b) four processing
cores
90 percent parallel with (a) four processing cores and (b) eight processing
cores
a. F= 60% 1-F=40%
s=2
According to amdhal law
Speedup=1/((1-F)+(F/s))=1/((0.40)+(0.60/2))=1/0.70=1.41
b.
Speedup=1/((0.40)+(0.60/4))=1/0.55=1.81
13 ) determine if the following problems exhibit task or data parallelism :
• Using a separate thread to generate a thumbnail for each photo in a collection • Transposing a
matrix in parallel • A networked application where one thread reads from the network and another
writes to the network • The fork-join array summation application described in Section 4.5.2 • The
Grand Central Dispatch system
Answer :
‫؟؟؟؟؟؟؟؟‬
14) We discussed Google's Chrome browser and its practice of opening each new
website in a separate process. Would the same benefits have been achieved if
instead Chrome had been designed to open each new website in a separate thread?
Explain.
15) Using Amdahl’s Law, calculate the speedup gain of an application that has a 60
percent parallel component for (a) two processing cores and (b) four processing
cores.
a. F= 60% 1-F=40%
s=2
According to amdhal law
Speedup=1/((1-F)+(F/s))=1/((0.40)+(0.60/2))=1/0.70=1.41
b.
Speedup=1/((0.40)+(0.60/4))=1/0.55=1.81
16 ) What are two differences between user-level threads and kernel-level threads? Under
what circumstances is one type better than the other?
‫دو تفاوت میان دنباله پیام های کاربری و هسته ای چیست ؟ در چه شرایطی ‪ ،‬یک نوع بهتر از نوع دیگر است ؟‬
‫پاسخ‬
‫الف ( دنباله پیام های کاربر توسط هسته مجهولند درحالیکه هسته نسبت به دنباله پیام های خود آگاه است ‪.‬‬
‫ب ( در سیستم هایی که از نگاشت ‪;M 1‬یا ‪ N;M‬استفاده می کنند ‪ ،‬دنباله پیام های کاربر از طریق کتابخانه دنباله پیام برنامه‬
‫ریزی می شوند و هسته ‪ ،‬دنباله پیام های خودش را برنامه ریزی می کند‪.‬‬
‫ج ( دنباله پیام های هسته الزم نیست که با پردازش مطابقت داشته باشند ‪ ،‬درحالیکه همه دنباله پیام های کاربر متعلق به یک‬
‫پردازش هستند ‪ .‬بطور کلی نگهداری دنباله پیام های هسته به نسبت دنباله پیام های کاربر گران قیمت تر هستند چون این دنباله‬
‫پیام ها باید با ساختار داده های هسته نشان داده شوند‪.‬‬
‫‪17 ) Provide three programming examples in which multithreading provides better‬‬
‫‪performance than a single-threaded solution.‬‬
‫سه مثال برنامه نویسی که در آنها یک راه حل با چندین دنباله پیام باعث عملکرد بهتر از یک راه حل با یک دنباله پیام می شوند‬
‫ارائه دهید‪.‬‬
‫پاسخ‬
‫الف ) سرور یک وب سایت که به درخواست ها در یک بند جداگانه خدمات رسانی می کند‬
‫ب ) برنامه های موازی مانند ضرب ماتریس ها که روی بخش های مختلف یک ماتریس بصورت موازی کار می کنند ‪.‬‬
‫ج ) یک برنامه ء ِ ‪ GUI‬تعاملی ‪ ،‬درجاییکه یک بند برای نظارت بر ورودی های کاربر و بند دیگر برای نشان دادن برنامه‬
‫های درحال اجرا و بند سوم برای نظارت بر عملکرد استفاده می شوند ‪ ،‬مثل یک دیباگر یا اشکال یاب عمل می کند‪.‬‬
‫‪26) What are two differences between user-level threads and kernel-level threads? Under‬‬
‫?‪what circumstances is one type better than the other‬‬
‫منابع استفاده شده در زمانیکه قرار است یک دنباله پیام تولید شود چیست ؟ این منابع چقدر با منابعی که برای تولید‬
‫یک فرایند نیاز هست متفاوتند ؟‬
‫پاسخ ‪:‬‬
‫چون یک دنباله پیام از یک فرایند کوچکتر است ‪ ،‬پس ایجاد یک دنباله پیام معموال به نسبت تولید فرایند به منابع‬
‫کمتری نیاز دارد ‪ .‬ایجاد یک فرایند به تخصیص بلوک کنترل فرایند) ‪ )PCB‬نیاز دارد تا ساختاری از داده های‬
‫بزرگ و ‪ PCB‬خود شامل یک نقشه حافظه ‪ ،‬لیستی از فایل های باز و متغیرهای محیطی است ‪ .‬تخصیص و‬
‫مدیریت نقشه حافظه معموال فعالیتی زمان بر هست ‪ .‬ایجاد دنباله پیام های کاربر یا هسته ‪ ،‬شامل تخصیص داده های‬
‫با ساختار کوچک بوده تا وضعیت رجیستر ‪ ،‬پشته و اولویت آن را حفظ کند‪.‬‬
‫‪18 ) 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 thread to an LWP‬‬
‫فرض کنید یک سیستم عامل ‪ ،‬دنباله پیام های کاربر را به دنباله پیام های هسته ‪ ،‬آن هم با استفاده از مدل خیلی – به – خیلی ‪،‬‬
‫نگاشت می دهد ‪ ،‬و این نگاشت از طریق ‪ LWP‬ها صورت می گیرد‪ .‬عالوه بر این ‪ ،‬این سیستم به توسعه دهنده اجازه می دهد تا‬
‫دنباله پیام های وابسته به زمان را برای استفاده در سیستم زمان واقعی تولید کند ‪ .‬آیا الزم است که دنباله پیام های وابسته به زمان‬
‫را به یک ‪ LWP‬متصل کنیم ؟ توضیح دهید‬
‫‪.‬پاسخ‬
‫‪ :‬بله‪ .‬زمانبندی برای نرم افزارهای وابسته به زمان بسیار مهم است ‪ .‬اگر دنباله پیامی را به عنوان دنباله ء زمان واقعی عالمت‬
‫گذاری کرده باشیم ‪ ،‬اما آن را به یک ‪ LWP‬متصل نساخته باشیم ‪ ،‬آنگاه دنباله پیام باید صبر کند تا پیش از اجرا به یک ‪LWP‬‬
‫متصل گردد ‪ .‬در نظر بگیرید که اگر دنباله پیام وابسته به زمان در حال اجرا باشد ) به یک ‪LWP‬متصل باشد ( ‪ ،‬و سپس به‬
‫یک دسته از پیام ها تبدیل شود ) برای مثال ورودی خروجی باید توسط یک دنباله پیام وابسته به زمان با اولویت باالتر اجرا شود‬
‫و منتظر قفل خروج متقابل باشد ( ‪ ،‬درحالیکه دنباله پیام وابسته به زمان به یک دسته از پیام ها تبدیل شده‪LWP ،‬هم به یک‬
‫دنباله پیام دیگر متصل می شود‪ .‬وقتی دنباله پیام وابسته به زمان برنامه ریزی می شود که دوباره به اجرا در بیاید ابتدا باید صبر‬
‫کرد که این دنباله به یک ‪ LWP‬متصل گردد‪ .‬با اتصال ‪ LWP‬به دنباله پیام وابسته به زمان مطمئن می شوید که این دنباله پیام می‬
‫تواند با تاخیر حداقل طبق برنامه ریزی دقیقا یکبار اجرا شود‪.‬‬
‫‪19 ) Linux does not distinguish between processes and threads. Instead, Linux treats both in‬‬
‫‪the same way allowing a task to be more akin to a process or a thread depending on the set‬‬
‫‪of flags passed to the clone() system call. However, other operating systems, such as‬‬
‫‪Windows, treat processes and threads differently. Typically, such systems use a notation in‬‬
‫‪which the data structure for a process contains pointers to the separate threads belonging to‬‬
‫‪the process. Contrast these two approaches for modeling processes and threads within the‬‬
‫‪kernel.‬‬
‫‪20) The program shown in Figure 4.23 uses the Pthreads API. What would be the‬‬
‫?‪output from the program at LINE C and LINE P‬‬
‫تکت‬
‫‪Output at LINE C is 5. Output at LINE P is 0.‬‬
21 ) Consider a multiprocessor system and a multithreaded program written using the manyto-many threading model. Let the number of user-level threads in the program be greater
than the number of processors in the system. Discuss the performance implications of the
number of kernel threads allocated to the program is equal to the number of processors .
When the number of kernel threads is exactly equal to the number of processors, then it is possible
that all of the processors might be utilized simultaneously. However, when a kernel thread blocks
inside the kernel, due to a page fault or while invoking
system calls, the corresponding processor would remain idle.
22 ) Contrast the two approaches of not distinguishing between user and process threads
and treating processes and threads differently for modeling processes and threads within the
kernel.
On one hand, in systems where processes and threads are considered as similar entities, some of the
operating system code could be simplified. A scheduler, for instance, can consider the different
processes and threads on an equal footing without requiring special code to examine the threads
associated with a process during every scheduling step. On the other hand, this uniformity could
make it harder to impose process-wide resource constraints in a direct manner. Instead, some extra
complexity is required to identifywhich threads correspond towhich process and perform the relevant
accounting tasks.
Chapter 5
5.1) In Section 5.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.
The system clock is updated at every clock interrupt. If interrupts were disabled—particularly for a long period of
time—it is possible the system clock could easily lose the correct time. The system clock is also used for scheduling
purposes. For example, the time quantum for a process is expressed as a number of clock ticks. At every clock
interrupt, the scheduler determines if the time quantum for the currently running process has expired. If clock
interrupts were disabled, the scheduler could not accurately assign time quantums. This effect can be minimized by
disabling clock interrupts for only very short periods.
5.2)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.
These operating systems provide different locking mechanisms depending on the application developers’ needs.
Spinlocks are useful for 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 a sleep queue. Mutexes are useful for locking resources. Solaris 2
uses adaptive mutexes, meaning that the mutex is implemented with a spin lock on multiprocessor machines.
Semaphores and condition variables 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.
Busy waiting means that a process is waiting for a condition to be satisfied in a tight loop without relinquishing the
processor. Alternatively, a process could wait by relinquishing the processor, and block on a condition and wait to be
awakened at some appropriate time in the future. Busy waiting can be avoided but incurs the overhead associated
with putting a process to sleep and having to wake it up when the appropriate program state is reached.
5.4) Explain why spinlocks are not appropriate for single-processor systems yet are often used in multiprocessor
systems.
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 program condition required for the first process to make
progress. In a multiprocessor system, other processes execute on other processors and thereby modify the program
state in order to release the first process from the spinlock.
5.5) Show that, if the wait() and signal() semaphore operations are not executed atomically, then mutual
exclusion may be violated.
A wait operation atomically decrements the value associated with a semaphore. If two wait operations are executed
on a semaphore when its value is 1, if the two operations are not performed atomically, then it is possible that both
operations might proceed to decrement the semaphore value, thereby violating mutual exclusion.
5.6) Illustrate how a binary semaphore can be used to implement mutual exclusion among n processes.
The n processes share a semaphore, mutex, initialized to 1. Each process Pi is organized as follows:
do {
wait(mutex);
/* critical section */
signal(mutex);
/* remainder section */
}
while (true);
5.7) Race conditions are possible in many computer systems. Consider a banking system that maintains an account
balance with two functions: deposit(amount) and withdraw(amount). These two functions are passed the amount
that is to be deposited or withdrawn from the bank account balance. Assume that a husband and wife share a
bank account. 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.
Assume the balance in the account is 250.00 and the husband calls withdraw(50) and the wife calls deposit(100).
Obviously the correct value should be 300.00. Since these two transactions will be serialized, the local value of
balance for the husband becomes 200.00, but before he can commit the transaction, the deposit(100) operation
takes place and
updates the shared value of balance to 300.00. We then switch back to the 1husband and the value of the shared
balance is set to 200.00 - obviously an incorrect value.
5.8 The first known correct software solution to the critical-section problem for two processes was developed by
Dekker. The two processes, P0 and P1, share the following variables:
The structure of process Pi (i == 0 or 1) is shown in Figure 5.21. The other process is Pj (j == 1 or 0). Prove that the
algorithm satisfies all three requirements for the critical-section problem
This algorithm satisfies the three conditions of mutual exclusion. (1) Mutual exclusion is ensured through the use of
the flag and turn variables. If both processes set their flag to true, only one will succeed. Namely, the process whose
turn it is. The waiting process can only enter its critical section when the other process updates the value of turn. (2)
Progress is provided, again through the flag and turn variables. This algorithm does not provide strict alternation.
Rather, if a process wishes to access their critical section, it can set their flag variable to true and enter their critical
section. It only sets turn to the value of the other process upon exiting its critical section. If this process wishes to
enter its critical section again - before the other process - it repeats the process of entering its critical section and
setting turn to the other process upon exiting. (3) Bounded waiting is preserved through the use of the TTturn
variable. Assume two processes wish to enter their respective critical sections. They both set their value of flag to
true, however only the thread whose turn it is can proceed, the other thread waits. If bounded waiting were not
preserved, it would therefore be possible that the waiting process would have to wait indefinitely while the first
process repeatedly entered - and exited - its critical section. However, Dekker’s algorithm has a process set the value
of turn to the other process, thereby ensuring that the other process will enter its critical section next..
5.9) 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;
This algorithm satisfies the three conditions. Before we show that the three conditions are satisfied, we give a brief
explanation of what the algorithm does to ensure mutual exclusion. When a process i requires access to critical
section, it first sets its flag variable to want in
to indicate its desire. It then performs the following steps: (1) It ensures that all processes whose index lies between
turn and i are idle. (2) If so, it updates its flag to in cs and checks whether there is already some other process that
has updated its flag to in cs. (3) If not and
if it is this process's turn to enter the critical section or if the process indicated by the turn variable is idle, it enters
the critical section. Given the above description, we can reason about how the algorithm satisfies the requirements
in the following manner:
(1) Mutual exclusion is ensured: Notice that a process enters the critical section only if the following requirements is
satisfied: no other process has its flag variable set to in cs. Since the process sets its own flag variable set to in cs
before checking the status of other processes, we are guaranteed that no two processes will enter the critical section
simultaneously.
(2) Progress requirement is satisfied: Consider the situation where multiple processes simultaneously set their flag
variables to in cs and then check whether there is any other process has the flag variable set to in cs. When this
happens, all processes realize that there are competing processes, enter the next iteration of the outer while(1) loop
and reset their flag variables to want in. Now the only process that will set its turn variable to in cs is the process
whose index is closest to turn. It is however possible that new processes whose index values are even closer to turn
might decide to enter the critical section at this point and therefore might be able
to simultaneously set its flag to in cs. These processes would then realize there are competing processes and might
restart the process of entering the critical section. However, at each iteration, the index values of processes that set
their flag variables to in cs become
closer to turn and eventually we reach the following condition: only one process (say k) sets its flag to in cs and no
other process whose index lies between turn and k has set its flag to in cs. This process then gets to enter the critical
section.
(3) Bounded-waiting requirement is met: The bounded waiting requirement is satisfied by the fact that when a
process k desires to enter the critical section, its flag is no longer set to idle. Therefore, any process whose index
does not lie between turn and k cannot enter the critical section. In the meantime, all processes whose index falls
between turn and k and desire to enter the critical section would indeed enter the critical section (due to the fact
that the system always makes progress) and the turn value monotonically becomes closer to k. Eventually, either
turn becomes k or there are no processes whose index values lie between turn and k, and therefore process k gets to
enter the critical section.
All the elements of flag are initially idle. The initial value of turn is immaterial (between 0 and n-1). The structure of
process Pi is shown in Figure 6.19. Prove that the algorithm satisfies all three requirements for the critical-section
problem.
5.10) Explain why implementing synchronization primitives by disabling interrupts is not appropriate in a singleprocessor system if the synchronization primitives are to be used in user-level programs.
- If a user-level program can disable interrupts, then it can disable the timer interrupt and prevent context switching
from taking place, thereby allowing it to use the processor without letting other processes execute.
5.11) Explain why interrupts are not appropriate for implementing synchronization primitives in multiprocessor
systems.
Interrupts are not sufficient because disabling them only prevents other processes from executing on the processor
in which interrupts were disabled; there are no limitations on what could be happening on other processors. Thus,
disabling interrupts cannot guarantee mutually exclusive access to program state.
5.12 The Linux kernel has a policy that a process cannot hold a spinlock while attempting to acquire a semaphore.
Explain why this policy is in place.
Because acquiring a semaphore may put the process to sleep while it is waiting for the semaphore to become
available. Spinlocks are to only be held for short durations and a process that is sleeping may hold the spinlock for
too long a period.
5.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.
5.14 Describe how the compare and swap() instruction can be used to provide mutual exclusion that satisfies the
bounded-waiting requirement
5.15 Consider how to implement a mutex lock using an atomic hardware instruction. Assume that the following
structure defining the mutex lock is available:
(available == 0) indicates that the lock is available, and a value of 1 indicates that the lock is unavailable. Using this
struct, illustrate how the following functions can be implemented using the test and set() and compare and swap()
instructions:
• void acquire(lock *mutex)
• void release(lock *mutex)
Be sure to include any initialization that may be necessary
5.16) The implementation of mutex locks provided in Section 6.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.
5.17) 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.
(1) If the lock is to be held for a short duration, it makes more sense to use a spinlock as it may in fact be faster than
using a mutex lock which requires suspending - and awakening - the waiting process.
(2) If it is to be held for a long duration, a mutex lock is preferable as this allows the other processing core to
schedule another process while the locked process waits.
(3) If the thread may be put to sleep while holding the lock, a mutex lock is definitely preferable as you wouldn't
want the waiting process to be spinning while waiting for the other process to wake up.
5.18) Assume that a context switch takes T time. Suggest an upper bound (in terms of T) for holding a spinlock. If
the spinlock is held for any longer, a mutex lock (where waiting threads are put to sleep) is a better alternative.
5.19) A multithreaded web server wishes to keep track of the number of requests it services (known as hits).
Consider the two following strategies to prevent a race condition on the variable hits. The first strategy is to use a
basic mutex lock when updating hits:
5.20) Consider the code example for allocating and releasing processes shown in Figure 6.20.
a. Identify the race condition(s).
b. Assume you have a mutex lock named mutex with the operations acquire() and release(). Indicate where the
locking needs to be placed to prevent the race condition(s).
c. Could we replace the integer variable
int number of processes = 0
with the atomic integer
atomic t number of processes = 0
to prevent the race condition(s)?
a. There is a race condition on the variable number of processes.
b. A call to acquire() must be placed upon entering each function and a call to release() immediately before exiting
each function.
c. No, it would not help. The reason is because the race occurs in the allocate process() function where number of
processes is first tested in the if statement, yet is updated afterwards, based upon the value of the test. it is possible
that number of processes = 254 at the time of the test, yet because of the race condition, is set to 255 by another
thread before it is incremented yet again.
5.21) 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. Illustrate how semaphores can be used by a
server to limit the number of concurrent connections.
A semaphore is initialized to the number of allowable open socket connections. When a connection is accepted, the
acquire() method is called; when a connection is released, the release() method is called. If the system reaches the
number of allowable socket connections, subsequent calls to acquire() will block until an existing connection is
terminated and the release method is invoked.
5.22 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
5.23 Show how to implement the wait() and signal() semaphore operations in multiprocessor environments using
the test and set() instruction. The solution should exhibit minimal busy waiting
5.24 Exercise 4.26 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—what changes would be
necessary to the solution for this exercise? Implement your modified solution
5.25) Demonstrate that monitors and semaphores are equivalent to the degree that they can be used to
implement solutions to the same types of synchronization problems.
5.26 Design an algorithm for a bounded-buffer monitor in which the buffers (portions) are embedded within the
monitor itself.
5.27 The strict mutual exclusion within a monitor makes the bounded-buffer monitor of Exercise 5.26 mainly
suitable for small portions. a. Explain why this is true. b. Design a new scheme that is suitable for larger portions.
5.28 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.
5.29) Describe how the signal() operation associated with monitors differs from the corresponding operation
defined for semaphores.
Consider a scenario where there are no threads waiting for a signal. In a monitor, if the signal operation is called, it is
ignored and forgotten since there are no waiting threads. In a semaphore, the value of the semaphore is increased
and thus it will affect future threads that intend to wait for a signal.
5.30) Suppose the signal() statement can appear only as the last statement in a monitor function. Suggest how the
implementation described in Section 6.7 can be simplified in this situation.
5.31) Consider a system consisting of processes P1, P2, ..., Pn, each of which has a unique priority number. Write a
monitor that allocates three identical printers to these processes, using the priority numbers for deciding the
order of allocation.
The monitor code is
type resource = monitor
var P: array[0..2] of boolean;
X: condition;
procedure acquire (id: integer, printer-id: integer);
begin
if P[0] and P[1] and P[2] then X.wait(id)
if not P[0] then printer-id := 0;
else if not P[1] then printer-id := 1;
else printer-id := 2;
P[printer-id]:=true;
end
procedure release (printer-id: integer)
begin
P[printer-id]:=false;
X.signal;
end
begin
P[0] := P[1] := P[2] := false;
end
5.32) 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.
int sumid=0; / Shared var that contains the sum of the process ids currently accessing the file /
int waiting=0; / Number of process waiting on the semaphore OkToAccess /
semaphore mutex=1; / Our good old Semaphore variable ;) /
semaphore OKToAccess=0; / The synchronization semaphore /
get_access(int id)
{
sem_wait(mutex);
while(sumid+id > n) {
waiting++;
sem_signal(mutex);
sem_wait(OKToAccess);
sem_wait(mutex);
}
sumid += id;
sem_signal(mutex);
}
release_access(int id)
{
int i;
sem_wait(mutex);
sumid -= id;
for (i=0; i < waiting;++i) {
sem_signal(OKToAccess);
}
waiting = 0;
sem_signal(mutex);
}
main()
{
get_access(id);
do_stuff();
release_access(id);
}
Some points to note about the solution:
Release_access wakes up all waiting processes. It is NOT ok to wake up the first waiting process in this problem --that process may have too large a value of id. Also, more than one process may be eligible to go in (if those
processes have small ids) when one process releases access.
Woken up processes try to see if they can get in. If not, they go back to sleep.
Waiting is set to 0 in release_access after the for loop. That avoids unnecessary signals from subsequent
release_accesses. Those signals would not make the solution wrong, just less efficient as processes will be woken up
unnecessarily.
5.33) 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?
5.34 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 readers– writers 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? (Hint: Restrict the generality of B; see [Kessels (1977)].)
5.35 Design an algorithm for 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 function tick() in your monitor at regular intervals.
5.36 Programming Exercise 3.20 required you to design a PID manager that allocated a unique process identifier to
each process. Exercise 4.20 required you to modify your solution to Exercise 3.20 by writing a program that
created a number of threads that requested and released process identifiers. Now modify your solution to
Exercise 4.20 by ensuring that the data structure used to represent the availability of process identifiers is safe
from race conditions. Use Pthreads mutex locks, described in Section 5.9.4.
5.37 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 wishes to obtain a number of resources, it invokes the decrease count() function:
5.38 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.
5.39 Exercise 4.22 asked you to design a multithreaded program that estimated using the Monte Carlo technique.
In that exercise, you were asked to create a single thread that generated random points, storing the result in a
global variable. Once that thread exited, the parent thread performed the calcuation that estimated the value of .
Modify that program so that you create several threads, each of which generates random points and determines if
the points fall within the circle. Each thread will have to update the global count of all points that fall within the
circle. Protect against race conditions on updates to the shared global variable by using mutex locks.
5.40 Exercise 4.23 asked you to design a program using OpenMP that estimated using the Monte Carlo technique.
Examine your solution to that program looking for any possible race conditions. If you identify a race condition,
protect against it using the strategy outlined in Section 5.10.2.
5.41 A barrier is a tool for synchronizing the activity of a number of threads. When a thread reaches a barrier
point, it cannot proceed until all other threads have reached this point as well. When the last thread reaches the
barrier point, all threads are released and can resume concurrent execution. Assume that the barrier is initialized
to N—the number of threads that must wait at the barrier point:
init(N);
Each thread then performs some work until it reaches the barrier point
Using synchronization tools described in this chapter, construct a barrier that implements the following API:
• int init(int n)—Initializes the barrier to the specified size.
• int barrier point(void)—Identifies the barrier point. All threads are released from the barrier when the last
thread reaches this point.
The return value of each function is used to identify error conditions. Each function will return 0 under normal
operation and will return −1 if an error occurs. A testing harness is provided in the source code download to test
your implementation of the barrier.
Chapter 6
6.1) a CPU-scheduling algorithm determines an order for the execution of its scheduled processes. given n
processes to be schedules on one processor, how many different schedules are possible? Give a formula in terms
of n.
n! (n factorial = n × n - 1 × n - 2 × ... × 2 × 1).
6.2) explain the difference between preemptive and nonpreemptive scheduling.
Preemptive cpu scheduling algorithms may take the cpu away from a running process BEFORE it has finished its
current cpu burst. A non-preemptive cpu scheduling algorithm will allow a running process to completely finish its
current cpu burst.
Non-preemptive cpu scheduling would not be used in a computer center since it cannot provide reasonable
response time to interactive users. It will unfairly allow some processes to keep the cpu for relatively long periods of
time will other processes wait for a response.
6.3) suppose that the following processes arrive for execution at the times indicated. Each process will run for the
amount of time listed. In answering the questions, use nonpreemptive scheduling , and base all decisions on the
information you have at the time the decision must be made.
a. what is the average turnaround time for these processes with the FCFS scheduling algorithm?
b. what is the average turnaround time for these processes with the SJF scheduling algorithm?
c. the SJF algorithm is supposed to improve performance, but notice that we chose to run process P1 at time 0
because we did not know that two shorter processes would arrive soon. Compute what the average turnaround
time will be if the CPU is left idle for the first 1 unit and then SJF scheduling is used. Remember that processes p1
and p2 are waiting during this idle time, so their waiting time may increase. This algorithm could be known as
future-knowledge scheduling.
((P1's waiting time + P1's brust time) +
(P2's waiting time + P2's brust time) +
(P1's waiting time + P1's brust time)) / 3
a. ((0 + 8) + (7.6 + 4) + (11+ 1) ) / 3 = 10.5333
b. ((0 + 8) + (8.6 + 4) + (7+ 1) ) / 3 = 9.5333
c. ((6 + 8) + (1.6 + 4) + (0+ 1) ) / 3 = 6.866
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.
6.4) what advantage is there in having different time-quantum sizes at different levels of a multilevel queueing
system?
Processes which need more frequent servicing, such as interactive processes, can be in a queue with a small q.
Processes that are computationally intensive can be in a queue with a larger quantum, requiring fewer context
switches to complete the processing, making more efficient use of the CPU.
6.5) many CPU-scheduling algorithms are parameterized. For example, the RR algorithm requires a parameter to
indicate the time slice. Multilevel feedback queues require parameters to define the number of queues, the
scheduling algorithms for each queue , the criteria used to move processes between queues , and so on.
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 may include another (for example , the FCFS algorithm is the RR algorithm with an
infinite time quantum). What (if any) relation holds between the following pairs of algorithm sets?
A. priority and SJF
B. multilevel feedback queues and FCFS
C. priority and FCFS
D. RR and SJF
a. The shortest job has the highest priority.
b. The lowest level of MLFQ is FCFS.
c. FCFS gives the highest priority to the job having been in existence the longest.
d. None.
6.6) suppose that a CPU scheduling algorithm 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?
It will favor the I/O-bound programs because of the relatively short CPU burst request by them; however, the CPUbound 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.
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-toone 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. Timing is crucial to real-time applications. If a thread is marked as real-time but is not bound to an LWP,
the thread may have to wait to 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 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 blocked, the LWP it was
attached to has been assigned to another thread. When the real-time thread has been scheduled to run again, it
must first wait to be attached to an LWP. By binding an LWP to a real-time thread you are ensuring the thread will be
able to run with minimal delay once it is scheduled.
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?
Priority = (recent CPU usage / 2) + base where base = 60
P1 = (40/2) + 60 = 80
P2 = (18/2) + 60 = 69
P3 = (10/2) + 60 = 65
Lowers the relative priority of CPU-bound processes.
6.10 Why is it important for the scheduler to distinguish I/O-bound programs from CPU-bound programs?
I/O-bound programs have the property of performing only a smallamount of computation before performing I/O.
Such programs typicallydo not use up their entire CPU quantum. CPU-bound programs, onthe other hand, use their
entire quantum without performing anyblocking I/O operations.Consequently, one could make better use of
thecomputer’s resources by giving higherpriority to I/O-bound programs and allow them to execute ahead of the
CPU-bound programs.
6.11) discuss how the following pairs of scheduling criteria conflict in certain settings.
a. CPU utilization and response time
b. average turnaround time and maximum waiting time
c. I/O device utilization and CPU utilization
• CPU utilization and response time: CPU utilization is increased if the overheads associated with context switching is
minimized. The context switching overheads could be lowered by performing context switches infrequently. This
could however result in increasing the response time for processes.
• Average turnaround time and maximum waiting time: Average turnaround time is minimized by executing the
shortest tasks first. Such a scheduling policy could however starve long-running tasks and thereby increase their
waiting time.
• I/O device utilization and CPU utilization: CPU utilization is maximized by running long-running CPU-bound tasks
without performing context switches. I/O device utilization is maximized by scheduling I/O-bound jobs as soon as
they become ready to run, thereby incurring the overheads of context switches.a
6.12) one technique for implementing lottery scheduling works by assigning processes lottery tickets, which are
used for allocating CPU time. Whenever a scheduling decision has to be made , a lottery ticket is chosen at
random, and the process holding that ticket gets the CPU . the BTV operating system implements lottery winner
getting 20miliseconds of CPU time (20 milliseconds * 50 = 1). Describe how the BTV scheduler can ensure tat
higher-priority threads receive more attention from the CPU than lower-priority threads.
By assigning more lottery tickets to higher-priority processes.
6.13) most scheduling algorithms maintain a run queue, which lists processes eligible options: (1) each processing
core has its own run queue , or (2) a single run queue is shared by all processing cores. what are the advantages
and disadvantages of each of these approaches?
The primary advantage of each processing core having its own run queue is that there is no contention over a single
run queue when the scheduler is running concurrently on 2 or more processors. When a scheduling decision must be
made for a processing core, the scheduler only need to look no further than its private run queue. A disadvantage of
a single run queue is that it must be protected with locks to prevent a race condition and a processing core may be
available to run a thread, yet it must first acquire the lock to retrieve the thread from the single queue. However,
load balancing would likely not be an issue with a single run queue, whereas when each processing core has its own
run queue, there must be some sort of load balancing between the different run queues.
6.14) consider the exponential average formula used to predict the length of the next CPU burst. What are the
implications of assigning the following values to the parameters used by the algorithm ?
a. α = 0 and τ0 = 100 milliseconds
b. α = 0.99 and τ0 = 10 milliseconds
When α = 0 and τ0 = 100 MS, the formula always makes
a prediction of 100 MS for the next CPU burst. When α = 0.99 and τ0 = 10 MS, the most recent behavior of the
process is given much higher weight than the past history associated with the process. Consequently,
the scheduling algorithm is almost memoryless, and simply predicts the length of the previous burst for the next
quantum of CPU execution.
6.15) A variation of the round-robin scheduler is the regressive round-robin scheduler. This scheduler assigns each
process a time quantum and a priority. The initial value of a time quantum is 50 milliseconds. However, every time
a process has been allocated the CPU and uses its entire time quantum (does not block for I/O), 10 milliseconds is
added to its time quantum, and its priority level is boosted. (The time quantum for a process can be increased to a
maximum of 100 milliseconds.) When a process blocks before using its entire time quantum, its time quantum is
reduced by 5 milliseconds, but its priority remains the same. What type of process (CPU-bound or I/O-bound) does
the regressive round-robin scheduler favor? Explain.
This scheduler would favor CPU bound processes as they are rewarded with a longer time quantum as well as
priority boost whenever they consume an entire time quantum. This scheduler does not penalize I/O bound
processes as they are likely to block for I/O before consuming their entire time quantum, but their priority remains
the same
6.16) consider the following set of processes, with the length of the CPU burst time given in milliseconds:
The processes are assumed to have arrives in the order p1,p2,p3,p4,p5 , all at time 0.
a. draw four Gantt charts that illustrate the execution of these processes using the following scheduling
algorithms: FCFS, SJF, nonpreemptive priority (a larger priority number implies a higher priority), and RR (quantum
= 2).
b. what is the turnaround time of each process for each of the scheduling algorithms in part a?
c. what is the waiting time of each process for each of these scheduling algorithms?
d. which of the algorithms results in the minimum average waiting time (over all processes)?
b) To find turn around time we use: completion time - arrival time
•Turn around time for FCFS scheduling algorithm will be:
P1 = 2-0= 2
P2= 3-0 = 3
P3 = 11-0 = 11
P4 = 15-0 = 15
P5 = 20-0 = 20
• Turn around time for SJF scheduling algorithm
P1 = 3-0= 3
P2 = 1-0 = 1
P3= 20-0 = 20
P4= 7-0 = 7
P5 = 12-0 = 12
• Turnaround time for non-preemptive algorithm
P1 = 15-0 = 15
P2 = 20-0 = 20
P3 = 8-0 = 8
P4 = 19-0 = 19
P5 = 13 - 0 = 13
• Turnaround time for RR
P1 = 2-0=2
P2= 3-0= 3
P3= 20 - 0 = 20
P4 = 19-0 = 19
P5 = 18-0 = 18
c) To find waiting time we use (turnaround time - burst time)
•Waiting time for FCFS
P1= 2-2 = 0
P2 = 3-1 = 2
P3 = 11-8 = 3
P4 = 15-4 = 11
P5 = 20-5= 15
• Waiting time for SJF
P1= 3-2 = 1
P2 = 1-1 = 0
P3 = 20-8 = 12
P4 = 7-4 = 3
P5 = 12-5 = 7
• Waiting time for non-preemptive
P1= 15-2 = 13
P2 = 20-1 = 19
P3 = 8-8 = 0
P4 = 19-4 = 15
P5 = 13 - 5 = 8
• Waiting time for RR
P1 = 2-2 = 0
P2 = 3-1 = 2
P3 = 20-8 =12
P4 = 13-4= 9
P5= 18-5=13
d) Average waiting time
•'For FCFS
= (0+2+3+11+15)/5
= 31/5
= 6.2milliseconds
•average waiting time For SJF
(1+0+12+3+7)/5
=23/5
= 4.6milliseconds
• Average waiting time For non-preemptive
(13+9+0+15+8)/5
=55/5
=11milliseconds
• average waiting time For RR
(0+2+12+9+13)/5
=7.2milliseconds
6.17) the following processes are being scheduled using a preemptive , round robin scheduling algorithm.
Each process is assigned a numerical priority, with a higher number indicating a higher relative priority. in addition
to processes listed below the system also has an idle task (which consumes no CPU resources and is identified as P
idle). This task has priority 0 and is scheduled whenever the system has no other available processes to run the
length of a time quantum in 10 units. If a process is preempted by a higher-priority process, the preempted
process is placed at the end of the queue.
a. show the scheduling order of the processes using a Gantt chart.
b. what is the turnaround time for each process?
c. what is the waiting time for each process?
d. what is the CPU utilization rate?
a.Gantt chart in handwritten notes.b.p1: 20-0 -20, p2: 80-25 = 55, p3: 90-30 = 60, p4: 75-60 = 15, p5: 120-100 = 20,
p6: 115-105 = 10c.1 p1: 0, p2: 40, p3: 35, p4: 0, p5: 10, p6: 0d.105/120 = 87.5 percent
6.18) The nice command is used to set the nice value of a process on Linux, as well as on other UNIX systems.
Explain why some systems may allow any user to assign a process a nice value >= 0 yet allow only the root (or
administrator) user to assign nice values < 0.
Nice values < 0 are assigned a higher relative priority and such systems may not allow non-root processes to assign
themselves higher priorities
6.19) Which of the following scheduling algorithms could result in starvation?
a. First-come, first-served
b. Shortest job first
c. Round robin
d. Priority
Shortest job first and priority-based scheduling algorithms could result in starvation.
6.20) Consider a variant of the RR scheduling algorithm in which the entries in the ready queue are pointers to the
PCBs.
a. What would be the effect of putting two pointers to the same process in the ready queue?
b. What would be two major advantages and two disadvantages of this scheme?
c. How would you modify the basic RR algorithm to achieve the same effect without the duplicate pointers?
a. In effect, that process will have increased its priority since by getting time more often it is receiving
preferential treatment.
b. The advantage is that more important jobs could be given more time, in other words, higher priority in
treatment. The consequence, of course, is that shorter jobs will suffer.
c. Allot a longer amount of time to processes deserving higher priority. In other words, have two or more
quantum's possible in the Round-Robin scheme.
6.21) Consider a system running ten I/O-bound tasks and one CPU-bound task. Assume that the I/O-bound tasks
issue an I/O operation once for every millisecond of CPU computing and that each I/O operation takes 10
milliseconds to complete. Also assume that the context-switching overhead is 0.1 millisecond and that all
processes are long-running tasks. Describe the CPU utilization for a round-robin scheduler when:
a. The time quantum is 1 millisecond
b. The time quantum is 10 milliseconds
(a) The time quantum is 1 millisecond: Irrespective of which process is scheduled, the scheduler incurs
a 0.1 millisecond context-switching cost for every context-switch. This results in a CPU utilization of
1/1.1 * 100 = 91%.
(b) The time quantum is 10 milliseconds: The I/O-bound tasks incur a context switch after using up
only 1 millisecond of the time quantum. The time required to cycle through all the processes is
therefore 10*1.1 + 10.1 (as each I/O-bound task executes for 1 millisecond and then incur the context
switch task, whereas the CPU-bound task executes for 10 milliseconds before incurring a context
switch). The CPU utilization is therefore 20/21.1 * 100 = 94%.
6.22) Consider a system implementing multilevel queue scheduling. What strategy can a computer user employ to
maximize the amount of CPU time allocated to the user’s process?
The program could maximize the CPU time allocated to it by not fully utilizing its time quantum's. It could use a large
fraction of its assigned quantum, but relinquish the CPU before the end of the quantum, thereby increasing the
priority associated with the process.
6.23) Consider a preemptive priority scheduling algorithm based on dynamically changing priorities. Larger priority
numbers imply higher priority. When a process is waiting for the CPU (in the ready queue, but not running), its
priority changes at a rate α. When it is running, its priority changes at a rate β. All processes are given a priority of
0 when they enter the ready queue. The parameters α and β can be set to give many different scheduling
algorithms.
a. What is the algorithm that results from β > α > 0?
b. What is the algorithm that results from α < β < 0?
a. Since β is a rate and it is greater than that of α, processes that are running will continually
receive a much higher priority. At the same time, the priority of processes waiting to be run will increase, however,
this increase will not be as fast as the process
running on the CPU. This will result in a first-come first-serve (FCFS) type of situation where when no process is
running the one that has been waiting the longest will be ran, and its priority will continuously increase faster than
the others until it stops.
b. New processes in this scheme will always be ran immediately, and older processes
will sit in the ready queue until the most recent process finishes. This is essentially
Last In First Out.
6.24) Explain the how the following scheduling algorithms discriminate either in favor of or against short
processes:
a. FCFS
b. RR
c. Multilevel feedback queues
a. FCFS—discriminates against short jobs since any short jobs arriving after long jobs will have a longer waiting time
b. RR—treats all jobs equally (giving them equal bursts of CPU time) so short jobs will be able to leave the system
faster since they will finish first.
c. Multilevel feedback queues—work similar to the RR algorithm— they discriminate favorably toward short jobs.
6.25) Using the Windows scheduling algorithm, determine the numeric priority of each of the following threads.
a. A thread in the ABOVE NORMAL PRIORITY CLASS with a relative priority of HIGHEST
b. A thread in the NORMAL_PRIORITY_CLASSwith a relative priority of NORMAL
c. A thread in the BELOW NORMAL PRIORITY CLASS with a relative priority of ABOVE NORMAL
:
a.26
b.8
c.14
6.26) Assuming that no threads belong to the REALTIME PRIORITY CLASS and that none may be assigned a TIME
CRITICAL priority, what combination of priority class and priority corresponds to the highest possible relative
priority in Windows scheduling?
HIGHpriority class and HIGHESTpriority within that class. (numeric priority of 15)
6. 27) Consider the scheduling algorithm in the Solaris operating system for time-sharing threads.
a. What is the time quantum (in milliseconds) for a thread with priority 15? With priority 40?
b. Assume that a thread with priority 50 has used its entire time quantum without blocking. What new priority
will the scheduler assign this thread?
c. Assume that a thread with priority 20 blocks for I/O before its time quantum has expired. What new priority
will the scheduler assign this thread?
a. 160 and 40
b. 35
c. 54
6.28) Assume that two tasks, A and B, are running on a Linux system. The nice values of A and B are −5 and +5,
respectively. Using the CFS scheduler as a guide, describe how the respective values of vruntime vary between the
two processes given each of the following scenarios:
• Both A and B are CPU-bound.
• A is I/O-bound, and B is CPU-bound.
• A is CPU-bound, and B is I/O-bound.
a. Vruntime for A will be smaller than it is for B, resulting in a greater priority for A than B.
b. A will still have a smaller vruntime than B since it is I/O-bound and will require less CPU time.
c. B will have a smaller vruntime than A since it is I/O-bound and will require less CPU time.
6.29) Discuss ways in which the priority inversion problem could be addressed in a real-time system. Also discuss
whether the solutions could be implemented within the context of a proportional share scheduler.
The priority inversion problem could be addressed by temporarily changing the priorities of the processesinvolved.
Processes that are accessing resources needed by a higher-priority process inherit the higher priority until they are
finished with the resources in question. When they are finished, their priority reverts to its original value.
Thissolution can be easily implemented within a proportional share scheduler;the shares of the high-priority
processes are simply transferredto the lower-priority process for the duration when it is accessing the resources.
6.30 Under what circumstances is rate-monotonic scheduling inferior to earliest-deadline-first scheduling in
meeting the deadlines associated with processes?
Consider two processes P1and P2where p1= 50, t1= 25 andp2= 75, t2= 30. If P1were assigned a higher priority than
P2, then thefollowing scheduling events happen under rate-monotonic scheduling.P1is scheduled at t= 0, P2is
scheduled at t= 25, P1is scheduled att= 50, and P2is scheduled at t= 75. P2is not scheduled early enough tomeet its
deadline. The earliest deadline schedule performs the followingscheduling events: P1is scheduled at t= 0, P2is
scheduled at t= 25,P1is scheduled at t= 55, and so on. This schedule actually meetsthe deadlines and therefore
earliest-deadline-first scheduling is moreeffective than the rate-monotonic scheduler.
6.31) Consider two processes, P1 and P2, where p1 = 50, t1 = 25, p2 = 75, and t2 = 30.
a. Can these two processes be scheduled using rate-monotonic scheduling? Illustrate your answer using a Gantt
chart such as the ones in Figure 5.21–Figure 5.24.
b. Illustrate the scheduling of these two processes using earliest deadline-first (EDF) scheduling.
Consider when P1is assigned a higher priority thanP2with the ratemonotonic scheduler. P1is scheduled at t= 0, P2is
scheduled at t= 25,P1is scheduled at t= 50, and P2is scheduled at t= 75. P2is notscheduled early enough to meet its
deadline. When P1is assigned alower priority than P2, then P1does not meet its deadline since it willnot be
scheduled in time.
6.32) Explain why interrupt and dispatch latency times must be bounded in a hard real-time system.
following tasks: save the currently executing instruction, determine thetype of interrupt, save the current process
state, and then invoke theappropriate interrupt service routine. Dispatch latency is the cost associatedwith stopping
one process and starting another. Both interrupt anddispatch latency needs to beminimized in order to ensure that
real-timetasks receive immediate attention. Furthermore, sometimes interruptsare disabled when kernel data
structures are being modified, so theinterrupt does not get serviced immediately. For hard real-time systems,the
time-period for which interrupts are disabled must be bounded inorder to guarantee the desired quality of service .
Chapter 7
7.1 List three examples of deadlocks that are not related to a computersystem environment.
Two cars crossing a single-lane bridge from opposite directions.
A person going down a ladder while another person is climbing up the ladder.
Two trains traveling toward each other on the same track.
‫دو اتومبیل در حال عبور از یک پل تک خیط از جهت های مخالف هستند‬.
‫شخیص که از نردبان ن‬.
‫پایی یم رود در حایل که شخص دیگری از نردبان باال یم رود‬
‫دو قطار در همان مسی در حال حرکت به سمت یکدیگر هستند‬.
7.2 Suppose that a system is in an unsafe state. Show that it is possible for the threads to complete their
execution without entering a deadlocked state.
An unsafe state may not necessarily lead to deadlock, it just means that we cannot guarantee that deadlock will not
occur. Thus, it is possible that a system in an unsafe state may still allow all processes to complete without deadlock
occurring. Consider the situation where a system has 12 resources allocated among processes P0, P1, and P2. The
resources are allocated according to the following policy:
‫ بلکه این بدان ن‬، ‫ممکن است یک کشور ناامن لزوما به بن بست نرساند‬
‫ن‬
‫ بنابراین‬.‫تضمی کنیم که بن بست رخ نخواهد داد‬
‫معن است که ما نیم توانیم‬
‫ وضعیت را در نظر بگیید که‬.‫ ممکن است که یک سیستم در یک حالت ناامن هنوز هم اجازه دهد تمام فرآیندها بدون وقوع بن بست کامل شوند‬،
‫ منابع مطابق خط ی‬.‫است‬:
‫ منبع در ن‬12 ‫ یک سیستم دارای‬P0 ، P1 ‫ و‬P2 ‫مش زیر اختصاص داده یم شوند‬
‫بی فرآیندهای‬
Currently there are two resources available. This system is in an unsafe state as process P1 could complete, thereby
freeing a total of four resources. But we cannot guarantee that processes P0 and P2 can complete. However, it is
possible that a process may release resources before requesting any further. For example, process P2 could release a
resource, thereby increasing the total number of resources to five. This 19 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.
‫حاض دو منبع در ر‬
‫ در حال ن‬P1 .‫ در نتیجه چهار منبع را آزاد یم کند‬، ‫یم تواند تکمیل شود‬
‫ این سیستم در حالت ناامن است زیرا فرآیند‬.‫دسیس است‬
‫ن‬
‫تضمی کنیم که فرآیندهای‬
‫ اما ما نیم توانیم‬P0 ‫ و‬P2 ‫ این امکان وجود دارد که یک فرآیند ممکن است منابع را‬، ‫ با این حال‬.‫یم توانند کامل شوند‬
‫ر‬
‫بیشی ی‬
‫ فرآیند‬، ‫ به عنوان مثال‬.‫منتش کند‬
‫ قبل از درخواست‬P2 .‫ در نتیجه تعداد کل منابع به پنج مورد افزایش یم یابد‬، ‫یم تواند یک منبع را آزاد کند‬
‫را ن‬
‫ بن بست اجازه یم دهد تا روند‬7 ‫ فصل‬20 19 ‫ این‬P0 ‫ در نتیجه فرآیند‬، ‫ که در مجموع نه منبع را آزاد یم کند‬، ‫ به اتمام برسد‬P2 ‫نی یم توان‬
‫تکمیل کرد‬.
7.3 Consider the following snapshot of a system:
Allocation
Max
Available
AB C D
AB C D
AB C D
T0
0012
0012
1520
T1
1000
1750
T2
1354
2356
T3
0632
0652
T4
0014
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?
c. If a request from thread T1 arrives for (0,4,2,0), can the request be granted immediately?
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).
b. The system is in a safe state? Yes. With Available being equal to (1, 5, 2, 0), either process P0 or P3 could run.
Once process P3 runs, it releases its resources, which allow all other existing processes to run.
c. The request can be granted immediately? This results in the value of Available being (1, 1, 0, 0). One ordering of
processes that can finish is P0, P2, P3, P1, and P4.
‫ مقادیر نیاز به فرآیندهای‬.‫ آ‬P0 ‫ از طریق‬P4 ‫ و‬، ‫) است‬0 ، 2 ، 0 ، 0( ، )2 ، 0 ، 0 ، 1( ، )0 ، 5 ، 7 ، 0( ، )0 ، 0 ، 0 ، 0( ‫به ترتیب‬
)0، 6، 4، 2(
‫رن‬
‫ یم توان روند‬، )0 ، 2 ، 5 ، 1( ‫داشی برابر برابر با‬
‫ با‬.‫ ب سیستم در حالت ایمن است؟ آره‬P0 ‫ یا‬P3 ‫ پس از اجرای فرآیند‬.‫ را اجرا کرد‬P3 ‫ منابع‬،
‫خود را ی‬.
‫ که اجازه یم دهد همه فرایندهای موجود اجرا شوند‬، ‫منتش یم کند‬
‫ ییک از سفارشات فرآیندهای پایان یافته یم‬.‫) را نتیجه یم دهد‬0 ، 0 ، 1 ، 1( ‫ج درخواست بالفاصله قابل اعطای است؟ این مقدار ارزش موجود‬
‫ تواند‬P0 ، P2 ، P3 ، P1 ‫ و‬P4 ‫باشد‬.
7.4 A possible method for preventing deadlocks is to have a single, higher-order resource that must be requested
before any other resource. For example, if multiple threads attempt to access the synchronization objects A · · · E,
deadlock is possible. (Such synchronization objects may include mutexes, semaphores, condition variables, and
the like.)We can prevent deadlock by adding a sixth object F. Whenever a thread 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: the locks for objects A · · · E are contained within the lock for object F. Compare this scheme with
the circular-wait scheme of Section 8.5.4.
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.
‫ی‬
ً
‫ ر‬.‫بزرگ را ب ه همراه دارد‬
‫بهی است سیاست قفل کردن را تا حد امکان محدود کنید‬
‫مناسن نیست زیرا دامنه بسیار‬
‫این احتماال راه حل‬.
‫ی‬
7.5 Prove that the safety algorithm presented in Section 8.6.3 requires an order of m × n2 operations.
7.6 Consider a computer system that runs 5,000 jobs per month and has no deadlock-prevention or deadlockavoidance scheme. Deadlocks occur about twice per month, and the operator must terminate and rerun about ten
jobs per deadlock. Each job is worth about two dollars (in CPU time), and the jobs terminated tend to be about
half done when they are aborted. A systems programmer has estimated that a deadlock-avoidance algorithm (like
the banker’s algorithm) could be installed in the system with an increase of about 10 percent in the average
execution time per job. Since the machine currently has 30 percent idle time, all 5,000 jobs per month could still
be run, although turnaround time would increase by about 20 percent on average.
a. What are the arguments for installing the deadlock-avoidance algorithm?
b. What are the arguments against installing the deadlock-avoidance algorithm?
An argument for installing deadlock avoidance in the system is that we could ensure deadlock would never occur. In
addition, despite the increase in turnaround time, all 5,000 jobs could still run. An argument against installing
deadlock avoidance software is that deadlocks occur infrequently and they cost little when they do occur.
‫ عالوه‬.‫استدالل در مورد نصب اجتناب از بن بست در سیستم این است که یم توانیم اطمینان حاصل کنیم که هرگز بن بست وجود نخواهد داشت‬
‫ استدالل در مورد نصب نرم افزار جلوگیی از بن بست این‬.‫ شغل هنوز هم یم توانند کار کنند‬5000 ‫ همه‬، ‫ با وجود افزایش زمان چرخش‬، ‫بر این‬
‫است که بن بست به ندرت اتفاق یم افتد و در صورت بروز آنها هزینه کیم دارند‬.
7.7 Can a system detect that some of its threads are starving? If you answer “yes,” explain how it can. If you
answer “no,” explain how the system can dealwith the starvation problem.
Starvation is a difficult topic to define as it may mean different things for different systems. For the purposes of this
question, we will define starvation as the situation whereby a process must wait beyond a 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 process requests a resource, a timer is
started. If the elapsed time exceeds T, then the process is considered to be starved. One strategy for dealing with
starvation would be to adopt a policy 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 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.
‫ی‬
‫ن‬
‫گرسنیک موضویع دشوار برای تعریف است زیرا ممکن است به معنای ن‬
‫ ما‬، ‫ برای اهداف این سوال‬.‫مختلف برای سیستمهای مختلف باشد‬
‫چیهای‬
‫ی‬
‫ر‬
‫ر‬
‫ شاید به طور‬- ‫درخواسن فراتر از یک دوره معقول‬
‫وضعین تعریف خواهیم کرد که فرایند آن باید قبل از دریافت یک منبع‬
‫گرسنیک را به عنوان‬
‫ی‬
‫ن‬
‫منطف ر‬
‫ر‬
‫ر‬
.
.
‫شناسای یک دوره زمای است‬
، ‫ منتظر بماند ییک از راه های تشخیص گرسنیک‬- ‫ نامحدود‬- T ‫تلف یم شود وقن یک فرآیند از‬
‫این امر غی‬
‫ی‬
‫ی‬
‫ر‬
‫سیی شده بیش از‬
‫ فرایند‬، ‫باشد‬
‫ اگر زمان ر‬.‫ یک تایمر یشوع یم شود‬، ‫ یک منبع درخواست یم کند‬T ‫ یك اسیاتژی‬.‫گرسنیک در نظر گرفته یم شود‬
‫ی‬
‫ن‬
‫ر‬
، ‫ به عنوان مثال‬.‫طوالی ترین انتظار را داشته باشد اختصاص یم یابد‬
‫سیاسن است كه در آن منابع فقط به فرایندی كه‬
‫برای مقابله با گرسنیک اتخاذ‬
‫ ر‬.‫ به تعویق یم افتد‬،
‫ اگر فرآیند‬Pa ‫ بیش از فرآیند شب در انتظار منبع‬X ‫ درخواست پردازش‬، ‫ باشد‬Pb ‫ تا زمان برآورده شدن درخواست‬Pa ‫اسیاتژی‬
‫ر‬
، ‫ ممکن است منبیع به فرآیندی اعطا شود که کمی از روند دیگر منتظر بماند‬، ‫ در این سناریو‬.‫دیگر سختگیانه تر از آنچه که فقط ذکر شد است‬
‫ اما اگر فرایند دیگری گرسنه ر‬.‫مشوط بر اینکه روند دیگر گرسنه نباشد‬
‫ ی‬.
‫ ابتدا درخواست آن ارضا یم شود‬، ‫تلف شود‬
7.8 Consider the following resource-allocation policy. Requests for and releases of resources are allowed at any
time. If a request for resources cannot be satisfied because the resources are not available, thenwe check any
threads that are blocked waiting for resources. If a blocked thread has the desired resources, then these resources
are taken away from it and are given to the requesting thread. The vector of resources forwhich the blocked
thread is waiting is increased to include the resources that were taken away. For example, a system has three
resource types, and the vector Available is initialized to (4,2,2). If thread T0 asks for (2,2,1), it gets them. If T1 asks
for (1,0,1), it gets them. Then, if T0 asks for (0,0,1), it is blocked (resource not available). If T2 now asks for (2,0,0),
it gets the available one (1,0,0), as well as one that was allocated to T0 (since T0 is blocked). T0’s Allocation vector
goes down to (1,2,1), and its Need vector goes up to (1,0,1).
a. Can deadlock occur? If you answer “yes,” give an example. If you
answer “no,” specify which necessary condition cannot occur.
b. Can indefinite blocking occur? Explain your answer.
a. Deadlock cannot occur because preemption exists.
b. Yes. A process may never acquire all the resources it needs if they are continuously .
c. preempted by a series of requests such as those of process .
‫ بن بست نیم تواند رخ دهد زیرا پیشگیی وجود دارد‬.‫آ‬.
‫ اگر یک فرآیند ممکن باشد هرگز به طور مداوم تمام منابع مورد نیاز خود را بدست نیاورد‬.‫ب آره‬.
‫پیشگوی شده توسط یک شی درخواستها مانند درخواستهای پردازش‬
‫ج‬.
‫ی‬
7.9 Consider the following snapshot of a system:
Allocation Max
AB C D AB C D
T0 3 0 1 4 5 1 1 7
T1 2 2 1 0 3 2 1 1
T2 3 1 2 1 3 3 2 1
T3 0 5 1 0 4 6 1 2
T4 4 2 1 2 6 3 2 5
Using the banker’s algorithm, determine whether or not each of the following states is unsafe. If the state is safe,
illustrate the order in which the threads may complete. Otherwise, illustrate why the state is unsafe.
a. Available = (0, 3, 0, 1)
b. Available = (1, 0, 0, 2)
a. Not safe. Processes P2, P1, and P3 are able to finish, but no remaining processes can finish. b. Safe. Processes P1,
P2, and P3 are able to finish. Following this, processes P0 and P4 are also able to finish.
‫ فرآیندهای‬.‫ امن نیست‬.‫ آ‬P2 ، P1 ‫ و‬P3 ‫قادر به اتمام هستند اما هیچ فرآیند باقیمانده نیم تواند به پایان برسد‬.
‫ن‬
‫ فرآیندهای‬.‫ یی خطر‬.‫ ب‬P1 ، P2 ‫ و‬P3 ‫ فرآیندهای‬، ‫ پس از این‬.‫ قادر به اتمام هستند‬P0 ‫ و‬P4 ‫نی قادر به اتمام هستند‬.
7.10 Suppose that you have coded the deadlock-avoidance safety algorithm that determines if a system is in a safe
state or not, and now have been asked to implement the deadlock-detection algorithm. Can you do so by simply
using the safety algorithm code and redefining Maxi = Waitingi + Allocationi, where Waitingi is a vector specifying
the resources for which thread I is waiting and Allocationi is as defined in Section 8.6? Explain your 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 Waitingmatrix fulfills a role similar to the Need matrix, therefore Max =
Waiting + Allocation
‫ر‬
‫ هنگام محاسبه الگوریتم ن‬.‫درخواسن است که ممکن است یک فرایند انجام شود‬
‫ر‬
‫ بردار‬.‫ آره‬Max ‫ از ماتریس‬، ‫ایمن‬
‫حداکی‬
‫ نمایانگر‬Need ‫استفاده‬
.
‫ یم کنیم که نمایانگر‬Max - Allocation ‫ است راه دیگر برای فکر کردن در این مورد‬Max = Need + Allocation ، ‫ مطابق این سؤال‬.‫است‬
‫ ی‬Max = Waiting + Allocation
Waitingmatrix ‫ بنابراین‬، ‫نقش شبیه به ماتریس نیاز دارد‬
7.11 Is it possible to have a deadlock involving only one single-threaded process? Explain your answer.
No. This follows directly from the hold-and-wait condition.
‫ این به طور مستقیم از یشط انتظار و انتظار منتیه یم شود‬.‫خی‬.
7.12 Consider the traffic deadlock depicted in Figure 8.12.
a. Show that the four necessary conditions for deadlock hold in this example.
b. State a simple rule for avoiding deadlocks in this system.
a. The four necessary conditions for a deadlock are (1) mutual exclusion; (2) hold-and-wait; (3) no preemption; and
(4) circular wait. The mutual exclusion condition holds since only one car can occupy a space in the roadway. Holdand-wait occurs where a car holds onto its place in the roadway while it waits to advance in the roadway. A car
cannot be removed (i.e. preempted) from its position in the roadway. Lastly, there is indeed a circular wait as each
car is waiting for a subsequent car to advance. The circular wait condition is also easily observed from the graphic.
b. A simple rule that would avoid this traffic deadlock is that a car may not advance into an intersection if it is clear it
will not be able immediately to clear the intersection.
‫رن‬
‫ یشط محرومیت‬.‫) انتظار مدور‬4( ‫) هیچ پیشگیی؛ و‬3( .‫صی کردن‬
‫) نگه‬2( .‫) محرومیت متقابل است‬1( ‫ چهار یشط الزم برای بن بست‬.‫آ‬
‫داشی و ی‬
‫ انتظار و انتظار در حایل اتفاق یم افتد که یک اتومبیل در حایل‬.‫فضای را در جاده کنار جاده اشغال کند‬
‫متقابل از آنجا که فقط یک اتومبیل یم تواند‬
‫ی‬
‫ اتومبیل را نیم توان از موقعیت خود در جاده عبور داد ن‬.‫ جای خود را در جاده بگذارد‬، ‫پیشفت در جاده است‬
‫که منتظر ی‬
، ‫ شانجام‬.)‫(یعن پیش فرض‬
‫ یشایط انتظار دایره ای ن‬.‫پیشفت است‬
‫نی به ر ر‬
‫ماشی بعدی برای ی‬
‫ن‬
‫احن از گرافیک‬
‫در حقیقت یک انتظار دایره ای وجود دارد که هر اتومبیل منتظر یک‬
‫مشاهده یم شود‬.
، ‫ نیم تواند یک اتومبیل را به سمت تقاطع پیش برد‬، ‫ب یک قانون ساده که مانع از این بن بست ترافییک یم شود این است که اگر واضح باشد‬
‫بالفاصله قادر به پاک کردن تقاطع نخواهد بود‬.
7.13 Draw the resource-allocation graph that illustrates deadlock from the program example shown in Figure 8.1
in Section 8.2.
7.14 In Section 6.8.1, we described a potential deadlock scenario involving processes P0 and P1 and semaphores S
and Q. Draw the resourceallocation graph that illustrates deadlock under the scenario presented in that section.
7.15 Assume that a multithreaded application uses only reader–writer locks for synchronization. Applying the four
necessary conditions for deadlock, is deadlock still possible if multiple reader–writer locks are used?
YES.
(1)Mutual exclusion is maintained, as they cannot be shared if there is a writer.
(2)Hold-and-wait is possible, as a thread can hold one reader—writer lock while waiting to acquire another.
(3) You cannot take a lock away, so no preemeption is upheld.
(4) A circular wait among all threads is possible
‫آره‬.
‫ آنها را نیم توان به ر‬، ‫ زیرا در صورت وجود نویسنده‬، ‫محرومیت متقابل حفظ یم شود‬.
(1) ‫اشیاک گذاشت‬
‫رن‬
(2) ‫ نویسنده در حایل که منتظر است تا دیگری را‬- ‫ زیرا یک موضوع یم تواند یک خواننده را نگه داشته باشد‬، ‫داشی و انتظار ممکن است‬
‫نگه‬
‫بدست آورد‬.
(3) ‫ بنابراین هیچ پیشگیی ای تأیید نیم شود‬، ‫شما نیم توانید قفل کنید‬.
(4) ‫انتظار کیل در همه موضوعات ممکن است‬
7.16 The program example shown in ‫ ز‬8.1 doesn’t always lead to deadlock. Describe what role the CPU scheduler
plays and how it can contribute to deadlock in this program.
If thread_one is scheduled before thread_two and thread_one is able to acquire both mutex locks before
thread_two is scheduled, deadlock will not occur. Deadlock can only occur if either thread_one or thread_two is able
to acquire only one lock before the other thread acquires the second lock
‫ اگر‬thread_one ‫ قبل از‬thread_two ‫ برنامه ز شده باشد و‬thread_one ‫ بتواند هر دو قفل‬mutex ‫ را قبل از برنامه ریزی‬thread_two
‫ر‬
‫صوری رخ دهد که یا‬
‫ بن بست ممکن است تنها در‬.‫ بن بست وجود نخواهد داشت‬، ‫ بدست آورد‬thread_one ‫ یا‬thread_two ‫بتواند تنها یک‬
‫ بدست آورد‬، ‫قفل را قبل از اینکه موضوع دیگر قفل دوم را بدست آورد‬
7.17 In Section 8.5.4, we described a situation in which we prevent deadlock by ensuring that all locks are
acquired in a certain order. However, we also point out that deadlock is possible in this situation if two threads
simultaneously invoke the transaction() function. Fix the transaction() function to prevent deadlocks.
Add a new lock to this function. This third lock must be acquired before the two locks associated with the accounts
are acquired. The transaction() function now appears as follows:
‫ تابع معامله () اکنون‬.‫ این قفل سوم باید قبل از به دست آوردن دو قفل مرتبط با حساب ها خریداری شود‬.‫یک قفل جدید به این عملکرد اضافه کنید‬
‫به یشح زیر است‬:
7.18 Which of the six resource-allocation graphs shown in Figure 8.12 illustrate deadlock? For those situations that
are deadlocked, provide the cycle of threads and resources. Where there is not a deadlock situation, illustrate the
order in which the threads may complete execution.
7.19 Compare the circular-wait scheme with the various deadlock-avoidance schemes (like the banker’s algorithm)
with respect to the following issues:
a. Runtime overhead
b. System throughput
A deadlock-avoidance scheme tends to increase the runtime overheads due to the cost of keep track of the current
resource allocation. However, a deadlock-avoidance scheme allows for more concurrent use of resources than
schemes that statically prevent the formation of deadlock. In that sense, a deadlockavoidance scheme could increase
system throughput
.
‫ یک طرح‬، ‫ با این حال‬.‫ یک طرح اجتناب از بن بست تمایل به افزایش هزینه های اضافه کار دارد‬، ‫با توجه به هزینه پیگیی تخصیص منابع فعیل‬
، ‫ به این معنا‬.‫ یم کند‬، ‫ایستای مانع از ایجاد بن بست یم شوند‬
‫های که بطور‬
‫ی‬
‫ امکان استفاده همزمان از منابع را نسبت به طرح ی‬، ‫اجتناب از بن بست‬
‫یک طرح بن بست ممکن است توان سیستم را افزایش دهد‬
7.20 In a real computer system, neither the resources available nor the demands of threads for resources are
consistent over long periods (months). Resources break or are replaced, new processes and threads
come and go, and new resources are bought and added to the system. If deadlock is controlled by the banker’s
algorithm, which of the following changes can be made safely (without introducing the possibility of deadlock),
and under what circumstances?
a. Increase Available (new resources added).
b. Decrease Available (resource permanently removed from system).
c. Increase Max for one thread (the thread needs or wants more
resources than allowed).
d. Decrease Max for one thread (the thread decides it does not need
that many resources).
e. Increase the number of threads.
f. Decrease the number of threads.
7.21 Consider the following snapshot of a system:
Allocation Max
AB C D AB C D
T0 2 1 0 6 6 3 2 7
T1 3 3 1 3 5 4 1 5
T2 2 3 1 2 6 6 1 4
T3 1 2 3 4 4 3 4 5
T4 3 0 3 0 7 2 6 1
What are the contents of the Need matrix?
7.22 Consider a system consisting of four resources of the same type that are shared by three threads, each of
which needs at most two resources. Show that the system is deadlock free.
Using the terminology of Section Section 7.6.2, we have: a. ∑ i < m + n b. Maxi ≥ 1 for all i Proof: Needi = Maxi −
Allocationi If there exists a deadlock state then: c. ∑ i = m Use a. to get: Σ Needi + Σ Allocationi = Σ Maxi < m + n Use c.
to get: Σ Needi + m < m + n Rewrite to get: ∑ i < n This implies that there exists a process Pi such that Needi = 0. Since
Maxi ≥ 1 it follows that Pi has at least one resource that it can release. Hence the system cannot be in a deadlock
state.
‫ الف‬:‫ این موارد را داریم‬، ‫ بخش‬7.6.2 ‫با استفاده از اصطالحات بخش‬. ∑ i <m + n b. Maxi ≥ 1 ‫برای همه من اثبات‬: Needi = Maxi Allocationi ‫ آنگاه‬، ‫اگر حالت بن بست وجود دارد‬: c. ∑ i = m ‫ از‬a ‫ برای به دست آوردن‬.‫استفاده کنید‬: Σ Needi + All Allocationi = Max
‫این بدان ن‬
Maxi <m + n ‫ از‬c ‫ برای به دست آوردن‬.‫استفاده کنید‬: Σ Needi + m <m + n ‫بازنویش را برای بدست آوردن‬: ∑ i <n ‫معن است که‬
‫ی‬
‫ یک فرآیند‬Pi ‫ از آنجا که‬.0 = ‫ وجود دارد که نیاز‬Maxi ≥ 1 ‫ نتیجه یم گید که‬Pi ‫ از این رو‬. .‫حداقل یک منبع دارد که یم تواند آن را منتش کند‬
‫سیستم نیم تواند در حالت بن بست باشد‬.
7.23 Consider a system consisting of m resources of the same type being shared by n threads. A thread can
request or release only one resource at a time. Show that the system is deadlock free if the following two
conditions hold:
a. The maximum need of each thread is between one resource and m resources.
b. The sumof all maximumneeds is less than m + n.
7.24 Consider the version of the dining-philosophers problem in which the chopsticks are placed at the center of
the table and any two of them can be used by a philosopher. Assume that requests for chopsticks are made one at
a time. Describe a simple rule for determining whether a particular request can be satisfied without causing
deadlock given the current allocation of chopsticks to philosophers.
The following rule prevents deadlock: when a philosopher makes a request for the first chopstick, do not grant the
request if there is no other philosopher with two chopsticks and if there is only one chopstick remaining.
‫ر‬
‫ ر‬:‫قانون زیر مانع از بن بست یم شود‬
‫ن‬
‫ اگر فیلسوف دیگری با دو قالده چنگال‬، ‫اولی بار از چوب انتخاب یم کند‬
‫درخواسن برای‬
‫وقن یک فیلسوف‬
‫ر‬
‫ درخواست را تحویل ندهید‬، ‫وجود نداشته باشد و اگر فقط یک چوپان باق مانده باشد‬.
7.25 Consider again the setting in the preceding exercise. Assume now that each philosopher requires three
chopsticks to eat. Resource requests are still issued one at a time. Describe some simple rules for determining
whether a particular request can be satisfied without causing deadlock given the current allocation of chopsticks
to philosophers.
When a philosopher makes a request for a chopstick, allocate the request if: 1) the philosopher has two chopsticks
and there is at least one chopstick remaining, 2) the philosopher has one chopstick and there are at least two
chopsticks remaining, 3) there is at least one chopstick remaining, and there is at least one philosopher with three
chopsticks, 4) the philosopher has no chopsticks, there are two chopsticks remaining, and there is at least one other
philosopher with two chopsticks assigned.
‫ر‬
‫درخواسن برای یک دکوراسیون را ارائه یم دهد ‪ ،‬درخواست را به او اختصاص دهید‬
‫‪:‬هنگایم که یک فیلسوف‬
‫‪ .1.‬فیلسوف دو قالده چنگال دارد و حداقل یک قالده سنج ر‬
‫باق مانده است‬
‫‪.‬فیلسوف یک قالده چنگال دارد و حداقل دو قالده چنگال ر‬
‫باق مانده است ‪.2‬‬
‫‪ .3.‬حداقل یک چوپان ر‬
‫باق مانده است ‪ ،‬و حداقل یک فیلسوف با سه خردسال وجود دارد‬
‫‪ .4.‬فیلسوف فاقد صابون است ‪ ،‬دو قالده چنگال ر‬
‫باق مانده است ‪ ،‬و حداقل یک فیلسوف دیگر با دو صابون منتخب وجود دارد‬
‫‪7.26 We can obtain the banker’s algorithm for a single resource type from the general banker’s algorithm simply‬‬
‫‪by reducing the dimensionality of the various arrays by 1. Show through an example that we cannot implement‬‬
‫‪the multipleresource- type banker’s scheme by applying the single-resource-type scheme to each resource type‬‬
‫‪individually.‬‬
7.27 Consider the following snapshot of a system:
Allocation Max
AB C D AB C D
T0 1 2 0 2 4 3 1 6
T1 0 1 1 2 2 4 2 4
T2 1 2 4 0 3 6 5 1
T3 1 2 0 1 2 6 2 3
T4 1 0 0 1 3 1 1 2
Using the banker’s algorithm, determine whether or not each of the
following states is unsafe. If the state is safe, illustrate the order in which
the threads may complete. Otherwise, illustrate why the state is unsafe.
a. Available = (2, 2, 2, 3)
b. Available = (4, 4, 1, 1)
c. Available = (3, 0, 1, 4)
d. Available = (1, 5, 2, 2)
7.28 Consider the following snapshot of a system:
Allocation Max Available
AB C D AB C D AB C D
T0 3 1 4 1 6 4 7 3 2 2 2 4
T1 2 1 0 2 4 2 3 2
T2 2 4 1 3 2 5 3 3
T3 4 1 1 0 6 3 3 2
T4 2 2 2 1 5 6 7 5
Answer the following questions using the banker’s algorithm:
a. Illustrate that the system is in a safe state by demonstrating an
order in which the threads may complete.
b. If a request from thread T4 arrives for (2, 2, 2, 4), can the request be
granted immediately?
c. If a request from thread T2 arrives for (0, 1, 1, 0), can the request be
granted immediately?
d. If a request from thread T3 arrives for (2, 2, 1, 2), can the request be
granted immediately?
EX-30
The optimistic assumption is that there will not be any form of circular wait in terms of resources allocated and
processes making requests for them. This assumption could be violated if a circular wait does indeed occur in
practice.
7.29 What is the optimistic assumption made in the deadlock-detection algorithm? How can this assumption be
violated?
The optimistic assumption is that there will not be any form of circular wait in terms of resources allocated and
processes making requests for them. This assumption could be violated if a circular wait does indeed occur in
practice.
‫ر‬
‫ اگر‬.‫ هیچ نوع انتظار مدور وجود نخواهد داشت‬، ‫درخواسن برای آنها‬
‫فرض خوش بینانه این است که از نظر منابع اختصاص یافته و فرآیندهای‬
ً
‫ این فرض را یم توان نقض کرد‬، ‫واقعا یک انتظار صلیح در عمل رخ دهد‬.
7.30 A single-lane bridge connects the two Vermont villages of North Tunbridge and South Tunbridge. Farmers in
the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become
deadlocked if a northbound and a southbound farmer get on the bridge at the same time. (Vermont farmers are
stubborn and are unable to back up.) Using semaphores and/or mutex locks, design an algorithm in pseudocode
that prevents deadlock. Initially, do not be concerned about starvation (the situation in which northbound
farmers prevent southbound farmers from using the bridge, or vice versa).
7.31 Modify your solution to Exercise 8.30 so that it is starvation-free.
EX-31
7.32 Implement your solution to Exercise 8.30 using POSIX synchronization. In particular, represent northbound
and southbound farmers as separate threads. Once a farmer is on the bridge, the associated thread will sleep for a
random period of time, representing traveling across the bridge. Design your program so that you can create
several threads representing the northbound and southbound farmers. 8.33 In Figure 8.7, we illustrate a
transaction() function that dynamically acquires locks. In the text, we describe how this function presents
difficulties for acquiring locks in a way that avoids deadlock. Using the Java implementation of transaction() that is
provided in the source-code download for this text, modify it using the System. identityHashCode() method so
that the locks are acquired in order.
Chapter 8
8.1 Name two differences between logical and physical addresses.
A logical address does not refer to an actual existing address; rather, it refers to an abstract address in an abstract
address space. Contrast this with a physical address that refers to an actual physical address in memory. A logical
address is generated by the CPU and is translated into a physical address by the memory management unit(MMU).
Therefore, physical addresses are generated by the MMU.
8.2 Consider a system in which a program can be separated into two parts: code and data. The CPU knows
whether it wants an instruction (instruction fetch) or data (data fetch or store). Therefore, two base– limit register
pairs are provided: one for instructions and one for data. The instruction base–limit register pair is automatically
read-only, so programs can be shared among different users. Discuss the advantages and disadvantages of this
scheme.
The major advantage of this scheme is that it is an effective mechanism for code and data sharing. For example, only
one copy of an editor or a compiler needs to be kept in memory, and this code can be shared by all processes
needing access to the editor or compiler code. Another advantage is protection of code against erroneous
modification. The only disadvantage is that the code and data must be separated, which is usually adhered to in a
compiler-generated code.
8.3 Why are page sizes always powers of 2?
Recall that paging is implemented by breaking up an address into a page and offset number. It is most efficient to
break the address into X page bits and Y offset bits, rather than perform arithmetic on the address to calculate the
page number and offset. Because each bit position represents a power of 2, splitting an address between bits results
in a page size that is a power of 2.
8.4 Consider a logical address space of 64 pages of 1,024 words each, mapped onto a physical memory of 32
frames.
a. How many bits are there in the logical address?
b. How many bits are there in the physical address?
a. Logical address: 16 bits
b. Physical address: 15 bits
8.5 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?
By allowing two entries in a page table to point to the same page frame in memory, users can share code and data.
If the code is reentrant, much memory space can be saved through the shared use of large programs such as text
editors, compilers, and database systems. “Copying” large amounts of memory could be effected by having different
page tables point to the same memory location. However, sharing of nonreentrant code or data means that any user
having access to the code can modify it and these modifications would be reflected in the other user’s “copy.”
8.6 Describe a mechanism by which one segment could belong to the address space of two different processes.
Since segment tables are a collection of base–limit registers, segments can be shared when entries in the segment
table of two different jobs point to the same physical location. The two segment tables must have identical base
pointers, and the shared segment number must be the same in the two processes.
8.7 Sharing segments among processes without requiring that they have the same segment number is possible in
a dynamically linked segmentation system. a. Define a system that allows static linking and sharing of segments
without requiring that the segment numbers be the same. b. Describe a paging scheme that allows pages to be
shared without requiring that the page numbers be the same.
Both of these problems reduce to a program being able to reference both its own code and its data
without knowing the segment or page number associated with the address. MULTICS solved this problem by
associating four registers with each process. One register had the address of the current program segment, another
had a base address for the stack, another had a base address for the global data, and so on. The idea is 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 2-K
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 0. 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
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.
8.9 Explain the difference between internal and external fragmentation.
Internal fragmentation is the area occupied by a process but cannot be used by the process. This space is unusable
by the system until the process release the space. External fragmentation exists when total free memory is enough
for the new process but it's not contiguous and can't satisfy the request. Storage is fragmented into small holes.
8.10 Consider the following process for generating binaries. A compiler is used to generate the object code for
individual modules, and a linkage editor is used to combine multiple object modules into a single program binary.
How does the linkage editor change the binding of instructions and data to memory addresses? What information
needs to be passed from the compiler to the linkage editor to facilitate the memory-binding tasks of the linkage
editor?
The linkage editor has to replace unresolved symbolic addresses with the actual addresses associated with the
variables in the final program binary. In order to perform this, the modules should keep track of instructions that
refer to unresolved symbols. During linking, each module is assigned a sequence of addresses in the overall program
binary and when this has been performed, unresolved references to symbols exported by this binary could be
patched in other modules since every other module would contain the list of instructions that need to be patched.
8.11 Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB, and 125 KB (in order), how would the
first-fit, best-fit, and worst-fit algorithms place processes of size 115 KB, 500 KB, 358 KB, 200 KB, and 375 KB (in
order)? Rank the algorithms in terms of how efficiently they use memory.
8.12 Most systems allow a program to allocate more memory to its address space during execution. Allocation of
data in the heap segments of programs is an example of such allocated memory. What is required to support
dynamic memory allocation in the following schemes? a. Contiguous memory allocation b. Pure segmentation c.
Pure paging
a. contiguous-memory allocation: might require relocation of the entire program since there is not enough space for
the program to grow its allocated memory space. b. pure segmentation: might also require relocation of the
segment that needs to be extended since there is not enough space for the segment to grow its allocated memory
space. c. pure paging: incremental allocation of new pages is possible in this scheme without requiring relocation of
the program’s address space.
8.13 Compare the memory organization schemes of contiguous memory allocation, pure segmentation, and pure
paging with respect to the following issues: a. External fragmentation b. Internal fragmentation c. Ability to share
code across processes
The contiguous memory allocation scheme suffers from external fragmentation as address spaces are allocated
contiguously and holes develop as old processes die and new processes are initiated. It also does not allow processes
to share code, since a process’s virtual memory segment is not broken into noncontiguous fine grained segments.
Pure segmentation also suffers from external fragmentation as a segment of a process is laid out contiguously in
physical memory and fragmentation would occur as segments of dead processes are replaced by segments of new
processes. Segmentation, however, enables processes to share code; for instance, two different processes could
share a code segment but have distinct data segments. Pure paging does not suffer from external fragmentation, but
instead suffers from internal fragmentation. Processes are allocated in page granularity and if a page is not
completely utilized, it results in internal fragmentation and a corresponding wastage of space. Paging also enables
processes to share code at the granularity of pages.
8.14 On a system with paging, a process cannot access memory that it does not own. Why? How could the
operating system allow access to other memory? Why should it or should it not?
An address on a paging system is a logical page number and an offset. The physical page is found by searching a table
based on the logical page number to produce a physical page number. Because the operating system controls the
contents of this table, it can limit a process to accessing only those physical pages allocated to the process. There is
no way for a process to refer to a page it does not own because the page will not be in the page table. To allow such
access, an operating system simply needs to allow entries for non-process memory to be added to the process’s
page table. This is useful when two or more processes need to exchange data—they just read and write to the same
physical addresses (which may be at varying logical addresses). This makes for very efficient interprocess
communication.
8.15 Explain why mobile operating systems such as iOS and Android do not support swapping.
There are three reasons: First is that these mobile devices typically use flash memory with limited capacity and
swapping is avoided because of this space constraint. Second, flash memory can support a limited number of write
operations before it becomes less reliable. Lastly, there is typically poor throughput betweenmainmemory and flash
memory
8.16 Although Android does not support swapping on its boot disk, it is possible to set up a swap space using a
separate SD nonvolatile memory card. Why would Android disallow swapping on its boot disk yet allow it on a
secondary disk?
Primarily because Android does not wish for its boot disk to be used as swap space for the reasons outlined in the
previous question – the boot disk has limited storage capacity. However, Android does support swapping, it is just
that users must provide their own separate SD card for swap space.
8.17 Compare paging with segmentation with respect to how much memory the address translation structures
require to convert virtual addresses to physical addresses.
Paging requires more memory overhead to maintain the translation structures. Segmentation requires just two
registers per segment: one to maintain the base of the segment and the other to maintain the extent of the
segment. Paging on the other hand requires one entry per page, and this entry provides the physical address in
which the page is located.
8.18 Explain why address space identifiers (ASIDs) are used.
ASIDs provide address space protection in the TLB as well as supporting TLB entries for several different processes at
the same time.
8.19 Program binaries in many systems are typically structured as follows. Code is stored starting with a small,
fixed virtual address, such as 0. The code segment is followed by the data segment that is used for storing the
program variables. When the program starts executing, the stack is allocated at the other end of the virtual
address space and is allowed to grow toward lower virtual addresses. What is the significance of this structure for
the following schemes? a. Contiguous memory allocation b. Pure segmentation c. Pure paging
1) Contiguous-memory allocation requires the operating system to allocate the entire extent of the virtual address
space to the program when it starts executing. This could be much larger than the actual memory requirements of
the process. 2) Pure segmentation gives the operating system flexibility to assign a small extent to each segment at
program startup time and extend the segment if required. 3) Pure paging does not require the operating system to
allocate the maximum extent of the virtual address space to a process at startup time, but it still requires the
operating system to allocate a large page table spanning all of the program’s virtual address space. When a program
needs to extend the stack or the heap, it needs to allocate a new page but the corresponding page table entry is
preallocated.
8.20 Assuming a 1-KB page size, what are the page numbers and offsets for the following address references
(provided as decimal numbers): a. 3085 b. 42095 c. 215201 d. 650000 e. 2000001
a. page = 1; offset = 391 b. page = 18; offset = 934 c. page = 29; offset = 304 d. page = 0; offset = 256 e. page = 1;
offset = 1
8.21 The BTV operating system has a 21-bit virtual address, yet on certain embedded devices, it has only a 16-bit
physical address. It also has a 2-KB page size. How many entries are there in each of the following? a. A
conventional, single-level page table b. An inverted page table
Conventional, single-level page table will have 210 = 1024 entries. Inverted page table will have 25 = 32 entries.
8.22 What is the maximum amount of physical memory?
216 = 65536 (or 64-KB.)
8.23 Consider a logical address space of 256 pages with a 4-KB page size, mapped onto a physical memory of 64
frames. a. How many bits are required in the logical address? b. How many bits are required in the physical
address?
a. 2 5 = 32 + 21 0 = 1024 = 15 bits.
b. 2 4 = 32 + 21 0 = 1024 = 14 bits.
8.24 Consider a computer system with a 32-bit logical address and 4-KB page size. The system supports up to 512
MB of physical memory. How many entries are there in each of the following?
a. 2 20 entries.
b. 512 K K/4k = 128K entries.
8.25 Consider a paging system with the page table stored in memory. a. If a memory reference takes 50
nanoseconds, how long does a paged memory reference take? b. If we add TLBs, and 75 percent of all page-table
references are found in the TLBs, what is the effective memory reference time? (Assume that finding a page-table
entry in the TLBs takes 2 nanoseconds, if the entry is present.)
a. 400 nanoseconds: 200 nanoseconds to access the page table and 200 nanoseconds to access the word in memory.
b. Effective access time = 0.75 × (200 nanoseconds) + 0.25 × (400 nanoseconds) = 250 nanoseconds
8.26 Why are segmentation and paging sometimes combined into one scheme?
Segmentation and paging are often combined in order to improve upon each other. Segmented paging is helpful
when the page table becomes very large. A large contiguous section of the page table that is unused can be
collapsed into a single-segment table entry with a page-table address of zero. Paged segmentation handles the case
of having very long segments that require a lot of time for allocation. By paging the segments, we reduce wasted
memory due to external fragmentation as well as simplify the allocation.
8.27 Explain why sharing a reentrant module is easier when segmentation is used than when pure paging is used.
Since segmentation is based on a logical division of memory rather than a physical one, segments of any size can be
shared with only one entry in the segment tables of each user. With paging there must be a common entry in the
page tables for each page that is shared.
8.28 Consider the following segment table:
a. 219 + 430 = 649 b. 2300 + 10 = 2310 c. illegal reference, trap to operating system d. 1327 + 400 = 1727 e. illegal
reference, trap to operating system
8.29 What is the purpose of paging the page tables?
In certain situations the page tables could become large enough that by paging the page tables, one could simplify
the memory allocation problem (by ensuring that everything is allocated as fixed-size pages as opposed to variablesized chunks) and also enable the swapping of portions of page table that are not currently used.
8.30 Consider the hierarchical paging scheme used by the VAX architecture. How many memory operations are
performed when a user program executes a memory-load operation?
When a memory load operation is performed, there are three memory operations that might be performed. One is
to translate the position where the page table entry for the page could be found (since page tables themselves are
paged). The second access is to access the page table entry itself, while the third access is the actual memory load
operation.
8.31 Compare the segmented paging scheme with the hashed page table scheme for handling large address
spaces. Under what circumstances is one scheme preferable to the other?
When a program occupies only a small portion of its large virtual address space, a hashed page table might be
preferred due to its smaller size. The disadvantage with hashed page tables however is the problem that arises due
to conflicts in mapping multiple pages onto the same hashed page table entry. If many pages map to the same entry,
then traversing the list corresponding to that hash table entry could incur a significant overhead; such overheads are
minimal in the segmented paging scheme where each page table entry maintains information regarding only one
page
8.32 Consider the Intel address-translation scheme shown in Figure 8.22. a. Describe all the steps taken by the
Intel Pentium in translating a logical address into a physical address. b. What are the advantages to the operating
system of hardware that provides such complicated memory translation?
c. Are there any disadvantages to this address-translation system? If so, what are they? If not, why is this scheme
not used by every manufacturer?
a. The selector is an index into the segment descriptor table. The segment descriptor result plus the original offset is
used to produce a linear address with a dir, page, and offset. The dir is an index into a page directory. The entry
fromthe page directory selects the page table, and the page field is an index into the page table. The entry from the
page table, plus the offset, is the physical address. b. Such a page-translation mechanism offers the flexibility to
allow most operating systems to implement their memory scheme in hardware, instead of having to implement
some parts in hardware and some in software. Because it can be done in hardware, it is more efficient (and the
kernel is simpler). c. Address translation can take longer due to the multiple table lookups it can invoke. Caches help,
but there will still be cache misses.
Download