Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.

Similar presentations


Presentation on theme: "1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review."— Presentation transcript:

1 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review

2 2 What is a process? A process is 1.a program in execution. 2.an abstraction of a running program. 3.a data structure that includes everything needed to manage a running program.

3 3 A thread is a lightweight process. What is a thread?

4 4 Process Hierarchies A process may create sub-processes, called a “child” process. The process that creates a child process is called the “parent” process.

5 5 Process / Thread States Possible process states –running –blocked –ready Transitions between states shown

6 6 Implementation of Processes (1) Fields of a process table entry

7 7 Threads The Thread Model (1) (a) Three processes each with one thread (b) One process with three threads

8 8 The Thread Model (2) Items shared by all threads in a process Items private to each thread

9 9 The Thread Model (3) Each thread has its own stack

10 10 Thread Usage (1) A word processor with three threads

11 11 Thread Usage (2) A multithreaded Web server

12 12 Implementing Threads in User Space A user-level threads package

13 13 Implementing Threads in the Kernel A threads package managed by the kernel

14 14 Hybrid Implementations Multiplexing user-level threads onto kernel- level threads

15 15 Interprocess Communication Race Conditions A Race Condition may occur when Two or more processes want to access a shared resource at same time and the result depends upon who runs when.

16 16 Two Atomic Functions (Semaphores) down(s)up(s) {if (s > 0){if (1 or more processes s--;waiting for s) elsepick_one_and_wake_it_up; go to sleep;else } s++; } These semaphore functions were proposed by E.W. Dijkstra in 1965. Def’n: An atomic action is an action that is guaranteed to be indivisible – that is, it cannot be interrupted until completed.

17 17 Monitors (1) Example of a monitor

18 18 Monitors (2) Outline of producer-consumer problem with monitors –only one monitor procedure active at one time –buffer has N slots

19 19 Monitors (3) Solution to producer-consumer problem in Java (part 1)

20 20 Monitors (4) Solution to producer-consumer problem in Java (part 2) Static class our_monitor {// this is a monitor private int buffer[] = new int[N]; private int count = 0, lo = 0, hi = 0;// counters and indices public synchronized void insert(int val) { if (count == N) go_to_sleep();// if the buffer is full, go to sleep buffer[hi] = val;// insert an item into the buffer hi = (hi + 1) % N;// slot to place next item in count = count + 1;// one more item in the buffer now if (count == 1) notify();// if consumer was sleeping, wake it up. } public synchronized int remove() { int val; if (count == 0) go_to_sleep();// if the buffer is empty, go to sleep val = buffer[lo];// fetch an item from the buffer lo = (lo + 1) % N;// slot to fetch next item from count = count – 1;// one less item in the buffer if (count == N-1) notify();// if producer was sleeping, wake it up. return val; } private void go_to_sleep() { try{wait();} catch(InterruptedExecption exc) {}; }

21 21 Message Passing The producer-consumer problem with N messages

22 22 Barriers Use of a barrier –processes approaching a barrier –all processes but one blocked at barrier –last process arrives, all are let through

23 23 Introduction to Deadlocks Formal definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause Usually the event is release of a currently held resource None of the processes can … –run –release resources –be awakened

24 Coffman, E.G., Elphick, M.J. and Shoshani, A. 24 Four Conditions for Deadlock 1. Mutual exclusion condition each resource assigned to 1 process or is available 2. Hold and wait condition process holding resources can request additional 3. No preemption condition previously granted resources cannot forcibly taken away 4. Circular wait condition must be a circular chain of 2 or more processes each is waiting for resource held by next member of the chain

25 Holt, R.C. 25 Deadlock Modeling (1) Modeled with directed graphs –resource R assigned to process A –process B is requesting/waiting for resource S –process C and D are in deadlock over resources T and U

26 26 Detection with One Resource of Each Type Note the resource ownership and requests A cycle can be found within the graph, denoting deadlock

27 27 Strategies for dealing with Deadlocks 1. just ignore the problem altogether 2. detection and recovery 3. dynamic avoidance careful resource allocation 4. prevention negating one of the four necessary conditions

28 28 The Ostrich Algorithm Pretend there is no problem Reasonable if –deadlocks occur very rarely –cost of prevention is high UNIX and Windows take this approach It is a trade off between –convenience –correctness

29 29 Recovery from Deadlock Recovery through killing processes –crudest but simplest way to break a deadlock –kill one of the processes in the deadlock cycle –the other processes get its resources –choose process that can be rerun from the beginning

30 30

31 31 Deadlock Prevention Summary of approaches to deadlock prevention


Download ppt "1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review."

Similar presentations


Ads by Google