Download presentation
Presentation is loading. Please wait.
Published byLisa Gehrig Modified over 6 years ago
1
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Lecture 7: Whirlwind tour of Threads, CPU Scheduling ... & Assignment 2 Joe McCarthy CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
2
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
3
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Hint re: no output Substitute execlp( “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
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
5
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 > 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
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Chapter 4: Threads [selected excerpts] Material derived, in part, from Operating Systems Concepts with Java, 8th Ed. © 2009 Silberschatz, Galvin & Gagne 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
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
10
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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
11
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
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 Implementing the Runnable interface 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
Implementation of producer thread: Producer.java import java.util.Date; class Producer implements Runnable { private Channel<Date> queue; public Producer( Channel<Date> queue ) { this.queue = queue; } @SuppressWarnings("unchecked") public void run() { Date message; while ( true ) { SleepUtilities.nap(); message = new Date(); // produce an item System.out.println( "Producer produced " + message ); queue.send(message); // enter it into the buffer /usr/apps/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
17
Java Threads - Producer-Consumer
Implementation of consumer thread: Consumer.java import java.util.Date; class Consumer implements Runnable { private Channel<Date> queue; public Consumer( Channel<Date> queue ) { this.queue = queue; } @SuppressWarnings("unchecked") public void run() { Date message; while ( true ) { SleepUtilities.nap(); System.out.println( "Consumer wants to consume." ); message = queue.receive(); // consume an item from buffer if ( message != null ) System.out.println( "Consumer consumed " + message ); /usr/apps/CSS430/examples/threads CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
18
Chapter 5: CPU Scheduling
[selected excerpts] Material derived, in part, from Operating Systems Concepts with Java, 8th Ed. © 2009 Silberschatz, Galvin & Gagne CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
19
[from Chapter 1: Introduction]
CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
20
[from Chapter 1: Introduction]
CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
21
[from Chapter 1: Introduction]
CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
22
[from Chapter 1: Introduction]
CPU Scheduling Multiprogramming [from Chapter 1: Introduction] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
23
[from Chapter 3: Processes]
CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
24
[from Chapter 3: Processes]
CPU Scheduling Process Control Blocks [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
25
[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
26
[from Chapter 3: Processes]
CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
27
[from Chapter 3: Processes]
CPU Queues [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
28
[from Chapter 3: Processes]
Process States [from Chapter 3: Processes] CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
29
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
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
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
33
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
34
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
35
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
36
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
37
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
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 (Ready Running) 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 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
40
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
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
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, P3 The Gantt Chart for the schedule is: Waiting times? Average waiting time? P1 P2 P3 24 27 30 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
43
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, P3 The Gantt Chart for the schedule is: Waiting times: P1 = 0; P2 = 24; P3 = 27 Average waiting time: ( ) / 3 = 17.0 P1 P2 P3 24 27 30 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
44
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? P1 P3 P2 6 3 30 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
45
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: ( ) / 3 = 3.0 Much better than previous case Convoy effect short process behind long process P1 P3 P2 6 3 30 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
46
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: 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
47
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Round Robin (RR) Each process gets a time quantum (q) of CPU time, usually 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
Example of RR with Time Quantum = 4
Process Burst Time P1 24 P2 3 P3 3 The Gantt chart is: P1 P2 P3 4 7 10 14 18 22 26 30 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
49
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
50
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
51
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: 80/20 priority using time slices 80% to foreground using RR 20% to background using FCFS CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
52
Multilevel Queue Scheduling
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
53
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
54
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
55
Multilevel Feedback Queue
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
56
Example of Multilevel Feedback Queue
Three queues: Q0, Q1, Q2; all use FCFS If blocked for I/O, job returned to Q0 when I/O done New jobs enter queue Q0 When activated (given CPU), job receives 8 milliseconds If not done, job is preempted, demoted to queue Q1 Jobs in Q1 When activated, job receives 16 additional milliseconds If not done, job is preempted, demoted to Q2 Jobs in Q2 When activated, job runs until terminated or interrupted If not done, job is preempted, returned to Q2 CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
57
Programming Assignment 2
Scheduler.java CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
58
CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
For next time Readings Chapter 4: Threads Chapter 5: CPU Scheduling CSS 430: Operating Systems - Threads, Scheduling, Assignment 2
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.