1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
-
Upload
cassandra-barton -
Category
Documents
-
view
226 -
download
0
Transcript of 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
![Page 1: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/1.jpg)
1
Processes, Threads, Race Conditions & Deadlocks
Operating Systems Review
![Page 2: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/2.jpg)
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.
![Page 3: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/3.jpg)
3
A thread is a lightweight process.
What is a thread?
![Page 4: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/4.jpg)
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.
![Page 5: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/5.jpg)
5
Process / Thread States
• Possible process states– running– blocked– ready
• Transitions between states shown
![Page 6: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/6.jpg)
6
Implementation of Processes (1)
Fields of a process table entry
![Page 7: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/7.jpg)
7
ThreadsThe Thread Model (1)
(a) Three processes each with one thread(b) One process with three threads
![Page 8: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/8.jpg)
8
The Thread Model (2)
• Items shared by all threads in a process
• Items private to each thread
![Page 9: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/9.jpg)
9
The Thread Model (3)
Each thread has its own stack
![Page 10: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/10.jpg)
10
Thread Usage (1)
A word processor with three threads
![Page 11: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/11.jpg)
11
Thread Usage (2)
A multithreaded Web server
![Page 12: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/12.jpg)
12
Implementing Threads in User Space
A user-level threads package
![Page 13: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/13.jpg)
13
Implementing Threads in the Kernel
A threads package managed by the kernel
![Page 14: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/14.jpg)
14
Hybrid Implementations
Multiplexing user-level threads onto kernel- level threads
![Page 15: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/15.jpg)
15
Interprocess CommunicationRace Conditions
A Race Condition may occur whenTwo or more processes want to access a shared resource at
same time and the result depends upon who runs when.
![Page 16: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/16.jpg)
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.
![Page 17: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/17.jpg)
17
Monitors (1)
Example of a monitor
![Page 18: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/18.jpg)
18
Monitors (2)
• Outline of producer-consumer problem with monitors– only one monitor procedure active at one time– buffer has N slots
![Page 19: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/19.jpg)
19
Monitors (3)
Solution to producer-consumer problem in Java (part 1)
![Page 20: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/20.jpg)
20
Monitors (4)
Solution to producer-consumer problem in Java (part 2)
Static class our_monitor { // this is a monitorprivate 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 sleepbuffer[hi] = val; // insert an item into the bufferhi = (hi + 1) % N; // slot to place next item incount = count + 1; // one more item in the buffer nowif (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 sleepval = buffer[lo]; // fetch an item from the bufferlo = (lo + 1) % N; // slot to fetch next item fromcount = count – 1; // one less item in the bufferif (count == N-1) notify(); // if producer was sleeping, wake it up.return val;
}
private void go_to_sleep() {try{wait();} catch(InterruptedExecption exc) {};
}}
![Page 21: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/21.jpg)
21
Message Passing
The producer-consumer problem with N messages
![Page 22: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/22.jpg)
22
Barriers
• Use of a barrier– processes approaching a barrier– all processes but one blocked at barrier– last process arrives, all are let through
![Page 23: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/23.jpg)
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
![Page 24: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/24.jpg)
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
![Page 25: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/25.jpg)
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
![Page 26: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/26.jpg)
26
Detection with One Resource of Each Type
• Note the resource ownership and requests• A cycle can be found within the graph, denoting
deadlock
![Page 27: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/27.jpg)
27
Strategies for dealing with Deadlocks1. just ignore the problem altogether
2. detection and recovery
3. dynamic avoidance • careful resource allocation
4. prevention • negating one of the four necessary conditions
![Page 28: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/28.jpg)
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
![Page 29: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/29.jpg)
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
![Page 30: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/30.jpg)
30
![Page 31: 1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.](https://reader030.fdocuments.net/reader030/viewer/2022032612/56649ed15503460f94bdfe92/html5/thumbnails/31.jpg)
31
Deadlock Prevention
Summary of approaches to deadlock prevention