100% found this document useful (1 vote)
620 views43 pages

Process Management

In this ppt, we discussed Process management component of an operating system (OS) that oversees the creation, execution, scheduling, and termination of processes.

Uploaded by

Elakkia U
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
620 views43 pages

Process Management

In this ppt, we discussed Process management component of an operating system (OS) that oversees the creation, execution, scheduling, and termination of processes.

Uploaded by

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

Process

Management
UNIT II
Process Management

 Introduction of process
 Various features of process
 Interprocess communication
Process Concepts

 processes vs Thread
 What is a process?
 A process can be a thought of as a program in execution
 What is thread?
 A thread is the unit of execution within a process.
Windows Task Manager
Process Explorer
Process Concepts

 When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data.
 A process is more than a program code which is called text section
 A process includes a stack which contains temporary data ( such as functions
parameters, return address , local variables)
 Data section – it contains global variables
 Heap – which is a memory that is dynamically allocated during process run time
Contd..
#include <stdio.h> int main()
{
#include <stdlib.h> // Local variable (Stored in Stack)
// Global variable (Stored in Data Segment -
int local_var = 5;
Initialized)
// Dynamically allocated variable (Stored in
int global_var = 10; Heap)
// Static variable (Stored in BSS Segment - int *heap_var = (int *)malloc(sizeof(int));
Uninitialized) *heap_var = 15;
static int static_var; // Perform addition
add(global_var, *heap_var);
void add(int a, int b) // Print memory locations
{ printf("Global Variable Address: %p\n",
// Local variable (Stored in Stack) (void*)&global_var);
int result = a + b; printf("Static Variable Address: %p\n",
printf("Addition Result: %d\n", (void*)&static_var);
printf("Local Variable Address: %p\n",
result); (void*)&local_var);
} printf("Heap Variable Address: %p\n",
(void*)heap_var);
printf("Function Address (Code Segment): %p\
n", (void*)&add); // Free allocated heap
memory free(heap_var); return 0; }
Process State

 As a process executes, it changes state.


 The state of a process is defined in part by the current activity of
that process.
 When a process executes, it passes through different states. These stages may
differ in different operating systems, and the names of these states are also not
standardized.
Each process can be in one of the
following states
NEW The process is being created

RUNNING Instructions are being executed

WAITING The process is waiting for some event to occur

READY The process is waiting to be assigned to a processor

TERMINATED The process has finished execution


Diagram of process state
Diagram of Process State
Process Control Block

 Each process is represent in the operating system by a Process


Control Block (PCB)- also called task control block.
 PCB -> we use to represent a particular process in the operating
system.
 A Process Control Block is a data structure maintained by the Operating System
for every process.
Process Control Block
 The PCB is identified by an integer process ID (PID). Unique ID
Process Control Block
 Process state – The current state of the process i.e., whether it
is ready, running, waiting, or whatever.
 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
Process Scheduling
 The objective of multiprogramming is to have some process running at all times, to Maximize
CPU utilization.
 The objective of time sharing is to switch the CPU among processes so frequently that users can
interact with each program while it is running.
 To meet these objectives , the process scheduler selects an available process(possibly from a set
of several available processes) for program execution on CPU
 For a single-processor system, there will never be more than one running process.
 If there are more processes, the rest will have to wait until the CPU is free and can be rescheduled
scheduling queues
Ready Queue And Various I/O Device Queues 19

Ms.U.Elakkiya AP/IT
Schedulers
 Two types
 Long-term Scheduler
 Short-term scheduler
 Short-term scheduler (or CPU scheduler) – selects which process should be executed next and
allocates CPU
 Short-term scheduler is invoked frequently (milliseconds)  (must be fast)
 Long-term scheduler (or job scheduler) – selects which processes should be brought into the
ready queue
 Long-term scheduler is invoked infrequently (seconds, minutes)  (may be slow)
Contd..

 Most of the processes can be described as either:


 I/O-bound process – spends more time doing I/O than computations,
many short CPU bursts
 CPU-bound process – spends more time doing computations; few
very long CPU bursts
 Long-term scheduler strives for good process mix of I/O and CPU bound
Medium term scheduling
Context Switch

 What is context switch?

 When does it occur?

 What context switch do in an operating system?


Context switch

 Interrupt cause the operating system to change a CPU from its current task and to
run a kernel routine.
 Such operations happen frequently on general-purpose computers.
 When an interrupt occurs, the system needs to save the current context of the
process currently running on the CPU so that it can restore that context when its
processing is done, essentially suspending the process and then resuming it.
 The context is represented in the PCB of the process
CPU Switch From Process to
Process
Contd..

 Switching the CPU to another process requires performing a state save of the
current process and a state restore of a different process.
 This task is known as context switch
 Context-switch time is pure overhead, because the system does no useful work
while switching.
 It speed varies from machine to machine , depending on memory speed , the
number of registers that must be copied, and the existence of special instructions
 A typical speed is a few millisecconds.
Operations on Processes

 A process may create several new processes via a create-process


system call , during the course of execution.

 The creating process is called a parent process and the new


processes are called the children of that process

 Each of these new processes may in turn create other


processes , forming a tree of processes
Typical process tree in Linux
Contd..
 init process serves as the root parent process for all user process.
 Once the system booted, init process can create various user
process(web server , print , ssh)
 In the diagram can see 3 childrens for init process
 kthreadd process – create additional process that perform behalf of
kernel
 sshd - managing the clients that connect to the system by using
ssh(Secure shell)
 login – managing clients that directly log onto the system
 Command ps is used to list the process
Process Creation 30
 During the execution , a process may create a several new process.

Ms.U.Elakkiya AP/IT
Parent process – creating process
Children process – new process
 Generally, process identified and managed via a process identifier (pid) , which is
an integer.
 pid provides a unique value for each process in the system, and used as an index to
access various attributes of process within kernel
Contd..
When a process creates a new process , two possibilities exist in
terms of execution:
1. The parent continues to execute concurrently with its children
2.The parent waits until some or all of its children have terminated.
 When a process creates a child process , that child will need certain
resources(CPU time, memory, files, I/O devices) to accomplish its
task
 It may obtain its resources directly from OS or it may be
constrained to a subset of the resources of the parent process
 Theparent have to partition its resources among is children or share
some resources
Contd..
There are also two possibilities in terms of the address space of the new process.

 The child process is a duplicate of the parent process( it has the same program and data as a
parent).

 The child process has a new program loaded into it.


Contd..
 Process creation is achieved through the fork() system call.
 The newly created process is called the child process and the process that initiated it (or the
process when execution is started) is called the parent process
 After the fork() system call, now we have two processes - parent and child processes. How to
differentiate them? Very simple, it is through their return values.
Contd..
Contd..
 After creation of the child process, let us see the fork() system call details.
#include<sys/types.h>
#include<unistd.h>
pid_t fork(void);
 Creates the child process.
 After this call, there are two processes, the existing one is called the parent process and the
newly created one is called the child process.
Contd..
The fork() system call returns either of the three values −
 Negative value to indicate an error, i.e., unsuccessful in creating the child process.
 Returns a zero for child process.
 Returns a positive value for the parent process. This value is the process ID of the newly created
child process.
Let us consider a simple
program.
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
fork();
printf(“ called fork() system call”);
return 0;
}
Contd..

 UNIX examples
 system call creates new process
 exec() system call used after a fork() to replace the process’ memory space with a
new program
Process Termination

 Voluntary
 Normal exit
 Internal error (EX:If no input file find)
 Involuntary
 Fatal Error (Illegal memory access/ divide by zero)
 explicitly killed by another process
Operation on process –
process termination
 A process terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit() system call
 At that point , the process may return a status value( typically an integer) to its
parent process(via the wait() system call)
 All the resources of the process – including physical and virtual memory open
files, I/O buffers - are deallocated by the operating system
Contd..
Termination can occur in other circumstances as well:
 A process can cause the termination of another process via an appropriate system call
 Usually, such a system call can be invoked only by the parent that is to be terminated.
 Otherwise, the users could kill each other’s jobs
Contd..
A parent may terminate the execution of one of its children for a variety of reasons, such as these:

1.The child has exceeded its usage of some of the resources that it
has been allocated.(To determine whether this has occurred, the
parent must have a mechanism to inspect the state of its children)

2.The task assigned to the child is no longer required.

3.The parent is exiting , and the operating system does not allow a
child to continue if its parent terminates – cascading termination
Contd..

 A zombie process or defunct process is a dead process that has completed


execution but still has an entry in the process table, usually because of bugs
and coding errors.
 If the child process is dead but its parent process is alive,
the child process is declared zombie.
 An orphan process is a process that is still executing, but whose parent has died.
These process is adopted by init (Process ID 1). Orphan process will use all
resources which is required for its execution.

Common questions

Powered by AI

A process can be in any of the following states: NEW, READY, RUNNING, WAITING, and TERMINATED . A process is in the NEW state when it is being created. Once created, it moves to the READY state, where it waits for CPU allocation by the scheduler. When the CPU scheduler selects the process, it moves to the RUNNING state to execute . If the process needs to wait for an event like I/O completion, it transitions to the WAITING state. Once the event is completed, it returns to the READY state . Finally, the process enters the TERMINATED state when it finishes execution or is explicitly killed, marking the end of its lifecycle . State transitions occur based on system scheduling and resource availability .

A Process Control Block (PCB) is a data structure maintained by the operating system for every process . It contains all the essential information about the process such as the process state (ready, running, waiting, etc.), program counter (location of the next instruction to execute), CPU registers, memory-management information (memory allocated to the process), CPU scheduling information (like priorities and scheduling queue pointers), accounting information (CPU time used, time limits, etc.), and I/O status information (devices allocated and list of open files). This information is crucial for process management to ensure the process is executing correctly and efficiently .

In operating systems like Linux, processes are organized in a hierarchical structure known as a process tree. The init process, with PID 1, acts as the root from which all other processes descend . When a process creates a new process using the fork() system call, it becomes the parent of the newly created child process. This relationship is represented as branches in the process tree, forming a parent-child hierarchy . This structure is essential for process management as it allows the operating system to track all processes and manage resources allocation, control the execution of tasks, and handle process termination scenarios. For instance, a parent process may allocate resources to the child or terminate the child upon exit or completion of its task .

The process scheduler aims to maximize CPU utilization by ensuring that the CPU is always executing a process . In a multiprogramming environment, this might mean switching between processes frequently to give the appearance of simultaneous execution, which is crucial for time-sharing systems . There are different types of schedulers involved: the long-term scheduler (job scheduler) selects which processes should be brought into the ready queue and is invoked infrequently, so it can afford to be slower . On the other hand, the short-term scheduler (CPU scheduler) selects which of the ready processes will be executed next and needs to be fast as it is invoked frequently . Also, there is a medium-term scheduler which can be involved in swapping processes in and out of memory to balance the load between I/O and CPU-bound processes .

The fork() system call is used to create a new process in UNIX-based systems. When fork() is called, it creates a child process by duplicating the address space of the parent process . After a successful fork() call, two processes are created: the parent and the child process. The distinction between these two processes can be made by checking the return value of fork(). The parent process receives the child's PID (a positive value), while the child process receives a return value of zero . If fork() fails, it returns a negative value to indicate an error in the creation of the child process .

A process is an independent program in execution, which includes a set of resources like memory, I/O devices, etc. It has separate address spaces divided into sections such as stack, heap, text, and data . Each process has its own Process Control Block (PCB), which contains information like the process state, program counter, and memory-management information . A thread, on the other hand, is the smallest unit of execution within a process. It operates within the process's memory space and shares resources like code, data, and files with other threads within the same process, but has its own stack, register set, and program counter . Threads are more efficient in terms of execution as context switching between threads of the same process requires saving and loading hardware contexts but typically consumes less overhead than switching between processes .

Context switching refers to the process where the CPU switches from executing one process to another. It involves saving the state of the currently running process in its PCB and loading the state of the next process to execute . Context switching is considered an overhead because the CPU does not perform any useful work during this time; it only transitions between processes. The time taken for a context switch can significantly affect system performance, as it depends on factors like memory speed and the number of registers that need to be saved . While context switching is necessary for multitasking, frequent switching can decrease CPU efficiency and responsiveness, especially in systems with high process loads .

Zombie processes occur when child processes have completed execution but still reside in the process table due to the parent not calling wait() to read their exit status. They consume minimal resources but can fill the process table, preventing new processes from being started if the table gets full . Orphan processes, however, are processes whose parent has terminated. These are adopted by the init process in Unix-based systems and persist until they complete execution. While orphans are generally not problematic, having many orphans could indicate poor process management, potentially leading to improper resource utilization . Proper handling and cleanup of these processes are important for maintaining system stability and efficiency .

The fork() system call is used to create a copy of the existing process, resulting in two processes: the original (parent) and the newly created one (child) with identical code, data, and heap memory segments . It provides a mechanism for a process to create a new one. In contrast, the exec() family of system calls is used to replace the current process's memory space with a new program. After a fork(), a process can call exec() to run a different program, whereas fork() only duplicates the existing process without altering the current program . The combination of fork() and exec() is fundamental to Unix process management, allowing for flexibility in starting new programs with separate execution paths .

Short-term and long-term schedulers perform distinct roles in process management in operating systems. The short-term scheduler, also known as the CPU scheduler, is responsible for selecting which ready process should execute next and is invoked very frequently as processes need to be rotated in and out of the CPU regularly, which demands low-latency, high-speed decision-making . It’s crucial for maintaining performance and responsiveness in time-sharing systems. Conversely, the long-term scheduler, or job scheduler, determines which jobs or processes should enter the system for processing and is invoked infrequently (e.g., every few seconds or minutes). It balances the mix of CPU-bound and I/O-bound processes, aiming for optimal CPU utilization . This strategic overview helps maintain system stability and load balancing over longer periods.

You might also like