1 / 38

Operating Systems

Operating Systems. Operating Systems. Unit 4: Dining Philosophers Deadlock Indefinite postponement. Dining Philosophers. Dining Philosophers Example. monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // following slides

georgeshawn
Download Presentation

Operating Systems

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. Operating Systems Operating Systems Unit 4: Dining Philosophers Deadlock Indefinite postponement

  2. Dining Philosophers COP 5994 - Operating Systems

  3. Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // following slides void putdown(int i) // following slides void test(int i) // following slides void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } } COP 5994 - Operating Systems

  4. Dining Philosophers void pickup(int i) { state[i] = hungry; test(i); if (state[i] != eating) self[i].wait(); } COP 5994 - Operating Systems

  5. Dining Philosophers void putdown(int i) { state[i] = thinking; // test left and right neighbors test((i+4) % 5); test((i+1) % 5); } COP 5994 - Operating Systems

  6. Dining Philosophers void test(int i) { if ( (state[(i + 4) % 5] != eating) && (state[i] == hungry) && (state[(i + 1) % 5] != eating)) { state[i] = eating; self[i].signal(); } } COP 5994 - Operating Systems

  7. Java Monitors • enables thread mutual exclusion and synchronization • Signal-and-continue monitors • Allow a thread to signal that the monitor will soon become available • Maintain a lock on monitor until thread exits monitor • method keyword synchronized COP 5994 - Operating Systems

  8. Java Monitors • wait method • releases lock on monitor, thread is placed in wait set • condition variable is “this” object • when thread reenters monitor, reason for waiting may not be met • notify and notifyAll • signal waiting thread(s) Homework COP 5994 - Operating Systems

  9. Dining Philosopher with Semaphore ? semaphore chopstick[5]; // Initially set to 1 Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1); Deadlock ? COP 5994 - Operating Systems

  10. Deadlock COP 5994 - Operating Systems

  11. Simple Resource Deadlock COP 5994 - Operating Systems

  12. Related: Indefinite postponement • Also called indefinite blocking or starvation • Occurs due to biases in a system’s resource scheduling policies • Aging • Technique that prevents indefinite postponement by increasing process’s priority as it waits for resource COP 5994 - Operating Systems

  13. Concepts: sharing & preemption • some resources may be shared • files, devices, code, … • preemptible resources • e.g. processors and main memory • can be removed from a process without loss of work • non-preemptible resources • e.g. tape drives and optical scanners • cannot be removed from the process without loss of work COP 5994 - Operating Systems

  14. Conditions for Deadlock • Mutual exclusion • resource accessed by only one process at a time • Hold-and-wait-for • process holds resource(s) while waiting for other resource(s) • No-preemption • system cannot remove resource from the process until the process has finished using the resource • Circular-wait • processes are in a “circular chain” where each process is waiting for resource(s) of next process in chain COP 5994 - Operating Systems

  15. Dealing with Deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection • Deadlock recovery COP 5994 - Operating Systems

  16. Deadlock Prevention • Condition a system to remove any possibility of deadlocks occurring • avoid the four deadlock conditions • mutual exclusion cannot be avoided COP 5994 - Operating Systems

  17. Denying the “Wait-For” Condition • process must request all resources at once • inefficient resource allocation COP 5994 - Operating Systems

  18. Denying the “No-Preemption” Condition • preempt resource from process • loss of work • substantial overhead for process restart COP 5994 - Operating Systems

  19. Denying the “Circular-Wait” Condition • Use a linear ordering of resources • each resource gets unique number • process requests resources in ascending order • Drawbacks • Requires the programmer to determine the ordering or resources for each system COP 5994 - Operating Systems

  20. Dijkstra’s Banker’s Algorithm • deadlock avoidance • less stringent than deadlock prevention • control resource allocation • yields better resource utilization • idea: • grant resource to process in return for promise to release it in “finite time” • differentiate between safe and unsafe states • allocate resources to processes only if it results in safe state COP 5994 - Operating Systems

  21. Banker’s Algorithm: assumptions • fixed number of identical resources • fixed number of processes • max(Pi) : • maximum number of resources for Process Pi • loan(Pi ) : • current number of resources held by Process Pi • claim(Pi) : • max(Pi) - loan(Pi) COP 5994 - Operating Systems

  22. Example: Safe State • guarantees that all current processes can complete their work within a finite time • P2 may proceed to completion COP 5994 - Operating Systems

  23. Example: Unsafe State • no process can guarantee to complete COP 5994 - Operating Systems

  24. Safe-State-to-Unsafe-State Transition P3 requests an additional resource COP 5994 - Operating Systems

  25. Banker’s Algorithm example safe or unsafe ? unsafe COP 5994 - Operating Systems

  26. Weaknesses in the Banker’s Algorithm • Requires fixed number of resources • Requires fixed population of processes • only requires granting all requests within “finite time” • only requires that clients return resources within “finite time” • Requires processes to state maximum needs in advance COP 5994 - Operating Systems

  27. Deadlock Detection • problem: • has deadlock occurred ? • how to determine if deadlock has occurred • check for circular wait • can incur significant runtime overhead COP 5994 - Operating Systems

  28. Tool: Resource-Allocation Graph COP 5994 - Operating Systems

  29. Tool: Resource-Allocation Graph COP 5994 - Operating Systems

  30. Reduction of Resource-Allocation Graphs • Rules • If a process’s resource requests may be granted, the graph may be reduced by that process • If a graph can be reduced by all its processes, there is no deadlock • If a graph cannot be reduced by all its processes, the irreducible processes constitute the set of deadlocked processes in the graph COP 5994 - Operating Systems

  31. Example: Graph Reduction 1/4 COP 5994 - Operating Systems

  32. Example: Graph Reduction 2/4 COP 5994 - Operating Systems

  33. Example: Graph Reduction 3/4 COP 5994 - Operating Systems

  34. Example: Graph Reduction 4/4 no deadlock ! COP 5994 - Operating Systems

  35. Deadlock Recovery • Process termination • Abort all deadlocked processes • Abort one process at a time until the deadlock cycle is eliminated • considerations: • suspend/resume feature • checkpoint/rollback COP 5994 - Operating Systems

  36. Deadlock Recovery • In which order should we choose to abort? • Priority of the process • How long process has computed, and how much longer to completion • Resources the process has used • Resources process needs to complete • How many processes will need to be terminated • Is process interactive or batch? COP 5994 - Operating Systems

  37. Current Deadlock Strategies • Deadlock is viewed as limited annoyance in personal computer systems • Some systems implement basic prevention methods • Some others ignore the problem, because checking deadlocks would reduce systems’ performance • Deadlock continues to be an important research area COP 5994 - Operating Systems

  38. Agenda for next week: • Chapter 8 • Processor scheduling • Read ahead ! COP 5994 - Operating Systems

More Related