charles abzug, ph.d. - james madison university · pdf fileround-robin scheduling 5. priority...
Post on 28-Mar-2018
216 Views
Preview:
TRANSCRIPT
1
PROCESSES & THREADS
Charles Abzug, Ph.D.Department of Computer Science
James Madison UniversityHarrisonburg, VA 22807
Voice Phone: 540-568-8746; Cell Phone: 443-956-9424E-mail: abzugcx@JMU.edu OR CharlesAbzug@ACM.org
Home Page: https://users.cs.jmu.edu/abzugcx/public/index.htm
© 2008 Charles Abzug
10-Oct-2008 © 2008 Charles Abzug 2
Tanenbaum, Andrew S. (2008). Modern Operating Systems. Third Edition. Upper Saddle River, NJ: Prentice-Hall. ISBN: 0-13-031358-0.
CHAPTER 2: Processes and Threads
Sobell, Mark G. (2005). A Practical Guide to Linux Commands, Editors, and Shell Programming. Upper Saddle River, NJ: Prentice-Hall Professional Technical Reference. ISBN: 0-13-147823-0 (alk. paper).
CHAPTER 3:
2
10-Oct-2008 © 2008 Charles Abzug 3
CPU-Scheduling Algorithms (for Processes or Threads)
1. First-Come-First-Served
2. Shortest Job First
3. Shortest Remaining Time Next
4. Round-Robin Scheduling
5. Priority Scheduling
6. Priority Scheduling with Multiple Queues
7. Shortest Process Next
8. "Guaranteed" Scheduling
9. Lottery Scheduling
10. Fair-Share Scheduling
10-Oct-2008 © 2008 Charles Abzug 4
Part I: PROCESSES
3
10-Oct-2008 © 2008 Charles Abzug 5
Multiprogramming
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-1.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 6
Initiators of Process Creation
1. System Initialization (Boot-Up)
2. Explicit User-Initiation (e.g., issuance of a CLI command)
3. Existing Process via issuance of a Process-Creation System Call
4. Long-Term Scheduler, in accommodating a submitted batch job
4
10-Oct-2008 © 2008 Charles Abzug 7
Initiators of Process Termination
1. Completion of assigned task: normal termination (voluntary)
2. Exit upon encountering a specifically chosen error (voluntary)
3. Occurrence of a fatal error (involuntary)
4. Explicitly ordered by another (usually ancestral) process (involuntary)
10-Oct-2008 © 2008 Charles Abzug 8
Kinds of Processes
1. Foreground
2. Background
3. Daemon
5
10-Oct-2008 © 2008 Charles Abzug 9
Simple Model of Process States and Process Transitions
NOTE: Fourth state (not shown): Terminated
Causal events for transition:
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-2.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 10
Individual Processes, and theUnderlying OS for Interrupt-Handling & Scheduling
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-3.© 2008 Pearson Education
6
10-Oct-2008 © 2008 Charles Abzug 11
Contents of a Process Descriptor
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-4.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 12
Sequence of ActivitiesFollowing the Occurrence of an Interrupt
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-5.© 2008 Pearson Education
7
10-Oct-2008 © 2008 Charles Abzug 13
Degree of MultiprogrammingTanenbaum (2008). Modern
Operating Systems. 3rd Edition.Figure 2-6.
© 2008 Pearson Education
UCPU = 1 – p n
% CPU Utilization Depends onthe Degree of Multiprogramming
10-Oct-2008 © 2008 Charles Abzug 14
Part II: THREADS
8
10-Oct-2008 © 2008 Charles Abzug 15
Word Processor with Multiple Threads
1. Waits for and then handles input from keyboard.2. Reformats the document in background.3. Autosaves the document at the specified time interval, copying out the current
content onto the disk (foreground).4. Sends the document to the print SPOOLer.
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-7.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 16
Web Server with Multiple Threads
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-8.© 2008 Pearson Education
9
10-Oct-2008 © 2008 Charles Abzug 17
Web Server: Dispatcher Thread
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-9a.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 18
Web Server: Worker Thread
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-9b.© 2008 Pearson Education
10
10-Oct-2008 © 2008 Charles Abzug 19
Alternative Approaches to the Design of a Web Server
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-10.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 20
Single-Threaded and Multi-Threaded Processes
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-11.© 2008 Pearson Education
11
10-Oct-2008 © 2008 Charles Abzug 21
Items Shared by All the Threads of a Single Process,and Items Unique to Each Thread
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-12.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 22
Separate Stack for Each Thread
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-13.© 2008 Pearson Education
12
10-Oct-2008 © 2008 Charles Abzug 23
Function Calls for POSIX Threads (Pthreads)
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-14.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 24
Example of a Program that Uses Pthreads
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-15.© 2008 Pearson Education
13
10-Oct-2008 © 2008 Charles Abzug 25
User-Level Threads vs. Kernel-Level Threads
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-16.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 26
Multiplexing of Several User-Level ThreadsOnto a Single Kernel-Level Thread
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-17.© 2008 Pearson Education
14
10-Oct-2008 © 2008 Charles Abzug 27
Creation of a New Threadin Response to the Arrival of a Message
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-18.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 28
Inter-Thread Conflicts in the Use of a Global Variable
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-19.© 2008 Pearson Education
15
10-Oct-2008 © 2008 Charles Abzug 29
Private Global Variables
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-20.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 30
Race Conditions
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-21.© 2008 Pearson Education
16
10-Oct-2008 © 2008 Charles Abzug 31
Mutual Exclusion from a Critical Region
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-22.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 32
Use of a Spin Lock to Achieve Mutual Exclusion
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-23.© 2008 Pearson Education
Process 0: Process 1:
17
10-Oct-2008 © 2008 Charles Abzug 33
Peterson's Algorithm for Achieving Mutual Exclusion
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-24.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 34
Use of an Atomic "Test and Set Lock" (TSL)Machine Instruction to Achieve Mutual Exclusion
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-25.© 2008 Pearson Education
18
10-Oct-2008 © 2008 Charles Abzug 35
Entering and Leaving the Critical Region Using "XCHG"
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-26.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 36
Race Condition in the "Producer-Consumer Problem"
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-27.© 2008 Pearson Education
19
10-Oct-2008 © 2008 Charles Abzug 37
Use of Semaphores in the "Producer-Consumer Problem"
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-28.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 38
Use of mutex_lock and mutex_unlockto Achieve Mutual Exclusion
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-29.© 2008 Pearson Education
20
10-Oct-2008 © 2008 Charles Abzug 39
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-30.© 2008 Pearson Education
POSIX Threads: Procedure Calls for Mutual Exclusion
10-Oct-2008 © 2008 Charles Abzug 40
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-31.© 2008 Pearson Education
POSIX Threads: Procedure Calls Pertaining to Condition Variables
21
10-Oct-2008 © 2008 Charles Abzug 41
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-32a.© 2008 Pearson Education
POSIX Threads: Use of Mutexes and Condition Variables to Solve the Producer-Consumer Problem
10-Oct-2008 © 2008 Charles Abzug 42
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-32b.© 2008 Pearson Education
POSIX Threads: Use of Mutexes and Condition Variables to Solve the Producer-Consumer Problem
22
10-Oct-2008 © 2008 Charles Abzug 43
Use of the Monitor, a Programming-Language Construct,to Achieve Mutual Exclusion
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-33.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 44
Use of a Monitor to Effect Mutual Exclusionin the "Producer-Consumer Problem"
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-34.© 2008 Pearson Education
23
10-Oct-2008 © 2008 Charles Abzug 45
The "Producer-Consumer Problem: Solution in Java (1)
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-35"a".© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 46
The "Producer-Consumer Problem: Solution in Java (2)
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-35"b".© 2008 Pearson Education
24
10-Oct-2008 © 2008 Charles Abzug 47
Message-Passing Used to Solvethe "Producer-Consumer Problem:
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-36.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 48
Use of a Barrier to Enforcethe Synchronization of Multiple Processes
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-37.© 2008 Pearson Education
Last process hasreached the barrier.All processes maynow pass through.
All processes areapproaching the barrier.None has yet reached it.
Several processes havereached the barrierand are waiting until
all are present.
25
10-Oct-2008 © 2008 Charles Abzug 49
CPU-Bound and I/O-Bound Processes
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-38.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 50
Goals of the Scheduling Algorithm:Dependent upon the Computing Environment
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-39.© 2008 Pearson Education
26
10-Oct-2008 © 2008 Charles Abzug 51
Long-Term (Admission) Scheduler, Medium-Term (Memory) Scheduler, and Short-Term (CPU) Scheduler
NOTE: This figure did not come from our course text.
10-Oct-2008 © 2008 Charles Abzug 52
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
27
10-Oct-2008 © 2008 Charles Abzug 53
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
10-Oct-2008 © 2008 Charles Abzug 54
Effectiveness of "Shortest-Job First" Scheduling
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-40.© 2008 Pearson Education
Jobs run in order of arrival:First-Come-First-Served (FCFS)
Shortest Jobs run First (SJF)
28
10-Oct-2008 © 2008 Charles Abzug 55
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
10-Oct-2008 © 2008 Charles Abzug 56
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
4. Round-Robin Scheduling (with Time Quantum): Interactive, PREEMPTIVE
29
10-Oct-2008 © 2008 Charles Abzug 57
Round-Robin Scheduling
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-41.© 2008 Pearson Education
Job 'B' gets the processor:
Job 'B' uses up its quantum:
10-Oct-2008 © 2008 Charles Abzug 58
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
4. Round-Robin Scheduling (with Time Quantum): Interactive, PREEMPTIVE
5. Priority Scheduling (with Time Quantum): Interactive, PREEMPTIVE
30
10-Oct-2008 © 2008 Charles Abzug 59
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
4. Round-Robin Scheduling (with Time Quantum): Interactive, PREEMPTIVE
5. Priority Scheduling (with Time Quantum): Interactive, PREEMPTIVE
6. Priority Scheduling with Multiple Queues (with Time Quantum): Interactive,
10-Oct-2008 © 2008 Charles Abzug 60
Priority-Based Scheduling
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-42.© 2008 Pearson Education
31
10-Oct-2008 © 2008 Charles Abzug 61
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
4. Round-Robin Scheduling (with Time Quantum): Interactive, PREEMPTIVE
5. Priority Scheduling (with Time Quantum): Interactive, PREEMPTIVE
6. Priority Scheduling with Multiple Queues (with Time Quantum): Interactive,PREEMPTIVE
7. Shortest Process Next: Interactive, can be PREEMPTIVE or NON-Preemptive
10-Oct-2008 © 2008 Charles Abzug 62
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
4. Round-Robin Scheduling (with Time Quantum): Interactive, PREEMPTIVE
5. Priority Scheduling (with Time Quantum): Interactive, PREEMPTIVE
6. Priority Scheduling with Multiple Queues (with Time Quantum): Interactive,PREEMPTIVE
7. Shortest Process Next: Interactive, can be PREEMPTIVE or NON-Preemptive
8. "Guaranteed" Scheduling (better name: "Equitable" Scheduling); Interactive,
32
10-Oct-2008 © 2008 Charles Abzug 63
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
4. Round-Robin Scheduling (with Time Quantum): Interactive, PREEMPTIVE
5. Priority Scheduling (with Time Quantum): Interactive, PREEMPTIVE
6. Priority Scheduling with Multiple Queues (with Time Quantum): Interactive,PREEMPTIVE
7. Shortest Process Next: Interactive, can be PREEMPTIVE or NON-Preemptive
8. "Guaranteed" Scheduling (better name: "Equitable" Scheduling); Interactive,PREEMPTIVE
9. Lottery Scheduling (with Time Quantum): Interactive , PREEMPTIVE
10-Oct-2008 © 2008 Charles Abzug 64
CPU-Scheduling Algorithms1. First-Come-First-Served (FCFS): Batch Environment, NON-Preemptive
2. Shortest Job First (SJF): Batch Environment, NON-Preemptive
3. Shortest Remaining Time Next (SRT): Batch Environment, PREEMPTIVE
4. Round-Robin Scheduling (with Time Quantum): Interactive, PREEMPTIVE
5. Priority Scheduling (with Time Quantum): Interactive, PREEMPTIVE
6. Priority Scheduling with Multiple Queues (with Time Quantum): Interactive,PREEMPTIVE
7. Shortest Process Next: Interactive, can be PREEMPTIVE or NON-Preemptive
8. "Guaranteed" Scheduling (better name: "Equitable" Scheduling); Interactive,PREEMPTIVE
9. Lottery Scheduling (with Time Quantum): Interactive , PREEMPTIVE
10. Fair-Share Scheduling (with Time Quantum): Interactive, PREEMPTIVE
33
10-Oct-2008 © 2008 Charles Abzug 65
Scheduling of User-Level Threads
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-43a.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 66
Scheduling of Kernel-Level Threads
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-43b.© 2008 Pearson Education
34
10-Oct-2008 © 2008 Charles Abzug 67
The "Dining Philosophers Problem"
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-44.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 68
Deadlock or Starvationin the "Dining Philosophers Problem"
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-45.© 2008 Pearson Education
35
10-Oct-2008 © 2008 Charles Abzug 69
Use of Semaphores to Solvethe "Dining Philosophers Problem" (1)
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-46a.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 70
Use of Semaphores to Solvethe "Dining Philosophers Problem" (2)
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-46b.© 2008 Pearson Education
36
10-Oct-2008 © 2008 Charles Abzug 71
Use of Semaphores to Solvethe "Readers & Writers Problem"
Tanenbaum (2008). ModernOperating Systems. 3rd Edition.
Figure 2-47.© 2008 Pearson Education
10-Oct-2008 © 2008 Charles Abzug 72
END
top related