Download presentation
1
Threads & Scheduling What are threads?
vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?
2
Processes versus Threads
Control + address space + resources fork() Thread = Control only PC, stack, registers pthread_create() One process may contain many threads
3
Threads Diagram Threads in a process Advantages:
Each has a thread ID, PC, registers, stack share: Address space in process (e.g., code section, data, resources) Advantages: Less overhead in creating and destroying threads Cheaper, faster communication than IPC
4
Threads Example, C/C++, POSIX
#include <pthread.h> #include <stdio.h> void* start(void* arg) { int tmp, thread_index = (int)arg; tmp = 200 * (int)arg; arg = (void *) tmp; printf("Thread %d about to terminate\n",thread_index); return arg; } int main() { pthread_t thr; int i = 1,st; void* rv; st = pthread_create(&thr,NULL,start,(void*)i); if (st!=0) printf("pthread_create failed with status: %d\n",st); pthread_join(thr,&rv); printf("thread %d returned %d\n",i,rv); return 0;
5
POSIX Thread Example, II
Compile: gcc –g –o test test.c –lpthread pthread_create pthread_join Principal thread blocks until the specified child terminates Second argument stores the return value
6
Classifying Threaded Systems
One or many address spaces, one or many threads per address space MS-DOS
7
Classifying Threaded Systems
One or many address spaces, one or many threads per address space MS-DOS Embedded systems
8
Classifying Threaded Systems
One or many address spaces, one or many threads per address space UNIX, Ultrix, MacOS (< X), Win95 MS-DOS Embedded systems
9
Classifying Threaded Systems
One or many processes, one or many threads per process UNIX, Ultrix, MacOS (< X), Win95 MS-DOS Mach, Linux, Solaris, WinNT Embedded systems
10
Threads What are threads? Where does OS implement threads?
vs. processes Where does OS implement threads? Kernel, user-level How does CPU schedule threads?
11
Kernel Threads Kernel threads: scheduled by OS
Switching threads requires context switch PC, registers, stack pointers BUT: when in same process, no mem mgmt. = no TLB “shootdown” Examples: Windows 95 & up Switching faster than for processes Can be scheduled on multiple processors
12
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: Green threads (Solaris) Cannot be scheduled on multiple processors
13
User-Level Threads: Advantages
Flexible: Can define problem-specific thread scheduling policy Computations first, service I/O second, etc. Each process can use different scheduling algorithm Can be much faster than kernel threads Context might be very small No system calls for creation, switching threads, synchronization (not cross user-kernel boundary)
14
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
15
Hybrid Model User-level threads mapped onto light-weight process (LWPs)
16
Hybrid Model: Advantages
“Best of both worlds” Multiplex multiple user-level threads to a smaller or equal number of kernel threads (take advantage of multiple processors) May not be a one-to-one mapping (flexible, faster)
17
Hybrid Model: 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 threads processes processors
18
Threads Roundup User-level threads Kernel-level threads Hybrid
Cheap, simple Not scheduled directly, 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
19
Threads & Scheduling What are threads?
vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?
20
Scheduling Overview Metrics Long-term vs. short-term
Interactive vs. servers Example algorithm: FCFS
21
Scheduling Multiprocessing: run multiple processes
Improves system utilization & throughput Overlaps I/O and CPU activities
22
Scheduling Processes Long-term scheduling: Short-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 Implementation considerations
23
Long-term Scheduling Goal:
select a good mix of I/O-bound and CPU-bound processes I/O-bound process: spending more of its time on doing I/O than computation CPU-bound process: using more of its time on computation, e.g., displaying video
24
Short-Term Scheduling
Kernel may run scheduler when: process switches from running to waiting process switches from running to ready (e.g. interrupt) process switches from waiting to ready processes terminated Non-preemptive system: Schedule only under 1 & 4 Preemptive system: Schedule under any in 1-4
25
Comparing Scheduling Algorithms
Important metrics: Utilization = % of time that CPU is busy Throughput = processes completing / time Response time = time between submission to first response Waiting time = time process spends on ready queue
26
Scheduling Issues Ideally: Conflicting goals
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
27
Scheduling: Interactive Systems
Goals for interactive systems: Minimize average response time Time between submission to first response 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
28
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
29
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
30
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
31
FCFS Scheduling: Example
Assumptions: Single CPU Non-preemptive Ignore context-switch time Length of the jobs: A:5, B:2, C:3 Ex1: Arrival time: B:0, C:1, A:2, no I/O Ex2: Arrival time: A:0, B:1, C:2, no I/O Ex3: Arrival time: A:0, B:1, C:2, A does I/O after 2 time units and I/O takes 2 time units Question: average wait time for these three examples?
32
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
33
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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.