answers_chapter_5_to_8

7

Click here to load reader

Upload: truptikodinariya9810

Post on 28-Mar-2015

282 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Answers_Chapter_5_to_8

1

Answers to Selected Exercises 2005/01/05 (Wed.)

Chapter 5 Thread

1. Provide two programming examples of multithreading that improve performance over a single-threaded solution.

(1) A Web server that services each request in a separate thread. (2) A parallelized application such as matrix multiplication where different parts of the matrix may be worked in parallel. (3) An interactive GUI program such as a debugger where a thread is used to monitor user input, another thread represents the running application, and a third thread monitors performance.

2. Provide two programming examples of multithreading that do not improve performance over

a single-threaded solution. (1) 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.

3. What are two differences between user-level threads and kernel-level threads? Under what

circumstances is one type better than the other? (1) Tow differences:

(a) User-level threads are unknown by the kernel, whereas the kernel is aware of kernel threads. (b) User-level threads are scheduled by the thread library and the kernel schedules kernel-level

threads. (2) Kernel-level threads are better than user-level threads when

(a) The thread performs a blocking system call (b) In a multiprocessor environment

(3) User-level threads are generally fast to create and manage; they are better than kernel-level threads when no blocking system call is performed.

Page 2: Answers_Chapter_5_to_8

Chapter 6 CPU Scheduling

1. Define the difference between preemptive and nonpreemptive scheduling. State why strict nonpreemptive scheduling is unlikely to be used in a computer center.

(1) Preemptive scheduling allows a process to be interrupted in the midst of its execution, taking the CPU away and allocating it to another process. Nonpreemptive scheduling ensures that a process relinquishes control of the CPU only when it finishes with its current CPU burst. (2) If nonpreemptive scheduling is used in a computer center, a process is capable of keeping other processes waiting for a long time.

2. Consider the following set of processes, with the length of the CPU-burst time given in

milliseconds: Process Burst Time Priority

P1 10 3 P2 1 1 P3 2 3 P4 1 4 P5 5 2

The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0. (a) Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF, a

nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum=1) scheduling.

(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 scheduling algorithms in part a? (d) Which of the schedules in part a results in the minimal average waiting time (over all

processes)?

(a) The four Gantt charts are

(b) Turnaround time 2

Page 3: Answers_Chapter_5_to_8

3

FCFS RR SJF Priority P1 10 19 19 16 P2 11 2 1 1 P3 13 7 4 18 P4 14 4 2 19 P5 19 14 9 6

(c) Waiting time

FCFS RR SJF Priority P1 0 9 9 6 P2 10 1 0 0 P3 11 5 2 16 P4 13 3 1 18 P5 14 9 4 1

(d) Shortest Job First

3. Suppose that the following processes arrive for execution at the times indicated. Each process

will run the listed amount of time. In answering the questions, use nonpreemptive scheduling and base all decisions on the information you have at the time the decision must be made.

Process Arrival Time Burst TimeP1 0.0 8 P2 0.4 4 P3 1.0 1

(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 there waiting time may increase. This algorithm could be known as future-knowledge scheduling.

(a) 10.53 (b) 9.53 (c) 6.86 Remember that turnaround time is finishing time minus arrival time, so you have to subtract the arrival times to compute the turnaround times. FCFS is 11 if you forget to subtract arrival time.

Page 4: Answers_Chapter_5_to_8

4

4. Consider the following 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. FCFS b. LIFO

5. Explain the differences in the degree to which the following scheduling algorithm discriminate

in favor of 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.

Page 5: Answers_Chapter_5_to_8

5

Chapter 7 Process Synchronization

1. Explain why spinlocks are not appropriate for uniprocessor systems yet may be suitable for multiprocessor systems.

In a uniprocessor system, if a thread of control uses a spinlock, and begins spinning on it because the lock is already held, the lock must be held by a process that is not executing. Spinning on the lock is thus a huge waste of time because the process spins through the loop continuously looking at a semaphore variable that cannot change value until the spinning process gives up the CPU and permits the process holding the lock to execute again, thus giving it a chance to finish using the lock and to release it. In a multiprocessor system, a process spins could be “unlocked” by the process that holds the lock running on another processor.

2. Explain the differences, in terms of speed, capacity, and resilience to failure, among the three

storage types: volatile, nonvolatile, and stable. a ) Speed: volatile > nonvolatile > stable

b ) Capacity: volatile < nonvolatile < stable c ) Resilience: volatile < nonvolatile < stable

3. Explain the concept of transaction atomicity. The concept of transaction atomicity means that the entire transaction is either successfully

committed or rolled back.

Page 6: Answers_Chapter_5_to_8

Chapter 8 Deadlocks

1. Is it possible to have a deadlock involving only one process? Explain your answer. No. This follows directly from the hold-and-wait condition. There must exist a process that is

holding a resource already allocated to it while waiting for additional resource that are currently being held by other processes.

2. Consider the traffic deadlock depicted in the following figure.

(a) Show that the four necessary conditions for deadlock indeed hold in this example. (b) State a simple rule that will avoid deadlocks in this system.

a) Consider each section of the street as a resource. i. Mutual exclusion condition applies, since only one vehicle can be on a section of the street

at a time. ii. Hold-and-wait condition applies, since each vehicle is occupying a section of the street,

and waiting to move on to the next section of the street. iii. No-preemptive condition applies, since a section of the street that is a section of the street

that is occupied by a vehicle cannot be taken away from it. iv. Circular wait condition applies, since each vehicle is waiting on the next vehicle to move.

That is, each vehicle in the traffic is waiting for a section of street held by the next vehicle in the traffic.

b) The simple rule to avoid traffic deadlock is that a vehicle should only enter an intersection if it is assured that it will not have to stop inside the intersection.

3. Suppose that a system is in an unsafe state. Show through an example that it is possible for the

processes to complete their execution without entering a deadlock state. No answer.

4. Can a system detect that some of its processes are starving? If you answer “yes”, explain how it

can. If you answer “no”, explain how the system can deal with the starvation problem. No answer.

6

Page 7: Answers_Chapter_5_to_8

7

5. Consider the following snapshot of a system:

Allocation Max AvailableABCD ABCD ABCD

P0 0 0 1 2 0 0 1 2 1 5 2 0 P1 1 0 0 0 1 7 5 0 P2 1 3 5 4 2 3 5 6 P3 0 6 3 2 0 6 5 2 P4 0 0 1 4 0 6 5 6

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?

a) P0 : 0 0 0 0, P1 : 0 7 5 0, P2 : 1 0 0 2, P3 : 0 0 2 0, P4 : 0 6 4 2 b) Yes. c) Yes.