You are on page 1of 76

OPERATING SYSTEM

UNIT 2- PROCESSES AND


SCHEDULING
Unit 2
syllabus
Objective

 Process concepts
 Process Scheduling- process scheduler
 Operations on Processes
 Cooperating processes in the Operating System
Process
concepts
 A process can be thought of as a program in execution,
 A process is the unit of work in most systems.
 process will need certain resources—such as CPU time, memory, files,
and I/O devices —to accomplish its task. These resources are allocated
to the process either when it is created or while it is executing.
 Operating-system processes execute system code, and user
processes execute user code.
 a process have multiple threads.
Program Vs
Process
 program is a passive entity, such as a file containing a list of
instructions stored on disk (often called an executable file),
 whereas a process is an active entity, with a program counter specifying
the next instruction to execute and a set of associated resources.
 A program becomes a process when an executable file is loaded into
memory.
 Two common techniques for loading executable files are double-clicking an
icon representing the executable file and entering the name of the
executable file on the command line (as in prog. exe or a. out.)
Role of
OS
 the creation and deletion of both user and system
processes;
 the scheduling of processes;
 and the provision of mechanisms for synchronization,
communication, and deadlock handling for processes.
Process in
memory
 A process is more than the program code, which is sometimes
known as the text section.
 It also includes the current activity, as represented by the value
of the program counter and the contents of the processor's
registers.
 A process generally also includes the process stack, which
contains temporary data((such as function parameters, return
addresses, and local variables),
 data section,
 A process may also include a heap, which is memory that is
dynamically allocated during process run time
Process
State
 As a process executes, it changes state. The state of a process is defined in
part
by the current activity of that process.
 New. The process is being created.
 Running. Instructions are being executed.
 Waiting. The process is waiting for some event to occur (such as an
I/O completion or reception of a signal).
 Ready. The process is waiting to be assigned to a processor.
 Terminated. The process has finished execution.
Process control block
(PCB).

 Each process is represented in the


operating system by a process
control block
 (PCB)—also called a task control block.
 It contains many pieces of
information associated with a specific
process
Process control block

(PCB).
Process state. The state may be new, ready, running, waiting, halted, and so on.
 Program counter. The counter indicates the address of the next instruction to be
executed for this process.
 CPU registers. The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-
purpose registers
 CPU-scheduling information. This information includes a process priority, pointers
to
scheduling queues, and any other scheduling parameters.
 Memory-management information. This information may include such information as
the value of the base and limit registers, the page tables, or the segment tables
 Accounting information. This information includes the amount of CPU and real time
used, time limits, account numbers, job or process numbers,
 I/O status information. This information includes the list of I/O devices allocated to the
process, a list of open files, and so on.
Thread
s
 process is a program that performs a single thread of execution.
 allow a process to have multiple threads of execution and thus to
perform more than one task at a time.
Process Scheduling- process
scheduler
 The objective of multiprogramming is to maximize
CPU utilization.
 The objective of time sharing is to switch the CPU among
processes so frequently that users can interact with each program
while it is running.
 the process scheduler selects an available process for
program execution on the CPU
Scheduling Queues

 As processes enter the system, they are put into a job queue, which consists
of all processes in the system.
 The processes that are residing in main memory and are ready and waiting
to execute are kept on a list called the ready queue.
 This ready queue is generally stored as a linked list.
 ready-queue header contains pointers to the first and final PCBs in the
list. Each PCB includes a pointer field that points to the next PCB in the
ready queue.
 Device queue
In LINUX, all active processes are represented using a doubly linked list
of task_struct, and the kernel maintains a pointer — current — to the
process currently executing on the system.
The ready queue and various I/O device
queues.
Queueing-diagram representation of process
scheduling.
Queuing-diagram
 Each rectangular box represents a queue.
 Two types of queues are present: the ready queue and a set of device queues.
 The circles represent the resources that serve the queues, and the arrows indicate the flow of
processes in the system.
 A new process is initially put in the ready queue. It waits there until it is selected for execution,
or is dispatched.
 Once the process is allocated the CPU and is executing, one of several events could occur:
 • The process could issue an I/O request and then be placed in an I/O queue.
 • The process could create a new subprocess and wait for the subprocess's termination.
 • The process could be removed forcibly from the CPU, as a result of an interrupt, and
be
put back in the ready queue.
Scheduler
s
 long-term scheduler, or job scheduler
 short-term scheduler, or CPU
scheduler
 medium-term scheduler
Scheduler
 sscheduling queues throughout its lifetime. The
A process migrates among the various
operating system must select, for scheduling purposes, processes from these queues
in some fashion. The selection process is carried out by the appropriate scheduler.
 These processes are spooled to a mass-storage device where they are kept for
later execution.
 The long-term scheduler, or job scheduler, selects processes from this pool and
loads them into memory for execution.
 The short-term scheduler, or CPU scheduler, selects from among the processes that
are ready to execute and allocates the CPU to one of them.
 The primary distinction between these two schedulers lies in frequency of execution.
 Minutes , milliseconds (fast)
Long-term scheduler -controls the degree of
multiprogramming
 It is important that the long-term scheduler make a careful selection.
 In general, most processes can be described as either L/O bound or
CPU bound.
 I/O-bound process is one that spends more of its time doing I/O than it
spends doing computations.
 A CPU-bound process, in contrast, generates I/O requests infrequently,
using more of its time doing computations.
 It is important that the long-term scheduler select a good process mix of I/O-
bound and CPU-bound
medium-term scheduler
 such as time-sharing systems, may
introduce an additional, intermediate
level of scheduling.
 SWAPPING-remove processes from
memory Later, the process can be
reintroduced into memory, and its
execution can be continued where it left
off.
 The process is swapped out, and is later
swapped in, by the medium-term
scheduler.
 Swapping may be necessary to improve
the process mix or because
 a change in memory requirements has
overcommitted available memory,
Context Switch

 The context is represented in the PCB of the process; it includes the value of the CPU
registers, the process state and memory-management information.
 Switching the CPU to another process requires performing a state save of the current
process and a state restore of a different process. This task is known as a context
switch.
 When a context switch occurs, the kernel saves the context of the old process in its PCB and
loads the saved context of the new process scheduled to run.
 Context-switch time is pure overhead, because the system does no useful work
while switching.
Operations on
Processes
 Process Creation
 Process
Termination
Process
Creation
 A process may create several new processes, via a create-process system
call,
during the course of execution.
 The creating process is called a parent process, and the new processes
are called the children of that process.
 Each of these new processes may in turn create other processes, a tree
of processes.
 operating systems identify processes according to a unique process identifier
(or pid), which is typically an integer number.
Solari
s
 In Solaris, the process at the top of the tree is the sched process, with pid of 0.
 The sched process creates several children processes—including pageout and f sf lush.
These processes are responsible for managing memory and file systems.
 The sched process also creates the i n i t process, which serves as the root parent process
for all user processes.
 In Figure 3.9, we see two children of i n i t — inetd and dtlogin.
 inetd is responsible for networking services such as t e l n e t and ftp;
 dtlogin is the process representing a user login screen. When a user logs in, dtlogin creates
an X-windows session
 (Xsession), which in turns creates the sdt_shel process.
 command-line shell—the C-shell or csh—is created
Resource
Sharing
 When a process creates a subprocess, that subprocess may be able to
obtain its resources directly from the operating system, or it may
be constrained to a subset of the resources of the parent process.
 The parent may have to partition its resources among its children,
 or it may be able to share some resources (such as memory or files)
among several of its children.
 Restricting a child process to a subset of the parent's resources
prevents any process from overloading the system by creating too
many subprocesses.
Executio
n
 When a process creates a new process, two possibilities exist in terms
of
 execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
 There are also two possibilities in terms of the address space of the
new process:
1. The child process is a duplicate of the parent process (it has the
same program and data as the parent).
2. The child process has a new program loaded into it.
Process
 A processTermination
terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit () system call.
 At that point, the process may return a status value (typically an integer) to its parent
process
(via the wait() system call).
 All the resources of the process—including physical and virtual memory, open files,
and I/O buffers—are deallocated by the operating system.
 A parent may terminate the execution of one of its children for a variety of
 reasons, such as these:
 • The child has exceeded its usage of some of the resources that it has been allocated.
 • The task assigned to the child is no longer required.
 • The parent is exiting, and the operating system does not allow a child to continue if its
parent terminates.
Cooperating processes in the Operating System

 there are many processes which may be either independent processes or


cooperating
processes that run in the operating system.
 A process is said to be independent when it cannot affect or be affected
by any other processes that are running the system.
 a cooperating process is one which can affect or affected by any another process
that is running on the computer. the cooperating process is one which shares data
with another process.
 1. Information sharing-many users may want the same piece of information
 2. Computation speedup-each subtask will be executing in parallel with another one
 3. Modularity-system dividing its functions into separate processes.
 4. Convenience-may have many tasks to perform at the same time
Data
sharing
 Directly share a logical address data space (i.e. code & data) - This may
result in data inconsistency. It is implemented on threads.
 Share data only through files/ messages - So we will deal with various to
order....orderly execution of cooperating process so that data
consistency is maintained.
Example- producer-consumer problem
 Producer  Consumer
process process
CPU
Scheduling
 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
 Multiple-Processor
Scheduling
 Real-Time CPU Scheduling
Objective
s
 To introduce CPU scheduling, which is the basis
for multiprogrammed operating systems
 To describe various CPU-scheduling algorithms
 To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system
Basic Concepts

 Maximum CPU
utilization obtained
with
multiprogramming
 CPU–I/O Burst Cycle –
Process execution consists
of a cycle of CPU execution
and I/O wait
 CPU burst followed by I/O
burst
 CPU burst distribution is of
main concern
Histogram of CPU-burst
Times
CPU
Scheduler
■ Short-term scheduler selects from among the processes in
ready queue, and allocates the CPU to one of them
● Queue may be ordered in various ways
■ CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
■ Scheduling under 1 and 4 is nonpreemptive
■ All other scheduling is preemptive
● Consider access to shared data
● Consider preemption while in kernel mode
● Consider interrupts occurring during crucial OS
activities
Dispatche
r
 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 running
Scheduling
Queues
 As processes enter the system, they are put into a job queue, which
consists of all processes in the system.
 The processes that are residing in main memory and are ready and
waiting to execute are kept on a list called the ready queue.
 This ready queue is generally stored as a linked list.
 ready-queue header contains pointers to the first and final PCBs in the
list. Each PCB includes a pointer field that points to the next PCB in the
ready queue.
 Device queue
Scheduling
Criteria
 CPU utilization – keep the CPU as busy as possible
 Throughput – # of processes that complete their
execution per time unit
 Turnaround time – amount of time to execute a
particular
process
 Waiting time – amount of time a process has been
waiting in the ready queue
 Response time – amount of time it takes from when a
request was submitted until the first response is
produced, not output (for time-sharing environment)
Scheduling Algorithm Optimization
Criteria
 Max CPU utilization
 Max throughput
 Min turnaround
time
 Min waiting time
 Min response time
First- Come, First-Served (FCFS)
Scheduling
Process Burst Time
P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 ,
P2 , P 3
The Gantt Chart for the schedule is:
P1 P2 P3

0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 =


27
 Average waiting time: (0 + 24 + 27)/3
= 17
FCFS Scheduling
(Cont.)
Suppose that the processes arrive in the
order:
P 2 , P3 , P 1
 The Gantt chart for the schedule is:
P2 P3 P1

0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect - short process behind long process
 Consider one CPU-bound and many I/O-bound
processes
Shortest-Job-First (SJF)
Scheduling
 Associate with each process the length of its next CPU
burst
 Use these lengths to schedule the process
with the shortest time
 SJF is optimal – gives minimum average waiting time
for a given set of processes
 The difficulty is knowing the length of the next
CPU request
 Could ask the user
Example of
SJF
ProcessArrival Time Burst
Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

 SJF scheduling
chart
P4 P1 P3 P2

0 3 9 16 24

 Average waiting time = (3 + 16 + 9 +


0) / 4 = 7
Determining Length of Next CPU
Burst
 Can only estimate the length – should be similar to
the previous one
 Then pick process with shortest predicted next CPU
burst

 Can bet done


exponential
1. = bylength
usingofthe
averaging
actual
th length of previous CPU bursts,
nCPU burst
n
using
2. τ n+1 = predicted value for the next CPU burst
3. α, 0 ≤ α ≤ 1
4. Define : τ n=1 = α tn + (1−α )τ n .

 Commonly, α set to ½
 Preemptive version called shortest-remaining-time-
first
Prediction of the Length of the Next CPU
Burst
Eαx=0amples of Exponential
Averaging
τ =τn+1 n τ n+1 = α tn + (1−α )τ n .
 Recent history does not
count  tn=most
 α =1 recent
 info
τ
n past
 τn+1 =
 Only the tactual
n last CPU burst history
=
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
 Now we add the concepts of varying arrival times and
preemption to the analysis
ProcessA arri Arrival TimeT Burst
Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt
Chart

P1 P2 P4 P1 P3

0 1 5 10 17 26

 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 =


26/4 = 6.5
msec
Priority
Scheduling
 A priority number (integer) is associated with each
process

 The CPU is allocated to the process with the highest


priority
(smallest integer ≡ highest priority)
 Preemptive
 Nonpreemptive

 SJF is priority scheduling where priority is the inverse of


predicted next CPU burst time

 Problem ≡ Starvation – low priority processes may


never execute

 Solution ≡ Aging – as time progresses increase the


priority of
Example of Priority
Scheduling
ProcessA arri Burst TimeT Priorit
y
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
 Priority scheduling Gantt
Chart

 Average waiting time = 8.2


msec
Priority
scheduling
 Starvation:
Low priority process waiting indefinitely for the CPU
 Aging
 Gradually increase the priority of the processes which is waiting
for a long time
Round Robin
(RR)
 Each process gets a small unit of CPU time (time
quantum q), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and added
to the end of the ready queue.
 If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU
time in chunks of at most q time units at once. No
process waits more than (n-1)q time units.
 Timer interrupts every quantum to schedule next
process
 Performance
 q large ⇒ FIFO

 q small ⇒ q must be large with respect to


context switch, otherwise overhead is too high
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

 Typically, higher average turnaround than SJF, but


better
response
 q should be large compared to context switch time
 q usually 10ms to 100ms, context switch < 10 usec
Time Quantum and Context Switch
Time
Turnaround Time Varies With The Time
Quantum

80% of CPU bursts


should be shorter than q
Multilevel
Queue
 Ready queue is partitioned into separate queues, eg:
 foreground (interactive)
 background (batch)
 Process permanently in a given queue
 Each queue has its own scheduling algorithm:
 foreground – RR
 background – FCFS
 Scheduling must be done between the queues:
 Fixed priority scheduling; (i.e., serve all from
foreground then from background). Possibility of
starvation.
 Time slice – each queue gets a certain amount of CPU
time which it can schedule amongst its processes; i.e.,
80% to foreground in RR
 20% to background in FCFS
Multilevel Queue
Scheduling
Multilevel Feedback Queue

 A process can move between the various queues; aging


can be implemented this way
 Multilevel-feedback-queue scheduler defined by the
following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process
will enter when that process needs service
Example of Multilevel
Feedback Queue
 Three
queues:
 Q0 – RR with time
milliseconds
quantum 8
 Q1 – RR 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
 At Q1 job is again served FCFS
to queue Q1
andreceives 16 additional milliseconds
 If it still does not complete, it is
preempted and moved to queue
Q2
Multiple-Processor
Scheduling
 CPU scheduling more complex when multiple CPUs
are available
 Homogeneous processors within a multiprocessor
 Asymmetric multiprocessing – only one processor
accesses the system data structures, alleviating the
need for data sharing
 Symmetric multiprocessing (SMP) – each processor is self-
scheduling, all processes in common ready queue, or
each has its own private queue of ready processes
 Currently, most common
 Processor affinity – process has affinity for processor on
which it is currently running
 soft affinity
 hard affinity
 Variations including processor sets
Multiple-Processor Scheduling – Load
Balancing

 If SMP, need to keep all CPUs loaded for efficiency


 Load balancing attempts to keep workload
evenly distributed
 Push migration – periodic task checks load on each
processor, and if found pushes task from overloaded
CPU to other CPUs
 Pull migration – idle processors pulls waiting task from
busy processor
Multicore
Processors
 Recent trend to place multiple processor cores on
same physical chip
 Faster and consumes less power
 Multiple threads per core also growing
 Takes advantage of memory stall to make
progress on another thread while memory
retrieve happens
Real-Time CPU
Scheduling
 Can present obvious
challenges
 Soft real-time systems – no
guarantee as to when
critical real-time process
will be scheduled
 Hard real-time systems – task
must be serviced by its
deadline
 Two types of latencies
affect performance
1. Interrupt latency – time from
arrival of interrupt to start of
routine that services interrupt

2. Dispatch latency – time for


schedule to take current
process off CPU and switch to
another
Real-Time CPU
Scheduling
 Hard Real time System
 Resource Reservation
- Scheduler admit and guarantee to complete on
time
- Special s/w and h/w
- Impossible with sec memory
Soft Real time System
Real time process high priority
No degradation for real time process
priority Dispatch latency should be very
small
- allow system calls preemptible
-safe locations for sys call
Real-Time CPU Scheduling
(Cont.)
 Conflict phase of
dispatch
latency:
1. Preemption
of any
process
running in
kernel mode
2. Release by
low-
priority
process of
resources
needed by
high-
priority
processes
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
THANK
YOU

You might also like