1 / 38

CS414 Review Session I

CS414 Review Session I. Rama. Today’s Agenda . Brief Overview of Syllabus Processes and Threads Process Scheduling Process Synchronization Deadlocks Example Questions and Solutions Question Time. Processes vs Threads. Processes . Code Segment Data Segments Resources

zulema
Download Presentation

CS414 Review Session I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS414 Review Session I Rama

  2. Today’s Agenda • Brief Overview of Syllabus • Processes and Threads • Process Scheduling • Process Synchronization • Deadlocks • Example Questions and Solutions • Question Time

  3. Processes vs Threads

  4. Processes • Code Segment • Data Segments • Resources • Files open, Devices open, System resources • Process Control Block • Process state, book-keeping, access rights

  5. Threads • Individual Threads • Stack • Instruction Pointer • Thread Control Block • Register image, thread state, priority info etc… • Shared with other threads of that process • Memory segments, Code segments • Resources

  6. The kernel is not aware of the existence of threads All thread management is done by the application, using a thread library Thread switching does not require kernel mode privileges Scheduling is application specific User level threads

  7. Advantages Thread switching does not involve the kernel: no mode switching Scheduling can be application specific: choose the best algorithm. ULTs can run on any OS. Only needs a thread library Inconveniences Most system calls are blocking and the kernel blocks processes. So all threads within the process will be blocked The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors Plus and Minus of ULTs

  8. All thread management is done by kernel No thread library but an API to the kernel thread facility Kernel maintains context information for the process and the threads Switching between threads requires the kernel Scheduling occurs on a thread basis, usually Ex: Windows NT and OS/2 Kernel Level Threads

  9. Advantages the kernel can simultaneously schedule many threads of the same process on many processors blocking is done on a thread level kernel routines can be multithreaded Inconveniences thread switching within the same process involves the kernel. We have 2 mode switches per thread switch this results in a significant slow down Plus and Minus of KLTs

  10. Process/Thread State Diagram

  11. Process/Thread Scheduling • Long Term Scheduling • Admission Control • I/O bound vs CPU bound • Medium Term Scheduling • Number of processes in memory (SWAPPER) • Short Term Scheduling • Select from ready processes (Dispatcher) • Pre-emption vs non-preemption

  12. Scheduling Metrics • Completion Time • Finish time of a process • Turn Around Time • Finish time – Start time • CPU bound jobs • Response Time • Time at which first response to user is given • I/O bound jobs • Throughput • Number of processes completed per unit time.

  13. Scheduling Policies • First Come First Serve • Shortest Job First (Shortest Remaining Time First) • Round Robin Scheduling • Priority Based Scheduling

  14. Multiple Feedback Queues Different RQs may have different quantum values

  15. Synchronization Primitives • Semaphores • Condition Variables • Monitors

  16. Semaphores (Operations) • Initialize counter (VERY IMPORTANT) • Wait or P or Down (blocks process) • Signal or V or Up (releases process) • BEWARE • Don’t assign values to count (S.count = -1) • Don’t read values of count (if S.count == -1) • Use only the above operations.

  17. Monitors • Shared Variables. • Only one process currently inside a monitor. • Block on a condition variable. • Wait( c ) • Another process releases the variable. • Signal( c )

  18. Synchronization Tips • Shared Variables • Always use mutex semaphores to access shared variables. • Or use monitors for shared variables. • Synchronization • Use semaphores or condition variables for synchronization • Don’t forget to initialize.

  19. How to implement Semaphores? • Hardware • Atomic instructions (tset, xchng) • Software • Enable/disable interrupts • Spinlocks (multi-processor systems)

  20. Things to avoid!!! • Race Conditions • NO SYNCHRONIZATION • Deadlocks • Busy Waiting • Starvation

  21. Conditions for deadlock • Mutual Exclusion • Hold and Wait • No preemption (of resources) • Circular Wait

  22. Resource Allocation Graph R1 R3 P1 is holding an instance of R2 and waiting for an instance of R1 P2 is holding an instance of R1 and R2 and waiting for an instance of R3 P3 is holding an instance of R3 R1 P1 P2 P3 R2 R4

  23. Example 1 (review question 1) Flight Reservation Algorithm (Ithaca, Miami, Dallas, San Diego, Seattle) • One server per city and one request per server at a time • Only decides about out going flights • Connecting flights => both legs confirmed • Eg: Mr. Mosse wants ticket from Ithaca to San Diego via Dallas • Server at Ithaca sends a request to server at dallas, waits for confirmed ticket before booking from ithaca to dallas.

  24. Example 1 contd… 1a) Solve the synchronization problem using semaphores. • Request should not be served until server is free • Server should not start until there is a request Write down procedures for client and server for booking tickets:

  25. Client Synch := 0 begin submit request V(synch) P(mutex) processing request end Server Mutex := 1 repeat P(synch) service request V(mutex) until false; Solution 1 (Wrong)

  26. Client mutex := 1 synch := 0 begin P(mutex) submit request; V(sync) process reply; end Server repeat P(sync) service request V(mutex) until false Solution 2

  27. Example 1 contd… 1b) Describe a deadlock scenario in this problem. Show that all 4 conditions hold. Solution: Mutual exclusion : only one request at a time Hold-Wait : Wait for a connection flight No Preemption : Got to wait for reply Circular Wait : A requests : Ithaca to San Diego via Dallas B requests : Dallas to Ithaca via San Diego C requests : San Diego to Dallas via Ithaca

  28. Example 1 (contd…) 1c) Give a strategy to remove deadlock. • Order Cities in alphabetical order. • Always process requests in alphabetical order • A requests from Dallas to San Diego before Ithaca to Dallas • B requests Dallas to San Diego before San Diego to Ithaca • RAG won’t work with 5 servers because a cycle in a RAG does not imply deadlock.

  29. Example 2 (review question 2) There are 3 processes Process Run Time per thread #threads P1 6 1 P2 3 2 P3 2 3

  30. Example 2 contd… a) Kernel Level Threads with preemptive round robin scheduling . How will the first 6 time units be scheduled? • 1 time unit per each of the 6 threads • 1 quantum for P1, 2 for P2 and 3 for P3 b) User Level Threads • 2 time units per process.

  31. Example 3 (review question 3) Communicating Monitors • A process executing a procedure in a monitor could call a procedure in some other monitor. • When a process waits to enter the other monitor, it is still considered active in the first monitor. • What is wrong in allowing this to happen? • Deadlock could arise: Process A in monitor M calls a procedure in monitor N where B is currently active. If B calls a procedure in monitor M. Then there is a deadlock.

  32. Example 4 Shower Room in a Co-Ed Dorm • There is a shower room in a co-ed dorm with plenty of showers (infinite!!!) • Both men and women come to the shower room to take a shower. • Give a synchronized procedure for both men and women to use the shower room subject to following conditions

  33. Example 4 contd… 1.  Any number of male students can use the showers at the same time 2. Any number of female students can use the showers at the same time 3. While male students are using the showers, female students wait. 4. While female students are using the showers, male students wait. 5. If any female student is waiting and a male student is using the showers, no additional male student can enter until all that female and any others who are waiting have gotten in 6. If any male student is waiting and a female student is using the showers, no additional female students can enter until that male and any others who are waiting have gotten in. 

  34. Solution What do we need to take care of? • Make women wait when men are taking bath • Make men wait when women are taking bath • Use synchronization semaphores for this What do need for book keeping • Number of men and women waiting or using showers • Shared variables need mutex semaphore.

  35. Solution to Example 4 var nm, nw, wm, wf: Integer := {0,0,0,0} var mwait, wwait, mutex: Semaphore := {0,0,1}; Nm => Number of men showering Nw => Number of women showering Wm => Number of men waiting Ww => Number of women waiting  

  36. Solution contd… MEnter: P(mutex); if(nw > 0 or ww > 0) { wm := wm+1; V(mutex); P(mwait); } else { nm :=nm+1; V(mutex); } MLeave: P(mutex); nm := nm-1; if(nm = 0 and ww > 0) { while(ww>0) { ww := ww-1; nw := nw+1; V(wwait); } } V(mutex);

  37. Solution contd… WEnter: P(mutex); if(nm > 0 or wm > 0) { ww := ww+1; V(mutex); P(wwait); } else { nw :=nw+1; V(mutex); } WLeave: P(mutex); nw := nw-1; if(nw = 0 and wm > 0) { while(wm>0) { wm := wm-1; nm := nm+1; V(mwait); } } V(mutex);

  38. Question Time Good Luck for the Prelim.

More Related