Lecture3 OS
Lecture3 OS
● Process States
● Operations on Processes
2
The Process Concept
3
What is a Process?
● A process can create another process, delete another process or it can schedule
another process as per the codes 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
● 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.
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.
10
Process vs. Program: Points to remember
● A program is a set of instructions written to perform a specific task.
● The main difference between a program and a process is that a program can exist
independently but a process cannot exist without a program.
● 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.
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.
14
Topics for today…
● Process States
● Operations on Processes
15
Process States
A process can change its state during its lifetime. The
various states of a process are (5 states):
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
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.
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.
● Process States
● Operations on Processes
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
function25mins
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
• 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 printing10mins
• 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?
• 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 printing10mins
• 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
31
Process Creation
More in Lab
32
Summing up Process Creation
33
Process Termination
There are two methods a process can terminate:
• 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
36
Summing up Process Termination
37
Topics for today…
● Process States
● Operations on Processes
38
Process Control Block (PCB)
• 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.
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
CPUSCHEDULING?
Process table to find the reference
to the PCB with the help of the
corresponding Process ID.
Next Week