580 likes | 586 Views
Lecture 7: Whirlwind tour of Threads, CPU Scheduling ... & Assignment 2. Joe McCarthy. Outline. Potentially helpful hints execlp( “cat”, “cat”, NULL ), testpipe3.cpp /usr/apps/CSS430/examples Selected excerpts from Chapter 4: Threads Chapter 5: CPU Scheduling Assignment 2 Next time:
E N D
Lecture 7:Whirlwind tour of Threads, CPU Scheduling... & Assignment 2 Joe McCarthy CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Outline • Potentially helpful hints • execlp( “cat”, “cat”, NULL ), testpipe3.cpp • /usr/apps/CSS430/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
Hint re: no output • Substituteexeclp( “cat”, “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
Hint re: hanging • testpipe3.cpp • uw1-320-lab: /usr/apps/CSS430/examples • Uses • fork(), pipe(), dup2() • echo, cat • Designed to • Echo it’s first argument through a pipe […]$ ./testpipe3 hello hello • … and then hang CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
/* 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 dup2( fd[0], 0 ); // copy read-end of pipe to STDIN close( fd[1] ); // close write-end of pipe (not needed) execlp( "cat", "cat", NULL ); } else { // this is the grand-child close( fd[0] ); // close read-end of pipe (not needed) dup2( fd[1], 1 ); // copy write-end of pipe to STDOUT execlp( "echo", "echo", argv[1], NULL ); } } CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Chapter 4: Threads [selected excerpts] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Single and Multithreaded Processes CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Multithreaded Server Architecture CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Thread Libraries • Thread libraryprovides 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
Java Thread States CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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.html • Implementing the Runnable interface • http://download.oracle.com/javase/6/docs/api/java/lang/Runnable.html public class MyShell extends Thread { … void run() { } } /usr/apps/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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 MessageQueue<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/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Java Threads - Producer-Consumer • Implementation of abstract data type:MessageQueue.java import java.util.Vector; public class MessageQueue<E> implements Channel<E> { private Vector<E> queue; public MessageQueue() { queue = new Vector<E>(); } public void send( E item ) { queue.addElement( 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/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Java Threads - Producer-Consumer /usr/apps/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Java Threads - Producer-Consumer /usr/apps/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Chapter 5: CPU Scheduling [selected excerpts] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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
CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
CPU Scheduling Multiprogramming [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
CPU Scheduling Process Control Blocks [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
CPU Scheduling Can all PCBs have access to CPU simultaneously? [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
CPU Queues [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Process States [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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
Histogram of CPU-burst Durations CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Alternating Sequence of CPU & I/O Bursts CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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
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
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
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
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
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
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
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
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
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
P1 P2 P3 0 24 27 30 First-Come, First-Served (FCFS) • Example: 3 processes with the following burst times: • P1= 24; P2= 3; P3= 3 • Suppose that the processes arrive in the order: P1, P2, P3The Gantt Chart for the schedule is: • Waiting times? • Average waiting time? CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
P1 P2 P3 0 24 27 30 First-Come, First-Served (FCFS) • Example: 3 processes with the following burst times: • P1= 24; P2= 3; P3= 3 • Suppose that the processes arrive in the order: P1, P2, P3The Gantt Chart for the schedule is: • Waiting times: P1= 0; P2= 24; P3= 27 • Average waiting time: (0 + 24 + 27) / 3 = 17.0 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order: P2 , P3 , P1 • The Gantt chart for the schedule is: • Waiting times? • Average waiting times? CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order: P2 , P3 , P1 • The Gantt chart for the schedule is: • Waiting times: P1 = 6;P2 = 0;; P3 = 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
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: • Preemptivecan interrupt current process, switch to new one • Nonpreemptivewait until current process finishes, then switch CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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
P1 P2 P3 P1 P1 P1 P1 P1 0 10 14 18 22 26 30 4 7 Example of RR with Time Quantum = 4 ProcessBurst Time P1 24 P2 3 P3 3 • The Gantt chart is: CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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
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