0% found this document useful (0 votes)
13 views

Lecture3 OS

Uploaded by

Avan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture3 OS

Uploaded by

Avan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

LECTURE 3

PROCESS MANAGEMENT: Part1


Process, its States and Operations

Dr. Akshi Kumar


Topics for today…

● The Process Concept

● Process States

● Operations on Processes

● Process Control Block

2
The Process Concept

● An operating system executes a variety of programs:


 Batch system – jobs
 Time-shared systems – user programs or tasks

● Textbook uses the terms job and process almost interchangeably

● Process – a program in execution; process execution must progress in


sequential fashion

3
What is a Process?

● A Process is an execution of a specific program.

● It is an active entity that actions the purpose of the


application.

● Multiple processes may be related to the same


program.

● For example, if you double-click on Google


Chrome browser, you start a process that runs
Google Chrome and when you open another
instance of Chrome, you essentially create a second
process.
4
What is a Program in OS?
● When we write and compile the code, we get an executable file.
● The executable file which gets created is known as a program. The executable
file created has all the instructions or the code within it.
● The file will be inactive unless we execute it that's why a program is a passive
entity.
● A program is a system activity that has a set of instructions, and it performs a
specific task.
● In batch processing systems it is known as executing jobs and in the real-time
operating system, it is known as a program.
● A user can run many programs simultaneously.
5
● Edge.exe, notepad.exe, or any executable files are examples of programs.
What is a Process in OS?
● When we execute a program, it is known as a process.

● The process executes all the lines of code in the program.

● A process can create another process, delete another process or it can schedule
another process as per the codes written in the program.

● When a program is executed, a process is started, and it needs memory address,


CPU, and I/O devices while it is running.

● A process is responsible for performing specified tasks written in the program.

● For example, when we double click on Edge.exe, this will execute an instance
of the program and the Edge browser will start running, this is a process.
6
What is a Process?
After you write a program in any language, two steps follow:
1. Compiling
2. Running/Executing

● The second step is what makes that program a process.

● You double-click any software in your computer system, or you tap on any
application your mobile or you write a command like $./a.out, all these convert
the application(program) into a process.

● Every application is a program until you execute it by double click or a tap or a


command, after which it becomes a process.

So, a process is a program in execution 7


What is a Process?

PCB Process Control


Block…will see at the
end of the lecture

8
Process vs. Program
● Process is an executing part of a program whereas a program is a group of
ordered operations to achieve a programming goal.

● The process has a shorter and minimal lifespan whereas program has a longer
lifespan.

● Process contains many resources like a memory address, disk, printer while
Program needs memory space on the disk to store all instructions.

● When we distinguish between process and program, Process is a dynamic or


active entity whereas Program is a passive or static entity.

● To differentiate program and process, Process has considerable overhead


whereas Program has no significant overhead cost.
9
Process vs. Program

10
Process vs. Program: Points to remember
● A program is a set of instructions written to perform a specific task.

● A process is an instance of the program i.e, a process is started when a program is


executed.

● The main difference between a program and a process is that a program can exist
independently but a process cannot exist without a program.

● A program's lifespan is longer since it is stored in secondary memory until it is


manually erased, whereas a process' lifespan is shorter and limited because it is
terminated after the work is completed.

● In the case of a process, the resource requirements are substantially larger, it may
demand processor, memory, and I/O resources to complete the process successfully.
A program, on the other hand, only requires memory for storage.
11
Structure of a Process
Now, once you initiate a process, the
operating system loads it into the memory
(RAM). Inside the RAM the structure of the
process looks like as shown below:
1. Text – program code
2. Data – contains global variables
3. Stack – contains temporary data
i. function parameters
ii. return addresses
iii. Local variables
4. Heap – memory allocated dynamically
during run time
12
Structure of a Process
● The text section contains the program or the code,
the data section contains the global variables.

● These two sections have fixed size because neither the


code is going to change, nor the variables used in the
program.

● Heap is used for dynamic memory allocation. Now,


we use dynamic memory allocation when we cannot
determine the memory required. Hence the heap
section can grow in size if required.

13
Structure of a Process
● Lastly, the stack section is used for functions. Again,
the size of this section is also variable. Because it is
difficult to determine the number of function calls
required.

● Consider a program for factorial calculation which


uses recursive functions. If the number is 5 then the
function call is 5 times. If the number for factorial is
20, then the function call is 20 times. So, the system
does not know what exactly will be the stack size.
Hence, the stack size is variable.

14
Topics for today…

● The Process Concept

● Process States

● Operations on Processes

● Process Control Block

15
Process States
A process can change its state during its lifetime. The
various states of a process are (5 states):

● New – when a process is created

● Ready – when the process is in the RAM and is


waiting for CPU allocation (Waiting for processor)

● Running – the process gets the CPU and is


executing in this state

● Waiting – the process is waiting for some event to


occur or an I/O device

● Terminated – the process finishes its execution


either normally or forcefully 16
Process State Diagram

17
Explanation: Process States
● Step 1 − Whenever a new process is created, it
is admitted into ready state.
● Step 2 − If no other process is present at
running state, it is dispatched to running based
on scheduler dispatcher.
● Step 3 − If any higher priority process is ready,
the uncompleted process will be sent to the
waiting state from the running state.
● Step 4 − Whenever I/O or event is completed
the process will send back to ready state based
on the interrupt signal given by the running
state.
● Step 5 − Whenever the execution of a process Video: https://youtu.be/Y3mQYaQsrvg
is completed in running state, it will exit to
terminate state, which is the completion of
process.
Process State and memory

● While waiting, the process still occupies the main


memory which is a constraint with limited memory
availability as the I/O activity or event might take
time to complete.

● Therefore, the process in wait state can be shifted


to secondary memory by defining 2 new states:
 suspend wait state
 suspend ready state

19
Process State and memory

20
Suspended States
Suspend Ready State Suspend Wait State
● If a process with a higher priority • If a process with a higher priority needs to be
needs to be executed while the main executed while the main memory is full, the
memory is full, the process goes from process goes from the wait state to the
ready to suspend ready state.
suspend wait state.
● Moving a lower-priority process from • Moving a lower-priority process from the
the ready state to the suspend ready
wait state to the suspend wait state frees up
state frees up space in the ready state
for a higher-priority process. space in the ready state for a higher-priority
process.
● Until the main memory becomes
available, the process stays in the • The process gets moved to the suspend-ready
suspend-ready state. The process is state once the resource becomes accessible.
brought to its ready state when the The process is shifted to the ready state once
main memory becomes accessible. the main memory is available. 21
Suspended State-Why?
● It is much more preferable to move a given
process from its wait state to suspend wait
state.

Consider the situation where a high-priority process


comes, and the main memory is full.

Then there are two options for making space for it.
They are:
1. Suspending the processes that have lesser
priority than the ready state.
2. Transferring the lower-priority processes
from wait to the suspend wait state.

Now, out of these:


• Moving a process from a wait state to a suspend
wait state is the superior option.
• It is because this process is waiting already for a
resource that is currently unavailable. 22
Topics for today…

● The Process Concept

● Process States

● Operations on Processes

● Process Control Block

23
Operations on Process
One can perform two operations on a process in OS:
1. Process creation
2. Process Termination

24
Process Creation
● The first operation that you can perform on the process is that you can create a
new process.

● So, a process, while its running can create a new process/ processes.

● The process that is creating the new process is called the PARENT process,
whereas the new process that is created is called the CHILD process.

● The CHILD process in turn can further create new child process.

25
Why to create a child process?
● Before, we move ahead, we need to understand what is the need to create a
child process!
Why will parent create a child? Why can’t it do the work by itself?
Let’s understand with the help of the following scenario:
You have a process, which does two
things (2 functions to do):
• The first function is that it is required
to print a document.
• The second function is that it is
required to do a complex calculation.
Both these task are going to take some
time (assuming hypothetically), say
function1 10mins
function25mins
26
Total time to finish the process =15mins
Why to create a child process?
• So, now for function1 to print a document, the
resource that the process requires: PRINTER

• For function2, the resource that the process requires


to complete calculation: PROCESSOR (CPU)

• The printer and the CPU are both different resources


and the functions, 1 and 2 are independent of each
other, but it is the limitation of the process that can
do only one task at a time, that is it cannot perform
both the task simultaneously.

• As it can do one thing at a time, hence it took 10 plus


5 minutes in total, 15 minutes to complete that task.
27
Why to create a child process?
• Now, let us suppose that, the process (we call it as P) creates another process, P1

• This process is the PARENT process P and it creates a new process, P1, which is called
the CHILD process.

• The Process P itself takes the task of printing the document and it assigns the calculation
part to P1.

28
Why to create a child process?

• So, now there are two process and both have got their own resource, one has got the
printer and the other has got the CPU.

• Both can execute the same time and the total time taken by the process to complete will be
equal to the time taken by the slowest process, which in this case is printing10mins

• We have saved 5 minutes by dividing the task among the two processes where one of the
tasks first performed by the parent process and there are task was performed by the child
29
process.
Why to create a child process?

The child process and


the parent process can
perform them at the
same time.

• So, now there are two process and both have got their own resource, one has got the
printer and the other has got the CPU.

• Both can execute the same time and the total time taken by the process to complete will be
equal to the time taken by the slowest process, which in this case is printing10mins

• We have saved 5 minutes by dividing the task among the two processes where one of the
tasks first performed by the parent process and there are task was performed by the child
30
process.
When a Process creates a new process

For example, suppose the parent is to execute


c=a+b; while the child has to execute a=e+f;
Now, the parent cannot proceed until the value
of ‘a’ is calculated by the child.

which means that the child process will also


have the same code as the parent process.
Thus, the code in the parent should clearly
define the task for itself and the child
process.

31
Process Creation

• Every process in the system will be identified by a process identifier, the


child, the parent, every process will be have a unique process ID called the
PID (Process Identifier)

• No 2 processes in the system can have the same PID

How to create the child process?


The parent is going to make use with system call.

Every OS has got a different set of system calls.

More in Lab
32
Summing up Process Creation

33
Process Termination
There are two methods a process can terminate:

• Normal termination – A process finishes executing its final statement. All


the resources allocated to it are freed by the operating system.
using the exit() system call

• Forced Termination – a parent process can terminate its child process by


invoking the appropriate system call. The parent can terminate the child due
to the following reasons:
1. Child exceeds its usage of resources using the abort() system call
2. Task assigned to the child is no longer required
3. Parent exits and OS does not allow child to run if parent terminates
34
Process Termination
• The user can also forcefully terminate a process if it is not required
anymore.

• For example, suppose you are listening to a song on a media player and
suddenly you no more want to listen to the song and want to watch a movie
on television. So you close the player thus forcibly killing the process before
it could complete the song.

• Certain operating systems do not allow a process to exist without its parent.
Now, suppose a process terminates either normally or forcibly. Then all its
child process will also be killed, which may in turn kill their child process.
This is called cascading termination.
35
Process in Linux and Windows
● In the Linux operating system, if the parent process will terminate then all
associated child processes will be forced to exit but,

● In the Windows operating system, the child process will continue running
if their parent process terminated.

● When a process is terminated but its status is not read by the parent process
yet then the process is known as a zombie process

(We will learn more about this lab session)

36
Summing up Process Termination

37
Topics for today…

● The Process Concept

● Process States

● Operations on Processes

● Process Control Block

38
Process Control Block (PCB)

• While creating a process the operating system performs several operations.

• To identify the processes, it assigns a process identification number (PID) to


each process. As the operating system supports multi-programming, it needs
to keep track of all the processes.

• For this task, the process control block (PCB) is used to track the process’s
execution status.

• Each block of memory contains information about the process state, program
counter, stack pointer, status of opened files, scheduling algorithms, etc.
Process Control Block (PCB)

• All these information is required and must be saved when the process is
switched from one state to another.

• When the process makes a transition from one state to another, the operating
system must update information in the process’s PCB.

Process Control block (PCB) is a data structure that stores information of


a process.
Process Control Block (PCB)
Information associated with each process (also called task
control block)
● Process state – running, waiting, etc
● Program counter – location of instruction to next execute
● CPU registers – contents of all process-centric registers
● CPU scheduling information- priorities, scheduling queue
pointers
● Memory-management information – memory allocated to
the process
● Accounting information – CPU used, clock time elapsed
since start, time limits
● I/O status information – I/O devices allocated to process, list
of open files
Where is PCB Stored?

PCBs are stored in specially reserved memory for the operating system known
as kernel space.
**Note: **The Random Access Memory (RAM) can be logically divided into two distinct regions namely -
the kernel space and the user space. kernel space is the core of the operating system. It normally has full access to
all memory and machine hardware and it can’t be accessed by the user.
How is PCB Stored?
• PCB is unique for every process which consists of various attributes such as
process ID, priority, registers, program counters, process states, list of open
files, etc.

• PCBs are stored in the form of LinkedList in memory as shown in the figure.
How is PCB Stored?
• Operating System uses Process Table to find the PCB present in memory.

• Process table is a table that contains Process ID and the reference to the
corresponding PCB in memory. We can visualize the Process table as a
dictionary containing the list of all the processes running.
So, whenever a context switch
CONTEXT SWITCHING?
occurs between processes the
Which process to get
operating system refers to the
CPUSCHEDULING?
Process table to find the reference
to the PCB with the help of the
corresponding Process ID.

Next Week

You might also like