Osc Unit 2
Osc Unit 2
1. Process Concepts
2. Process Scheduling
3. Operation on Processes
4. CPU Scheduling
5. Scheduling Criteria
6. Scheduling Algorithms
• First Come First Serve (FCFS)
• Shortest Job First (SJF)
• Round Robin (RR)
7. Multilevel Queue Scheduling
8. Case Studies
• Unix
• Linux
• Windows
• Exam Questions
Process Concepts
• Process
• Process States
• Process Control Block (PCB)
• Switch from Process to Process
• Process Scheduling Queues
• Schedulers
|<<
2
3
Process
• A program in execution is called a Process.
• includes:
– Program Counter (current activity and
contents of reg.)
– Stack(temporary data, local variables,return)
– Data Section(global variables, static variables)
– Heap.(store dynamic memory allocated
– variables)
– Text.(code section)
|<<
4
5
Process States
As a process executes, it changes state
Process States:
The states of a process are as follows:
New (Create): In this step, the process is about to be created but
not yet created. It is the program that is present in secondary
memory that will be picked up by OS to create the process.
Ready: New -> Ready to run. After the creation of a process, the
process enters the ready state i.e. the process is loaded into the
main memory. The process here is ready to run and is waiting to
get the CPU time for its execution. Processes that are ready for
execution by the CPU are maintained in a queue called ready
queue for ready processes. 7
Run: The process is chosen from the ready queue by the CPU for
execution and the instructions within the process are executed by
any one of the available CPU cores.
8
Process Control Block (PCB)
9
Context Switching in OS (Operating System):
10
CPU Switch From Process to Process
|<<
11
Steps for Context Switching:
14
Process Scheduling Queues
• Ready queue(RAM)
– Set of all processes residing in main memory, ready and waiting to
execute
• Device queues
– Set of processes waiting for an I/O device
15
Ready Queue and Various I/O Device Queues
16
Representation of Process Scheduling
|<<
17
The Operating system manages various types of queues for each
of the process states. The PCB related to the process is also
stored in the queue of the same state. If the Process is moved
from one state to another state then its PCB is also unlinked
from the corresponding queue and added to the other state queue
in which the transition is made.
18
Schedulers
1.Long–term Scheduler (or Job scheduler)
• Long term scheduler is also known as job scheduler. It chooses the processes from the pool (secondary
memory) and keeps them in the ready queue maintained in the primary memory.
• Long Term scheduler mainly controls the degree of Multiprogramming. The purpose of long term scheduler is
to choose a perfect mix of IO bound and CPU bound processes among the jobs present in the pool.
• If the job scheduler chooses more IO bound processes then all of the jobs may reside in the blocked state all the
time and the CPU will remain idle most of the time. This will reduce the degree of Multiprogramming.
Therefore, the Job of long term scheduler is very critical and may affect the system for a very long time.
19
2.Short–term Scheduler (or CPU scheduler or Dispatcher)
-Selects from among the processes that are ready to execute and
allocates the CPU to one of them.
-invoked whenever an event occurs, leading to interruption.
Ex: clock interrupts, I/O interrupts, operating system calls,
signals, etc.
-A scheduling algorithm is used to select which job is going to
be dispatched for the execution. The Job of the short term
scheduler can be very critical in the sense that if it selects job
whose CPU burst time is very high then all the jobs after that,
will have to wait in the ready queue for a very long time.
-This problem is called starvation which may arise if the short
term scheduler makes some mistakes while selecting the job.
-It must select a new process for the CPU frequently(high
usage). It must be very fast.
20
3.Addition of Medium Term Scheduling
• In order to free up main memory for other processes, swapping the process
back in later when more memory is available, or when the process has been
unblocked and is no longer waiting for a resource.
Question: Locate where Medium term scheduler works?
|<<
21
Contd..
22
Threads:
A thread is the subset of a process and is also known as the lightweight
process. A process can have more than one thread, and these threads are
managed independently by the scheduler. All the threads within one
process are interrelated to each other. Threads have some common
information, such as data segment, code segment, files, etc., that is
shared to their peer threads. But contains its own registers, stack, and
counter.
23
Process Thread
A process is an instance of a program that is being Thread is a segment of a process or a lightweight
executed or processed. process that is managed by the scheduler
independently.
Processes are independent of each other and hence don't Threads are interdependent and share memory.
share a memory or other resources.
Each process is treated as a new process by the The operating system takes all the user-level threads
operating system. as a single process.
If one process gets blocked by the operating system, If any user-level thread gets blocked, all of its peer
then the other process can continue the execution. threads also get blocked because OS takes all of them
as a single process.
Context switching between two processes takes much Context switching between the threads is fast because
time as they are heavy compared to thread. they are very lightweight.
The data segment and code segment of each process are Threads share data segment and code segment with
independent of the other. their peer threads; hence are the same for other
threads also.
The operating system takes more time to terminate a Threads can be terminated in very little time.
process.
New process creation is more time taking as each new A thread needs less time for creation.
process takes all the resources.
24
Operation on Processes
• Process Creation
• Parent process creates children processes, which, in turn
create other processes, forming a tree of processes
-Using create –process system call.
• Resource sharing
– Parent and children share all resources.
– Children share subset of parent’s resources.
– Parent and child share no resources.
• Execution
– Parent and children execute concurrently.
– Parent waits until children terminate.
Operating System Concepts
Process Creation (Cont.)
• Address space
– Child is duplicate of parent(same program and data as
parent).
– Child has a new program loaded into it.
• UNIX examples
– Fork() system call creates a new duplicate process
– exec() system call is used after a fork to replace the
current process’s memory space with a new program.
fork() is used to create a new process that is an exact duplicate of the calling
process, while exec() is used to replace the current process image with another
one, typically after a fork() system call.
Operating System Concepts
A Tree of Processes On A Typical UNIX System
35
Dispatcher
• Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler; this
involves:
– switching context
– switching to user mode
– jumping to the proper location in the user
program to restart that program
• Dispatch latency – time it takes for the dispatcher
to stop one process and start another process.
Scheduling Criteria
•CPU utilization – keep the CPU as busy as possible
•Throughput – no.of processes that complete their
execution per unit time.
•Turnaround time – amount of time to execute a
particular process.(Time of submission of a process to
the time of completion)
Turnaround time = Completion time - arrival time
or
Turnaround time = Waiting time + burst time
ResponseTime=ArrivalTime-Ready\QueueTime
• Waiting Time
A scheduling algorithm does not affect the time required to
complete the process once it starts execution. It only
affects the waiting time of a process i.e. time spent by a process
waiting in the ready queue.
38
Scheduling Algorithm Optimization Criteria
• FCFS Scheduling
• SJF Scheduling
– Non-Preemptive
– Preemptive
• Priority Scheduling
• Round Robin
• Multilevel Queue Scheduling
|<<
40
41
42
43
44
First- Come, First-Served (FCFS) Scheduling
P2 P3 P1
0 3 6 30
• Commonly, α set to ½
• Preemptive version called shortest-remaining-time-
first
Prediction of the Length of the Next CPU Burst
Examples of Exponential Averaging
• =0
– n+1 = n
– Recent history does not count
• =1
– n+1 = tn
– Only the actual last CPU burst counts
• If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
• Since both and (1 - ) are less than or equal
to 1, each successive term has less weight than
its predecessor
Example of Shortest-remaining-time-first
• After this time has elapsed, the process is preempted and added
to the end of the ready queue.
53
Example of RR with Time
Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Gantt chart:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
• Examples
– If a process in a queue dedicated to interactive processes
consumes too much CPU time, it will be moved to a (lower-
priority) queue.
– A process that waits too long in a lower-priority queue may be
moved to a higher-priority queue.
60
Multilevel Feedback Queue
64
Exercise: Five batch jobs P1 through P5 arrive for execution at
times indicated. Each process has a total CPU time requirement as
listed below:
|<<
65
Exercise: Five batch jobs P1 through P5 arrive for execution at
times indicated. Each process has a total CPU time requirement as
listed below:
Using Preemptive SJF, determine the average turnaround and
average waiting times. Also draw the Gantt charts.
|<< 66
More SJF Examples
1. SJF non-preemptive Proc Arrives Burst
P1 0 8
P2 1 4
P3 2 9
P4 3 5
And then preemptive
67
More Priority Examples
• Example Proc Arrival Burst Priority
P1 0 6 5
P2 2 2 3
P3 3 3 4
P4 9 3 2
P5 10 1 1
68
Five batch jobs P1 through P5 arrive for execution at times
indicated. Each process has a total CPU time requirement as listed
below:
Using RR with Q = 3 units, determine the average turnaround and
average waiting times. Also draw the Gantt charts.
Process Arrival CPU
time burst
P1 2 10
P2 1 2
P3 0 5
P4 4 6
P5 3 4
|<< 69
Multilevel Queue Examples
• ML queue, 2 levels
– RR @ 10 units
– FCFS
– RR gets priority over FCFS
• Proc Arrival Burst Queue
P1 0 12 FCFS
P2 4 12 RR
P3 8 8 FCFS
P4 20 10 RR
• Non-preemptive and preemptive
70
Example of Multilevel Feedback Queue
• Three queues:
– Q0 – time quantum 8 milliseconds
– Q1 – time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served FCFS. When it gains
CPU, job receives 8 milliseconds. If it does not finish in 8
milliseconds, job is moved to queue Q1.
71
Multilevel Feedback Queue Example
• Three levels
– RR at 8 units
– RR at 16 units
– FCFS, active
• Proc Arrival Burst
P1 0 32
P2 10 12
P3 30 10
• Non-preemptive and preemptive
72
Operating System Examples
• Windows XP scheduling
• Linux scheduling
73
Windows Scheduling
• Windows uses priority-based preemptive scheduling
• Highest-priority thread runs next
• Dispatcher is scheduler
• Thread runs until
– (1) blocks,
– (2) uses time slice,
– (3) preempted by higher-priority thread
• Real-time threads can preempt non-real-time
• 32-level priority scheme
• Variable class is 1-15, real-time class is 16-31
• Priority 0 is memory-management thread
• Queue for each priority
• If no run-able thread, runs idle thread
74
Windows Priority Classes
• Win32 API identifies several priority classes to which a process can belong
REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,
ABOVE_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CL
ASS, BELOW_NORMAL_PRIORITY_CLASS,
IDLE_PRIORITY_CLASS
All are variable except REALTIME
• A thread within a given priority class has a relative priority
TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL,
BELOW_NORMAL, LOWEST, IDLE
• Priority class and relative priority combine to give numeric priority
• Base priority is NORMAL within the class
• If quantum expires, priority lowered, but never below base
• If wait occurs, priority boosted depending on what was waited for
• Foreground window given 3x priority boost
75
Windows XP Priorities
76
Linux Scheduling
• Constant order O(1) scheduling time
• Preemptive, priority based
• Two priority ranges: time-sharing and real-time
• Real-time range from 0 to 99 and nice value from 100 to 140
• Map into global priority with numerically lower values
indicating higher priority
• Higher priority gets larger q
• Task run-able as long as time left in time slice (active)
• If no time left (expired), not run-able until all other tasks use
their slices
• The kernel maintains a list of all runnable tasks in a runqueue
• Because of its support for SMP, each processor maintains its
own queue and schedules itself independently.
• Each runqueue contains two priority arrays:
– Two priority arrays (active, expired)
– Tasks indexed by priority
– When no more active(active array is empty), arrays are
exchanged 77
Priorities and Time-slice length
78
List of Tasks Indexed
According to Priorities
79
Linux Scheduling (Cont.)
• Real-time scheduling according to POSIX.1b
– Real-time tasks have static priorities
• All other tasks have dynamic priorities that are based on
nice value plus or minus 5
– Interactivity of task determines plus or minus
• More interactive -> more minus
(A task’s interactivity is determined by how long it
has been sleeping while waiting for I/O.)
– Priority recalculated when task expired
– This exchanging arrays implements adjusted
priorities
80
Exam Questions
1. Explain various steps involved in change of a Process State.
2. Define Process States.
3. What is a process? Write the difference between process and
program.
4. Define Process and Program.
5. Compare & Contrast Process & Thread.
6. What is the difference between a "thread" and a "process"?
7. Compare the difference between a thread and a process. List
the system calls related to threads.
8. Define a thread. What are the uses of thread?
9. What are the reasons for Process Suspension?
10. What is process control block? Explain its structure.
Exam Questions
11. Describe the process state transition diagram. Explain Process
Control Block.
12. What are the types of CPU Scheduling?
13. What are preemptive and non-preemptive scheduling policies?
14. Compare preemptive and non-preemptive CPU scheduling.
15. What is CPU scheduler?
16. What is throughput, turnaround time, waiting time and
response time?
17. Explain any three CPU Scheduling algorithms.
Exam Questions |<<
18. Assume the following are the jobs to execute with one
processor:
Job Burst Priorit
Time y
1 10 3
2 1 1
3 2 3
4 1 4
The jobs are assumed to have arrived in the order 1, 2, 3, 4, 5.
5 5 2
a. Give Gantt–Chart illustrating the execution of these jobs
using FCFS, RR (quantum =1), Shortest Process Next, Shortest
Remaining Time.
b. What is the turn–around time, waiting time of each job for
each of the above scheduling algorithms?