1 threads & scheduling what are threads? vs. processes where does os implement threads?...
TRANSCRIPT
![Page 1: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/1.jpg)
1
Threads & Scheduling
What are threads? vs. processes
Where does OS implement threads? User-level, kernel
How does OS schedule threads?
![Page 2: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/2.jpg)
2
Processes versus Threads
Process = Control + address space + resources fork()
Thread = Control only
PC, stack, registers pthread_create()
One process may contain many threads
![Page 3: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/3.jpg)
3
Threads Diagram
Threads in a process 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
![Page 4: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/4.jpg)
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;
}
![Page 5: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/5.jpg)
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
![Page 6: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/6.jpg)
6
Classifying Threaded Systems
One or many address spaces, one or many threads per address space
MS-DOS
![Page 7: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/7.jpg)
7
Classifying Threaded Systems
One or many address spaces, one or many threads per address space
MS-DOS
Embedded systems
![Page 8: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/8.jpg)
8
Classifying Threaded Systems
One or many address spaces, one or many threads per address space
UNIX, Ultrix,MacOS (< X),
Win95
Embedded systems
MS-DOS
![Page 9: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/9.jpg)
9
Classifying Threaded Systems
One or many processes, one or many threads per process
UNIX, Ultrix,MacOS (< X),
Win95
Mach,Linux,Solaris,WinNT
MS-DOS
Embedded systems
![Page 10: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/10.jpg)
10
Threads
What are threads? vs. processes
Where does OS implement threads? Kernel, user-level
How does CPU schedule threads?
![Page 11: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/11.jpg)
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
![Page 12: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/12.jpg)
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
![Page 13: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/13.jpg)
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)
![Page 14: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/14.jpg)
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
![Page 15: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/15.jpg)
15
Hybrid Model
User-level threads mapped onto light-weight process (LWPs)
![Page 16: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/16.jpg)
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)
![Page 17: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/17.jpg)
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
threadscheduler
kernel
threadscheduler
threads
processes
processors
![Page 18: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/18.jpg)
18
Threads Roundup
User-level threads 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
![Page 19: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/19.jpg)
19
Threads & Scheduling
What are threads? vs. processes
Where does OS implement threads? User-level, kernel
How does OS schedule threads?
![Page 20: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/20.jpg)
20
Scheduling
Overview Metrics Long-term vs. short-term Interactive vs. servers Example algorithm: FCFS
![Page 21: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/21.jpg)
21
Scheduling
Multiprocessing: run multiple processes Improves system utilization &
throughput Overlaps I/O and CPU activities
![Page 22: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/22.jpg)
22
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 Implementation considerations
![Page 23: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/23.jpg)
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
![Page 24: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/24.jpg)
24
Short-Term Scheduling
Kernel may run scheduler when:1. process switches from running to waiting2. process switches from running to ready
(e.g. interrupt)3. process switches from waiting to ready4. processes terminated
Non-preemptive system: Schedule only under 1 & 4
Preemptive system: Schedule under any in 1-4
![Page 25: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/25.jpg)
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
![Page 26: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/26.jpg)
26
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
![Page 27: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/27.jpg)
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
![Page 28: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/28.jpg)
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
![Page 29: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/29.jpg)
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
![Page 30: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/30.jpg)
30
Scheduling Policies
FCFS (a.k.a., FIFO = First-In, First-Out)
Scheduler executes jobs to completionin arrival order
Early version: jobs did not relinquish CPU even for I/O
![Page 31: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/31.jpg)
31
FCFS Scheduling: Example
Question: average wait time for these three examples?
Assumptions:(1) Single CPU(2) Non-preemptive(3) Ignore context-switch
time(4) 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
![Page 32: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/32.jpg)
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
processesto wait for CPU
I/O devices remain idle
![Page 33: 1 Threads & Scheduling What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?](https://reader035.vdocuments.mx/reader035/viewer/2022062309/56649e0d5503460f94af6ca1/html5/thumbnails/33.jpg)
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.