Thread scheduler

Download Report

Transcript Thread scheduler

Chapter 2
Processes and Threads
2.4 - 2.5
Scheduling
Classical Problems
Scheduling – Process Behavior
Figure 2-38. Bursts of CPU usage alternate with periods of waiting
for I/O. (a) A CPU-bound process. (b) An I/O-bound process.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Categories of Scheduling Algorithms
•
•
•
Batch
Interactive
Real time
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling Algorithm Goals
Figure 2-39. Some goals of the scheduling algorithm under different
circumstances.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Batch Systems
•
•
•
First-come first-served
Shortest job first
Shortest remaining Time next
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
First-Come First-Served
•
•
•
•
•
•
Processes are assigned CPU in the order that they
request it
The process that is first in the queue gets as much time
as it needs
Subsequently added processes are placed behind it in
the queue
A single linked list keeps track of all ready processes
Strength: easy to understand and implement
Weakness: does not preempt processes
Shortest Job First
•
•
•
•
Assumes run times are known in advance
Scheduler puts the shortest job first
First job takes A minutes, second takes A+B minutes
Mean turnaround time is (4A + 3B + 2C +D)/4
•
•
•
•
Optimal only when all jobs are simultaneously available
Ex: Run times: 2, 4, 1, 1, 1 Arrival times are 0, 0, 3, 3, 3
A B C D E = 4.6 sec average
B C D E A = 4.4 sec average
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Shortest Remaining Time Next
•
•
•
•
Run time must be known in advance
Chooses process whose remaining run time is the
shortest
When a new job arrives, its total time is compared to the
remaining time of the currently running process.
If the new job needs less time to finish than the current
process, the current process is suspended and the new
job is started
Scheduling in Interactive Systems
•
•
•
•
•
•
•
Round-robin scheduling
Priority scheduling
Multiple queues
Shortest process next
Guaranteed scheduling
Lottery scheduling
Fair-share scheduling
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Round-Robin Scheduling
•
•
•
•
•
Each job is assigned a time interval, called a quantum
If the process is still running at the end of the quantum,
the CPU is preempted and given to another process
If the process finishes before the quantum has finished,
the CPU switching is done when the process finishes
Process switch: every time a quantum expires it takes a
certain amount of time to switch to another process
A longer quantum means that the preemption will not
occur as often and most processes will finish before the
quantum runs out
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Priority Scheduling
•
•
•
•
•
•
•
•
•
Each process is assigned a priority
Process with highest priority is run first
The scheduler may decrease the priority of the currently running process
with each clock tick
If the priority of a currently running process is lower than another process
in the queue a process switch occurs
Priority can be chosen by how important the user is, or how expensive the
job is to run.
Priorities can also be dynamically assigned
Algorithm: 1/f, where f is fraction of the last quantum the process used
Within a priority it is common to use round-robin to choose which job is run
If priorities are not adjusted occasionally, lower priority classes may never
be run
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Multiple Queues
•
•
•
•
•
Each priority class has its own amount of time to run
Class 1 runs for 1 quantum, 2 for 2, 3 for 4, 4 for 8 … etc
After each process uses up its quantum it is moved
down one priority class
Most systems give priority to interactive user processes
over background ones
To prevent a process from staying low priority forever,
whenever the Enter key is pressed at the terminal, the
current process is placed back up at the highest priority
Shortest Process Next
•
SPN chooses the process whose remaining run-time is the shortest.
– Example: The cashier in a store finds the person with the fewest items, and checks out
their items first.
•
SPN requires exact knowledge of how long a process will take in
advance. If the mechanism to determine what is the shortest job is
faulty then this might significantly reduce the throughput. If a system
has a lot of short jobs and one long job then the long job may never
run.
•
SPN tends to be the most optimal scheduling system for total number
of jobs completed short of Shortest Remaining Time (SRT).
•
Can cause the halting problem.
Guaranteed Scheduling
•
Makes promises about performance to the users/processes.
•
Computes the real amount of CPU a user/processes has
consumed. I.E. keeps track of how much CPU process has had
since its creation.
•
If there are n processes running on your pc, each process
should get 1/n of the CPU cycles.
– A ratio of .5 means that process has only had half of what it should have, and a
ratio of 2.0 means that a process has had twice as much as it was entitled to.
•
.
Runs the process with the lowest ratio until its ratio has moved
above it’s closest competitor.
Lottery Scheduling
•
Gives processes lottery tickets for various system resources
(CPU time).
•
When a scheduling decision is required a lottery ticket is
randomly chosen and the process holding that ticket gets the
resource.
•
More important processes can be given extra tickets to increase
odds of winning.
•
Highly Responsive
Fair-Share Scheduling
•
CPU usage is equally distributed among system users or
groups, as opposed of equal distribution among processes.
•
For example, if four users (A,B,C,D) are concurrently executing
one process each, the scheduler will logically divide the
available CPU cycles such that each user gets 25% of the
whole (100% / 4 = 25%). If user B starts a second process,
each user will still receive 25% of the total cycles, but both of
user B's processes will now use 12.5%. On the other hand, if a
new user starts a process on the system, the scheduler will
reset the available CPU cycles such that each user gets 20% of
the whole (100% / 5 = 20%).
Scheduling in Real-Time Systems
•
In real time systems time plays a crucial role. Usually the
system is connected to one or more external devices which
generate stimuli and has to react appropriately to them within a
fixed amount of time.
– Examples: aircraft control, over-temperature monitor in nuclear power station,
ABS, biomedical systems, robotics
•
•
hard-real time - missing a deadline has catastrophic effects
soft-real time - missing a deadline is undesirable but tolerable
•
Stimuli (events) may be:
– periodic (occurring at regular intervals)
– aperiodic (unpredictable)
Schedualability
•
•
Depending on the situation, it may happen that not all the
events can be handled.
Consider m periodic events
– event i occurs with period Pi and requires Ci second of CPU time
– The system is schedulable if:
Policy vs. Mechanism
•
Often a process has many children running under its control
performing different tasks. In this case only the process itself
knows which one is the most important or time critical
•
For this reason it is important to separate scheduling
mechanism from the scheduling policy
•
The scheduling mechanism (algorithm) defines the parameters
used by the scheduler
•
The user process is responsible for filling in those parameters
for its children
Thread Scheduling
Thread scheduler - part of the OS (usually) that is
responsible for sharing the available CPUs out between
the various threads.
processes are typically independent, while threads exist as
subsets of a process.
User-level threads vs kernel level threads:
- user-level thread maintains all its state in user space.
- An advantage is that no kernel resources need to be allocated per
thread,
and switching between threads can be done without changing address space.
- A disadvantage is that user level threads cannot execute while the
kernel is
busy, for instance, with paging or I/O. This would require
some knowledge
and participation on the part of the kernel.
Thread Scheduling (1)
Figure 2-43. (a) Possible scheduling of user-level threads with a 50msec process quantum and threads that run 5 msec per CPU
burst.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Scheduling (2)
Figure 2-43. (b) Possible scheduling of kernel-level threads with the
same characteristics as (a).
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (1)
Figure 2-44. Lunch time in the Philosophy Department.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (2)
Figure 2-45. A nonsolution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (3)
...
Figure 2-46. A solution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (4)
...
...
Figure 2-46. A solution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (5)
...
Figure 2-46. A solution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (6)
See the video:
http://www.youtube.com/watch?v=6HMm1iaxWpo
Try it out for yourself: http://www.onarat.com/
The Readers and Writers Problem (1)
...
Figure 2-47. A solution to the readers and writers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Readers and Writers Problem (2)
...
Figure 2-47. A solution to the readers and writers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639