Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Scheduled

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Processes & Threads

Scheduling
Review
• Process Model
– Pseudo-parallelism (Multi-programming, quantum or time slice)
– Context Switch (user mode  kernel mode, switch CPU to other
process – load/store PCB)
– Scheduling algorithm
• PCB
– Id, registers, scheduling information, memory management
information, accounting information, I/O status information, …
– State (New, Running, Ready, Blocked, Terminal)
• CPU Utilization
– Shows the CPU utilization
– 1 – pn
Review
• Threads
– Share the same address space and resources of the process
– Each thread has its own PC, registers and stack of execution
– There is no protection between threads in one process
– Have its own stack
®Improve context switch among processes, optimize quantum
– Are implemented in 3 modes: user, kernel, hybrid
Review
• IPC
– Resolving
• Race condition (Critical Region)
• Busy waiting (Priority Inversion)
– Good solution for race condition
• Software: Peterson solution (2 control variables)
• Hardware: TSL (atomically, individual)
– Good solution for race condition and busy waiting
• Software: binary semaphore (recommending about order in using),
monitors
• Hardware: mutexes
Objectives
• Scheduling
– Introduction
– Process Behavior
– When to schedule
– Categories of Scheduling algorithms
– Criteria/ Properties Term
– Scheduling in Batch System
Scheduling
Introduction
• Scheduler
– OS component that decides what process will be run and for
how long
– Uses a scheduling algorithm
• On OSs that support kernel-level threads, it is threads that
are being scheduled
• History
– In the old days of batch system with input the form of card images
on a magnetic tape: just run the next job on the tape
– In early computers, CPU time was a scarce resource: good
scheduling was of paramount importance!
– Nowadays, the CPU is not a scarce resource any more!
Furthermore, in PCs there aren’t many users competing…
However, scheduling algorithms have become more
sophisticated!
Scheduling
Process Behavior
• All process execution consists of a cycle of bursts of
computing (CPU execution) and I/O request (I/O wait)
• Compute-bound processes
– Spend most of their time computing
– Have long CPU bursts and thus infrequent I/O waits
• I/O-bound processes
– Spend most of their time waiting for I/O
– Have short CPU bursts and thus frequent I/O waits

Tanenbaum, Fig. 2-38.


Scheduling
Process Behavior
• Example

load store
add store CPU burst Process execution
read from file begin with CPU
burst, then I/O
wait for I/O I/O burst burst … the last
store increment index CPU burst will end
write to file CPU burst with a system
request to terminal
wait for I/O I/O burst execution
Scheduling
When to schedule
• A key issued related to scheduling is when to make
scheduling decisions
• Process creation
– A decision needs to be made whether run the parent or child
process
• Process termination
– A decision must be made when a process exits. That process can
no longer run, so some other process must be chosen from the set
of ready processes. If no process is ready, a system-supplied idle
process is normally run
• Process blocking
– When a process blocks, another process has to be selected to run
Scheduling
When to schedule
• Interrupt occurrence
– If the interrupt came from an I/O device that has now completed
its work, some process that was blocked waiting for the I/O may
now be ready to run
• Clock interrupt occurrence
– non-preemptive scheduling algorithms
– preemptive scheduling algorithms
• CPU scheduling decisions may take place when a process:
– Switches from running to blocked state (I/O or wait for child
processes).
– Switches from running to ready state (interrupted).
– Switches from blocked to ready (completion of I/O).
– Terminates.
→ The process executes following above steps in order, we can say it
is scheduled in non-preemptive. Otherwise, it is scheduled in
preemptive
Scheduling
When to schedule
• non-preemptive scheduling algorithms
– Picks a process to run and then just lets it run until it blocks or
until it voluntarily releases the CPU (will not be forceably
suspended, no scheduling decisions)
– Once a process is in the running state, it will continue until it
terminates or blocks itself for I/O
– Applying to the batch system
• preemptive scheduling algorithms
– The process can run (continuously) for a maximum of some fixed
time. If it is still running at the end of this time, it is suspended
and the scheduler will pick another process to run (needs timer)
– Currently running process may be interrupted and moved to the
Ready state by the operating system
– Allows for better service since any one process cannot
monopolize the processor for very long
– Applying to the time-sharing or real time
Scheduling
Categories of Scheduling Algorithms
• Batch
– Non-preemptive algorithms
– Preemptive algorithms with long time periods for each process
– Reduces processes switches and increase performance
• Interactive
– Preemptive algorithms are needed to prevent the situation that
the preemptive keeps one process from hogging the CPU and
denying service to the others. Even if no process intentionally ran
forever, one process might shut out all the others indefinitely due
to a program bug
• Real-Time
– Preemption normally used, but sometimes not needed because the
processes know that they may not run for long periods of time and
usually do their work and block quickly
Scheduling
Criteria/ Properties Term
• Fairness – equivalent processes get equivalent CPU times
• Policy enforcement – if the local policy is that safety
control processes get to run whenever they want to, even if
it means the payroll is 30 sec late, the scheduler has to make
sure this policy enforced
• Policy vs. Mechanism
– The policies what is to be done
– The mechanism specifies how it is to be done
– The mechanism is a thing that implements the policy
– Ex:
• The timer construct for ensuring CPU protection (mechanism)
• The decision of how long the timer is set for a particular user (policy)
Scheduling
Criteria/ Properties Term
• Throughput
– The number of processes that complete their
execution per time unit.
– Ex: In long processes, the rate may be one process per
hour. For short processes, it may be 10 processes per
second.
• Turnaround time
– Amount of time to execute a particular process.
– Is the sum of the periods spent waiting in the ready
queue, executing on the CPU, doing I/O, etc…
– Is the time from the process is submitted until it is
completed (time of complete – arrival time)
Scheduling
Criteria/ Properties Term
• CPU utilization
– The utilization of CPU.
– Can range from 0 to 100 percent.
– It should range from 40 percent (lightly loaded system) to 90
percent (heavily used system).
• Response time
– In an interactive system, turnaround time may not be the best
criterion.
– Often, a process can produce some output fairly early and can
continue computing new results.
– This measure is the amount of time it takes from when a
request was submitted until the first response is produced.
• Proportionality
– When a request that is perceived as complex takes a long time,
users accept that, but when a request that is perceived as simple
takes a long time, users get irritated
Scheduling
Categories of Scheduling Algorithms
• All systems
– Fairness – giving each process a fair share of CPU
– Policy enforcement – seeing that stated policy is carried out
– Balance – keeping all parts of the system busy
• Batch systems
– Throughput – maximize jobs per hour
– Turnaround time – minimize time between submission and
termination
– CPU utilization – keep the CPU busy all the time
• Interactive systems
– Response time – respond (react) to request quickly
– Proportionality – meet, if possible, user’s expectations
• Real-time systems
– Meeting deadlines – avoid losing data
– Predictability – avoid quality degradation in multimedia
systems
Scheduling in Batch System
First-Come First Served (FCFS)
• The simplest CPU scheduling algorithm!
• Is non-preemptive
• The process that entered the ready state first, will get
the CPU first and will hold it until it is blocked (or
finished)
• Simple to understand and implement
• It requires a single queue of ready processes:
– If a process enters the ready state, it is linked onto the tail of
the ready queue.
– If the CPU is free, it takes the process at the head.
Scheduling in Batch System
First-Come First Served (FCFS)
CPU
tail head


Scheduling in Batch System
First-Come First Served (FCFS)
CPU
tail head
P

New
Scheduling in Batch System
First-Come First Served (FCFS)
CPU
tail head


Scheduling in Batch System
First-Come First Served (FCFS)
CPU
tail head
P

New
Scheduling in Batch System
First-Come First Served (FCFS)
CPU
tail head


Scheduling in Batch System
First-Come First Served (FCFS)
CPU
tail head


Scheduling in Batch System
First-Come First Served (FCFS)
CPU
tail head

• Waiting time of each process: started process time – arrival time


• Ex:
– (Process:BurstTime) in order (P1:24), (P2:3), (P3:3)
– Waiting time for P1 = 0; P2 = 24; P3 = 27
– Average waiting time: (0 + 24 + 27)/3 = 17
– Average Turnaround time: (24 + 27 + 30)/3 = 27
Scheduling in Batch System
Example

Avg waiting time = 4.6


Avg turnaround time = 8.6

0 5 10 15 20

A
B
C
D
E
Scheduling in Batch System
First-Come First Served (FCFS)
• Consider FCFS scheduling in a dynamic situation where we have
one CPU-bound process and many I/O-bound process.
– The CPU-bound process will get and hold the CPU.
– All the other processes will finish their I/O and will move into the ready
queue, waiting for the CPU.
– Eventually, the CPU bound process moves to an I/O devices.
– All the I/O-bound process execute quickly and move back to the I/O queues.
– Again, the CPU-bound process will then move back and hold the CPU, and
all the I/O processes have to wait in the ready queue.
• The above situation is called a convoy effect.
– All the other processes wait for the one big process to get of the CPU.
– Result in lower CPU and device utilization.
• Ex:
– (Process:BurstTime) in order (P2:3), (P3:3), (P1:24)
– Waiting time for P1 = 6; P2 = 0; P3 = 3
– Average waiting time: (6 + 0 + 3)/3 = 3
– Average turnaround time: (6 + 3 + 30)/3 = 13
Scheduling in Batch System
First-Come First Served (FCFS)

(P1:24), (P2:3),
(P2:3), (P3:3),
(P3:3) (P1:24)
Avg Waiting Time 17 3
Avg Turnaround Time 27 13
Scheduling in Batch System
Shortest Job First (SJF)
• Runtime is known in advance (nonpreemptive)
• When several equally important jobs are sitting in the input queue
waiting to be started, the scheduler picks the shorted job first
– Another more appropriate term – shortest-next-CPU-burst scheduling
algorithm
– When the CPU is available, it is assigned to the process that has the smallest
next CPU burst
• Is the optimal algorithm (only) when all the jobs are available
simultaneously
• Ex:
– (Process:BurstTime) (P1:6), (P2:8), (P3:7), (P4: 3)
– Average waiting time: (3+ 16 + 9 + 0)/4 = 7
– Average turnaround time: (3 + 9 + 16 + 24)/4 = 13
Scheduling in Batch System
Example

Avg waiting time = 3.6


Avg turnaround time = 7.6

0 5 10 15 20

A
B
C
D
E
Scheduling in Batch System
Shortest Remaining Time Next (SRT)
• Is a preemptive version of shortest job first
• The scheduler always chooses the process whose remaining
runtime is the shortest
• Preempt the currently executing process, if the next CPU burst
of the newly arrived process is shorter than “what is left” of the
currently executing process
• When a new job arrives, its total is compared to the current
process’s remaining time.
• If the new job needs less time to finish than the current process,
the current process is suspended and the new job started
• Ex:
– (Process:ArrivalTime:BurstTime) (P1:0:9), (P2:2:4), (P3:4:1), (P4:5:4)
– Average waiting time: (9+ 1+ 0 + 2)/4 = 3
– Average turnaround time: (18 + 5 + 1 + 6)/4 = 7.5
Scheduling in Batch System
Example

Avg waiting time = 3.2


Avg turnaround time = 7.2

0 5 10 15 20

A
B min(1, 6) Min(5, 4)

C Min(5, 5, 2)

E Min(5, 2, 5)
Min(5, 5)
D
Summary
• Scheduling

Q&A
Next Lecture
• Scheduling (cont)
• Classic IPC Problems

You might also like