You are on page 1of 34

Unite – 05

Operating Systems
Manages the Processes in Computers.

Theory Lesson

Umayanga Wijesinghe
B.sc Software Engineering
Working as Soft.Eng.

P a g e 1 | 34
Operating Systems Process Management in Computer

What is Process?

1. Definition I

➢ Process is a fundamental concept in modern operating systems.


➢ A process is basically a program in execution that performs the
actions specified in that program.
➢ Process is not a program. A program may have many processes.

2. Definition II

➢ A process is a program in execution


o the OS helps you to create, schedule, and terminates the
processes which is used by CPU.
➢ A program is a static set of instructions

For example,
when we write a program in C or C++ and compile it, the compiler creates binary
code. The original code and binary code are both programs. When we actually
run the binary code, it becomes a process.

P a g e 2 | 34
What does a process look like in memory?

i. Stack: - The Stack stores temporary data like function


parameters, returns addresses, and local variables.

ii. Heap: - Allocates memory, which may be processed during its run
time.
iii. Data: - It contains the variable.

iv. Text: -Text Section includes the current activity, which is


represented by the value of the Program Counter.

a. What is a process

A process exists in a limited span of time. Two or more processes could


be executing the same program, each using their own data and
resources.

P a g e 3 | 34
b. What is a program

A program is a piece of code which may be a single line or millions of


lines. A computer program is usually written by a computer
programmer in a programming language.

A computer program is a collection of instructions that performs a specific task


When execute by a computer.

For example, here is a simple program written in C programming language

#include <stdio.h>

int main ()
{
printf ("Hello, World! \n");
return 0;
}

Process creation and termination

When a new process is created, the operating system builds the date structures that
are used to manage the process and allocates space in main memory to the process.

➢ Reasons for process creation

1. New batch job


2. User starts a program
3. OS creates process to provide a service
4. Running program starts another process

P a g e 4 | 34
➢ Reasons for process termination:

1. Normal termination,
2. Execution time-limit exceeded,
3. A resource requested is unavailable,
4. An execution errors
5. A memory access violation,
6. An operating system or parent process request
7. Parent process has terminated.

Swapping

Swapping is a mechanism in which a process can be swapped temporarily out


of main memory to secondary storage and make that memory available to
other processes.
At some later time, the system swaps back the process from the secondary
storage to main memory.

P a g e 5 | 34
Though performance is usually affected by swapping process but it helps in
running multiple and big processes in parallel and that's the reason Swapping
is also known as a technique for memory compaction.

Interrupts?

Interrupts are signals sent to the CPU by external devices, normally


I/O devices. They tell the CPU to stop its current activities and
execute the appropriate part of the operating system.

➢ Interrupt is an event that alters the sequence of execution of process.

➢ Interrupt can occur due to a time expiry an OS service request I/O


completion.
Example-: when a disk driver has finished transferring the
requested data, it generates an interrupt to the OS to inform the
OS that the task is over.

➢ Interrupts are important because they give the user better control over
the computer. Without interrupts, a user may have to wait for a given
application to have a higher priority over the CPU to be ran. This ensures
that the CPU will deal with the process immediately.

Interrupt Handling

Generally, I/O models are slower than CPU. After each I/O call, CPU has
to sit idle until I/O device complete the operation, and so processor
saves the status of current process and executes some other process.
When I/O operation is over, I/O devices issue an interrupt to CPU then stores
the original process and reserves execution.

P a g e 6 | 34
Process Scheduling Queues

The Operating System maintains the following important process scheduling


queues –

1. Job queue − This queue keeps all the processes in the system.
2. Ready queue − This queue keeps a set of all processes residing
residing in main memory, ready and waiting to execute. A new process
is always put in this queue.
3. Device queues − The processes which are blocked blocked due to
unavailability unavailability of an I/O device constitute this queue.

➢ Types of Processes
1. I/O Processes
2. Processor Process

P a g e 7 | 34
Characteristics of a Process

A process has following attributes.

a. Process Id
b. Process State
c. CPU registers
d. Accounts information
e. I/O status information
f. CPU scheduling information

Process Management

➢ In multiprogramming environment,

The OS decides which process gets the processor when and for how
much time. This function is called process scheduling.

➢ An Operating System does the following activities for processor managements:


1. Keeps tracks of processor and status of process. The program
responsible for this
2. task is known as traffic controller.
3. Allocates the processor (CPU) to a process.
4. De-allocates processor when a process is no longer required.

Function of Process Management

1. Creating and Deleting processes.


2. Scheduling processes on the CPU.
3. Suspending and resuming process.
4. Providing mechanisms for process communication

P a g e 8 | 34
Process state transition diagram

As a process executes, it changes states

1. New/Created – The process is being created.


2. Ready -The process is waiting to be assigned to a processor.
3. Running - Instructions are being executed.
4. Waiting - The process is waiting for some event to occur (such as
an I/O completion)
5. Terminated /Exit - The process has finished execution

Process Transitions

The operating system’s role is to manage the execution of existing


and newly created processes by moving them between the two states
until they finish.

P a g e 9 | 34
Seven State Process Transition Diagram

Swapped out and waiting / Ready and Suspend


➢ If a process in ready state, for a longer time that process moves to the
virtual memory (in secondary storage) in order to provide space for
other high priority

➢ The state of this process needs to be resumed, when the main memory
is ok. Then the state is changed again to ready state.

Swapped out and blocked / Blocked and Suspended

➢ If the main memory is too loaded or to give the space to high priority
processes, the processes with block state in main memory are moved
to virtual memory as swapped out and blocked state.

➢ The state of this process needs to be resumed, when the main memory
is ok. Then the state is changed again to blocked state.
P a g e 10 | 34
Process Control Block (PCB)

A Process Control Block is a data structure maintained by the


Operating System for every process. The PCB is identified by an
integer process ID (PID).

➢ A PCB keeps all the information needed to keep track of a process as


listed below in the table:
NO Information & Description
1 Process State

The current state of the process


i.e., whether it is ready, running, waiting, or whatever
2 Process ID

Unique identification for each of the process in the operating


system.
3 Pointer

A pointer to parent process.


4 Program Counter

Program Counter is a pointer to the address of the next instruction


to be executed for this process.
5 CPU registers

Various CPU registers where process need to be stored for


execution for running state.
6 Memory management information

This includes the information of page table, memory limits,


Segment table depending on memory used by the operating
system.
7 IO status information

This includes a list of I/O devices allocated to the process.


8 Accounting information

This includes the amount of CPU used for process execution, time
limits, execution ID etc.
P a g e 11 | 34
The architecture of a PCB is completely dependent on Operating System and
may contain different information in different operating systems.

Here is a simplified diagram of a PCB:

Process ID

State

Pointer

Priority

Program Counter

CPU Register

I/O Information

Accounting Information

Context Switching

In an operating System, suspending the current process temporarily and


resuming it again or starting another process is called context switch.

P a g e 12 | 34
A context switch is the mechanism to store and restore the state or context of
a CPU in Process Control block so that a process execution can be resumed
from the same point at a later time. Using this technique, a context switcher enables
multiple processes to share a single CPU. Context switching is an essential part
of a multitasking operating system features.

1. Using this technique, a context switcher enables multiple processes


to share a single CPU.

2. Context switching can significantly affect performance as modern


computers have a lot of general and status registers to be saved

P a g e 13 | 34
Process scheduling

The process scheduling is the activity of the process manager that handles the
removal of the running process from the CPU and the selection of another
process on the basis of a particular strategy.

Process scheduling is an essential part of a Multiprogramming operating systems.


Such operating systems allow more than one process to be loaded into the
executable memory at a time and the loaded process shares the CPU using
time multiplexing.

Process Scheduling Queues

The OS maintains all PCBs in Process Scheduling Queues. When the state of a
process is changed, its PCB is unlinked from its current queue and moved to
its new state queue.

Schedulers

Operating system selects process from the queues. The selecting process
is carried out by the appropriate scheduler.

P a g e 14 | 34
Types of Scheduling

➢ Long-term scheduling (Job scheduling)


It determines which programs are admitted to the system for
processing. A long-term scheduler decides which process are to be
admitted to the ready queue.

➢ Medium-term scheduling:
Medium term scheduling is in charge of swapping processes
between the main memory and the secondary storage.

➢ Short-term scheduling (low-level scheduling):


Determines which ready process will be assigned the CPU when it
next becomes available.

Schedulers - Comparison

• Long Term Scheduler • Short Term Scheduler • Medium Term


Scheduler

• Job Scheduler • CPU scheduler • Processes swapping


scheduler

• Selects processes from • Selects those • Swapped out/Re-


a pool and loads them processes which are introduces the
into the memory for ready to execute for processes into memory
execution dispatching and execution can be
continued.

• Controls the degree of • Provides lesser control • Controls the degree of


multiprogramming over the degree of multiprogramming
multiprogramming

• Speed is lesser than • Speed is fastest among • Speed is in between


short term scheduler other two (short and long term
schedulers)

P a g e 15 | 34
Process Schedulers
➢ Turnaround time:
Time required for a particular process to complete, from
submission time to completion.
➢ Response time:
The time taken in an interactive program from the issuance of a
command to the commence of a response to that command.
➢ Throughput:
Number of processes completed per unit time. May range from 10 /
second to 1 / hour depending on the specific processes.
➢ Waiting time:
How much time a process spends in the ready queue waiting its
turn to get on the CPU.

Scheduling Policies

➢ Non-preemptive

once the resources (CPU cycles) is allocated to a process, the process


holds the CPU till it gets terminated or it reaches a waiting state. In case of
non-preemptive scheduling does not interrupt a process running CPU in
middle of the execution.

➢ Preemptive

Preemptive scheduling is used when a process switches from running


state to ready state or from waiting state to ready state.

The resources (mainly CPU cycles) are allocated to the process for the
limited amount of time and then is taken away, and the process is again
placed back in the ready queue if that process still has CPU burst time
remaining

P a g e 16 | 34
What is Dispatcher?

It is a module that provides control of the CPU to the process. The


Dispatcher should be fast so that it can run on every context switch.
Dispatch latency(late) is the amount of time needed by the CPU scheduler
to stop one process and start another.

Functions performed by Dispatcher:

➢ Context Switching
➢ Switching to user mode
➢ Moving to the correct location in the newly loaded program.

Important CPU scheduling Terminologies

1. Burst Time/Execution Time:


It is a time required by the process to complete
execution. It is also called running time.
2. Arrival Time:
when a process enters in a ready state
3. Finish Time:
when process complete and exit from a system
4. Turnaround time
Turnaround time is the total amount of time spent by the
process from coming in the ready state for the first time to its
completion.

Turnaround time = Burst time + Waiting time


or
Turnaround time = Exit time - Arrival time

5. Jobs:
It is a type of program without any kind of user interaction.

P a g e 17 | 34
6. Throughput
Throughput is a way to find the efficiency of a CPU. It can be defined
as the number of processes executed by the CPU in a given
amount of time.

For example,
let's say, the process P1 takes 3 seconds for execution,
P2 takes 5 seconds, and P3 takes 10 seconds.
So, throughput, in this case,
The throughput will be (3+5+10)/3
= 18/3
= 6 seconds.

Operating System Scheduling algorithms

A Processes schedules different processes to be assigned to the CPU based on


particular scheduling algorithms. There are six popular process scheduling algorithms
which we are going to discuss in this chapter −

1. First-Come, First-Served (FCFS) Scheduling.


2. Shortest-Job-Next (SJN) Scheduling.
3. Round Robin (RR) Scheduling.
4. Priority Scheduling.

First Come First Serve (FCFS)

➢ First Come First Serve (FCFS) is an operating system scheduling algorithm that
automatically executes queued requests and processes in order of their
arrival.
➢ In this type of algorithm, processes which requests the CPU first get the
CPU allocation first.
➢ This is managed with a FIFO queue.

P a g e 18 | 34
Example of FCFS scheduling

A real-life example of the FCFS method is buying a movie ticket on the ticket
counter. In this scheduling algorithm, A person is served according to the queue
manner. The person who arrives first in the queue first buys the ticket and then
the next one.

This will continue until the last person in the queue purchases the ticket. Using
this algorithm, the CPU process works in a similar manner.

Example 1: FCFS Scheduling

An example of five processes arriving at different times. Each process has a different
burst time.

Process Arrival Time Bust Time


P1 2 6
P2 5 3
P3 1 8
P4 0 3

1. Calculate the finishing Time for each process?

2. Calculate the waiting Time?

3. Calculate the Average Waiting Time?

4. Calculate the Turnaround Time?

5. Calculate the Average Turnaround Time?

P a g e 19 | 34
Solution -:

1. Calculate the finishing Time for each process?

Step 0 - The process begins with P4 which has arrival time 0.


Step 1 - At time=1, P3 arrives. P4 is still executing. Hence, P3 is kept in a
queue.
Step 2- At time= 2, P1 arrives which is kept in the queue.
Step 3 - At time=3, P4 process completes its execution.
Step 4 - At time=4, P3, which is first in the queue, starts execution.

Gang Chart -:

Process 4 Process 3 Process 1 Process 2

0 3 11 17 20

P1 finishing time = ……………………… P2 finishing time = ………………………

P3 finishing time = ………………………. P4 finishing time = ………………………

2. Calculate the waiting Time?

Waiting time = Start Time - Arrival time


WT = ST - AT

P4 waiting time = …………………………… P3 waiting time = …………………………..


P1 waiting time = …………………………… P2 waiting time = …………………………..

3. Calculate the average waiting Time?

Average Waiting Time = ………………………………………………..

= …………………………………………………

P a g e 20 | 34
4. Calculate the turnaround Time for each process?

Turnaround = Finishing Time – Arrival Time


TAT = FT - AT

P1 Turnaround time = ………………………. P2 Turnaround time =…………………

P3 Turnaround time = ……………………… P4 Turnaround time = …………………

5. Calculate the average turnaround Time?

Average Turnaround Time = …………………………………………………………


= …………………………………………………………
= …………………………………………………………

Example 2:

In the following example, We have 4 processes with process ID P0, P1, P2, and P3.
The arrival time and burst time of the processes are given in the following table.

Process Arrival Burst Finishing Turnaround Wait time


time Time Time Time
P0 0 6
…….…… …….…… …….……
P1 1 8
…….…… …….…… …….……
P2 2 10
…….…… …….…… …….……
P3 3 12
…….…… …….…… …….……

1. Calculate finishing Time, waiting time and turnaround time?


2. Calculate average waiting Time?
3. Calculate average turnaround Time?
P a g e 21 | 34
Answer -:

1. Filling the blank in below Gang chart?

Process 0 Process 1 Process 2 Process 3

A = …… B = …… C = …… D = …… E = ……

P0 finishing time = ………………….. P1 finishing time = …………………………

P2 finishing time = ………………….. P3 finishing time = ……………………….

2. Calculate waiting Time?

P0 waiting time = ……………………. P1 waiting time = …………………….

P2 waiting time = ……………………. P3 waiting time = ……………………..

3. Calculate the average waiting Time?

Average Waiting Time = ………………………………………………………..


= ……………………………………………………….
= ……………………………………………………….
= ……………………………………………………….

4. Calculate the turnaround waiting Time?

P0 Turnaround time = ………………………………………………………..


P1 Turnaround time = ………………………………………………………..
P2 Turnaround time = ………………………………………………………..
P3 Turnaround time = ………………………………………………………..

5. Calculate the average turnaround Time?


Average Turnaround Time = ………………………………………………………..
= ………………………………………………………..
P a g e 22 | 34
Example 3:

1. Calculate waiting Time


2. Calculate average waiting Time
3. Calculate Turnaround Time.
4. Calculate average Turnaround Time

Example 4:
Consider the following set of four processes. Their arrival time and time required
to complete the execution are given in following table. Consider all time values in
milliseconds.

Process Arrival Time required for completion (&T) (CPU


Time Burst Time)
(T0)
P0 0 10
P1 1 6
P2 3 2
P3 5 4

P a g e 23 | 34
1. Fill the blank columns in bellow table? Finish Time, Turnaround Time,
waiting time.

Process Arrival CPU Burst Finish Turnaround Waiting Time


Time Time Time Time
P0 0 10
…….. …….. ……..
P1 1 6
…….. …….. ……..
P2 3 2
…….. …….. ……..
P3 5 4
…….. …….. ……..

1. Average Turnaround Time = ……………………………………..……..


= ……………………………………………………….
= ……………………………………………………….
= ……………………………………………………….
= ……………………………………………………….

2. Average Turnaround Time = …………………………………..………..


= ……………………………………………………….
= ……………………………………………………….
= ……………………………………………………….
= ……………………………………………………….

P a g e 24 | 34
Shortest Job First (SJN)

➢ This is also known as shortest job first, or SJF

➢ This is a non-preemptive, pre-emptive scheduling algorithm.

➢ Best approach to minimize waiting time.

Question: 1

Process Arrival Time required for completion (&T) (CPU


Time Burst Time
P0 0 5
P1 1 3
P2 2 6
P3 3 4

1. Calculate the Finishing Time for each process?

P0 P0 P0 P0 P0 P1 P1 P1 P3 P3 P3 P3 P2 P2 P2 P2 P2 P2

0
5 8 14 18

Finish Time P0 = 5 Finish Time P1 = 8


Finish Time P2 = 14 Finish Time P3 = 18

2. Calculate the Waiting Time?

Waiting Time P0 = 0 Waiting Time P1 = 4

Waiting Time P2 = 10 Waiting Time P3 = 5

3. Calculate the Average waiting Time?


= (0 + 4+ 10+ 5) / 5
= 19 / 5 = 4.4 ms

P a g e 25 | 34
Question 2:
Given: Table of processes, and their Arrival time, burst time.

Process Arrival Burst Time


Time
P0 0 5
P1 1 3
P2 2 8
P3 3 6

1. Calculate finishing time and waiting Time.


2. Calculate average waiting Time
3. Calculate Average Turnaround Time.

P0 P1 P3 P2
0 5 16 22

Round Robin Scheduling

➢ Round Robin is the preemptive process scheduling algorithm.


➢ Each process is provided a fix time to execute, it is called a quantum.
➢ Once a process is executed for a given time period, it is preempted and
other process executes for a given time period.

Question 1:
Consider this following three processes

Process Arrival Burst Application Time


Time Time (quantum slice)
P1 0 4 2 2
P2 1 3 2 1
P3 2 5 2 2 1

P a g e 26 | 34
Step 1 - The execution begins with process P1, which has burst time 4. Here,
every process executes for 2 seconds. P2 and P3 are still in the
waiting queue.

Step 2 - At time =2, P1 is added to the end of the Queue and P2 starts
executing.

P a g e 27 | 34
Step 3 - At time=4 , P2 is preempted and add at the end of the queue. P3
starts executing.

Step 4 - At time=6 , P3 is preempted and add at the end of the queue. P1


starts executing.

Step 5 - At time=8 , P1 has a burst time of 4. It has completed execution. P2


starts execution

P a g e 28 | 34
Step 6 - P2 has a burst time of 3. It has already executed for 2 interval. At
time=9, P2 completes execution. Then, P3 starts execution till it
completes.

Question 2:

Consider the set of 5 processes whose arrival time and burst time are given below.

Process ID Arrival Time Burst Time


P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3

If the CPU scheduling policy is Round Robin with time quantum = 2 unit,
i. Fill in the blank column in below table? Turnaround time and waiting time.

ii. Calculate the average waiting time

iii. Average Turnaround time.

P a g e 29 | 34
Process ID Exit Time Turnaround Time Waiting Time
P1 13
………………… …………………
P2 12
………………… …………………
P3 5
………………… …………………
P4 9
………………… …………………
P5 14
………………… …………………

i. Average Turnaround time = ……………………………………………………………………


= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………

ii. Average waiting time = ……………………………………………………………………


= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………

P a g e 30 | 34
Question 3:

Consider the set of 6 processes whose arrival time and burst time are given below-

Process ID Arrival Time Burst Time


P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3

If the CPU scheduling policy is Round Robin with time quantum = 2, calculate the
average waiting time and average Turnaround time.

i. Fill in the blank column in below table? Turnaround time and waiting time.
ii. calculate the average waiting time
iii. average Turnaround time.

P a g e 31 | 34
i. Fill in the blank column in below table? Turnaround time and waiting time.

Process ID Exit Time Turnaround Time Waiting Time


P1 8
……………….. ……………….
P2 18
………………… ……………….

P3 6
………………. ……………….

P4 9
………………. ……………….

P5 21
………………. ……………….
P6 19
………………. ……………….

Answer: -
i. Average Turnaround time = ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………

ii. Average waiting time = …………….……………………………………………………


= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………
= ……………………………………………………………………

P a g e 32 | 34
Priority Scheduling

➢ Scheduler will always choose a process of higher priority over one of lower
priority
➢ Use multiple ready queues to represent multiple levels of priority
➢ Lower-priority may suffer starvation
➢ Allow a process to change its priority based on its age or execution history

Priority Queuing

P a g e 33 | 34
P a g e 34 | 34

You might also like