Threads scheduling
Download
1 / 33

Threads & Scheduling - PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on

Threads & Scheduling. What are threads? vs. processes Where does OS implement threads? User-level, kernel How does OS schedule threads?. Processes versus Threads. Process = Control + address space + resources fork() Thread = Control only PC, stack, registers pthread_create()

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Threads & Scheduling' - rad


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Threads scheduling
Threads & Scheduling

  • What are threads?

    • vs. processes

  • Where does OS implement threads?

    • User-level, kernel

  • How does OS schedule threads?


Processes versus threads
Processes versus Threads

  • Process =

    • Control + address space + resources

    • fork()

  • Thread =

    • Control only

      • PC, stack, registers

    • pthread_create()

  • One process may contain many threads


Threads diagram
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


Threads example c c posix
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;

}


Posix thread example ii
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


Classifying threaded systems
Classifying Threaded Systems

  • One or many address spaces, one or many threads per address space

MS-DOS


Classifying threaded systems1
Classifying Threaded Systems

  • One or many address spaces, one or many threads per address space

MS-DOS

Embedded systems


Classifying threaded systems2
Classifying Threaded Systems

  • One or many address spaces, one or many threads per address space

UNIX, Ultrix,

MacOS (< X),

Win95

MS-DOS

Embedded systems


Classifying threaded systems3
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


Threads
Threads

  • What are threads?

    • vs. processes

  • Where does OS implement threads?

    • Kernel, user-level

  • How does CPU schedule threads?


Kernel threads
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


User level threads
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


User level threads advantages
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)


User level threads disadvantages
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


Hybrid model
Hybrid Model

  • User-level threads mapped onto light-weight process (LWPs)


Hybrid model advantages
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)


Hybrid model load balancing

threads

thread

scheduler

thread

scheduler

processes

kernel

processors

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


Threads roundup
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


Threads scheduling1
Threads & Scheduling

  • What are threads?

    • vs. processes

  • Where does OS implement threads?

    • User-level, kernel

  • How does OS schedule threads?


Scheduling
Scheduling

  • Overview

  • Metrics

  • Long-term vs. short-term

  • Interactive vs. servers

  • Example algorithm: FCFS


Scheduling1
Scheduling

  • Multiprocessing: run multiple processes

    • Improves system utilization & throughput

    • Overlaps I/O and CPU activities


Scheduling processes
Scheduling Processes

  • Long-term scheduling:

    • How does OS determinedegree 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


Long term scheduling
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


Short term scheduling
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


Comparing scheduling algorithms
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


Scheduling issues
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


Scheduling interactive systems
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


Scheduling servers
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


Scheduling algorithms roundup
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


Scheduling policies
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


Fcfs scheduling example
FCFS Scheduling: Example

  • Question: average wait time for these three examples?

  • 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


Fcfs advantages disadvantages
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


Summary
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.


ad