Solutions

advertisement
6.1 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:
boolean flag[2]; /* initially false */
int turn;
The structure of process Pi (i == 0 or 1) is shown in Figure 6.25; the other process is Pj (j == 1 or 0).
Prove that the algorithm satisfies all three requirements for the critical-section problem.
Answer: 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 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 TT turn 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.
6.7 Describe how the Swap() instruction can be used to provide mutual exclusion that satisfies the
bounded-waiting requirement.
Answer:
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = Swap(&lock, &key);
waiting[i] = FALSE;
/* critical section */
j = (i+1) % n;
while ((j != i) && !waiting[j])
j = (j+1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
/* remainder section */
} while (TRUE);
6.23 Why do Solaris, Linux, and Windows 2000 use spinlocks as a synchronization mechanism only
on multiprocessor systems and not on single-processor systems?
Answer: Solaris, Linux, and Windows 2000 use spinlocks as a synchronization mechanism only on
multiprocessor systems. Spinlocks are not appropriate for single-processor systems because the
condition that would break a process out of the spinlock could 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.
7.5 In a real computer system, neither the resources available nor the demands of processes for
resources are consistent over long periods (months). Resources break or are replaced, new processes
come and go, 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 process (the process needs more resources than allowed, it may want more)
d. Decrease Max for one process (the process decides it does not need that many resources)
e. Increase the number of processes.
f. Decrease the number of processes.
Answer:
a. Increase Available (new resources added) - This could safely be changed without any problems.
b. Decrease Available (resource permanently removed from system)
- This could have an effect on the system and introduce the possibility of deadlock as the safety of the
system assumed there were a certain number of available resources.
c. Increase Max for one process (the process needs more resources than allowed, it may want more)
- This could have an effect on the system and introduce the possibility of deadlock.
d. Decrease Max for one process (the process decides it does not need that many resources)
- This could safely be changed without any problems.
e. Increase the number of processes - This could be allowed assuming that resources were allocated to
the new process(es) such that the system does not enter an unsafe state.
f. Decrease the number of processes - This could safely be changed without any problems.
7.6 Consider a system consisting of four resources of the same type that are shared by three processes,
each of which needs at most two resources. Show that the system is deadlock-free.
Answer: Suppose the system is deadlocked. This implies that each process is holding one resource
and is waiting for one more. Since there are three processes and four resources, one process must be
able to obtain two resources. This process requires no more resources and, therefore it will return its
resources when done.
7.11 Consider the following snapshot of a system:
Allocation
Max
Available
ABCD
ABCD
ABCD
P0
0012
0012
1520
P1
1000
1750
P2
1354
2356
P3
0632
0652
P4
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 process P1 arrives for (0,4,2,0), can the request be granted immediately?
Answer:
a. What is the content of the matrix Need? 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. Is the system 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. If a request from process P1 arrives for (0,4,2,0), can the request be granted immediately? Yes it
can. 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.
7.12 What is the optimistic assumption made in the deadlock-detection algorithm? How could this
assumption be violated?
Answer: 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 in practice.
8.3 Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600 KB (in order), how
would each of the first-fit, best-fit, and worst-fit algorithms place processes of 212 KB, 417 KB, 112
KB, and 426 KB (in order)?Which algorithm makes the most efficient use of memory?
Answer:
a. First-fit:
b. 212K is put in 500K partition
c. 417K is put in 600K partition
d. 112K is put in 288K partition (new partition 288K = 500K - 212K)
e. 426K must wait
f. Best-fit:
g. 212K is put in 300K partition
h. 417K is put in 500K partition
i. 112K is put in 200K partition
j. 426K is put in 600K partition
k. Worst-fit:
l. 212K is put in 600K partition
m. 417K is put in 500K partition
n. 112K is put in 388K partition
o. 426K must wait
In this example, Best-fit turns out to be the best.
8.5 Compare the main 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
Answer: Contiguous memory allocation scheme suffers from external fragmentation as address
spaces are allocated contiguously and holes develop as old processes dies 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 non-contiguous 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.7 Compare paging with segmentation with respect to the amount of memory required by the address
translation structures in order to convert virtual addresses to physical addresses.
Answer: 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.9 Consider a paging system with the page table stored in memory.
a. If a memory reference takes 200 nanoseconds, how long does a paged memory reference take?
b. If we add associative registers, and 75 percent of all page-table references are found in the
associative registers, what is the effective memory reference time? (Assume that finding a page-table
entry in the associative registers takes zero time, if the entry isthere.)
Answer:
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.12 Consider the following segment table:
Segment
Base Length
0
219
600
1
2300 14
2
90
100
3
1327 580
4
1952 96
What are the physical addresses for the following logical addresses?
a. 0,430
b. 1,10
c. 2,500
d. 3,400
e. 4,112
Answer:
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
Download