fall 2015 silicon valley university confidential1 operating systems dr. jerry shiao, silicon valley...

50
Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

Upload: leonard-allen

Post on 13-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

Fall 2015SILICON VALLEY UNIVERSITY

CONFIDENTIAL 1

Operating Systems

Dr. Jerry Shiao, Silicon Valley University

Page 2: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 2

CPU Scheduling Overview

CPU Scheduling Basic Concepts

Scheduler in a Multi-Programming Environment. Maximize CPU Utilization. Concept of CPU Bursts and I/O Bursts.

Scheduling Criteria Comparing CPU Scheduling Algorithms

Scheduling Algorithms First-Come-First-Serve Shortest-Job-First Priority Round Robin Multilevel Queue Scheduling

Thread Scheduling User-Level Threads and Kernel-Level Threads

Multiple-Processor Scheduling Processor Afinitity Load Balancing Multicore Processors Virtualization

Computer System Schedulers

Copyright @ 2009 John Wiley & Sons Inc.

Page 3: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 3

CPU Scheduling

Objective of Multiprogramming Maximize CPU Utilization: To have some process running at all times. Several processes kept in memory. Alternating sequence of CPU execution – I/O Wait …

. . . Load Store . . . Wait for I/O . . . Store Inc . . . Wait for I/O . . . Add Store Index Read From File Write to File

I/O Bound Process has many short CPU bursts. CPU Bound Process has few long CPU bursts.

Copyright @ 2009 John Wiley & Sons Inc.

CPU Burst 1-2 MSec CPU Burst 1-2 MSec

Page 4: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 4

CPU Scheduling

CPU-Burst Duration I/O Bound Process has many short CPU bursts. CPU Bound Process has few long CPU bursts. Burst distribution important for CPU-Scheduling Algorithm.

Copyright @ 2009 John Wiley & Sons Inc.

Curve showing large number of short burst (Time-Sharing processes).

A smaller number of long CPU bursts (Batch processes).

Page 5: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 5

CPU Scheduling CPU Scheduler Scheduler manages queues to minimize queuing delay

and to optimize performance in a queuing environment. Types of Scheduling:

Long-Term Scheduling Decision on which programs are admitted to the system for

processing, controlling the degree of Multiprogramming. Long-Term Scheduler may limit the degree of Multiprogramming to

provide satisfactory service to current set of processes. Medium-Term Scheduling

Decision to Swap in a process, manage degree of Multiprogramming. Short-Term Scheduling

Dispatcher, makes decision on which process to execute next. Invoked whenever event can lead to blocking of current process or

preempt the current running process: Clock Interrupts, I/O Interrupts, System Calls, Signals (Semphores).

I/O Scheduling Decision as to which process pending I/O request shall be handled by

the available I/O Device.Copyright @ 2009 Pearson Education, Inc.

Page 6: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 6

CPU Scheduling CPU Scheduler Scheduling and Process State Transition

Copyright @ 2009 Pearson Education, Inc.

Scheduling types and relation to Process State Transition.

Page 7: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 7

CPU Scheduling

CPU Scheduler Queuing Diagram for Scheduling:

Copyright @ 2009 Pearson Education, Inc.

Page 8: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 8

CPU Scheduling

CPU Scheduler Short Term Scheduler: Selects from Ready Queue the Process

Control Block ( PCB ). Ready Queue can be: FIFO Queue, Priority Queue, a Binary

Tree, or Unordered Link List. Preemptive and NonPreemptive Scheduling

NonPreemptive or Cooperative: Process keeps the CPU until it releases the CPU or . . .

Switch Running State to Waiting State (i.e. I/O Request or Wait for Child Process Termination).

Process Terminates. Used by hardware platforms without special timer hardware for real-time

clock. Process can monopolize CPU, hang the system. Windows 3.x, Old version of Macintosh Real-Time Operating System

Cannot allow real-time event processing to be interrupted.

Copyright @ 2009 John Wiley & Sons Inc.

Page 9: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 9

CPU Scheduling

Preemptive and NonPreemptive Scheduling (Cont) Preemptive:

Switch Running State to Ready State (i.e. on Interrupt). Switch Waiting State to Ready State (i.e. I/O Completion). Uses special timer hardware for real-time clock. Problems:

Process Shared Data: Using semaphores, mutexes, and spinlocks. Kernel critical data guarded from simultaneous use.

Process updating critical data is interrupted and second process reads the critical data, which the interrupted process has not completed updating.

Interrupt Handlers disable/enable interrupts. Windows 95, Mac OS X, Linux

Copyright @ 2009 John Wiley & Sons Inc.

Page 10: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 10

CPU Scheduling Scheduling Criteria Criteria for comparing CPU-Scheduling Algorithm:

Usually favors one class over another. CPU Utilization

Maximize. CPU 40% (lightly loaded) to 90% (heavily loaded).

Throughput Maximize. Schedule 10 processes per second ( Interactive Load ) to 1 process per

second ( Batch Load ). Turnaround Time

Minimize. Process execution time: Sum of periods waiting to swap into memory, waiting

in the Ready Queue, waiting for I/O, and executing on CPU. Waiting Time

Minimize. Sum of the periods the process spends waiting in the Ready Queue.

Response Time Minimize. Measure of time from the submission of a request until the time it takes to start

responding.Copyright @ 2009 John Wiley & Sons Inc.

Page 11: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 11

CPU Scheduling Scheduling Criteria Criteria for comparing CPU-Scheduling Algorithm (Cont) Short-Term Scheduling Criterias: Optimized towards System

Behavior (Real-Time, Time-Sharing, Batch System). User-Oriented:

Behavior of the system as perceived by the user or process. Interactive System: Response Time is elapsed time between the submission of a

request until the response appearing as output. Scheduling Policy minimize Response Time: Round-Robin. Switching processes frequently increases overhead, reducing throughput.

System-Oriented: Effective and efficent utilization of the processor or System Performance. Scheduling Policy minimize Turnaround Time or maximize Throughput: Shortest

Job First ( SJF ). Criteria not readily measureable:

Predictability: Service provided to users to exhibit same characteristics over time. Variation in Response Time or Turnaround Time mean wide swing in system

workloads, needing system tuning to stabilize.

Copyright @ 2009 Pearson Education, Inc.

Page 12: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 12

CPU Scheduling

Scheduling Algorithms First-Come, First-Served Scheduling

FCFS Scheduling is nonpreemptive. Problem for Time-Sharing Systems (needs CPU at regular

intervals). Simple algorithm, but long wait time.

Average Wait Time varies on order and CPU bursts.

Copyright @ 2009 John Wiley & Sons Inc.

P1 P3P2

P1 P2 P3

Average Wait Time = ((P1 = 0) + P2 + P3) / 3

P2 P3 P1

P2 P3 P1

Average Wait Time = (( P2 = 0) + P3 + P1) / 3 Wait Time.

Page 13: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 13

CPU Scheduling

Scheduling Algorithms Shortest-Job-First Scheduling

CPU is assigned to the process that has the smallest next CPU burst.

Minimum average waiting time for a set of processes. In Short term scheduling, predict the next CPU burst value. Preemptive SJF takes CPU when arrived process burst time is

less than the remaining time of current process. NonPreemptive SJF allows current process to complete burst

time before scheduling next process based on burst time. Length of CPU burst? Predict value of next CPU burst.

Copyright @ 2009 John Wiley & Sons Inc.

P2 P3 P1

P4 P2 P3 P1

Average Wait Time = ((P4 = 0) + P2 + P3 + P1 )/ 4

P4

Wait Time.

Page 14: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 14

CPU Scheduling Scheduling Algorithms FirstCome, First-Served Scheduling

Burst Time: P1: 20, P2: 12, P3: 8, P4: 16, P5: 4

Wait Time ( Order of Arrival ). P1: 0, P2: 20, P3: 32, P4: 40, P5: 56

Average Wait Time: 148 / 5 = 29.6 Shortest-Job-First Scheduling

Wait Time ( In Ready Queue at Same Time ). P1: 40, P2: 12, P3: 4, P4: 24, P5: 0

Average Wait Time: 80 / 5 = 16Copyright @ 2009 John Wiley & Sons Inc.

P1

20

P2

32

P3 P4 P5

40 56 600

P1

4

P2

12

P3 P4P5

24 40 600

Wait Time.

Wait Time.

Page 15: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 15

CPU Scheduling Scheduling Algorithms Priority Scheduling

SJF Algorithm is Priority Scheduling where priority is inverse of predicted next CPU burst ( Lower burst, higher priority ).

Preemptive: Compare arriving process priority with current running process priority.

Nonpreemptive: Arriving process priority higher than current running process priority placed in front of the Ready Queue.

Problem: Indefinite blocking of low priority processes until system lightly

loaded. Solution: Increase priority of processes in the Ready Queue ( Aging ).

0 1 6 16 18 19

Copyright @ 2009 John Wiley & Sons Inc.

Process/Burst Time (msec)/Priority: P1/10/3, P2/1/1, P3/2/4, P4/1/5, P5/5/2 = 8.2 msec Average

P2 P5 P1 P3 P4

Page 16: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 16

CPU Scheduling

Scheduling Algorithms Priority Scheduling Numbering Scheme

Priority 0 can be highest or lowest

Priorities Internal

Set according to Operating System (i.e. memory requirements, number of open files, number of I/O bursts).

External Importance, Process cost, Process sponsorship.

Static Fixed priority when process enter Operating System.

Dynamic Changing during processing (i.e. Amount of CPU usage, or aging (Length of

time waiting (a solution to starvation))

Copyright @ 2009 John Wiley & Sons Inc.

Page 17: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 17

CPU Scheduling Scheduling Algorithms Compare FCFS, SJF, Priority Scheduling

FCFS (First-come, First-Served) Non-preemptive.

SJF (Shortest Job First) Non-preemptive or Preemptive. When a new (shorter burst) job arrives, can preempt current

job or not. Possible process starvation.

Priority Non-preemptive or Preemptive. When a new (higher priority job arrives). When a processes priority changes. Possible process starvation. Linux 2.4 Non-realtime nonpreemptive. Linux 2.6 “Real-time” preemptive.

Copyright @ 2009 John Wiley & Sons Inc.

Page 18: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 18

CPU Scheduling

Scheduling Algorithms Round-Robin Scheduling

Designed for Time-Sharing or Multi-Tasking Operating Systems. Based on First-Come-First-Serve Algorithm, but use Preemption

to switch between processes. Preemption: Process will be preempted when quantum expires. Quantum: Time slice (unit of time) in which a process executes. Circular Queue: Process alternates quantum execution time. Average waiting time often long (process has to wait for other

processes to complete their quantum). Timer Interrupt when Quantum Expires.

Current process placed at end of Circular Queue. Current process completes before Quantum Expires, next process

selected, no Interrupt.

Copyright @ 2009 John Wiley & Sons Inc.

Page 19: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 19

CPU Scheduling

Scheduling Algorithms Round-Robin Scheduling

Waiting times: Average wait time: 30.4P1: 60 - 20 = 40P2: 44 - 12 = 32P3: 32 - 8 = 24P4: 56 - 16 = 40P5: 20 - 4 = 16

Copyright @ 2009 John Wiley & Sons Inc.

P1 P2

4

P3 P4 P5 P1

8 12 16 20 24

P2 P3

28

P4 P1 P2 P4

32 36 40 44 48

P1 P4

52

P1

56 60

P5 completes P3 completesP2 completes P1 completes

P4 completes

0

Order of Queue and CPU Burst Quantum = 4 Process runs until:P1: 20 P2: 12 P3: 8 P4: 16 P5: 4 a) Remaining CPU Burst < Quantum b) Remaining CPU Burst > Quantum Interrupt Context Switch to Next Process in Ready Queue.

Page 20: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 20

CPU Scheduling

Scheduling Algorithms Round-Robin Scheduling Choosing a Quantum:

Time Quantum too large, RR Policy becomes FCFS Policy. Time Quantum too small, RR Policy process time affected by

Context Switch time. Context Switch slows the execution of the process. Context Switch approximately less than 10 percent of the CPU time. Typically Context switch < 10 microseconds. But, more Context Switch, longer process has to wait for the CPU

again.

Copyright @ 2009 John Wiley & Sons Inc.

Page 21: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 21

CPU Scheduling

Scheduling Algorithms Round-Robin Scheduling Context Switch

Copyright @ 2009 John Wiley & Sons Inc.

Process Time = 10 Quantum Context Switches

12 0

6 1

1 9

Page 22: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 22

CPU Scheduling

Scheduling Algorithms Sample distribution based on Nonpreemptive FCS and SFJ. Preemptive RR. Response time

Measure of time from the submission of a request until the time it takes to start responding. Assume all jobs submitted at same time, in order given.

Turnaround time Process execution time. Time interval from submission of process until completion of

process. Assume all jobs submitted at same time

Copyright @ 2009 John Wiley & Sons Inc.

P1

4

P2

12

P3 P4P5

24 40 60

SJF

P1 P2

4

P3 P4 P5 P1

8 12 16 20 24

P2 P3

28

P4 P1 P2 P4

32 36 40 44 48

P1 P4

52

P1

56 60

RR

Page 23: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 23

CPU Scheduling

Scheduling Algorithms Sample Response Time Calculation (Measure of time from the submission of a

request until the time it takes to start responding).

Copyright @ 2009 John Wiley & Sons Inc.

Page 24: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 24

CPU Scheduling

Scheduling Algorithms Sample Response Time Calculation (Measure of time from the submission of a

request until the time it takes to start responding).

Copyright @ 2009 John Wiley & Sons Inc.

P1

4

P2

12

P3 P4P5

24 40 60

SJF

Page 25: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 25

CPU Scheduling

Scheduling Algorithms Sample Response Time Calculation (Measure of time from the submission of a

request until the time it takes to start responding).

Copyright @ 2009 John Wiley & Sons Inc.

P1 P24

P3 P4 P5 P18 12 16 20 24

P2 P328

P4 P1 P2 P432 36 40 44 48

P1 P452

P156 60

RR

Page 26: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 26

CPU Scheduling

Scheduling Algorithms Sample Turnaround Time Calculation (Process burst execution time and sum of wait

periods).

Copyright @ 2009 John Wiley & Sons Inc.

Page 27: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 27

CPU Scheduling

Scheduling Algorithms Sample Turnaround Time Calculation (Process burst execution time and sum of wait

periods).

Copyright @ 2009 John Wiley & Sons Inc.

P1

4

P2

12

P3 P4P5

24 40 60

SJF

Page 28: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 28

CPU Scheduling

Scheduling Algorithms Sample Turnaround Time Calculation (Process burst execution time and sum of wait

periods).

Copyright @ 2009 John Wiley & Sons Inc.

P1 P24

P3 P4 P5 P18 12 16 20 24

P2 P328

P4 P1 P2 P432 36 40 44 48

P1 P452

P156 60

RR

Page 29: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 29

CPU Scheduling

Scheduling Algorithms Performance Characteristics of Scheduling Algorithms

Round Robin ( RR ) Average waiting time, often high, causing above average

Turnaround Time. Turnaround Time good when CPU bursts shorter than Quantum.

Quantum provides good response time. Quantum too long, approach FCFS Policy behavior. Quantum too short, causes additional Context Switch overhead, Quantum 10 to 100 milliseconds.

Important for Interactive or Timesharing. Shortest-Job-First ( SJF ).

Best average waiting time, causing best Turnaround Time. CPU burst length or estimates difficult to predict.

Copyright @ 2009 John Wiley & Sons Inc.

Page 30: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 30

CPU Scheduling Scheduling Algorithms

Processes are classified into different process types. Multilevel Queue Scheduling

Partition Ready Queue into several separate queues. Queues are defined based on process property: interactive process type,

background process type, process priority. Each queue has its own Scheduling Algorithm.

Time-Sharing Queue scheduled by RR Algorithm. Batch Queue scheduled by FCFS Algorithm.

Each queue has priority: No process in the lower priority queues will run until the higher priority

queues has completed. When process in higher priority queue is received, the lower priority queue

processing is preempted. Prevent Starvation, Time-Slice between queues, each queue has % of CPU.

Inflexible: Process assigned to queue, process “type” does not change.

Copyright @ 2009 John Wiley & Sons Inc.

System Processes

Interactive Processes

Batch Processes

Page 31: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 31

CPU Scheduling

Scheduling Algorithms Multilevel Feedback Queue Scheduling Multilevel Queues, but allow process to move between

queues. Process exceeds quantum, placed in lower priority queue. Process not getting CPU time, placed in higher priority queue.

Defined with: The number of queue. Scheduling algorithm for each queue. Policy to upgrade a process to a higher-priority queue. Policy to downgrade a process to a lower-priority queue. Policy to determine which queue to place a process.

Copyright @ 2009 John Wiley & Sons Inc.

Page 32: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 32

CPU Scheduling

Scheduling Algorithms Multilevel Feedback Queue Scheduling

Copyright @ 2009 John Wiley & Sons Inc.

Multilevel Queue (Fixed) Multilevel Feedback Queue (Movement)

Page 33: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 33

CPU Scheduling

Thread Scheduling User-level and Kernel-level Threads

User-level thread mapped to kernel-level thread. Lightweight Process ( LWP )

Operating System schedules kernel-level threads. Process-Contention Scope ( PCS ) Scheduling

User-level threads scheduled by the User-level Thread Library. Kernel scheduler is NOT involved.

The User-level thread mapped to Kernel-level thread for scheduling by Operating System ( One-to-One, Many-to-One, Many-to-Many ).

PTHREAD SCOPE PROCESS schedules threads using PCS. System-Contention Scope ( SCS ) Scheduling

Kernel-level threads scheduled by the Operating System. PTHREAD SCOPE SYSTEM schedules threads using SCS. LWP creates/bind User-level thread to LWP (One-to-One policy).

Copyright @ 2009 John Wiley & Sons Inc.

Page 34: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 34

CPU Scheduling

Thread Scheduling#include <pthread.h>

#include <stdio.h>

int main(int argc, char *argv[])

. . .

/* set the scheduling algorithm to PCS or SCS */

pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);

/* create the threads */

for (i = 0; i < 5; i++)

pthread create(&tid[i],&attr,runner,NULL);

/* now join on each thread */

for (i = 0; i < 5; i++)

pthread join(tid[i], NULL);

}

/* Each thread will begin control in this function */

void *runner(void *param) {

/* do some work ... */

pthread exit(0);

}

Copyright @ 2009 John Wiley & Sons Inc.

Sets the attribute set for the thread as Process or System Contention scope. This attribute set to System Contention.

Create the thread based on the attributes.

NOTE: Contention Scopes are Operating System Dependent. Linux and MAC OS X Operating Systems allow only PTHREAD_SCOPE_SYSTEM (One-to-One Mapping).

Wait for the threads to complete.

Page 35: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 35

CPU Scheduling

Multiple-Processor Scheduling Processor Affinity

Cache Memory: Process prefers running on a specific CPU. Keep Cache Memory Validated. Soft Affinity: Operating System has policy to keep process on same

CPU, but not guaranteed. Hard Affinity: Process cannot migrate to other CPU.

Linux, Solaris.

Main Memory Architecture: Non-Uniform Memory Access (NUMA) when memory access speed non-uniform.

Copyright @ 2009 John Wiley & Sons Inc.

CPU

Memory

Fast Access

CPU

Memory

Fast Access

Slow Access( NUMA )

CPU and Memory Affinity.

Page 36: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 36

CPU Scheduling Multiple-Processor Scheduling Load Balancing

Keep CPU processing balanced. Only when processors have separate CPU Ready Queues.

Distributing processes between CPUs can counteracts Processor Affinity ( Imbalance determined by queue threshold ).

Push Migration: Linux/FreeBSD Special process checks CPU Ready Queues and redistributes processes by

pushing process from one CPU Ready Queue to another.

Pull Migration: Linux/FreeBSD Idle processor pulls a waiting process from a CPU’s Ready Queue.

Copyright @ 2009 John Wiley & Sons Inc.

CPU CPU

Ready Queue Ready Queue

Migration Thread

Every 200 Milliseconds

Page 37: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 37

CPU Scheduling

Multicore Processors Scheduling Multiple Processor Cores on Same Physical Chip

Each core separate set of registers. Multicore SMPs are faster and consume less power than single

core multiprocessor SMPs. Multithreading processor cores: Two or more hardware threads

assigned to each core. Memory Stall: A hardware thread will block when waiting for I/O to arrive or

write, cache memory miss. Allows another hardware thread to use the CPU.

Copyright @ 2009 John Wiley & Sons Inc.

C CMCMCM

C CMCMCMThread 0

Thread 1

Time

Computer Cycle

Memory Stall Cycle

Page 38: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 38

CPU Scheduling

Multicore Processors Scheduling Each hardware thread is a logical processor.

Dual-core dual-threaded CPU has four logical processors.

Multithread Processor Implementation Coarse-grained: Hardware thread executes until memory stall ( or long-latency

event ) occurs. Processor now switch to another hardware thread. Cost of thread switching is high ( instruction pipeline has to be flushed ).

Fine-grained ( Interleaved ): Hardware thread switches at boundary of instruction cycle. All interleaved multithreading processor has hardware support for thread switching and is fast.

Two Levels of Scheduling: Scheduling Algorithm by Operating System to select software thread to run. Each core decides which hardware thread to run. Round Robin (UltraSPARC T1). Events to trigger thread switch given priority (Intel Itanium).

Copyright @ 2009 John Wiley & Sons Inc.

Page 39: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 39

CPU Scheduling

Virtualization and Scheduling Virtualization Software Schedules Physical CPU (Host

OS) for Each Virtual Machine (Guest OS). Multiple Scheduling Layers All Sharing CPU Cycle

Host Operating System Scheduling. Virtualization Software Scheduling Virtual Machine. Virtual Machine Operating System Running Own Scheduler.

100 Millisecond Virtual CPU time might take 1 second. Poor response times to uses logged into Virtual Machine. Time-of-day clocks are incorrect, timers take longer to trigger. Make a Virtual Machine scheduling algorithm appear poor.

Copyright @ 2009 John Wiley & Sons Inc.

Page 40: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 40

CPU Scheduling

Solaris Operating System Priority-Based Thread Scheduling

Thread belongs to one of six classes: each Class maintains different priorities and Scheduling Algorithm.

Time Sharing Class Default Class. Dynamic alters priorities using multilevel feedback queue. Inverse Relationship between priorities and time slices (quantum).

Higher priority, lower time slice. Good Response time for Interactive processes and good throughput

for CPU-bound processes. Interactive Class

Similar Scheduling Policy as Time Sharing Class. Used by KDE or GNOME Window Managers for higher priority.

Copyright @ 2009 John Wiley & Sons Inc.

Page 41: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 41

CPU Scheduling

Solaris Operating System Priority-Based Thread Scheduling

Real Time Class Highest Priority. Guaranteed Response within bounded time period.

System Class Executes kernel threads only (Scheduler and Paging Daemon). Priority fixed.

Fair Share Use CPU shares (not priorities), indicate entitlement to CPU. Solaris 9 (One-to-One Model).

Fixed Priority Same priority range as Time-Sharing Class, but not dynamically adjusted. Solaris 9 (One-to-One Model).

Copyright @ 2009 John Wiley & Sons Inc.

Page 42: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 42

CPU Scheduling Solaris Operating System

Time-Sharing and Interactive Threads

Copyright @ 2009 John Wiley & Sons Inc.

Priority Time Quantum Time Quantum Expired

Return From Sleep

0 200 0 50

5 200 0 50

10 160 0 51

15 160 5 51

20 120 10 52

25 120 15 52

30 80 20 53

35 80 25 54

40 40 30 55

45 40 35 56

50 40 40 58

55 40 45 58

59 20 49 59

Lowest Priority has highest Time Quantum

New Priority when used entire Quantum.

New Priority when NOT used entire Quantum (waiting for I/O).

Page 43: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 43

CPU Scheduling Solaris Operating System

Copyright @ 2009 John Wiley & Sons Inc.

Interrupt Threads

RealTime (RT) Threads

System (SYS) Threads

Fair Share (FSS) ThreadsFixed Priority (FX) ThreadsTimeShare (TS) ThreadsInteractive (IA) Threads

169

159

99

59

0

Highest

Lowest

First

Last

Scheduling Order

Global Priority

Scheduling Classes

Page 44: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 44

CPU Scheduling

Windows XP Operating System Priority-Based, Preemptive Scheduling Scheduling on the basis of threads Highest priority thread always be dispatched, and runs

until: Preempted by a higher priority thread Terminates Time quantum ends Makes a blocking system call (e.g., I/O)

32 priorities, each with own queue: Mem. Mgmt.: 0; Variable: 1 to 15; Real-Time: 16 to 31

Copyright @ 2009 John Wiley & Sons Inc.

Page 45: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 45

CPU Scheduling

Windows XP Operating System Within Priority Class contains Relative Priority All Classes Except Real-Time are Variable

Thread Quantum Runs Out: Lower Priority. Thread Released From Wait State: Raise Priority.

Copyright @ 2009 John Wiley & Sons Inc.

Real-Time High Above Normal

Normal Below Normal

Idle Priority

Time-Critcal 31 15 15 15 15 15

Highest 26 15 12 10 8 6

Above Normal 25 14 11 9 7 5

Normal 24 13 10 8 6 4

Below Normal 23 12 9 7 5 3

Lowest 22 11 8 6 4 2

Idle 16 1 1 1 1 1

Priority Classes

Rel

ati

ve

Pri

ori

ty

Page 46: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 46

CPU Scheduling

Linux Operating System Preemptive, Priority-Based Scheduler

Real-Time Priority Range: 0 – 99 Nice Range: 100 - 140

Copyright @ 2009 John Wiley & Sons Inc.

Real-Time Tasks

Other Tasks

0

99100

140

Numeric Priority Time Quantum

200 MSec

10 MSec

Relative Priority

Highest

Lowest

Higher Priority Tasks are assigned longer Time Quantums.

Page 47: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 47

CPU Scheduling

Linux Operating System Real-Time Tasks Assigned Static Priorities Other Tasks Has Dynamic Priorities

Nice Value and how long task is sleeping. Longer sleep time has higher priority adjustments ( typically I/O

bound tasks ). Shorter sleep time has lower priority adjustments ( typically

CPU-bound tasks ). Runqueue Data Structure per CPU

Active Priority Array: All tasks with time remaining in Quantum. Expired Priority Array: All tasks with expired Quantum. Scheduler selects highest priority task from the Active Priority Array. When Active Priority Array is empty (transitioned to Expired Priority

Array or in Wait Queues), arrays are swapped.

Copyright @ 2009 John Wiley & Sons Inc.

Page 48: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 48Fall 2015

CPU Scheduling

Linux 2.6 Scheduler Algorithm

Migration Thread

Task 1

Task 5 Task 6 Task X

Task 7 Task ZTask 68

. . .

1 0 0 . . . 0 0 1

140 . . . 3 2 1PriorityBitmaprunqueue

. . .Active [0] [1] [2]. . . [140]

Task 2 Task NTask 3

Task MTask 4

Expired [0] [1] [2]. . . [140]

Priority Arrays exchanged when all tasks have executed.

Used to Load Balance when CPU heavily utilized. Tries to distribute some processes.

Page 49: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 49

CPU Scheduling Algorithm Evaluation Methods Criteria: CPU Utilization, Response Time, Throughput

Analytic Evaluation: Uses given algorithm and system workload, produce a formula that evaluates performance of algorithm.

Deterministic Modeling: Predetermined workload ( i.e. measure wait time for FCFS, SJF, RR algorithms).

Problem: Requires exact numbers and ONLY applies to specific cases. Queueing Modeling: Estimate arrival rate and service rate, compute

utilization, average queue length, average wait time. Problem: Approximations of real systems and may not be accurate.

Simulations: Programming model of computer system, software data structures represents Operating System components.

Data ( i.e. CPU burst times, arrivals, departures) randomly generated (i.e. uniform, Poisson) or empirical.

Problem: Designing simulator is expensive. Only completely accurate way to evaluate a scheduling algorithm is to

implement it and observe the behavior in the Operating System.

Copyright @ 2009 John Wiley & Sons Inc.

Page 50: Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 50

CPU Scheduling Summary CPU Scheduling selects a waiting process from Ready Queue. Scheduling Algorithms

FCFS: Simple, but Short Processes Waits for Long Processes. SJF: Shortest Average Wait Time, but Predicting Next CPU Burst is Difficult, and

if Preemptive, can cause Starvation of Long Jobs. RR: Time-Shared (Interactive) System, but Selecting Quantum is Problematic.

Multilevel Queue Algorithms: Uses different algorithms for classes. Multilevel Feedback Queues: Moves process between queues. Multi-Processor Scheduling: Processor Affinity, Load Balancing. Operating Systems:

Solaris: Priority-based Thread Scheduling with 6 Priority Classes. Windows XP: Priority-based Thread Scheduling with 32 Priority Queues. Linux: Priority-based with Real-Time and Other Priority Ranges.

Algorithm Evaluation: Deterministic Modeling, Queueing Models, Simulations.

Copyright @ 2009 John Wiley & Sons Inc.