0% found this document useful (0 votes)
3 views29 pages

Module 2 Part - 1

Uploaded by

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

Module 2 Part - 1

Uploaded by

qnied7295
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROCESS MANAGEMENT

Program

 A program is a passive entity

 Written using a high-level language (C, C++, Java, etc.)

 Stored on secondary storage (hard disk / SSD)

 After compilation, it becomes machine code

 A program does nothing by itself unless executed

Example:
demo.c stored on disk is only a program, it is not active.

Process

 A process is a program in execution

 It is an active entity

 A process is created when a program is submitted for execution

 Managed and controlled by the operating system

 Requires system resources:

o CPU time

o Memory

o I/O devices

o Files

Key points:

 One program can create multiple processes

 Each process has:

o Its own memory space

o Program counter

o Registers

o State (new, ready, running, waiting, terminated)

Example:
 Opening the same application multiple times creates multiple
processes

Thread

 A thread is the smallest unit of CPU execution inside a process.

 Exists inside a process

 A process must have at least one thread

 Multiple threads within a process share:

o Code section

o Data section

o Files and OS resources

 Each thread has:

o Its own program counter

o Stack

o Registers

Important points:

 Threads enable parallelism

 Faster context switching than processes

 Improves CPU utilization

Single-threaded vs Multi-threaded Process


1. Single-threaded process:

 Only one thread

 One task at a time

2. Multi-threaded process:

 Multiple threads

 Tasks executed concurrently

 Better performance and responsiveness

PROCESS STATES

 As a process executes, it moves through different stages. This is


known as the Process Life Cycle

 The state of a process represents the current activity of the


process

 At any moment, a process is in one specific state

 The operating system changes process states to manage CPU and


resources efficiently

Process State Diagram

1. New State

 Process is being created


 OS has received a request to create a process

 Process Control Block (PCB) is created

 Resources are not yet fully allocated

2. Ready State

 Process is ready to execute and is waiting to be assigned to a


processor

 All required resources are allocated except CPU

 Process is waiting in the ready queue

 It is already in memory and waiting for CPU scheduling

3. Running State

 Process is currently executing on the CPU

 CPU instructions are being performed

 Only one process can be in running state per CPU core

4. Waiting (Blocked) State

 Process cannot continue execution

 Waiting for an external event like I/O operations

 CPU is released while waiting

5. Terminated State

 Process has completed execution

 OS releases all resources

 PCB is removed from process table

PROCESS CONTROL BLOCK (PCB)

Definition
 Process Control Block (PCB) is a data structure used by the
operating system to store all information about a process

 It represents a process inside the operating system

 Also called Task Control Block (TCB)

 Created when a process is created

 Deleted when the process is terminated

Purpose of PCB

 Helps the OS manage, control, and schedule processes

 Enables context switching

 Stores the complete execution context of a process

Major Fields of PCB

1. Process ID (PID)

 Unique identification number assigned to each process


 Used by the OS to distinguish between processes

2. Process State

 Indicates the current state of the process

 Possible states:

o New

o Ready

o Running

o Waiting (Blocked)

o Terminated

3. Program Counter

 Stores the address of the next instruction to be executed

 Essential to resume execution after interruption or context switch

4. CPU Registers

 Stores the current values of CPU registers used by the process

 Required to restore process execution accurately

5. CPU Scheduling Information

 Contains information needed by the CPU scheduler

 Includes:

o Process priority

o Time information

 Determines when and how long a process gets CPU time

6. Memory Management Information

 Details about memory used by the process

 Helps OS manage address space and provide memory protection


7. Accounting Information

 Keeps track of resource usage

 Includes:

o CPU time used

o Execution time

o User and system time

 Used for performance monitoring and billing

8. I/O Status Information

 Stores information about I/O devices allocated to the process

 Includes:

o List of open files

o I/O device status

 Helps manage input and output operations

Role of PCB in Context Switching

 When a process is interrupted:

o Current process state is saved into its PCB

o CPU loads PCB of next scheduled process

 Enables smooth switching between processes

PROCESS SCHEDULING

 Process scheduling is the activity of selecting one process from


multiple processes to use the CPU

 It enables efficient sharing of CPU among processes

 Controlled by the scheduler


Objectives of Process scheduling

1. Multiprogramming

 Multiple processes are kept in main memory

 Goal is to maximize CPU utilization

 CPU should never remain idle if at least one process is ready

 While one process waits for I/O, another process uses the CPU

 Increases system throughput

 Reduces CPU idle time

2. Time-Sharing

 Allows multiple users or tasks to interact with the system


simultaneously

 CPU switches between processes very fast

 Each process gets a small time slice

 Creates an illusion of parallel execution

 Improves system responsiveness

 Important for interactive systems

Process Scheduler

 A process scheduler is an OS component that selects one process


from the ready queue and allocates the CPU to it.

Types of Process Scheduler –

1. Long-Term Scheduler (Job Scheduler)

The Long-Term Scheduler is responsible for loading processes from disk


into main memory so they can begin execution. When a new process is
admitted, it moves into the Ready state.
Key Functions

 Transfers processes from the Job Queue to the Ready Queue.

 Decides Which process should be loaded into memory and how


many processes should be in memory

Performance

 It is the slowest among all schedulers, as it operates less


frequently.

2. Short-Term Scheduler (CPU Scheduler)

The Short-Term Scheduler (STS) is responsible for selecting a process


from the ready queue and assigning the CPU to it.

Key Functions

 Frequently selects the next process to execute from the ready state.

 Ensures no process suffers from starvation.

Performance

 It is the fastest scheduler, since it operates very frequently.

3. Medium-Term Scheduler

The Medium-Term Scheduler (MTS) manages swapping, which


temporarily moves processes between main memory and disk.

Key Functions

 Swaps process out of memory when they are waiting (e.g., blocked
for I/O).

Performance

 It operates faster than the long-term scheduler but slower


than the short-term scheduler.

Scheduling Queues

1. Job Queue

 Contains all processes that have entered the system

 Processes are waiting to be admitted into main memory


 Processes are stored in secondary memory

 Managed by the long-term scheduler

2. Ready Queue

 Contains processes that are:

o Loaded in main memory

o Ready to execute

 Processes wait here for CPU allocation

 Managed by the short-term scheduler

3. Waiting Queue (Blocked Queue)

The Waiting Queue contains processes that:

 Cannot execute even if CPU is free

 Are waiting for some event or resource

Possible Outcomes After CPU Allocation

1. Completion

 Process completes execution

 Moves from Running to Terminated state


 Resources are released

2. Preemption

 A higher-priority process arrives

 Current process is interrupted

 CPU state is saved

 Process moves back to Ready Queue

 Higher-priority process gets CPU

3. I/O Wait

 Process requests I/O device

 Moves to I/O waiting queue

 CPU is released

 After I/O completion, process returns to Ready Queue

CONTEXT SWITCHING

 Context switching is the process of saving the state of one running


process and loading the state of another process so that the CPU
can switch between them.

 Enables multiple processes to share the CPU


Context of a Process

 Context refers to the complete execution state of a process

 Stored in the Process Control Block (PCB)

 Includes:

o Process state

o Program counter

o CPU registers

o Stack pointer

o Scheduling information

 This information must be saved so the process can resume later


from the same point

When Context Switching Occurs

 Timer interrupt (time slice expires)

 Higher-priority process becomes ready

 Process requests I/O

 Process completes execution

 Hardware or software interrupt occurs

Why Context Switching Is Needed

 Supports multitasking

 Ensures CPU is efficiently shared

 Allows suspended processes to resume from the same point

 Maintains system responsiveness


PROCESS OPERATIONS (CREATION AND TERMINATION)

1. Process Creation
Definition

 Process creation is the mechanism by which a running process


creates a new process

 The creating process is called the parent process

 The newly created process is called the child process

 Creation is done using a fork() system call

Parent – Child Relationship

 A parent process can create multiple child processes

 Each child can further create its own children

 This forms a process tree structure

Characteristics of Parent and Child Processes

 Each process has a unique Process ID (PID)

 Child process stores the PID of its parent

 Parent and child execute as separate processes

Execution Behaviour After Process Creation

a. Concurrent Execution

 Parent and child execute simultaneously

 Both compete for CPU time

 Execution order is decided by the scheduler

Example: Downloading a File in a Browser

 Parent process: Web browser main process

 Child process: File download process

What happens

1. You click Download

2. Browser creates a child process to handle downloading


3. Download continues in background

4. You can:

o Open new tabs

o Scroll pages

o Watch videos

b. Parent Waiting for Child

 Parent process may wait until the child finishes execution

 Parent uses a wait system call

 Parent resumes only after child terminates

Example: Installing an application

 Parent process: Installer launcher

 Child process: Installation process

What happens

1. You start installation

2. Installer creates a child process

3. Parent waits

4. Installation completes

5. Installer closes

2. Process Termination

Definition

 Process termination occurs when a process finishes execution

 Process requests deletion from the OS using an exit system call


 Operating system removes the process from memory

Actions During Termination

 Process returns a status value to parent

 Parent collects status using wait system call

 OS deallocates all resources

Reasons for Process Termination

1. Self-Termination

 Process completes its task normally

 Executes its final statement

 Calls exit system call

2. Termination by Another Process

 Usually done by the parent process

 Prevents unauthorized termination by other users

 Ensures system security and control

Why a Parent Terminates Its Child

1. Resource Limit Exceeded

 Child uses more CPU time, memory, or resources than allowed

 Parent monitors child execution

 Parent terminates child to protect system stability

2. Task No Longer Required

 Assigned task becomes unnecessary

 Parent stops child execution to save resources


3. Parent Termination

 In some operating systems:

o When parent terminates

o All child processes are also terminated

 Prevents orphan processes

INTERPROCESS COMMUNICATION (IPC)

It is the mechanism by which processes communicate and share data


with each other in an operating system. IPC is required only when
processes need to cooperate.

Types of Processes

a) Independent Processes

 Do not share data

 Cannot affect other processes

 Execute independently

Example:
Two separate programs running without any interaction.

b) Cooperating Processes

 Can affect or be affected by other processes

 Often share data

 Require IPC to communicate safely

Example:
A text editor and a file-saving process working together.

Why Processes Need to Cooperate

Processes cooperate for the following reasons:

1. Information Sharing
 Multiple processes need access to the same data

 Example: Many users reading the same file

2. Computational Speed

 Large task divided into smaller tasks

 Each task handles by different process run in parallel for faster


execution

3. Modularity

 System divided into smaller modules

 Each module is a separate process which communicate to complete


a task

4. Convenience

 User performs multiple tasks at once so processes must coordinate


smoothly

 Example: Typing while listening to music

IPC Models

There are two basic models of IPC:

1. Shared Memory Model

How it Works

 OS creates a shared memory region

 Cooperating processes access this memory

 One process writes data

 Another process reads data

Key Point

 Processes communicate by reading and writing memory

 Faster communication

 Requires synchronization
Example:
Process A writes data → Process B reads data from shared memory

2. Message Passing Model

How it Works

 Processes communicate by sending messages

 Messages go through the kernel

 Kernel delivers message to the destination process

Key Point

 No shared memory

 Safer but slower than shared memory

Example:
Process A sends message → Kernel → Process B receives message

1. Basic Operations in Message Passing

Communication always has:

1. Someone who sends

2. Someone who receives


Operations

 Send(message)

Used by a process to send data

Message is sent to the kernel

 Receive(message)

Used by a process to receive data

Kernel delivers the message to the receiver

2. Message Size

 OS must store, copy, and deliver messages

 Message size affects:

o memory usage

o performance

o complexity

A. Fixed-Size Messages

 Every message has same predefined size

 Example: exactly 64 bytes

System level implementation is easier but makes the task of


programming difficult

B. Variable-Size Messages

 Message size can change

 Exactly fits the data

System level implementation is difficult but programming task


becomes easier
3. Issues in Message Passing

A. Naming (Who talks to whom?)

 OS must know:

o sender

o receiver

 Without clarity, message can go to wrong process

Solution 1: Direct Communication

Here, the sender clearly names the receiver.

How it works

 Sender knows exactly which process to send message


 Receiver also knows who sent the message

Operations

send(P, message) // send message to process P

receive(Q, message) // receive message from process Q

Solution 2: Indirect Communication

Here, processes do not talk directly.

They communicate using a mailbox (or port).

What is a Mailbox?

A mailbox is a temporary storage area managed by the OS where:

Messages are placed by senders

Messages are picked up by receivers

Why mailbox was introduced


 To remove direct dependency
 To allow flexible communication

How it works

 Sender sends message to a mailbox


 Receiver reads message from mailbox

Operations

send (mailbox, message)

receive (mailbox, message)

B. Synchronization (Who waits?)

Synchronization decides whether processes must wait during


communication.

 If sender and receiver are not coordinated:

o sender may send too early

o receiver may receive too late

 Leads to data loss or blocking

Solution 1: Synchronous Communication (Blocking)

Here, processes wait.

 Sender waits until receiver gets message

 Receiver waits until message arrives

Solution 2: Asynchronous Communication (Non-blocking)

Here, processes do not wait.

 Sender sends and continues its execution

 Receiver reads later when available

3. Buffering (Where are messages stored?)

Buffering decides how many messages can be stored


 Sender and receiver may run at different speeds

 OS must decide:

o store message?

o how many messages?

Solution 1: Zero Capacity Buffer

 No storage

 Message is delivered only if receiver is ready

 Sender must wait

Solution 2: Finite Capacity Buffer

 Limited storage

 Few messages can be stored

 Sender waits only if buffer full

Solution 3: Infinite Capacity Buffer

 Unlimited storage

 Sender never waits

THREADS – User Threads and Kernel Threads

User Threads

 A user thread is a task created by an application


 It exists in user space
 It represents what the program wants to do
 User threads are managed by a user-level thread library
 Kernel is not aware of these threads
 Thread management (creation, scheduling, deletion) happens in
user space
Examples:

 Typing in Word
 Spell check in Word

Advantages:

 Fast creation and switching

 No kernel involvement

Disadvantages:

 If one thread makes a blocking system call, entire process blocks

 Kernel schedules the process, not individual threads

 Limited CPU utilization on multiprocessor systems

Kernel Threads

 Kernel threads are created and managed directly by the


operating system

 Kernel is fully aware of all threads

 Each thread is scheduled by the kernel

 Exists in kernel space

Examples:

 Word file process

Advantages:

 True parallel execution

 If one thread blocks, others can continue

 Better CPU utilization

Disadvantages:

 Slower to create and manage

 Higher overhead due to kernel involvement


How User Threads and Kernel Threads Are Linked

Example: Word Document Application

You are typing in a Word document.


At the same time, spell check is running in the background.

What is the User Thread here?

 Spell check is a user thread

 It is created by the Word application

 It exists in user space

 It contains logic like:

o Scan words

o Compare with dictionary

o Highlight errors

User thread cannot run directly on the CPU

Who runs this thread?

The CPU runs only kernel threads

So, the spell-check user thread must be linked to a kernel thread

How the link works

1. Word creates a user thread for spell check

2. Operating system provides a kernel thread

3. User thread is mapped to the kernel thread

4. Kernel thread is scheduled on the CPU

5. CPU executes the kernel thread

6. Kernel thread executes spell-check code on behalf of the user thread

If spell check waits (blocking case)


 Spell check waits for dictionary file (I/O)

 Kernel thread gets blocked

 OS schedules another kernel thread

 Word application remains responsive

Multithreading Models

Multithreading models describe how user threads are mapped to


kernel threads.

1. Many-to-One Model

Description

 Many user threads mapped to one kernel thread

Advantages

 Fast thread creation

 Managed in user space

Disadvantages

 If one thread blocks, entire process blocks

 Cannot use multiple CPUs

2. One-to-One Model

Description

 Each user thread mapped to one kernel thread


Advantages

 True parallel execution

 Better concurrency

Disadvantages

 High overhead

 Limited number of threads

3. Many-to-Many Model

Description

 Many user threads mapped to many kernels’ threads

Advantages

 No full blocking

 Flexible and efficient


Issues in Multithreading – Thread Cancellation

What is Thread Cancellation?

Thread cancellation is the process of terminating a thread before it


finishes execution.

Why Thread Cancellation is Needed

 Task no longer required

 User stops an operation


 Result already found

Example:
Stopping webpage loading when user clicks “Stop”.

Types of Thread Cancellation

1. Asynchronous Cancellation

 One thread immediately terminates another thread

 Cancelled thread has no chance to clean up

Problems

 Resources may not be released

 Shared data may become inconsistent

2. Deferred Cancellation

 Thread checks periodically whether it should terminate

 Terminates itself safely

Advantages

 Resources are released properly

 Data remains consistent

You might also like