Professional Documents
Culture Documents
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
2. Definition II
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?
ii. Heap: - Allocates memory, which may be processed during its run
time.
iii. Data: - It contains the variable.
a. What is a process
P a g e 3 | 34
b. What is a program
#include <stdio.h>
int main ()
{
printf ("Hello, World! \n");
return 0;
}
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.
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
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 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
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 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.
P a g e 8 | 34
Process state transition diagram
Process Transitions
P a g e 9 | 34
Seven State Process Transition Diagram
➢ The state of this process needs to be resumed, when the main memory
is ok. Then the state is changed again to ready state.
➢ 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)
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.
Process ID
State
Pointer
Priority
Program Counter
CPU Register
I/O Information
Accounting Information
Context Switching
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.
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.
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
➢ Medium-term scheduling:
Medium term scheduling is in charge of swapping processes
between the main memory and the secondary storage.
Schedulers - Comparison
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
➢ Preemptive
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?
➢ Context Switching
➢ Switching to user mode
➢ Moving to the correct location in the newly loaded program.
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.
➢ 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.
An example of five processes arriving at different times. Each process has a different
burst time.
P a g e 19 | 34
Solution -:
Gang Chart -:
0 3 11 17 20
= …………………………………………………
P a g e 20 | 34
4. Calculate the turnaround Time for each process?
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.
A = …… B = …… C = …… D = …… E = ……
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.
P a g e 23 | 34
1. Fill the blank columns in bellow table? Finish Time, Turnaround Time,
waiting time.
P a g e 24 | 34
Shortest Job First (SJN)
Question: 1
P0 P0 P0 P0 P0 P1 P1 P1 P3 P3 P3 P3 P2 P2 P2 P2 P2 P2
0
5 8 14 18
P a g e 25 | 34
Question 2:
Given: Table of processes, and their Arrival time, burst time.
P0 P1 P3 P2
0 5 16 22
Question 1:
Consider this following three processes
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.
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.
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.
P a g e 29 | 34
Process ID Exit Time Turnaround Time Waiting Time
P1 13
………………… …………………
P2 12
………………… …………………
P3 5
………………… …………………
P4 9
………………… …………………
P5 14
………………… …………………
P a g e 30 | 34
Question 3:
Consider the set of 6 processes whose arrival time and burst time are given below-
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.
P3 6
………………. ……………….
P4 9
………………. ……………….
P5 21
………………. ……………….
P6 19
………………. ……………….
Answer: -
i. Average Turnaround 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