Process Management in OS
Process Management in OS
Meti Dejene
tiimeekoo@gmail.com
Haramaya University
Chapter 2 - Processes and Threads
Overview
Early computers allowed only one program to be executed at a time.
This program had complete control of the system and had access to all the system’s
resources.
This evolution required firmer control and more compartmentalization of the various
programs.
That is why it is stated that, one of the services provided by an operating system is process
management.
Process
But what is process in the context of operating system?
For example,
In a given computer a user may have started (1) a video editing program and
instructed it to convert a one-hour video to a certain format (something that can take
hours) and (2) then gone off to surf the Web. (3) Meanwhile, a background process that
wakes up periodically to check for incoming email may have started running.
Multiprogramming: The concurrent residency of more than one program in the main
memory to increase the CPU utilization and improves the throughput by reducing the CPU
idle time
Process management is one of the most important abstractions that operating systems
provide to manage all process related activities.
With process abstraction the OS supports the ability to have (pseudo) concurrent
operation even when there is only one CPU available.
Cont.
So, in any multiprogramming system, the CPU switches from process to process
quickly back and forth, running each for tens or hundreds of milliseconds.
While, strictly speaking, at any one instant the CPU is running only one process, in the
course of 1 second it may work on several of them, giving the illusion of parallelism.
Overall, the OS is responsible for the following activities in connection with process
management:
scheduling of processes;
The address space contains the executable program, the program’s data, and its
stack (which contains temporary data such as function parameters, return
addresses, and local variables).
The program counter (PC) is a register that manages the memory address of
the instruction to be executed next.
Cont.
The memory layout of a process is typically
divided into multiple sections.
1. System initialization.
Some of these are foreground processes, that is, processes that interact with (human)
users and perform work for them.
Others run in the background and are not associated with particular users, but
instead have some specific function.
Processes that stay in the background to handle some activity such as email, Web
pages, news, printing, and so on are called daemons.
In UNIX, the ps program can be used to list the running processes and in Windows, the
task manager can be used.
Cont.
This is often when a running process issue system calls to create one or
more new processes to help it do its job.
Taking either of these actions starts a new process and runs the selected program in it.
Cont.
4. Initiation of a batch job.
The last situation in which processes are created applies only to the batch systems
found on large mainframes.
Here users can submit batch jobs to the system and when the operating system decides
that it has the resources to run another job, it creates a new process and runs the next
job from the input queue in it.
In UNIX, there is only one system call to create a new process: fork.
However, nothing lasts forever, not even processes. Sooner or later the new process will
terminate, usually due to one of the following conditions:
Word processors, Internet browsers, and similar programs always have an icon or menu
item that the user can click to tell the process to remove any temporary files it has
open and then terminate .
Cont.
2. Error exit
The other reason for termination is when fatal is error caused by the process, often due to
a program bug.
Examples include
a user types a command tries to compile a program and no such file exists, the
compiler simply announces this fact and exits.
Cont.
3. Killed by another process (involuntary).
The fourth reason is when a process executes a system call telling the operating system to
kill some other process.
Usually, such a system call can be invoked only by the parent of the process that is to be
terminated. (the killer must have the necessary authorization to do the kill)
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 parent is exiting, and the OS does not allow a child to continue if its parent
terminates (cascading termination).
Process Hierarchies
When a process can create one or more other processes, the creating process is called
a parent process, and the new processes are called the children of that process (referred
to as child processes).
Waiting/Blocked: The process is waiting for some event to occur (such as an I/O
completion or reception of a signal).
For this purpose, the OS maintains a table, called the process table aka process
control blocks (PCB), with one entry per process.
This entry contains each and every important information about the process
This is because, the information must be saved when the process is switched from
running to ready or blocked state so that it can be restarted later as if it had never
been stopped.
Cont.
Some of these important information include:
Process state: may be new, ready, running, waiting, halted, and so on.
Program counter: the address of the next instruction to be executed for this process.
CPU registers: These vary in number and type, depending on the computer architecture.
Along with the program counter, these are useful when an interrupt occurs, to allow the
process to be continued correctly afterward when it is rescheduled to run.
Accounting information: includes the amount of CPU and real time used, time limits,
account numbers, job or process numbers, and so on.
I/O status information: includes the list of I/O devices allocated to the process, a list of
open files, and so on.
Memory-management information: include such as the value of the base and limit
registers and the page tables, or the segment tables, and others.
Cont.
Context Switch
For multitasking, different circumstances may cause the operating system to
change a CPU from its current task and to run another task.
Switching the CPU to another process requires performing a state save of the
current process and a state restore of a different process.
In short, it is to save the current context of the process running on the CPU so
that it can restore that context later, essentially suspending the process and
then resuming it.
THREADS
They are lightweight than processes, as they are easier (i.e., faster) to create and
destroy than processes.
Threads in a process use the address space and resource of the process in which
they are running in.
When a multithreaded process is run on a single-CPU system, the threads take turns
running providing the illusion that the threads are running in parallel,
What threads add to the process model is to allow multiple executions to take place in
the same process environment.
Multiple threads of execution share a set of resources so that they can work together
closely to perform some task.
In pop-up thread system, the arrival of message causes the system to create a new
thread just to handle the message. Such a thread is called a pop-up thread.
Processes usually start with a single thread present and they can create new
threads by calling a library procedure such as thread create.
When a thread has finished its work, it can exit by calling a library procedure
thread exit.
In some thread systems, one thread can wait for a (specific) thread to exit by
calling a procedure, for example, thread join.
Another common thread call is thread yield, which allows a thread to voluntarily
give up the CPU to let another thread run.
There are several reasons (advantages) for having threads.
Since they are lighter weight than processes, they are easier (i.e., faster) to
create and destroy than processes.
The first method is to put the threads package entirely in user space. The kernel knows
nothing about them.
When threads are managed in user space, each process needs its own private
thread table to keep track of the threads in that process.
There is no thread table in each process. Instead, the kernel has a thread table that keeps
track of all the threads in the system.
Cont.
InterProcess Communication
This may be because the output of the first process must be passed to the
second process, and so on down the line or related processes are cooperating
to get some job done .
The second is making sure two or more processes do not get in each other’s
way.
If process A produces data and process B prints them, B has to wait until A
has produced some data before starting to print.
Cont.
There are two fundamental models of InterProcess communication: shared memory and
message passing.
Since message-passing systems are typically implemented using system calls and thus
require the more time-consuming task of kernel intervention.
Message passing is also easier to implement in a distributed system than shared memory.
Cont.
1. The Shared-memory Model
Processes can then exchange information by reading and writing data to the shared
region.
Once shared memory is established, all accesses are treated as routine memory accesses.
Race Conditions
Processes that are working together may share some common resource(may be
main memory or it may be a shared file) that each one can read and write.
Situations like this, where two or more processes are “racing” each other to
access/change the resource/data are called race conditions.
In such conditions, the final result depends on who runs precisely when.
Critical Regions
How do we avoid race conditions?
The key to preventing trouble in situations involving shared memory, shared files,
and shared everything else is to find some way to prohibit more than one
process from reading and writing the shared data at the same time.
Part of the time, a process is busy doing internal computations and other things that
do not lead to race conditions.
In a process, that segment of the program where the shared memory is accessed is
called the critical region or critical section.
Mutual Exclusion
If we could arrange matters such that no two processes were ever in their
critical regions at the same time, we could avoid races.
Thus, while one process is busy updating shared memory in its critical region, no
other process will enter its critical region and cause trouble.
Cont.
So, overall there are four conditions to hold to avoid race conditions and have a good
solution:
Back in the old days of batch systems with input in the form of card images on a
magnetic tape, executing a task (the scheduling algorithm) was simple: just run the
next job on the tape.
So, if only one CPU is available, a choice has to be made which process to run next
i.e., the process of removing an active task from the processor and replacing it with
a new one.
Cont.
The activity of the OS (particularly the process manager) that handles the removal of
the running process from the CPU and the selection of another process on the basis of
The part of the operating system that makes the choice is called the scheduler, and
the particular strategy or algorithm it uses for scheduling is called the scheduling
algorithm.
Process Behavior
Processes have different behavior and nearly all processes alternate bursts of
computing with (disk or network) I/O requests.
Some processes, such as the one in Fig. 2-39(a), spend most of their time computing,
while other processes, such as the one shown in Fig. 2-39(b), spend most of their time in
I/O operation.
The former are called compute-bound or CPU-bound; the latter are called I/O
bound.
Compute-bound processes typically have long CPU bursts and thus infrequent I/O
waits, whereas I/O-bound processes have short CPU bursts and thus frequent I/O waits.
CPU burst is the amount of time the process uses the processor.
Cont.
When to Schedule
That process can no longer run (since it no longer exists), so some other
process must be chosen from the set of ready processes.
Third, when a process blocks on I/O or for some other reason another process
has to be selected to run.
Cont.
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.
I. A non-preemptive scheduling algorithm picks a process to run and then just lets it
run until it blocks (either on I/O or waiting for another process) or voluntarily
releases the CPU.
II. A preemptive scheduling algorithm, in contrast, picks a process and lets it run for a
maximum of some fixed time.
If it is still running at the end of the time interval, it is suspended and the
scheduler picks another process to run (if one is available).
Cont.
In different environments different scheduling algorithms are needed. Why?
In batch systems, there are no users impatiently waiting at their terminals for a
quick response to a short request.
But preemption is 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.
Throughput is the number of jobs per hour that the system completes.
Turnaround time is the statistically average time from the moment that a batch
job is submitted until the moment it is completed.
It measures how long the average user has to wait for the output.
Response time, that is, the time between issuing a command and getting the
result.
Commonly Known Scheduling Algorithms
1. First-Come, First-Served (FSFS)
This is the simplest of all non-preemptive scheduling algorithms.
With this algorithm, processes are assigned the CPU in the order they request it.
When the first job enters the system from the outside, it is started immediately and
allowed to run as long as it wants to.
As other jobs come in, they are put onto the end of the queue.
When the running process blocks, the first process on the queue is run next.
When a blocked process becomes ready, like a newly arrived job, it is put on the end of
the queue, behind all waiting processes.
The pros of this algorithm is that it is easy to understand and to program; However it lacks
efficiency.
2. Shortest Job First
This is another non-preemptive batch algorithm that assumes the run times are
known in advance.
When several equally important jobs are sitting in the input queue waiting to be
started, the scheduler picks the shortest job first.
It is worth pointing out that shortest job first is optimal only when all the jobs are
available simultaneously.
3. Shortest Remaining Time Next
This is a preemptive version of shortest job first.
With this algorithm, the scheduler always chooses the process whose remaining run
time is the shortest.
When a new job arrives, its total time is compared to the current process’
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.
Each process is assigned a time interval, called its quantum, during which it is
allowed to run.
If the process is still running at the end of the quantum, the CPU is preempted and
given to another process.
The only really interesting issue with round robin is the length of the quantum.
Setting the quantum too short causes too many process switches and lowers the
CPU efficiency, but setting it too long may cause poor response to short
interactive requests.
5. Priority Scheduling
This is for cases where there may be multiple processes and some of them more important
than others based on some factor.
This need to take external factors into account leads to priority scheduling.
Each process is assigned a priority, and the runnable process with the highest priority is
allowed to run.
When this quantum is used up, the next-highest-priority process is given a chance to run.
It is often convenient to group processes into priority classes and use priority scheduling
among the classes but round-robin scheduling within each class.
6. Multi-level Queues
This is a different variation of priority scheduling.
Unlike in priority scheduling which puts all processes in the same queue, this
algorithm uses multiple priority queues separately for processes with common
characteristics.
Each queue will be assigned a quantum and can have its own scheduling
algorithms.
Then processes in the highest priority class were run for one quantum, processes in
the next-highest class were run for two quanta, processes in the next one were run
for four quanta, etc.
Whenever a process used up all the quanta allocated to it, it was moved down
one class.
7. Guaranteed Scheduling
This is a proportionate scheduling which aims to ensure a fair allocation of
CPU time among processes.
The basic idea here is to give processes lottery tickets for various system
resources, such as CPU time.
So far we have assumed that each process is scheduled without regard to who
its owner is.
Fair share scheduling takes into account which user owns a process before
scheduling it.
In this model, each user is allocated some fraction of the CPU and the
scheduler picks processes in such a way as to enforce it.
Thus, if two users have each been promised 50% of the CPU, they will each get
that, no matter how many processes they have in existence.
Scheduling Real-time Systems
Real-time scheduling algorithms are grouped into two primary categories: Static
and Dynamic.
decisions about what to run next are not made in real time instead scheduling
decisions are made before the system starts running.
This works only when there is perfect information available in advance about
the work to be done and the deadlines that have to be met.