Operating Systems CMPSCI 377 Lecture 5 Threads Scheduling



































- Slides: 35
Operating Systems CMPSCI 377 Lecture 5: Threads & Scheduling Emery Berger University of Massachusetts Amherst UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
Last Time: Processes n Process = unit of execution n Process control blocks n n n One at a time (on uniprocessor) n n Process state, scheduling info, etc. New, Ready, Waiting, Running, Terminated Change by context switch Multiple processes: n Communicate by message passing or shared memory UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 2
This Time: Threads & Scheduling n What are threads? n n Where does OS implement threads? n n vs. processes User-level, kernel How does OS schedule threads? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 3
Processes versus Threads n Process = n n n Control + address space + resources fork() Thread = n Control only n n n PC, stack, registers pthread_create() One process may contain many threads UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 4
Threads Diagram n Address space in process: shared among threads n Cheaper, faster communication than IPC UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 5
Threads Example, C/C++ n POSIX threads standard #include <pthread. h> #include <stdio. h> void * run (void * d) { int q = ((int) d); int v = 0; for (int i = 0; i < q; i++) { v = v + expensive. Computation(i); } return (void *) v; } main() { pthread_t t 1, t 2; int r 1, r 2; pthread_create (&t 1, run, 100); pthread_create (&t 2, run, 100); pthread_wait (&t 1, (void *) &r 1); pthread_wait (&t 2, (void *) &r 2); printf (“r 1 = %d, r 2 = %dn”, r 1, r 2); } UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 6
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 t 1 = new Worker (100); Worker t 2 = new Worker (100); try { t 1. start(); t 2. start(); t 1. join(); t 2. join(); } catch (Interrupted. Exception e) {} System. out. println ("r 1 = " + t 1. v + ", r 2 = " + t 2. v); } } UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 7
Classifying Threaded Systems n One or many address spaces, one or many threads per address space MS-DOS UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 8
Classifying Threaded Systems n One or many address spaces, one or many threads per address space MS-DOS Embedded systems UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 9
Classifying Threaded Systems n One or many address spaces, one or many threads per address space MS-DOS UNIX, Ultrix, Mac. OS (< X), Win 95 Embedded systems UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 10
Classifying Threaded Systems n One or many address spaces, one or many threads per address space MS-DOS Embedded systems UNIX, Ultrix, Mac. OS (< X), Win 95 Mach, Linux, Solaris, Win. NT UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 11
This Time: Threads n What are threads? n n Where does OS implement threads? n n vs. processes User-level, kernel How does CPU schedule threads? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 12
Kernel Threads n Kernel threads: scheduled by OS n n A. k. a. lightweight process (LWPs) Switching threads requires context switch n n PC, registers, stack pointers BUT: no mem mgmt. = no TLB “shootdown” ð Switching n n faster than for processes Hide latency (don’t block on I/O) Can be scheduled on multiple processors UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 13
User-Level Threads n No OS involvement w/user-level threads n n Only knows about process containing threads Use thread library to manage threads n n n Creation, synchronization, scheduling Example: Java green threads Cannot be scheduled on multiple processors UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 14
User-Level Threads: Advantages n No context switch when switching threads n n But… Flexible: n Allow problem-specific thread scheduling policy n n n 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 UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 15
User-Level Threads: Disadvantages n Requires cooperative threads n n n Must yield when done working (no quanta) Uncooperative thread can take over OS knows about processes, not threads: n n Thread blocks on I/O: whole process stops More threads ≠ more CPU time n n Process gets same time as always Can’t take advantage of multiple processors UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 16
Solaris Threads n Hybrid model: n User-level threads mapped onto LWPs UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 17
Threads Roundup n User-level threads n n Kernel-level threads n n Cheap, simple Not scheduled, blocks on I/O, single CPU Requires cooperative threads Involves OS – time-slicing (quanta) More expensive context switch, synch Doesn’t block on I/O, can use multiple CPUs Hybrid n “Best of both worlds”, but requires load balancing UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 18
Load Balancing n Spread user-level threads across LWPs so each processor does same amount of work n Solaris scheduler: only adjusts load when I/O blocks thread scheduler threads processes kernel processors UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 19
Load Balancing n n Two classic approaches: work sharing & work stealing Work sharing: give excess work away n Can waste time UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 20
Load Balancing n n Two classic approaches: work sharing & work stealing Work stealing: get threads from someone else n n Optimal approach Sun, IBM Java runtime n but what about OS? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 21
This Time: Threads n What are threads? n n Where does OS implement threads? n n vs. processes User-level, kernel How does OS schedule threads? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 22
Scheduling n n n Overview Metrics Long-term vs. short-term Interactive vs. servers Example algorithm: FCFS UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 23
Scheduling n Multiprocessing: run multiple processes n n Improves system utilization & throughput Overlaps I/O and CPU activities UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 24
Scheduling Processes n Long-term scheduling: n How does OS determine degree of multiprogramming? n n Number of jobs executing at once Short-term scheduling: n How does OS select program from ready queue to execute? n n n Policy goals Policy options Implementation considerations UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 25
Short-Term Scheduling n Kernel runs scheduler at least: n n Non-preemptive system: n n When process switches from running to waiting On interrupts When processes are created or terminated Scheduler must wait for these events Preemptive system: n Scheduler may interrupt running process UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 26
Comparing Scheduling Algorithms n Important metrics: n n 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 UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 27
Scheduling Issues n Ideally: n n Conflicting goals n ð Maximize CPU utilization, throughput & minimize waiting time, response time Cannot optimize all criteria simultaneously Must choose according to system type n n Interactive systems Servers UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 28
Scheduling: Interactive Systems n Goals for interactive systems: n Minimize average response time n n 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 n n Predictability often important Higher average better than low average, high variance UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 29
Scheduling: Servers n Goals different than for interactive systems n Maximize throughput (jobs done / time) n n Minimize OS overhead, context switching n Make efficient use of CPU, I/O devices Minimize waiting time n n Give each process same time on CPU May increase average response time UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 30
Scheduling Algorithms Roundup n FCFS: n n Round-robin: n n Shortest job first Multilevel Feedback Queues: n n Use quantum & preemption to alternate jobs SJF: n n First-Come, First-Served Round robin on each priority queue Lottery Scheduling: n n Jobs get tickets Scheduler randomly picks winner UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 31
Scheduling Policies FCFS (a. k. a. , FIFO = First-In, First-Out) n Scheduler executes jobs to completion in arrival order n Early version: jobs did not relinquish CPU even for I/O n Assume: n n Runs when processes blocked on I/O Non-preemptive UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 32
FCFS Scheduling: Example n Processes arrive 1 time unit apart: average wait time in these three cases? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 33
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-bound processes force I/O-bound processes to wait for CPU ð I/O devices remain idle § UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 34
Summary n Thread = single execution stream within process n n User-level, kernel-level, hybrid No perfect scheduling algorithm n n Selection = policy decision Base on processes being run & goals n n Minimize response time Maximize throughput etc. Next time: much more on scheduling UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 35