解題 - 5.4 Which of the following scheduling algorithms...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5.4 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. 5.5 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 millisecnd 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. What is the CPU utilization for a round-robin scheduler when: a. The time quantum is 1 millisecond b. The time quantum is 10 milliseconds • The time quantum is 1 millisecond: Irrespective of which process is scheduled, the scheduler incurs a 0.1 millisecond context-switching cost for every contextswitch. This results in a CPU utilization of 1/1.1 * 100 = 91%. • 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.2 Explain why spinlocks are not appropriate for single-processor systems yet are often used in multiprocessor systems. ANS: 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. 6.3 Explain why implementing synchronization primitives by disabling interrupts is not appropr- iate in a single-processor system if the synchr- onization primitives are to be used in user-level programs. If a user-level program is given the ability to disable interrupts, then it can disable the timer interrupt and prevent context switching from tak- ing place, thereby allowing it to use the proces- sor without letting other processes to execute. 6.7 Show how to implement the wait() and signal() semap- hore operations in multiprocessor environments using the TestAndSet() instruction. The solution should exhibit minimal busy waiting. 1. 2. 3. 4. 5. int guard = 0; int semaphore value = 0; wait() { while (TestAndSet(&guard) == 1); if (semaphore value == 0) { atomically add process to a queue of processes 6. waiting for the semaphore and set guard to 0; 7. } else 8. { semaphore value--; 9. guard = 0;}} 10.signal() 11. { while (TestAndSet(&guard) == 1); 12. if (semaphore value == 0 && 13. there is a process on the wait queue) 14. wake up the first process in the queue 15. of waiting processes 16. else 17. semaphore value++; 18. guard = 0; 19. } 6.8 The Sleeping-Barber Problem. A barbershop consists of awaiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers. customer : semaphore = 0 services waiting : int = 0 barber : int = 0 mutex : semaphore = 1 Barber : repeat wait(customer); wait(mutex); waiting = waiting – 1; signal(barber); signal(mutex); ….. cut hair(); ….. until false // # of the customers waiting for // # of waiting customers // # of the barber waiting customer // for waiting mutual exclusion Customer : wait(mutex); if(waiting < n) then begin waiting = waiting + 1; signal(customer); signal(mutex); wait(barber); get-haircut(); end else signal(mutex); 6.13 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. monitor file access { int curr sum = 0; int n; condition c; void access file(int my num) { while (curr sum + my num >= n) c.wait(); curr sum += my num; } void finish access(int my num) { curr sum -= my num; c.broadcast(); } } ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern