Process Management Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

PROCESSES AND PROCESS

MANAGEMENT

Rutherford, Andrew (Mr) (Summerstrand Campus North)


NELSON MANDELA UNIVERSITY
Table of Contents
Introduction........................................................................................................................................ 2
What is a Process? .......................................................................................................................... 2
Process States .................................................................................................................................. 3
Process Control Block ..................................................................................................................... 3
Process Scheduling ......................................................................................................................... 4
Process Scheduling Queues ...................................................................................................... 5
Context Switch .............................................................................................................................. 5
Schedulers......................................................................................................................................... 6
Long Term Scheduler .................................................................................................................. 6
Short Term Scheduler ................................................................................................................. 6
Medium Term Scheduler ............................................................................................................. 6
Operating System Scheduling Algorithms .................................................................................... 6
First-Come First-Served (FCFS) Scheduling ........................................................................... 7
Shortest-Job-Next (SJN) Scheduling (Non-Preemptive/Cooperative) ................................. 8
Shortest-Remaining Time (SRT) Scheduling (Preemptive) ................................................... 9
Round-Robin (RR) Scheduling ................................................................................................. 10
Priority Scheduling ..................................................................................................................... 11
Bibliography .......................................................................................................................................... 13

Page 1 of 13
Introduction
Gone are the days when computer systems only had one program running at a time. In such
a case that single program had access to most if not all system resources. Now we have
multiple programs in memory executing simultaneously. Hence the need for process
management

What is a Process?
A process is primarily a program in execution. A program becomes a process when an
executable file is loaded into memory. It has four sections as depicted below.

Max
Stack

Heap

Data

Text
0
Figure 1 - Process in Memory

Stack: Used to store temporary variables, i.e. local variables, function and procedure
parameters etc – its size is known at compile time.
Heap: Gets allocated dynamically, size is not known at compile time;
Data: Stores global variables

Text: stores the code being executed

e.g. int myFunction(int x, int y)


{ Text Section
return x + y
}

Int answer = myFunction(3,4); 3 and 4 placed on stack when function called

• The text and data sections have fixed sizes


• Stack and heap sections shrink and grow dynamically during execution.
• Each time a function is called stack grows. When function exits stack shrinks.
• Heap will grow as memory is dynamically allocated, shrinks when memory is
deallocated.
• The stack and heap grow toward one another.
• OS must ensure they do not overlap one another

Page 2 of 13
Process States

As a process executes, it changes state. The state of a process is defined in part by the current
activity of that process. Each process may be in one of the following states1:

• New: Process is just created


• Ready: Process waits to be assigned a processor by the OS so they can run. Process
may come into this state after New state or while running interrupted by the scheduler to
assign CPU to some other process.
• Running: Instructions are being executed.
• Waiting: Process is waiting for an event to occur e.g. I/O completion
• Terminated: Process has finished executing, waiting to be removed from memory by the
OS

New

Interrupted

Accepted
Ready Running
exit
By OS

Gets CPU Time

Waiting
I/O or Event Complete I/O or Event Wait

Figure 2 - Process State Diagram

Process Control Block


The OS maintains a data structure (Process Control Block – PCB) for every process. Each
PCB is identified by process ID (PID). A PCB keeps all the information needed by the OS to
keep track of a process. Simply stated the PCB simply serves as a storage area for all the
data needed to start, or restart, a process, along with some accounting data. The PCB is
maintained for the lifetime of a process and is only deleted once the process terminates.

1
These states and their names may differ across different operating systems

Page 3 of 13
Process state. The state may be new, ready, running, waiting and terminated.

Program counter The counter indicates the address of the next instruction to be executed
for this process.
CPU register The registers vary in number and type, depending on the
Information computer architecture. They include accumulators, index registers,
stack pointers, and general-purpose registers, plus any condition-code
information. This state information must be saved
when an interrupt occurs, to allow the process to be continued correctly
when it regains the CPU.
CPU-scheduling This information includes a process priority, pointers to scheduling
information queues, and any other scheduling parameters.
Memory- This includes the information of page table, memory limits, segment
management table depending on memory management technique used by the
information operating system. https://www.youtube.com/watch?v=p9yZNLeOj4s
Accounting This information includes the amount of CPU and real time used, time
information limits, account numbers, job or process numbers,
and so on.
I/O status This information includes the list of I/O devices allocated to the process,
information. a list of open files, and so on.

Process Scheduling
In a multiprogramming world maximizing CPU utilization is key i.e. some process should
always be running to avoid CPU idle time. The process scheduler must select an available
process for execution on a CPU core. Each core can run one process at a time. In general
processes can be categorized as either I/O-bound or CPU-bound.

• I/O-Bound – spends more time doing I/O then performing computations


• CPU-Bound – spends more time on computations than I/O

Process Scheduling Objectives


• Maximum CPU utilization
• Fair allocation of CPU
• Maximum throughput (number of processes executing per second)
• Minimum turn around time (time taken to finish execution)
• Minimum waiting time (time for which process waits in ready queue)
• Minimum Response Time (time when process produces first response)

Page 4 of 13
Process Scheduling Queues
The OS maintains all PCBs in Process Scheduling Queues. A separate queue for
each of the process states. PCBs of all processes in the same execution state are
placed in the same queue. As the state of a process changes, its PCB is removed
from its current queue and moved to its new state queue.
• Job queue − This queue keeps all the processes in the system.
• Ready queue − This queue keeps a set of all processes residing in main
memory, ready and waiting to execute. A new process is always put in this
queue.
• Device queues − The processes which are blocked due to unavailability of an
I/O device constitute this queue.

Job Queue Ready Queue CPU

I/O I/O Waiting


Device Queue

Figure 3 Queueing Diagram of Process Scheduling

Context Switch
Context switching is an essential part of a multitasking OS. A context switch is the
mechanism to store the state or context of a process losing the CPU and to restore the state
or context of a process gaining the CPU. This state is stored in the Process Control block so
that a process execution can be resumed from the same point later. A context switcher
enables multiple processes to share a single CPU.
When the scheduler switches the CPU from executing one process to execute another, the
state of the current running process is stored into the process control block. Next, the state
of the second process is loaded from its own PCB and used to set the Process Counter,
registers, etc. The second process can start executing.

Page 5 of 13
Schedulers
Schedulers are part of the OS that handle process scheduling in various ways. Their main
task is to select the jobs to be submitted into the system and to decide which process to run.
Three types of schedulers exist, although all three need not be present in an OS

Long Term Scheduler


Also called a job scheduler. The long-term scheduler determines which programs are
admitted to the system for processing i.e. it selects processes from the job queue and loads
them into memory for execution. Such processes will be loaded into memory for CPU
scheduling.
On some systems, the long-term scheduler may not be available or minimal. Time-sharing
operating systems e.g. Windows have no long term scheduler.

Short Term Scheduler


Also called the CPU scheduler or the dispatcher. Its main objective is to increase system
performance in accordance with the chosen set of criteria. It moves process between the
ready and running states. The CPU scheduler selects a process among the processes that
are ready to execute and allocates CPU to one of them i.e. it makes the decision as to which
process to execute next.

Medium Term Scheduler


Responsible for intermediate form of scheduling, called swapping. The key idea of swapping
is that sometimes it can be advantageous to remove a process from memory (and active
contention for the CPU) The process can be reintroduced into memory , at a later stage, and
its execution can be continued where it left off. It is known as swapping because a process
can be “swapped out” from memory to disk, where its current status is saved, and later
“swapped in” from disk back to memory, where its status is restored. Swapping is typically
only necessary when memory has been overcommitted and must be freed

Operating System Scheduling Algorithms


These algorithms are either non-preemptive or preemptive. Non-preemptive algorithms are
designed so that once a process enters the running state, it cannot be preempted until it
completes executing, whereas preemptive scheduling is based on priority or time-slicing.

• Priority: Scheduler may preempt a lower priority running process anytime a higher
priority process enters the ready state.
• Time-Slicing: Once a process has had use of the CPU for its designated time it must
yield the CPU

Page 6 of 13
First-Come First-Served (FCFS) Scheduling
Processes are assigned the CPU in the order they request it i.e. there is a single queue of
ready processes. When the first job enters the system, it is started immediately and allowed
to run as long as it wants to. It is not interrupted because it has run too long. As other jobs
come in, they are put onto the end of the queue. Should the running process blocks/waits,
the first process on the queue is run next. When a blocked/waiting process becomes ready,
like a newly arrived job, it is put on the end of the queue, behind all waiting processes.
Process Burst Time (ms) Consider the set of processes that arrive at time zero.
P1 5 The length of the CPU burst time given in millisecond.
P2 24 Now we will calculate the average waiting time,
P3 16 average turnaround time and throughput of FCFS.
P4 10
Burst Time = Time to execute
P5 3

Gantt Chart of Process execution


P1 P2 P3 P4 P5
0 5 29 45 55 58

Average waiting time


Waiting Time = Starting time – Arrival time

P1 = 0 ms
P2 = 5 – 0 = 5 ms Average Waiting Time = Waiting Time All Processes / Number of Processes
P3 = 29 – 0 = 29 ms
(0+5+29+45+55)/5 = 26.8ms
P4 = 45 – 0 = 45 ms
P5 = 55 – 0 = 55 ms
(0+5+29+45+55)/5

Average Turnaround Time


Turnaround Time = Waiting time in ready queue + executing time + waiting time for I/O

P1 = 0 + 5 + 0 = 5ms Total Turnaround Time = (5+29+45+55+58) = 192ms


P2 = 5 + 24 = 29 ms
Average Turnaround Time = Total Turnaround Time / Number Processes
P3 = 29 + 16 + 0 = 45 ms
P4 = 45 + 10 + 0 = 55 ms 192/5 = 38.4ms
P5 = 55 + 3 + 0 = 58 ms

Throughput
Total Burst Time / Number Processes
(5 + 24 + 16 + 10 + 3) /5 = 11.6ms i.e. 1 process executes every 11.6ms

Page 7 of 13
Shortest-Job-Next (SJN) Scheduling (Non-Preemptive/Cooperative)
Out of all available ready processes the scheduler selects the process with the shortest
execution time. This selected process will execute until completion. The scheduler will then
again select a process from the ready queue that has the shortest execution time.
Process Burst Time (ms) Consider the set of processes that arrive at time zero.
P1 5 The length of the CPU burst time given in millisecond.
P2 24 Now we will calculate the average waiting time,
P3 16 average turnaround time and throughput of SJN.
P4 10
Burst Time = Time to execute
P5 3

Gantt Chart of Process execution


P5 P1 P4 P3 P2
0 3 8 18 34 58

Average waiting time


Waiting Time = Starting time – Arrival time

P5 = 0 ms
P1 = 3 – 0 = 3ms Average Waiting Time = Waiting Time All Processes / Number of Processes
P4 = 8 – 0 = 8ms
(0+3+8+18+34)/5 = 12.6 ms
P3 = 18 – 0 = 18ms
P3 = 34 – 0 = 34ms

Average Turnaround Time


Turnaround Time = Waiting time in ready queue + executing time + waiting time for I/O

P5 = 0 + 3 + 0 = 3ms Total Turnaround Time = (3+8+18+34+58) = 121ms


P1 = 3 + 5 + 0 = 8ms
Average Turnaround Time = Total Turnaround Time / Number Processes
P4 = 8 + 10 + 0 = 18ms
P3 = 18 + 16 + 0 = 34ms 121/5 = 24.2ms
P2 = 34 + 24 + 0 = 58 ms

Throughput
Total Burst Time / Number Processes
(5 + 24 + 16 + 10 + 3) /5 = 11.6ms i.e. 1 process executes every 11.6ms

Page 8 of 13
Shortest-Remaining Time (SRT) Scheduling (Preemptive)
The scheduler selects the process from the ready queue with the shortest execution time,
however should a new process enter the ready queue, that has a shorter execution time
then the remaining execution time of the currently executing process, it may pre-empt the
running process, i.e. it gains control of the CPU

Process Burst Time (ms) Arrival Time (ms)


P1 3 0
P2 6 2
P3 4 4
P4 5 6
P5 2 8

Gantt Chart of Process execution


P1 P1 P1 P2 P3 P3 P3 P3 P5 P5 P2 P2 P2 P2 P2 P4 P4 P4 P4 P4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Gantt Chart Explained

• P1 arrived at 0ms. No other processes were in the queue therefore it started


• At 1ms P1 is still the only Process in the Queue so it continues
• At 2ms P2 enters the Queue but its execution time of 6ms is longer than the 1ms
remaining of P1 so P1 continues
• At 3ms P1 terminates, P2 is the only process left in the Queue so it starts
• At 4ms P3 enters the Queue. Its execution time of 4ms is less than 5ms remaining of
P2
• P3 thus preempts P2. It starts executing and P2 goes back into the Queue
• At 6ms P4 enters the Queue but its time of 5ms is greater than P3 remaining time of
2ms so nothing changes
• P3 finishes at 7ms and at 8ms P5 enters the Queue. There are now three processes
in the Queue (P2-5ms, P4-5ms, P5-2ms)
• P5 is the shortest so it executes
• No new processes enter the Queue so when P5 finishes there are two processes left
in the queue (P2-5ms and P4-5ms)
• Since both have the same execution time remaining FCFS is applied by the
scheduler and P2 will execute to completion
• P4 will now execute

Average waiting time


Waiting Time = Starting time – Arrival time

P1 = 0ms
P2 = (3 – 2) + (10-4) = 7ms
Average Waiting Time = Waiting Time All Processes / Nr of Processes
P3 = 4 – 4 = 0ms
P4 = 15 – 6 = 9ms (0+7+0+9+0)/5 = 3.2ms
P5 = 8-8 =0ms

Page 9 of 13
Average Turnaround Time
Turnaround Time = Waiting time in ready queue + executing time + waiting time for I/O

P1 = 0 + 3 + 0 = 3ms Total Turnaround Time = (3+13+4+14+2) = 36ms


P2 = 7 + 6 = 13ms
Average Turnaround Time = Total Turnaround Time / Number Processes
P3 = 0 + 4 = 4ms
P4 = 9 + 5 = 14ms 36/5 = 7.2ms
P5 = 0 + 2 = 2ms

Throughput
Total Burst Time / Number Processes
(3+6+4+5+2)/5 = 4ms i.e. 1 process executes every 4ms

Round-Robin (RR) Scheduling


Each process is assigned a time interval (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. If the process has blocked/waiting or finished before the quantum has
elapsed, the CPU switching is done when the process blocks or finished.
Process Burst Time (ms) Consider the set of processes that arrive at time zero.
P1 30 The length of the CPU burst time is given in
P2 6 milliseconds. The time quantum is 5ms
P3 8

Gantt Chart of Process Execution

P1 P2 P3 P1 P2 P3 P1 P1 P1 P1
0 5 10 15 20 21 24 29 34 39 44

Average waiting time


Waiting Time = Starting time – Arrival time

P1 = (0 -0)+(15-5) + (24-20) = 14ms Average Waiting Time = Waiting Time All Processes / Nr of Processes
P2 = (5 – 0)+ (20-10) = 15ms
(14+15+16)/3 = 15ms
P3 = (10 – 0) + (21-15) = 16ms

Average Turnaround Time


Turnaround Time = Waiting time in ready queue + executing time + waiting time for I/O

P1 = 14 + 30 + 0 = 44ms Total Turnaround Time = (44+21+24) = 89ms


P2 = 15 + 6 + 0 = 21ms
Average Turnaround Time = Total Turnaround Time / Number Processes
P3 = 16 + 8 + 0 = 24ms
89/3 = 29.67ms

Throughput
Total Burst Time / Number Processes
(30+6+8)/3 = 14.67ms i.e. 1 process executes every 14.67ms

Page 10 of 13
Priority Scheduling
Each process is assigned a priority, and the process in the ready queue with the highest
priority is allowed to run. Processes with same priority are executed on first come first served
basis.
Process Burst Time (ms) Priority
P1 6 2
Consider the set of processes that arrive at time
P2 12 4 zero. Each process is assigned a priority
P3 1 5
P4 3 1
P5 4 3

Gantt Chart of Process Execution

P4 P1 P5 P2 P3
0 3 9 13 25 26

Average waiting time


Waiting Time = Starting time – Arrival time

P1 = (3 -0) = 3ms Average Waiting Time = Waiting Time All Processes / Nr of Processes
P2 = (13 – 0) = 13ms
(3+13+25+0+9)/5 = 10ms
P3 = (25 – 0) = 25ms
P4 = (0-0) = 0ms
P5 = (9-0) = 9ms

Average Turnaround Time


Turnaround Time = Waiting time in ready queue + executing time + waiting time for I/O

P1 = 3 + 6 + 0 = 9ms Total Turnaround Time = (9+25+26+3+13) = 76ms


P2 = 13 + 12 + 0 = 25ms
Average Turnaround Time = Total Turnaround Time / Number Processes
P3 = 25 + 1 + 0 = 26ms
P4 = 0 + 3 + 0 = 3ms 76/5 = 15.2ms
P5 = 9 + 4 + 0 = 13ms

Throughput
Total Burst Time / Number Processes
(6+12+1+3+4)/5 = 5.2ms i.e. 1 process executes every 5.2ms

Page 11 of 13
https://www.youtube.com/watch?v=THqcAa1bbFU (Process Scheduling)

https://www.youtube.com/watch?v=Jkmy2YLUbUY (CPU Scheduling Basics)

Page 12 of 13
Bibliography
Silberschatz, A., Baer Galvin, P., & Gagne, G. (2018). Operating System Concepts. Hoboken: John
Wiley & Sons, Inc.

Tannenbaum, A., & Bos, H. (2015). Modern Operating Systems. New Jersey: Pearson.

Tutorialspoint. (2021, 01 01). Operating System Tutorial. Retrieved from Tutorialspoint.com:


https://www.tutorialspoint.com/operating_system/

W3Schools. (2019, 01 01). Operating System Tutorial Library. Retrieved 2022, from W3Schools:
https://www.w3schools.in/operating-system-tutorial

Page 13 of 13

You might also like