Professional Documents
Culture Documents
Process Management
Process Concept
When the OS runs a program (i.e., a binary executable), this program is
loaded into memory and the control is transferred to this programs first
instruction. Then, the program starts to run.
A process is a program in execution.
A process is more than a program, because the former has a program
counter, stack, data section and so on (i.e., the runtime stuffs).
Moreover, multiple processes may be associated with one program (e.g.,
runs the same program, a web browser, twice).
An operating system executes a variety of programs:
Batch system jobs
Time-shared systems user programs or tasks
(Process in Memory)
Process State
At any moment, a process can be in one of the five states: new, running, waiting,
ready and terminated.
1. New: The process is being created
2. Running: The process is being executed
3. Waiting: The process is waiting for some event to occur (e.g., waiting for I/O
completion)
4. Ready: The process is waiting to be assigned to a processor.
5. Terminated: The process has finished execution.
Threads
A process is a program that performs a single thread of execution.
Process Scheduling
Since the number of processes is always larger than the number of CPUs, the
OS must maintain maximum CPU utilization.
To determine which process can do what, processes are chained into a
number of
Scheduling queues.
For example, in addition to the ready queue, each event may have its own
scheduling queue (i.e., waiting queue).
Process Scheduling Queues
Job queue set of all processes in the system
Ready queue set of all processes residing in main memory, ready and
waiting to execute
Device queues set of processes waiting for an I/O device
Processes migrate among the various queues
Ready Queue And Various I/O Device Queues
Schedulers
There are three types of schedulers
1. Long-Term (Job) Scheduler:
Selects which processes should be brought into the ready queue.
Invoked very infrequently (seconds, minutes); may be slow.
controls the degree of multiprogramming
2. Short-Term (CPU) scheduler:
Selects which process should execute next and allocates CPU.
invoked very frequently (milliseconds) - must be very fast
3. Medium-Term Scheduler:
swaps out process temporarily
balances load for better throughput
When CPU switches to another process, the system must save the state of the
old process and load the saved state for the new process
Context-switch time is overhead; the system does no useful work while
switching
Time dependent on hardware support
Operations on Processes
There are three commonly seen operations:
Process Creation: Create a new process. The newly created is the child of the
original. Use fork() or vfork() in Unix to create new processes.
Process Termination: Terminate the execution of a process. Under Unix, use
exit().
Process Join: Wait for the completion of a child process. Under Unix use
wait().
fork(), vfork(), exit() and wait() are system calls.
Use ps aux to see all running processes
Process Creation
Parent process create children processes, which, in turn create other
processes, forming a tree of processes
Resource sharing
o Parent and children share all resources
o Children share subset of parents resources
o Parent and child share no resources
Execution
o Parent and children execute concurrently
o Parent waits until children terminate
Address space
o Child duplicate of parent
o Child has a program loaded into it
UNIX examples
o fork system call creates new process
o exec system call used after a fork to replace the process memory
space with a new program
Synchronization
o Message passing may be either blocking or non-blocking
o Blocking is considered synchronous
Blocking send has the sender block until the message is
received
Blocking receive has the receiver block until a message is
available
o Non-blocking is considered asynchronous
Non-blocking send has the sender send the message and
continue
Non-blocking receive has the receiver receive a valid message
or null
Buffering
Queue of messages attached to the link; implemented in one of three ways
1.
Zero capacity 0 messages Sender must wait for receiver
(rendezvous)
2.
Bounded capacity finite length of n messages Sender must wait if
link full
3.
Unbounded capacity infinite length Sender never waits
Process execution repeats the CPU burst and I/O burst cycle.
When a process begins an I/O burst, another process can use the CPU for a
CPU burst.
The durations of CPU bursts have been measured extensively. Although they
vary greatly from process to process and from computer to computer they tend to
have a frequency curve similar to that shown in the above Figure. The curve is
generally characterized as exponential or hyper exponential, with a large number of
short CPU bursts and a small number of long CPU bursts. An I/O-bound program
typically has many short CPU bursts. A CPU-bound program might have a few
long CPU bursts. This distribution can be important in the selection of an
appropriate CPU-scheduling algorithm.
CPU-bound and I/O-bound
A process is CPU-bound if it generates I/O requests infrequently, using more
of its time doing computation.
A process is I/O-bound if it spends more of its time to do I/O than it spends
doing computation.
A CPU-bound process might have a few very long CPU bursts.
An I/O-bound process typically has many short CPU bursts.
Levels of Scheduling
High Level Scheduling or Job Scheduling
o Selects jobs allowed to compete for CPU and other system resources.
Intermediate Level Scheduling or Medium Term Scheduling
o Selects which jobs to temporarily suspend/resume to smooth
fluctuations in system load.
Low Level (CPU) Scheduling or Dispatching
o Selects the ready process that will be assigned the CPU
o Ready Queue contains PCBs of processes.
CPU Scheduler
What does a CPU scheduler do?
When the CPU is idle, the OS must select another process to run.
This selection process is carried out by the short-term scheduler (or CPU
scheduler).
The CPU scheduler selects a process from the ready queue, and allocates
the CPU to it.
The ready queue does not have to be a FIFO one. There are many ways to
organize the ready queue.
Selects from among the processes in memory that are ready to execute, and
allocates the CPU to one of them.
Non-preemptive Scheduling
o Once CPU has been allocated to a process, the process keeps the CPU
until
Process exits OR
Scheduling Criteria
There are many criteria for comparing different scheduling algorithms. Here are
five common ones:
1.
2.
3.
4.
5.
CPU Utilization
Throughput
Turnaround Time
Waiting Time
Response Time
CPU Utilization:
o Keep the CPU and other resources as busy as possible
o CPU utilization ranges from 0 to 100 percent.
o Normally 40% is lightly loaded and 90% or higher is heavily loaded.
Throughput:
o Number of processes that complete their execution per time unit.
o Higher throughput means more jobs get done.
o However, for long processes, this rate may be one job per hour, and, for
short (student) jobs, this rate may be 10 per minute.
Turnaround time:
o Amount of time to execute a particular process from its entry time.
o From a users point of view, turnaround time is more important than CPU
utilization and throughput.
o Turnaround time is the sum of
waiting time before entering the system
waiting time in the ready queue
waiting time in all other events (e.g., I/O)
time the process actually running on the CPU
Waiting time :
Amount of time a process has been waiting in the ready queue.
o Why only ready queue?
Scheduling Algorithms
We will discuss a number of scheduling algorithms:
1. First-Come, First-Served (FCFS)
2. Shortest-Job-First (SJF)
3. Priority
4. Round-Robin
5. Multilevel Queue
6. Multilevel Feedback Queue
1. First-Come, First-Served:
Policy: Process that requests the CPU FIRST is allocated the CPU FIRST.
o FCFS is a non-preemptive algorithm.
Implementation - using FIFO queues
o Incoming process is added to the tail of the queue.
o Process selected for execution is taken from head of queue.
Performance metric - Average waiting time in queue.
FCFS is not preemptive. Once a process has the CPU, it will occupy the
CPU until the process completes or voluntarily enters the wait state.
Example
Process Burst Time
P1
24
P2
P3
P1
24
P2
27
P3
30
Example 2
Suppose that the processes arrive in the order
P2 , P3 , P1
Then Gantt chart for the schedule is:
0
P2
P3
P1
30
FCFS: Problems
It is easy to have the convoy effect: all the processes wait for the one
big process to get off the CPU. CPU utilization may be low. Consider
a CPU-bound process running with many I/O-bound process.
It is in favor of long processes and may not be fair to those short ones.
What if your 1-minute job is behind a 10-hour job?
It is troublesome for time-sharing systems, where each user needs to
get a share of the CPU at regular intervals.
Shortest-Job-First (SJF) Scheduling
Each process in the ready queue is associated with the length of its next CPU
burst.
When a process must be selected from the ready queue, the process with the
smallest next CPU burst is selected.
Thus, the processes in the ready queue are sorted in CPU burst length.
Two schemes:
o non preemptive once CPU given to the process it cannot be
preempted until completes its CPU burst
o Preemptive if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt. This scheme is
known as the Shortest-Remaining-Time-First (SRTF)
SJF is optimal gives minimum average waiting time for a given set of
processes
Example of Non-Preemptive SJF
Process
Arrival Time
Burst Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
SJF (non-preemptive)
0
P1
3
P3
7 8
P2
12
P4
16
Proces Arrival
s
Time
A
B
C
D
How the Scheduling done:
Gang chart
0
1
2
3
Burs
t
Time
8
4
9
5
Define
SJF Problems:
It is difficult to estimate the next burst time value accurately.
SJF is in favor of short jobs. As a result, some long jobs may not have a
chance to run at all. This is starvation.
The preemptive version is usually referred to as shortest-remaining-timefirst scheduling, because scheduling is based on the remaining time of a
process.
Priority Scheduling
A priority number (integer) is associated with each process. Can be based on
o Cost to user
o Importance to user
o Aging
o %CPU time used in last X hours.
The CPU is allocated to the process with the highest priority (smallest
integer highest priority)
o Preemptive
o nonpreemptive
SJF is a priority scheduling where priority is the predicted next CPU burst
time
Problem Starvation low priority processes may never execute
Solution Aging as time progresses increase the priority of the process
The scheduler always picks the process (in ready queue) with the highest
priority to run.
Then:
Burst Time
53
17
68
24
P1
0
P2
20
P3
37
P4
57
P1
77
P3
97
P4
117
P1
121
P3
134
P3
154
Other Examples:
162
Thir
d example:
A process P can run only if all queues above the queue that contains P are
empty.
When a process is running and a process in a higher priority queue comes in,
the running process is preempted.
Scheduling
o 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.
o At Q1 job is again served FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted and moved
to queue Q2.