#OS Lecture Note 2 Process Management-1
#OS Lecture Note 2 Process Management-1
Mattu University
Engineering and Technology College
Department of Computer Science
Operating System
By: Tekalign B.
Lecture Two
Process scheduling
Inter-process communication
Deadlock
Detection
Prevention
Avoidance
2/1/2024
What is a Process?
4
Only one process can be running in the CPU at any given time!
2/1/2024
The Process model
5
Multiprogramming of four
programs
Conceptual model
4 independent processes
Processes run sequentially
Only one program active at any
instant!
That instant can be very short…
2/1/2024
Process Control Blocks (PCBs)/Process Descriptors
6
Process operations can be easily controlled with the help of
process control block (PCB).
Typically include information such as
Process identification number (PID)
Process state - running, waiting, etc
Program counter – location of instruction to next execute
CPU scheduling information- priorities, scheduling pointers
A pointer to the process’s parent process
Memory-management information –
memory allocated to the process
Process Table
The OS maintains pointers to
each process’s PCB in a system-
wide or per-user process table
Allows for quick access to PCBs
When a process is terminated,
the OS removes the process from
the process table and frees all of
the process’s resources
What is Thread?
8
Thread Example
Suppose that the word processor is written as a two threaded
program.
One thread interacts with the user and
the other handles reformatting in the background.
As soon as the sentence is deleted from page 1, the interactive
thread tells the reformatting thread to reformat the whole book.
Process Communication
Cont’d…
17
Process Creation
When is a new process created?
System initialization : one or more processes created when the
OS starts up
Execution of a process creation system call by a running
process : something explicitly asks for a new process
A user request to create a process (system call executed from
user shell)
Initiation of a batch job: Already running processes
User programs
System daemons
Cont’d…
18
Process Creation …
Parent process creates children processes, which, in turn create
other processes, forming a tree of processes
Generally, process identified and managed via a process identifier
(pid) init
pid = 1
Execution options
Parent and children execute concurrently bash khelper pdflush sshd
pid = 8416 pid = 6 pid = 200 pid = 3610
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
Cont’d…
19
Process Termination
When is a new process Terminated?
Conditions that terminate processes can be
1. Voluntary
Normal Exit
Error Exit
2. Involuntary
Fatal error (only sort of involuntary)
Killed by another process
Cont’d…
20
Process Termination …
Process executes last statement and then asks the operating system
to delete it using the exit() system call.
Returns status data from child to parent (via wait())
Process State
As a process executes, it changes state
Process States
Cont’d…
24
CPU Scheduler …
Max throughput
the process that has the shortest expected remaining process time.
its selection function is remaining execution time
Uses preemptive decision mode
Cont’d…
50
Cont’d…
51
Cont’d…
52
Cont’d…
53
Cont’d…
54
Cont’d…
55
4. Round Robin (RR) Scheduling Algorithm
Round Robin scheduling
Give each process a fixed time slot (quantum)
Rotate through “ready” processes
Each process makes some progress
Oldest, simplest, fairest algorithm
What’s a good quantum?
Too short: many process switches hurt efficiency
Too long: poor response to interactive requests
Typical length: 10–100 ms
Advantages of RR
It doesn’t face the issues of starvation
It deals with all process without any priority
Cont’d…
56
Cont’d…
57
Cont’d…
58
Advantages of RR
It doesn’t face the issues of starvation
All the jobs get a fair allocation of CPU
It deals with all process without any priority
Disadvantages of RR
Priorities cannot be set for the processes
Finding a correct time quantum is a quite difficult task in this
system
Cont’d…
59
5. Priority Scheduling Algorithm
Assign a priority to each process
“Ready” process with highest priority
allowed to run
Running process may be interrupted after its
quantum expires
Priorities may be assigned dynamically
Reduced when a process uses CPU time
Increased when a process waits for I/O
SJF is a priority scheduling where priority is
the predicted next CPU burst time.
Problem => Starvation – low priority
processes may never execute.
Solution => Aging – as time progresses
increase the priority of the process.
Cont’d…
60
Inter-Process Communication
61
Mechanism for process to communicate and to
synchronize their action.
Very briefly, there are three issues here:
The first: how one process can pass information to another.
The second: making sure two or more processes do not get in each
other's way,
• for example, two processes in an airline reservation system each
trying to grab the last seat on a plane for a different customer.
The third: proper sequencing when dependencies are present:
• 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’d…
62
Pipe:
A pipe is a data channel that is unidirectional.
Two pipes can be used to create a two-way data channel between two
processes. Pipes are used in Windows operating systems.
Socket:
The socket is the endpoint for sending or receiving data in a network.
Most of the operating systems use sockets for IPC.
File:
Signal: A signal is a way of communicating between processes.
Shared Memory: is the memory that can be simultaneously accessed by
multiple processes. Windows operating systems use shared memory.
Message Passing and Queue
Race Conditions
65
A race condition is potential IPC problem that occurs in an
operating system (OS) where two or more processes or threads
are executing concurrently.
A race condition is a situation that may occur inside a critical
section.
example
If two threads are simultaneously accessing and changing the
same shared resource, such as a variable or a file, the final
state of that resource depends on the order in which the
threads execute.
If the threads are not correctly synchronized, they can
overwrite each other's changes, causing incorrect results or
even system crashes.
Cont’d…
66
Cont’d…
67
Effects of Race Condition in OS
Deadlocks: This can happen if two processes try to access the
same resource simultaneously, and the OS doesn't have a
mechanism to ensure that only one can access the resource at a
time.
Data Corruption
Security Vulnerability
Performance Degradation
Cont’d…
68
Example
Two trains approaches each other at crossing, both will come
to full stop and neither shall start until other has gone.
Traffic only in one direction.
Each section of a bridge can be viewed as a resource.
If a deadlock occurs, it can be resolved if one car backs up
(preempt resources and rollback).
Several cars may have to be backed up if a deadlock occurs.
Cont’d…
74
Example2
Conditions for deadlock
75
These conditions of policy must be present for a deadlock to be
possible:
1. Mutual Exclusion – only one process at a time can use a resource.
If another process requests that resource, requesting process must wait
until the resource has been released.
2. No preemption – a resource only released by process’s volunteer
3. Hold and Wait - a process holding at least one resource is waiting
to acquire additional resources held by other processes
4. Circular Wait: : A set {P0, P1, …….Pn} of waiting state/
process must exists such that P0 is waiting for a resource that is
held by P1, P1 is waiting for the resource that is held by P2
…..P(n – 1) is waiting for the resource that is held by Pn and Pn is
waiting for the resources that is held by P4.
Methods for handling Deadlock
76
possibility of deadlock
Resource Pre-emption
Selecting a victim - minimize cost.
Rollback - return to some safe state, restart process from that state.
Starvation - same process may always be picked as victim; include number of rollback
in cost factor.
Examples of Process Management os Commands and
Tools
82