chapter 02
TRANSCRIPT
![Page 1: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/1.jpg)
1
Processes and Threads
Chapter 2
2.1 Processes2.2 Threads2.3 Interprocess communication2.4 Classical IPC problems2.5 Scheduling
![Page 2: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/2.jpg)
2
ProcessesThe Process Model
• Multiprogramming of four programs• Conceptual model of 4 independent, sequential processes• Only one program active at any instant
![Page 3: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/3.jpg)
3
Process Creation
Principal events that cause process creation
1. System initialization
2. Execution of a process creation system
3. User request to create a new process
4. Initiation of a batch job
![Page 4: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/4.jpg)
4
Process Termination
Conditions which terminate processes
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
![Page 5: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/5.jpg)
5
Process Hierarchies
• Parent creates a child process, child processes can create its own process
• Forms a hierarchy– UNIX calls this a "process group"
• Windows has no concept of process hierarchy– all processes are created equal
![Page 6: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/6.jpg)
6
Process States (1)
• Possible process states– running– blocked– ready
• Transitions between states shown
![Page 7: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/7.jpg)
7
Process States (2)
• Lowest layer of process-structured OS– handles interrupts, scheduling
• Above that layer are sequential processes
![Page 8: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/8.jpg)
8
Implementation of Processes (1)
Fields of a process table entry
![Page 9: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/9.jpg)
9
Implementation of Processes (2)
Skeleton of what lowest level of OS does when an interrupt occurs
![Page 10: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/10.jpg)
10
ThreadsThe Thread Model (1)
(a) Three processes each with one thread(b) One process with three threads
![Page 11: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/11.jpg)
11
The Thread Model (2)
• Items shared by all threads in a process
• Items private to each thread
![Page 12: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/12.jpg)
12
The Thread Model (3)
Each thread has its own stack
![Page 13: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/13.jpg)
13
Thread Usage (1)
A word processor with three threads
![Page 14: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/14.jpg)
14
Thread Usage (2)
A multithreaded Web server
![Page 15: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/15.jpg)
15
Thread Usage (3)
• Rough outline of code for previous slide(a) Dispatcher thread(b) Worker thread
![Page 16: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/16.jpg)
16
Thread Usage (4)
Three ways to construct a server
![Page 17: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/17.jpg)
17
Implementing Threads in User Space
A user-level threads package
![Page 18: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/18.jpg)
18
Implementing Threads in the Kernel
A threads package managed by the kernel
![Page 19: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/19.jpg)
19
Hybrid Implementations
Multiplexing user-level threads onto kernel- level threads
![Page 20: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/20.jpg)
20
Scheduler Activations
• Goal – mimic functionality of kernel threads– gain performance of user space threads
• Avoids unnecessary user/kernel transitions• Kernel assigns virtual processors to each process
– lets runtime system allocate threads to processors
• Problem: Fundamental reliance on kernel (lower layer)
calling procedures in user space (higher layer)
![Page 21: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/21.jpg)
21
Pop-Up Threads
• Creation of a new thread when message arrives(a) before message arrives(b) after message arrives
![Page 22: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/22.jpg)
22
Making Single-Threaded Code Multithreaded (1)
Conflicts between threads over the use of a global variable
![Page 23: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/23.jpg)
23
Making Single-Threaded Code Multithreaded (2)
Threads can have private global variables
![Page 24: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/24.jpg)
24
Interprocess CommunicationRace Conditions
Two processes want to access shared memory at same time
![Page 25: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/25.jpg)
25
Critical Regions (1)
Four conditions to provide mutual exclusion1. No two processes simultaneously in critical region
2. No assumptions made about speeds or numbers of CPUs
3. No process running outside its critical region may block another process
4. No process must wait forever to enter its critical region
![Page 26: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/26.jpg)
26
Critical Regions (2)
Mutual exclusion using critical regions
![Page 27: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/27.jpg)
27
Mutual Exclusion with Busy Waiting (1)
Proposed solution to critical region problem(a) Process 0. (b) Process 1.
![Page 28: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/28.jpg)
28
Mutual Exclusion with Busy Waiting (2)
Peterson's solution for achieving mutual exclusion
![Page 29: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/29.jpg)
29
Mutual Exclusion with Busy Waiting (3)
Entering and leaving a critical region using the
TSL instruction
![Page 30: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/30.jpg)
30
Sleep and Wakeup
Producer-consumer problem with fatal race condition
![Page 31: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/31.jpg)
31
Semaphores
The producer-consumer problem using semaphores
![Page 32: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/32.jpg)
32
Mutexes
Implementation of mutex_lock and mutex_unlock
![Page 33: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/33.jpg)
33
Monitors (1)
Example of a monitor
![Page 34: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/34.jpg)
34
Monitors (2)
• Outline of producer-consumer problem with monitors– only one monitor procedure active at one time– buffer has N slots
![Page 35: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/35.jpg)
35
Monitors (3)
Solution to producer-consumer problem in Java (part 1)
![Page 36: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/36.jpg)
36
Monitors (4)
Solution to producer-consumer problem in Java (part 2)
![Page 37: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/37.jpg)
37
Message Passing
The producer-consumer problem with N messages
![Page 38: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/38.jpg)
38
Barriers
• Use of a barrier– processes approaching a barrier– all processes but one blocked at barrier– last process arrives, all are let through
![Page 39: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/39.jpg)
39
Dining Philosophers (1)
• Philosophers eat/think• Eating needs 2 forks• Pick one fork at a time • How to prevent deadlock
![Page 40: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/40.jpg)
40
Dining Philosophers (2)
A nonsolution to the dining philosophers problem
![Page 41: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/41.jpg)
41
Dining Philosophers (3)
Solution to dining philosophers problem (part 1)
![Page 42: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/42.jpg)
42
Dining Philosophers (4)
Solution to dining philosophers problem (part 2)
![Page 43: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/43.jpg)
43
The Readers and Writers Problem
A solution to the readers and writers problem
![Page 44: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/44.jpg)
44
The Sleeping Barber Problem (1)
![Page 45: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/45.jpg)
45
The Sleeping Barber Problem (2)
Solution to sleeping barber problem.
![Page 46: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/46.jpg)
46
SchedulingIntroduction to Scheduling (1)
• Bursts of CPU usage alternate with periods of I/O wait– a CPU-bound process– an I/O bound process
![Page 47: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/47.jpg)
47
Introduction to Scheduling (2)
Scheduling Algorithm Goals
![Page 48: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/48.jpg)
48
Scheduling in Batch Systems (1)
An example of shortest job first scheduling
![Page 49: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/49.jpg)
49
Scheduling in Batch Systems (2)
Three level scheduling
![Page 50: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/50.jpg)
50
Scheduling in Interactive Systems (1)
• Round Robin Scheduling– list of runnable processes– list of runnable processes after B uses up its quantum
![Page 51: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/51.jpg)
51
Scheduling in Interactive Systems (2)
A scheduling algorithm with four priority classes
![Page 52: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/52.jpg)
52
Scheduling in Real-Time Systems
Schedulable real-time system
• Given– m periodic events
– event i occurs within period Pi and requires Ci seconds
• Then the load can only be handled if
1
1m
i
i i
C
P
![Page 53: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/53.jpg)
53
Policy versus Mechanism
• Separate what is allowed to be done with how it is done– a process knows which of its children threads
are important and need priority
• Scheduling algorithm parameterized– mechanism in the kernel
• Parameters filled in by user processes– policy set by user process
![Page 54: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/54.jpg)
54
Thread Scheduling (1)
Possible scheduling of user-level threads• 50-msec process quantum• threads run 5 msec/CPU burst
![Page 55: Chapter 02](https://reader036.vdocuments.us/reader036/viewer/2022062704/555ee4a2d8b42ab6408b52ae/html5/thumbnails/55.jpg)
55
Thread Scheduling (2)
Possible scheduling of kernel-level threads• 50-msec process quantum• threads run 5 msec/CPU burst