Lecture 7 Whirlwind tour of Threads CPU Scheduling

  • Slides: 58
Download presentation
Lecture 7: Whirlwind tour of Threads, CPU Scheduling. . . & Assignment 2 Joe

Lecture 7: Whirlwind tour of Threads, CPU Scheduling. . . & Assignment 2 Joe Mc. Carthy CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 1

Outline • Potentially helpful hints – execlp( “cat”, NULL ), testpipe 3. cpp –

Outline • Potentially helpful hints – execlp( “cat”, NULL ), testpipe 3. cpp – /usr/apps/CSS 430/examples • Selected excerpts from – Chapter 4: Threads – Chapter 5: CPU Scheduling • Assignment 2 • Next time: – Finish Chapters 4 & 5 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 2

Hint re: no output • Substitute execlp( “cat”, NULL ) for last stage of

Hint re: no output • Substitute execlp( “cat”, NULL ) for last stage of pipeline • If output comes through, problem is in that last stage • If output does not come through, substitute “cat” for next-to-last stage of pipeline • If output comes through, problem is in that stage • If no output comes through, problem is in first stage (or elsewhere) CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 3

Hint re: hanging • testpipe 3. cpp • uw 1 -320 -lab: /usr/apps/CSS 430/examples

Hint re: hanging • testpipe 3. cpp • uw 1 -320 -lab: /usr/apps/CSS 430/examples • Uses • fork(), pipe(), dup 2() • echo, cat • Designed to • Echo it’s first argument through a pipe […]$. /testpipe 3 hello • … and then hang CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 4

/* includes omitted from this excerpt */ int main( int argc, char** argv )

/* includes omitted from this excerpt */ int main( int argc, char** argv ) { int fd[2]; int pid; // only need one pid (why? ) assert(argc == 2); if ( pipe(fd) < 0 ) { perror( "pipe" ); exit( EXIT_FAILURE ); } else if ( ( pid = fork() ) < 0 ) { perror( "fork" ); exit( EXIT_FAILURE ); } else if ( pid > 0 ) { // this is the parent wait( NULL ); exit( EXIT_SUCCESS ); } else if ( ( pid = fork() ) < 0 ) { perror( "fork" ); exit( EXIT_FAILURE ); } else if ( pid > 0 ) { // this is [still] the child dup 2( fd[0], 0 ); // copy read-end of pipe to STDIN close( fd[1] ); // close write-end of pipe (not needed) execlp( "cat", NULL ); } else { // this is the grand-child close( fd[0] ); // close read-end of pipe (not needed) dup 2( fd[1], 1 ); // copy write-end of pipe to STDOUT execlp( "echo", argv[1], NULL ); } } CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 5

Chapter 4: Threads [selected excerpts] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2

Chapter 4: Threads [selected excerpts] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 6

Single and Multithreaded Processes CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 7

Single and Multithreaded Processes CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 7

Multithreaded Server Architecture CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 8

Multithreaded Server Architecture CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 8

User Threads • Thread management done by user-level threads library • Three primary thread

User Threads • Thread management done by user-level threads library • Three primary thread libraries: – POSIX Pthreads – Win 32 threads – Java threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 9

Thread Libraries • Thread library provides programmer with API for creating and managing threads

Thread Libraries • Thread library provides programmer with API for creating and managing threads • Two primary ways of implementing – Library entirely in user space – Kernel-level library supported by the OS CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 10

Java Thread States CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 11

Java Thread States CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 11

Java Threads • Java threads are managed by the JVM – 1 process, 1+

Java Threads • Java threads are managed by the JVM – 1 process, 1+ threads • Java threads may be created by: – Extending the Thread class • http: //download. oracle. com/javase/6/docs/api/java/lang/Thread. htm l – Implementing the Runnable interface • http: //download. oracle. com/javase/6/docs/api/java/lang/Runnable. h tml public class My. Shell extends Thread { … void run() { } } /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 12

Java Threads - Producer-Consumer • Main driver: Factory. java – Shared memory via queue

Java Threads - Producer-Consumer • Main driver: Factory. java – Shared memory via queue variable – Instantiates & starts 2 Threads import java. util. Date; public class Factory { public static void main( String[] args ) { // create the message queue Channel<Date> queue = new Message. Queue<Date>(); // create the producer and consumer threads Thread producer = new Thread( new Producer(queue) ); Thread consumer = new Thread( new Consumer(queue) ); // start the threads producer. start(); consumer. start(); } } /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 13

Java Threads - Producer-Consumer • Shared memory via abstract data type: Channel. java public

Java Threads - Producer-Consumer • Shared memory via abstract data type: Channel. java public interface Channel<E> { public void send(E item); public E receive(); } /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 14

Java Threads - Producer-Consumer • Implementation of abstract data type: Message. Queue. java import

Java Threads - Producer-Consumer • Implementation of abstract data type: Message. Queue. java import java. util. Vector; public class Message. Queue<E> implements Channel<E> { private Vector<E> queue; public Message. Queue() { queue = new Vector<E>(); } public void send( E item ) { queue. add. Element( item ); // add to tail (end) } public E receive() { if ( queue. size() == 0 ) return null; else // remove from head (front) return queue. remove( 0 ); } } /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 15

Java Threads - Producer-Consumer /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment

Java Threads - Producer-Consumer /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 16

Java Threads - Producer-Consumer /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment

Java Threads - Producer-Consumer /usr/apps/CSS 430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 17

Chapter 5: CPU Scheduling [selected excerpts] CSS 430: Operating Systems - Threads, Scheduling, Assignment

Chapter 5: CPU Scheduling [selected excerpts] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 18

CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2

CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 19

CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2

CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 20

CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment

CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 21

CPU Scheduling Multiprogramming [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling,

CPU Scheduling Multiprogramming [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 22

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 23

CPU Scheduling Process Control Blocks [from Chapter 3: Processes] CSS 430: Operating Systems -

CPU Scheduling Process Control Blocks [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 24

CPU Scheduling Can all PCBs have access to CPU simultaneously? [from Chapter 3: Processes]

CPU Scheduling Can all PCBs have access to CPU simultaneously? [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 25

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 26

CPU Queues [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment

CPU Queues [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 27

Process States [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment

Process States [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 28

Basic Concepts • Goal: Maximize CPU utilization via optimized multiprogramming • CPU–I/O Burst Cycle:

Basic Concepts • Goal: Maximize CPU utilization via optimized multiprogramming • CPU–I/O Burst Cycle: – Process execution consists of a cycle of • CPU execution • I/O wait • CPU burst durations vary – Within a process, across different processes CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 29

Histogram of CPU-burst Durations CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 30

Histogram of CPU-burst Durations CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 30

Alternating Sequence of CPU & I/O Bursts CSS 430: Operating Systems - Threads, Scheduling,

Alternating Sequence of CPU & I/O Bursts CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 31

CPU Scheduler • Selects next process to run (i. e. , to get CPU

CPU Scheduler • Selects next process to run (i. e. , to get CPU time) • Dispatches that process (allocates the CPU to it) • CPU scheduler called when a process … CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 32

CPU Scheduler • Selects next process to run (i. e. , to get CPU

CPU Scheduler • Selects next process to run (i. e. , to get CPU time) • Dispatches that process (allocates the CPU to it) • CPU scheduler called when a process: – Running terminated – Running waiting – Running ready – Waiting ready • Preemptive vs. non-preemptive CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 33

Dispatcher • Activates a process (gives control of the CPU to the process) selected

Dispatcher • Activates a process (gives control of the CPU to the process) selected by the short-term scheduler • Three steps: CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 34

Dispatcher • Activates a process (gives control of the CPU to the process) selected

Dispatcher • Activates a process (gives control of the CPU to the process) selected by the short-term scheduler • Three steps: CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 35

Dispatcher • Activates a process (gives control of the CPU to the process) selected

Dispatcher • Activates a process (gives control of the CPU to the process) selected by the short-term scheduler • Three steps: – Switch context – Switch to user mode – Jump to the proper location in user program to start/resume CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 36

Dispatcher • Activates a process (gives control of the CPU to the process) selected

Dispatcher • Activates a process (gives control of the CPU to the process) selected by the short-term scheduler • Three steps: – Switch context – Switch to user mode – Jump to the proper location in user program to start/resume • Dispatch latency: time it takes for the dispatcher to stop one user process and start another one CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 37

Scheduling Milestones • Submission Time – Time when job is submitted – E. g.

Scheduling Milestones • Submission Time – Time when job is submitted – E. g. , when command is entered in shell • Admission Time – Time when job is admitted to Ready queue – PCB creation & other housekeeping has to take place • Activation Time – Time when job is first activated – E. g. , when first output starts appearing on console – NB: may have several CPU-I/O burst cycles • Completion Time – Time when job finishes executing CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 38

Job (Process/Thread) Times • Response Time – Time before job is first activated –

Job (Process/Thread) Times • Response Time – Time before job is first activated – Activation Time – Submission Time – E. g. , time between entering command & first response • Wait Time – Total amount of time a job spends in Ready queue • Execution Time – Total amount of time a job spends Running – NB: may have several CPU-I/O burst cycles • Turnaround Time – Time a job takes to complete after it is submitted – Completion Time – Submission Time – Response Time + Wait Time + Execution Time + I/O Time CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 39

Scheduling Criteria • CPU utilization – % of time CPU is executing user processes

Scheduling Criteria • CPU utilization – % of time CPU is executing user processes • Throughput – # of processes that complete their execution per time unit • Average response time – average amount of between when a process is submitted & first response (for time-sharing environment) • Average waiting time – average amount of time a process waits in Ready queue • Average turnaround time – average amount of time to execute a process CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 40

Scheduling Optimization Criteria • CPU utilization: maximize – % of time CPU is executing

Scheduling Optimization Criteria • CPU utilization: maximize – % of time CPU is executing user processes • Throughput: maximize – # of processes that complete their execution per time unit • Average response time: minimize – average amount of between when a process is submitted & first response (for time-sharing environment) • Average waiting time: minimize – average amount of time a process waits in Ready queue • Average turnaround time: minimize – average amount of time to execute a process CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 41

First-Come, First-Served (FCFS) • Example: 3 processes with the following burst times: – P

First-Come, First-Served (FCFS) • Example: 3 processes with the following burst times: – P 1 = 24; P 2 = 3; P 3 = 3 • Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: P 1 0 P 2 24 P 3 27 30 • Waiting times? • Average waiting time? CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 42

First-Come, First-Served (FCFS) • Example: 3 processes with the following burst times: – P

First-Come, First-Served (FCFS) • Example: 3 processes with the following burst times: – P 1 = 24; P 2 = 3; P 3 = 3 • Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: P 1 0 P 2 24 P 3 27 30 • Waiting times: P 1 = 0; P 2 = 24; P 3 = 27 • Average waiting time: (0 + 24 + 27) / 3 = 17. 0 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 43

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order: P 2

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order: P 2 , P 3 , P 1 • The Gantt chart for the schedule is: P 2 0 P 3 3 P 1 6 30 • Waiting times? • Average waiting times? CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 44

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order: P 2

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order: P 2 , P 3 , P 1 • The Gantt chart for the schedule is: P 2 0 • • P 3 3 P 1 6 30 Waiting times: P 1 = 6; P 2 = 0; ; P 3 = 3 Average waiting time: (6 + 0 + 3) / 3 = 3. 0 Much better than previous case Convoy effect short process behind long process CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 45

Priority Scheduling • A priority number (integer) is associated with each process • The

Priority Scheduling • A priority number (integer) is associated with each process • The CPU is always allocated to the process with the highest priority • Two basic strategies: – Preemptive can interrupt current process, switch to new one – Nonpreemptive wait until current process finishes, then switch CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 46

Round Robin (RR) • Each process gets a time quantum (q) of CPU time,

Round Robin (RR) • Each process gets a time quantum (q) of CPU time, usually 10 -100 milliseconds – Selected from front of Ready queue (priority = queue size – process position) • If process does not block (I/O) within q ms: preempted – Added to the end of the Ready queue • General observations: – n processes in the Ready queue – each process gets 1/n of the CPU time (in slices of size q) – no process waits more than (n-1)q time units CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 47

Example of RR with Time Quantum = 4 Process P 1 P 2 P

Example of RR with Time Quantum = 4 Process P 1 P 2 P 3 Burst Time 24 3 3 • The Gantt chart is: P 1 0 P 2 4 P 3 7 P 1 10 P 1 14 P 1 18 22 P 1 26 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 P 1 30 48

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g.

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g. , foreground (interactive) & background (batch) • Each queue can have its own scheduling algorithm • Scheduling must be done between the queues • Simplest: Fixed priority scheduling – One possibility: 100% priority to foreground • Serve foreground if any; serve background only if no foreground • Problem? CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 49

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g.

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g. , foreground (interactive) & background (batch) • Each queue can have its own scheduling algorithm • Scheduling must be done between the queues • Simplest: Fixed priority scheduling – One possibility: 100% priority to foreground • Serve foreground if any; serve background only if no foreground • Problem: possibility of starvation – Another possibility? CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 50

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g.

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g. , foreground (interactive) & background (batch) • Each queue can have its own scheduling algorithm • Scheduling must be done between the queues • Simplest: Fixed priority scheduling – One possibility: 100% priority to foreground • Serve foreground if any; serve background only if no foreground • Problem: possibility of starvation. – Another possibility: 80/20 priority using time slices • 80% to foreground using RR • 20% to background using FCFS CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 51

Multilevel Queue Scheduling CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 52

Multilevel Queue Scheduling CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 52

Multilevel Feedback Queue • Process can migrate among queues – Queue placement based on

Multilevel Feedback Queue • Process can migrate among queues – Queue placement based on past behavior (feedback) • Multilevel feedback queue scheduler – Parameters? CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 53

Multilevel Feedback Queue • Process can migrate among queues – Queue placement based on

Multilevel Feedback Queue • Process can migrate among queues – Queue placement based on past behavior (feedback) • Multilevel feedback queue scheduler: – number of queues – scheduling algorithms for each queue – when to upgrade a process – when to demote a process – when to select a queue for service CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 54

Multilevel Feedback Queue CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 55

Multilevel Feedback Queue CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 55

Example of Multilevel Feedback Queue • Three queues: Q 0, Q 1, Q 2;

Example of Multilevel Feedback Queue • Three queues: Q 0, Q 1, Q 2; all use FCFS • If blocked for I/O, job returned to Q 0 when I/O done • New jobs enter queue Q 0 – When activated (given CPU), job receives 8 milliseconds – If not done, job is preempted, demoted to queue Q 1 • Jobs in Q 1 – When activated, job receives 16 additional milliseconds – If not done, job is preempted, demoted to Q 2 • Jobs in Q 2 – When activated, job runs until terminated or interrupted – If not done, job is preempted, returned to Q 2 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 56

Programming Assignment 2 Scheduler. java http: //courses. washington. edu/css 430/joemcc/prog 2 b. html CSS

Programming Assignment 2 Scheduler. java http: //courses. washington. edu/css 430/joemcc/prog 2 b. html CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 57

For next time • Readings – Chapter 4: Threads – Chapter 5: CPU Scheduling

For next time • Readings – Chapter 4: Threads – Chapter 5: CPU Scheduling CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 58