1 / 58

Lecture 7: Whirlwind tour of Threads, CPU Scheduling ... & Assignment 2

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:

brose
Download Presentation

Lecture 7: Whirlwind tour of Threads, CPU Scheduling ... & Assignment 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  3. 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

  4. 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

  5. /* 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

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

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

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

  9. 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

  10. 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

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

  12. 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

  13. 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

  14. 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

  15. 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

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

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

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

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

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

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

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

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

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

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

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

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

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

  29. 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

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

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

  32. 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

  33. 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

  34. 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 by the short-term scheduler • Three steps: CSS 430: Operating Systems - Threads, Scheduling, Assignment 2

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

More Related