U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.

Slides:



Advertisements
Similar presentations
CPU Scheduling Questions answered in this lecture: What is scheduling vs. allocation? What is preemptive vs. non-preemptive scheduling? What are FCFS,
Advertisements

Chapter 5 CPU Scheduling. CPU Scheduling Topics: Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
Chapter 3: CPU Scheduling
Cs238 CPU Scheduling Dr. Alan R. Davis.
Computer Science Lecture 6, page 1 CS677: Distributed OS Processes and Threads Processes and their scheduling Multiprocessor scheduling Threads Distributed.
Scheduling in Batch Systems
CS 3013 & CS 502 Summer 2006 Scheduling1 The art and science of allocating the CPU and other resources to processes.
OS Spring ’ 04 Scheduling Operating Systems Spring 2004.
CPU Scheduling. Schedulers Process migrates among several queues –Device queue, job queue, ready queue Scheduler selects a process to run from these queues.
02/04/2008CSCI 315 Operating Systems Design1 CPU Scheduling Algorithms Notice: The slides for this lecture have been largely based on those accompanying.
Cs238 CPU Scheduling Dr. Alan R. Davis. CPU Scheduling The objective of multiprogramming is to have some process running at all times, to maximize CPU.
Threads CSCI 444/544 Operating Systems Fall 2008.
CS 104 Introduction to Computer Science and Graphics Problems Operating Systems (2) Process Management 10/03/2008 Yang Song (Prepared by Yang Song and.
Wk 2 – Scheduling 1 CS502 Spring 2006 Scheduling The art and science of allocating the CPU and other resources to processes.
Job scheduling Queue discipline.
02/11/2004CSCI 315 Operating Systems Design1 CPU Scheduling Algorithms Notice: The slides for this lecture have been largely based on those accompanying.
1 Wednesday, June 14, 2006 "Beware of bugs in the above code; I have only proved it correct, not tried it." - Donald Knuth.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
1 Scheduling Algorithms FCFS First-Come, First-Served Round-robin SJF Multilevel Feedback Queues.
1Chapter 05, Fall 2008 CPU Scheduling The CPU scheduler (sometimes called the dispatcher or short-term scheduler): Selects a process from the ready queue.
CSS430 CPU Scheduling1 Textbook Ch5 These slides were compiled from the Applied OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s.
Threads & Scheduling What are threads?
CPU Scheduling Chapter 6 Chapter 6.
Chapter 6: CPU Scheduling
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-3 CPU Scheduling Department of Computer Science and Software Engineering.
CSC 360- Instructor: K. Wu CPU Scheduling. CSC 360- Instructor: K. Wu Agenda 1.What is CPU scheduling? 2.CPU burst distribution 3.CPU scheduler and dispatcher.
CS212: OPERATING SYSTEM Lecture 3: Process Scheduling 1.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
1 Scheduling Processes. 2 Processes Each process has state, that includes its text and data, procedure call stack, etc. This state resides in memory.
Scheduling Basic scheduling policies, for OS schedulers (threads, tasks, processes) or thread library schedulers Review of Context Switching overheads.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
Scheduling. Alternating Sequence of CPU And I/O Bursts.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
1 CSE451 Scheduling Autumn 2002 Gary Kimura Lecture #6 October 11, 2002.
CPU Scheduling CSCI 444/544 Operating Systems Fall 2008.
1 Scheduling The part of the OS that makes the choice of which process to run next is called the scheduler and the algorithm it uses is called the scheduling.
CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm.
Peng Lu. CPU Scheduling 1. Basic Concepts 2. Scheduling Criteria 3. Scheduling Algorithms 4. Multiple-Processor Scheduling 5. Real-Time Scheduling 2.
Process A program in execution. But, What does it mean to be “in execution”?
CE Operating Systems Lecture 7 Threads & Introduction to CPU Scheduling.
Lecture 7: Scheduling preemptive/non-preemptive scheduler CPU bursts
1 Uniprocessor Scheduling Chapter 9. 2 Aim of Scheduling Response time Throughput Processor efficiency.
Cpr E 308 Spring 2005 Process Scheduling Basic Question: Which process goes next? Personal Computers –Few processes, interactive, low response time Batch.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
ITFN 2601 Introduction to Operating Systems Lecture 4 Scheduling.
Department of Computer Science and Software Engineering
Uniprocessor Scheduling Chapter 9. Aim of Scheduling Assign processes to be executed by the processor or processors: –Response time –Throughput –Processor.
Distributed (Operating) Systems -Processes and Threads-
Processes & Threads Introduction to Operating Systems: Module 5.
Processor Scheduling Hank Levy. 22/4/2016 Goals for Multiprogramming In a multiprogramming system, we try to increase utilization and thruput by overlapping.
Lecture Topics: 11/15 CPU scheduling: –Scheduling goals and algorithms.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
Operating Systems Scheduling. Scheduling Short term scheduler (CPU Scheduler) –Whenever the CPU becomes idle, a process must be selected for execution.
1 Uniprocessor Scheduling Chapter 3. 2 Alternating Sequence of CPU And I/O Bursts.
Chapter 4 CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
Process Scheduling. Scheduling Strategies Scheduling strategies can broadly fall into two categories  Co-operative scheduling is where the currently.
CPU scheduling.  Single Process  one process at a time  Maximum CPU utilization obtained with multiprogramming  CPU idle :waiting time is wasted 2.
Lecturer 5: Process Scheduling Process Scheduling  Criteria & Objectives Types of Scheduling  Long term  Medium term  Short term CPU Scheduling Algorithms.
Scheduling.
CPU Scheduling Scheduling processes (or kernel-level threads) onto the cpu is one of the most important OS functions. The cpu is an expensive resource.
lecture 5: CPU Scheduling
Processes and Threads Processes and their scheduling
April 6, 2001 Gary Kimura Lecture #6 April 6, 2001
CPU scheduling decisions may take place when a process:
Processor Scheduling Hank Levy 1.
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture 5: Threads & Scheduling

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 2 Last Time: Processes Process = unit of execution Process control blocks Process state, scheduling info, etc. New, Ready, Waiting, Running, Terminated One at a time (on uniprocessor) Change by context switch Multiple processes: Communicate by message passing or shared memory

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 3 This Time: Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 4 Processes versus Threads Process = Control + address space + resources fork() Thread = Control only PC, stack, registers pthread_create() One process may contain many threads

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 5 Threads Diagram Address space in process: shared among threads Cheaper, faster communication than IPC

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 6 Threads Example, C/C++ POSIX threads standard #include void * run (void * d) { int q = ((int) d); int v = 0; for (int i = 0; i < q; i++) { v = v + expensiveComputation(i); } return (void *) v; } main() { pthread_t t1, t2; int r1, r2; pthread_create (&t1, run, 100); pthread_create (&t2, run, 100); pthread_wait (&t1, (void *) &r1); pthread_wait (&t2, (void *) &r2); printf (“r1 = %d, r2 = %d\n”, r1, r2); }

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 7 Threads Example, Java import java.lang.*; class Worker extends Thread implements Runnable { public Worker (int q) { this.q = q; this.v = 0; } public void run() { int i; for (i = 0; i < q; i++) { v = v + i; } } public int v; private int q; } public class Example { public static void main(String args[]) { Worker t1 = new Worker (100); Worker t2 = new Worker (100); try { t1.start(); t2.start(); t1.join(); t2.join(); } catch (InterruptedException e) {} System.out.println ("r1 = " + t1.v + ", r2 = " + t2.v); } }

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 8 Classifying Threaded Systems One or many address spaces, one or many threads per address space MS-DOS

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 9 Classifying Threaded Systems One or many address spaces, one or many threads per address space MS-DOS Embedded systems

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 10 Classifying Threaded Systems One or many address spaces, one or many threads per address space UNIX, Ultrix, MacOS (< X), Win95 Embedded systems MS-DOS

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 11 Classifying Threaded Systems One or many address spaces, one or many threads per address space UNIX, Ultrix, MacOS (< X), Win95 Mach, Linux, Solaris, WinNT MS-DOS Embedded systems

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 12 This Time: Threads What are threads? vs. processes Where does OS implement threads? User-level, kernel How does CPU schedule threads?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 13 Kernel Threads Kernel threads: scheduled by OS A.k.a. lightweight process (LWPs) Switching threads requires context switch PC, registers, stack pointers BUT: no mem mgmt. = no TLB “shootdown”  Switching faster than for processes Hide latency (don’t block on I/O) Can be scheduled on multiple processors

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 14 User-Level Threads No OS involvement w/user-level threads Only knows about process containing threads Use thread library to manage threads Creation, synchronization, scheduling Example: Java green threads Cannot be scheduled on multiple processors

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 15 User-Level Threads: Advantages No context switch when switching threads But… Flexible: Allow problem-specific thread scheduling policy Computations first, service I/O second, etc. Each process can use different scheduling algorithm No system calls for creation, context switching, synchronization  Can be much faster than kernel threads

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 16 User-Level Threads: Disadvantages Requires cooperative threads Must yield when done working (no quanta) Uncooperative thread can take over OS knows about processes, not threads: Thread blocks on I/O: whole process stops More threads ≠ more CPU time Process gets same time as always Can’t take advantage of multiple processors

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 17 Solaris Threads Hybrid model: User-level threads mapped onto LWPs

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 18 Threads Roundup User-level threads Cheap, simple Not scheduled, blocks on I/O, single CPU Requires cooperative threads Kernel-level threads Involves OS – time-slicing (quanta) More expensive context switch, synch Doesn’t block on I/O, can use multiple CPUs Hybrid “Best of both worlds”, but requires load balancing

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 19 Load Balancing Spread user-level threads across LWPs so each processor does same amount of work Solaris scheduler: only adjusts load when I/O blocks thread scheduler kernel thread scheduler threads processes processors

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 20 Load Balancing Two classic approaches: work sharing & work stealing Work sharing: give excess work away Can waste time

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 21 Load Balancing Two classic approaches: work sharing & work stealing Work stealing: get threads from someone else Optimal approach Sun, IBM Java runtime but what about OS?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 22 This Time: Threads What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 23 Scheduling Overview Metrics Long-term vs. short-term Interactive vs. servers Example algorithm: FCFS

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 24 Scheduling Multiprocessing: run multiple processes Improves system utilization & throughput Overlaps I/O and CPU activities

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 25 Scheduling Processes Long-term scheduling: How does OS determine degree of multiprogramming? Number of jobs executing at once Short-term scheduling: How does OS select program from ready queue to execute? Policy goals Policy options Implementation considerations

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 26 Short-Term Scheduling Kernel runs scheduler at least: When process switches from running to waiting On interrupts When processes are created or terminated Non-preemptive system: Scheduler must wait for these events Preemptive system: Scheduler may interrupt running process

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 27 Comparing Scheduling Algorithms Important metrics: Utilization = % of time that CPU is busy Throughput = processes completing / time Response time = time between ready & next I/O Waiting time = time process spends on ready queue

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 28 Scheduling Issues Ideally: Maximize CPU utilization, throughput & minimize waiting time, response time Conflicting goals Cannot optimize all criteria simultaneously  Must choose according to system type Interactive systems Servers

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 29 Scheduling: Interactive Systems Goals for interactive systems: Minimize average response time Time between waiting & next I/O Provide output to user as quickly as possible Process input as soon as received Minimize variance of response time Predictability often important Higher average better than low average, high variance

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 30 Scheduling: Servers Goals different than for interactive systems Maximize throughput (jobs done / time) Minimize OS overhead, context switching Make efficient use of CPU, I/O devices Minimize waiting time Give each process same time on CPU May increase average response time

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 31 Scheduling Algorithms Roundup FCFS: First-Come, First-Served Round-robin: Use quantum & preemption to alternate jobs SJF: Shortest job first Multilevel Feedback Queues: Round robin on each priority queue Lottery Scheduling: Jobs get tickets Scheduler randomly picks winner

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 32 Scheduling Policies FCFS (a.k.a., FIFO = First-In, First-Out) Scheduler executes jobs to completion in arrival order Early version: jobs did not relinquish CPU even for I/O Assume: Runs when processes blocked on I/O Non-preemptive

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 33 FCFS Scheduling: Example Processes arrive 1 time unit apart: average wait time in these three cases?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 34 FCFS: Advantages & Disadvantages + Advantage: Simple - Disadvantages: - Average wait time highly variable  Short jobs may wait behind long jobs - May lead to poor overlap of I/O & CPU  CPU-bound processes force I/O-bound processes to wait for CPU  I/O devices remain idle

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 35 Summary Thread = single execution stream within process User-level, kernel-level, hybrid No perfect scheduling algorithm Selection = policy decision Base on processes being run & goals Minimize response time Maximize throughput etc. Next time: much more on scheduling