Operating Systems
Operating Systems
Process Management
PROCESS CONCEPTS:
The Text section is made up of the compiled program code, read in from non-
volatile storage when the program is launched.
The Data section is made up the global and static variables, allocated and
initialized prior to executing the main.
The Heap is used for the dynamic memory allocation, and is managed via calls to
new, delete, malloc, free, etc.
The Stack is used for local variables. Space on the stack is reserved for local
variables when they are declared.
Process State:
As a process executes, it changes state. The process state defines the current activity of that
process.
A process may be in one of the following states:
New: The process is being created.
Ready: The process is waiting to be assigned to a processor.
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.
Terminated: The process has finished execution.
Note: Only one process can be running on any processor at any instant of time.
New Ready: The operating system creates a process and prepare the process to be
executed, then the operating system moved the process into “Ready” queue.
Ready Running: When it is time to select a process to run, the operating system selects one
of the jobs from the ready queue and move the process from ready state to running state.
Running Terminated: When the execution of a process has completed, then the operating
system terminates that process from running state.
Running Ready: When the time slot of the processor expired (or) it the processor received
any interrupt signal, then the operating system shifted running process to ready state. For
example, process P1 is executing by processor, in the mean time process P2 generate an
interrupt signal to the processor. Then the processor compare the priorities of process P1 and
P2, if P1> P2 then the processor continue the process P1. Otherwise the processor switched to
process P2, and the process P1 moved to ready state.
Running Waiting: A process is put into the waiting state, if the process need an event occur,
or an I/O devices require. The operating system does not provide the I/O or event immediately
then the process moved to waiting state by the operating system.
Waiting Ready: A process in the blocked state is moved to ready state when the event for
which it has been waiting occurs.
Process Control Block:
Each process is represented in the operating system by a Process Control Block (PCB). It is
also called a Task Control Block.
PCB serves as the repository for any information that may vary from process to 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 etc. Along with the program counter, this state information must be
saved when an interrupt occurs, to allow the process to be continued correctly
afterward.
CPU-scheduling information: This information includes a process priority, pointers to
scheduling queues and any other scheduling parameters.
Memory-management information: This information includes the base and limit
registers values, the page tables or the segment tables depending on the memory
system used by the operating system.
Accounting information: This 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: This information includes the list of I/O devices allocated to the
process, a list of open files and so on.
Operations on Process:
We have discussed the two major operations Process Creation and Process Termination.
Process Creation:
During the execution of a process in its life time, a process may create several new
processes.
The creating process is called a parent process and the new processes are called
children process.
Each of these new processes may create other processes forming a tree of processes
also known as process hierarchy.
Operating system identifies processes according to process identifier (pid).
Pid provides an unique integer number for each process in the system.
The below figure shows the process tree for the Linux OS that shows the name of each process
and its pid. In Linux process is called task.
The init process always has a pid of 1. The init process serves as the root parent process
for all user processes.
Once the system has booted, the init process can also create various user processes,
such as a web or print server, an ssh server etc. kthreadd and sshd are child processes
of init.
The kthreadd process is responsible for creating additional processes that perform tasks
on behalf of the kernel.
The sshd process is responsible for managing clients that connect to the system by using
secure shell (ssh).
When a process creates a child process, that child process will need certain resources
such as CPU time, memory, files, I/O devices to accomplish its task.
A child process 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.
When a process creates a new process there exist two possibilities for execution:
There are also two address-space possibilities for the new process:
1. The child process is a duplicate of the parent process (i.e) it has the same program and
data as the parent.
2. The child process has a new program loaded into it.
A process terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit( ) system call.
The process may return a status value 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 de allocated by the operating system.
A parent may terminate the execution of one of its children for a variety of reasons
such as:
1. The child has exceeded its usage of some of the resources that it has been
allocated.
2. The task assigned to the child is no longer required.
3. The parent is exiting and the operating system does not allow a child to continue if
its parent terminates.
Cascading Termination: If a parent process terminates either normally or abnormally then all
its children must also be terminated is referred as Cascading Termination. It is normally
initiated by operating system.
Cooperating Processes:
A process is said to be cooperating process, if it can affect or be affected by the other
processes executing in the system.
There are several reasons for providing an environment that allows process cooperation:
Information sharing: Since several users may be interested in the same piece of
information (for instance, a shared file), we must provide an environment to allow
concurrent access to these types of resources.
Computation speedup: If we want a particular task to run faster, we must break it into
subtasks, each of which will be executing in parallel with the others.
Modularity: We may want to construct the system in a modular fashion, dividing the
system functions into separate processes.
Convenience: Even an individual user may have many tasks to work on at one time. For
instance, a user may be editing, printing, and compiling in parallel.
Shared memory model: In the shared-memory model, a region of memory that is shared
by cooperating processes is established. Processes can then exchange information by reading
and writing data to the shared region.
Message –passing model: In the message-passing model, communication takes place by means
of messages exchanged between the cooperating processes.
The code for the producer process is shown in Figure 3.13, and the code for the consumer
process is shown in Figure 3.14. The producer process has a local variable next produced in
which the new item to be produced is stored. The consumer process has a local variable next
consumed in which the item to be consumed is stored.
Message-Passing Systems:
Message passing provides a mechanism to allow processes to communicate and to
synchronize their actions without sharing the same address space.
A message-passing facility provides at least two operations: send(message)
receive(message)
If processes P and Q want to communicate, they must send messages to and receive
messages from each other: a communication link must exist between them.
several methods for logically implementing a link and the send()/receive() operations:
Under direct communication, each process that wants to communicate must explicitly
name the recipient or sender of the communication.
In this scheme, the send() and receive() primitives are defined as:
1. send(P, message)—Send a message to process P.
2. receive(Q, message)—Receive a message from process Q
With indirect communication, the messages are sent to and received from mailboxes, or ports.
The send() and receive() primitives are defined as follows:
1. send(A, message)—Send a message to mailbox A.
2. receive(A, message)—Receive a message from mailbox A
Synchronization
Message passing may be either blocking or non blocking— also known as synchronous and
asynchronous.
1. Blocking send : The sending processes blocked until the messages received by the
receiving process or by the mailbox.
2. Non blocking send: The sending process sends the message and resumes operation.
3. Blocking receive: The receiver blocks until a message is available.
4. Non blocking receive :The receiver retrieves either a valid message or a null.
Buffering:
1. Zero capacity. The queue has a maximum length of zero;
2. Bounded capacity. The queue has finite length n;
3. Unbounded capacity. The queue’s length is potentially infinite;
THREADS:
A thread is a lightweight process (LWP) basic unit of CPU utilization; it comprises a
thread ID, a program counter, a register set, and a stack.
It shares with other threads belonging to the same process its code section, data
section, and other operating-system resources, such as open files and signals.
A traditional (or heavyweight) process has a single thread of control.
If a process has multiple threads of control, it can perform more than one
task at a time.
Thread States:
Born State : A thread is just created.
Ready state : The thread is waiting for CPU.
Running : System assigns the processor to the thread.
Sleep : A sleeping thread becomes ready after the designated sleep time expires.
Dead : The Execution of the thread finished.
Multithreading:
A process is divided into number of smaller tasks each task is called a Thread.
Number of Threads with in a Process execute at a time is called Multithreading.
If a program, is multithreaded, even when some portion of it is blocked, the whole
program is not blocked.
The rest of the program continues working If multiple CPU’s are available.
Multithreading gives best performance.
If we have only a single thread, number of CPU’s available, No performance benefits
achieved.
Process creation is heavy-weight while thread creation is light-weight.
Typing, Formatting, Spell check, saving are threads.
15
Types of threads:
User Threads :
Thread creation, scheduling, management happen in user space by Thread Library.
User threads are faster to create and manage.
If a user thread performs a system call, which blocks it, all the other threads in that
process one also automatically blocked, whole process is blocked.
Advantages:
Thread switching does not require kernel mode privileges. User level thread can run on
any operating systems.
User level threads are fast to create and manage.
Disadvantages:
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multi processing.
Kernel Threads:
kernel creates, schedules, manages these threads .
These threads are slower, manage.
If one thread in a process blocked, over all process need not be blocked.
Advantages
Kernel can simultaneously schedule multiple threads from the same process on multiple
processes.
If one thread in a process is blocked, the Kernel can schedule another thread of the
same process.
Kernel routines themselves can multithreaded.
Multithreading Models:
A relationship must exist between user threads and kernel threads. Three common
ways of establishing such a relationship are:
1. Many-to-One model
2. One-to-One model
3. Many-to-Many model.
Many-to-One model
The many-to-one model maps many user-level threads to one kernel thread.
Thread management is done by the thread library in user space, so it is efficient.
The entire process will block if a thread makes a blocking system call.
Only one thread can access the kernel at a time, multiple threads are unable to run in
parallel on multi core systems. Hence very few systems continue to use the model
because of its inability to take advantage of multiple processing cores.
One-to-One Model
The one-to-one model maps each user thread to a kernel thread.
It provides more concurrency than the many-to-one model by allowing another thread
to run when a thread makes a blocking system call.
It also allows multiple threads to run in parallel on multiprocessors.
The only drawback to this model is that creating a user thread requires creating the
corresponding kernel thread.
Because the overhead of creating kernel threads can burden the performance of an
application, most implementations of this model restrict the number of threads
supported by the system.
Linux, along with the family of Windows operating systems, implement the one-to-one
model.
Many-to-Many Model
The many-to-many model multiplexes many user-level threads to a smaller or equal
number of kernel threads. The number of kernel threads may be specific to either a particular
application or a particular machine.
Process Scheduling:
The act of determining which process is in the ready state, and should be moved to the
running state is known as Process Scheduling.
The objective of multiprogramming is to have some process running at all times, 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.
To meet these objectives, the Process Scheduler selects an available process for
program execution on the CPU.
Device Queue: Each device has its own device queue. It contains the list of processes
waiting for a particular I/O device.
Consider the above Queuing Diagram:
Two types of queues are present: the Ready Queue and a set of Device Queues. CPU
and I/O are the resources that serve the queues.
A new process is initially put in the ready queue. It waits there until it is selected for
execution or 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 child process and wait for the child’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.
2. Schedulers:
A process migrates among the various scheduling queues throughout its lifetime. For
scheduling purpose, the operating system must select processes from these queues. The
selection process is carried out by the Scheduler.
There are three types of Schedulers are used:
1. Long Term Scheduler
2. Short Term Scheduler
3. Medium Term Scheduler
Long Term Scheduler (New to ready state):
Initially processes are spooled to a mass-storage device (i.e Hard disk), where they are
kept for later execution.
Long-term scheduler or job scheduler selects processes from this pool and loads them
into main memory for execution. (i.e. from Hard disk to Main memory).
The long-term scheduler executes much less frequently, there may be minutes of time
between creation of one new process to another process.
The long-term scheduler controls the degree of multiprogramming (the number of
processes in memory).
The processes can be described as two types:
1. I/O bound process is one that spends more of its time doing I/O than it spends doing
computations.
2. CPU Bound process using more of its time doing computations and generates I/O requests
infrequently.
The long-term scheduler selects a good process mix of I/O-bound and CPU-bound processes.
If all processes are I/O bound, the ready queue will almost always be empty and the
CPU will remain idle for long time because I/O device processing takes a lot of time.
If all processes are CPU bound, the I/O waiting queue will almost always be empty. I/O
devices will be idle and CPU is busy for most of the time.
Thus if the system maintains the combination of CPU bound and I/O bound processes
then the system performance will be increased.
Short Term Scheduler (Ready to Running):
Short-term scheduler or CPU scheduler selects from among the processes that are
ready to execute and allocates the CPU to one of them. (i.e. a process that resides in
main memory will be taken by CPU for execution).
The short-term scheduler must select a new process for the CPU frequently.
The short term scheduler must be very fast because of the short time between
executions of processes.
Medium Term Scheduler:
Medium Term Scheduler does two tasks:
1. Swapping: Medium-term scheduler removes a process from main memory and stores it into
the secondary storage. After some time, the process can be reintroduced into main memory
and its execution can be continued where it left off. This procedure is called Swapping.
2. Medium Term Scheduler moves a process from CPU to I/O waiting queue and I/O queue to
ready queue.
CPU SCHEDULING:
CPU scheduling is the basis of Multi-programmed operating systems. By switching the
CPU among processes, the operating system can make the computer more productive.
In a single-processor system, only one process can run at a time. Others must wait until
the CPU is free and can be rescheduled.
The CPU will sit idle and waiting for a process that needs an I/O operation to complete.
If the I/O operation completes then only the CPU will start executing the process. A lot
of CPU time has been wasted with this procedure.
The objective of multiprogramming is to have some process running at all times to
maximize CPU utilization.
When several processes are in main memory, if one processes is waiting for I/O then
the operating system takes the CPU away from that process and gives the CPU to
another process. Hence there will be no wastage of CPU time.
3. Pre-emptive Scheduling:
In preemptive scheduling, the CPU is allocated to the processes for a limited time whereas,
in Non-preemptive scheduling, the CPU is allocated to the process till it terminates or switches
to the waiting state.
CPU-scheduling decisions may take place under the following four cases:
1. When a process switches from the running state to the waiting state.
Example: as the result of an I/O request or an invocation of wait( ) for the termination
of a child process.
2. When a process switches from the running state to the ready state.
Example: when an interrupt occurs
3. When a process switches from the waiting state to the ready state.
Example: at completion of I/O.
4. When a process terminates.
For situations 2 and 3 are considered as Pre-emptive scheduling situations.
4. Dispatcher:
The dispatcher is the module that gives control of the CPU to the process selected by the
short-term scheduler. Dispatcher function involves:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart that program.
The dispatcher should be as fast as possible, since it is invoked during every process switch. The
time it takes for the dispatcher to stop one process and start another process running is known
as the Dispatch Latency.
SCHEDULING CRITERIA:
Different CPU-scheduling algorithms have different properties and the choice of a
particular algorithm may favor one class of processes over another.
Waiting time: It is the amount of time that a process spends waiting in the ready queue.
Response time: It is the time from the submission of a request until the first response is
produced. Interactive systems use response time as its measure.
Note: It is desirable to maximize CPU utilization and Throughput and to minimize Turn- Around
Time, Waiting time and Response time.
The average waiting time under the FCFS policy is often quite long.
The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2 and 27
milliseconds for process P3.
Thus, the average waiting time is (0 + 24 + 27)/3 = 17 milliseconds.
Example:2 Let us consider same example above but with the processes arrived in the order P2,
P3, P1.
The processes coming at P2, P3, P1 the average waiting time (6 + 0 + 3)/3 = 3 milliseconds
whereas the processes are came in the order P1, P2, P3 the average waiting time is 17
milliseconds.
Disadvantage of FCFS:
FCFS scheduling algorithm is Non-preemptive, it allows one process to keep CPU for long time.
Hence it is not suitable for time sharing systems.
Shortest-Job-First Scheduling (SJF)
SJF algorithm is defined as “when the CPU is available, it is assigned to the process that
has the smallest next CPU burst”. If the next CPU bursts of two processes are the same, FCFS
scheduling is used between two processes.
SJF is also called as Shortest-Next CPU-Burst algorithm, because scheduling depends on the
length of the next CPU burst of a process, rather than its total length.
Consider the four processes with arrival times and burst times in milliseconds:
Example:
Consider the following set of processes that arrive at time 0 and the processes are arrived in
the order P1, P2, P3 and Time Quanta=4.
If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds.
Since it requires another 20 milliseconds, it is preempted after the first time quantum
and the CPU is given to the next process in the queue, process P2.
CPU burst of Process P2 is 3, so it does not need 4 milliseconds then it quits before its
time quantum expires. The CPU is then given to the next process P3.
Once each process has received 1 time quantum, the CPU is returned to process P1 for
an additional time quantum.
Priority Scheduling:
In this, each process is associated with a priority and CPU is allocated to a process
which is having higher priority. If two processes priorities are equal, the algorithm selects
the first arrived process out of these two processes ( i.e., FCFS ). The priorities are
ranging from 0 to a maximum number. The lowest number represents highest priority.
Ex : Consider the following set of processes, assumed to have arrived at time 0, in the order
P1, P2 ….. P5 with the length of the CPU-burst time given in milliseconds:
Process Burst Time(in m.sec) Priority
P1 10 2
P2 1 0
P3 2 4
P4 1 5
P5 5 1
Gantt Chart:
P2 P5 P1 P3 P4
0 1 6 16 18 19
Waiting time for P1 = 6 m.sec
Waiting time for P2 = 0 m.sec
Waiting time for P3 = 16 m.sec
Waiting time for P4 = 18 m.sec
Waiting time for P5 = 1 m.sec
Average waiting time: (6 + 0 + 16 + 18 + 1)/5 = 41/5 = 8.2 m.sec
Turnaround time for P1 = 16 m.sec
Turnaround time for P2 = 1 m.sec
Turnaround time for P3 = 18 m.sec
Turnaround time for P4 = 19 m.sec
Turnaround time for P5 = 6 m.sec
Average Turnaround time = (16 + 1 + 18 + 19 + 6)/5 = 60/5 = 12 m.sec
P2 P5 P1 P4 P1 P3
0 1 2 4 5 13 15
Waiting time for P1 = 1 m.sec
Waiting time for P2 = 0 m.sec
Waiting time for P3 = 10 m.sec
Waiting time for P4 = 0 m.sec
Waiting time for P5 = 0 m.sec
Average waiting time: (1 + 0 + 10 + 0 + 0)/5 = 11/5 = 2.2 m.sec
Turnaround time for P1 = 11 m.sec
Turnaround time for P2 = 1 m.sec
Turnaround time for P3 = 12 m.sec
Turnaround time for P4 = 1 m.sec
Turnaround time for P5 = 1 m.sec
Average Turnaround time = (11 + 1 + 12 + 1 + 1)/5 = 26/5 = 5.2 m.sec
Starvation: A process in the ready queue and waits for CPU for a long period of time is said to
be Indefinitely Blocked or Starvation. In the priority scheduling algorithm, a process with least
priority is not served and put it in the ready queue and waits for the CPU is said to be
indefinitely blocked.
Aging: Aging is a solution to the starvation problem. This technique gradually increases the
priority of processes that wait in the system for a long period of time.
The scheduler first executes all processes in queue 0. Only when queue 0 is empty
will it execute processes in queue 1. Similarly, processes in queue 2 will only be executed if
queues 0 and 1 are empty. A process that arrives for queue 1 will preempt a process in
queue a process in queue 1 will in turn be preempted by a process arriving for queue 0.
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.
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.
25