1. Process Concept
Definition
A Process is a program in execution. When a program is loaded into memory and starts running, it becomes a process.
A process is an active entity that includes:
- Program code
- Program Counter (PC)
- CPU Registers
- Stack
- Heap
- Data Section
Definition: A process is an instance of a program that is currently being executed by the CPU.
Example
- Program:
MS Word.exestored on the hard disk. - Process: When you open Microsoft Word, it is loaded into memory and starts executing, becoming a process.
Similarly:
- Chrome browser running
- Calculator application running
- Media player playing music
All are examples of processes.
Components of a Process
Every process consists of the following components:
- Program Code (Text Section)
- Contains executable instructions.
- Data Section
- Stores global and static variables.
- Stack
- Stores local variables, function calls, and return addresses.
- Heap
- Stores dynamically allocated memory.
- Program Counter (PC)
- Keeps track of the next instruction to execute.
- CPU Registers
- Store temporary data during execution.
Process Memory Layout
+----------------------+
| Stack |
| Local Variables |
+----------------------+
| Heap |
| Dynamic Memory |
+----------------------+
| Data Section |
| Global Variables |
+----------------------+
| Code (Text) |
| Program Instructions |
+----------------------+
Characteristics of a Process
- Dynamic in nature
- Has a unique Process ID (PID)
- Occupies memory during execution
- Uses CPU resources
- Can communicate with other processes
- Can create child processes
Process States
During execution, a process passes through different states.
Five-State Process Model
1. New State
- The process is being created.
- Memory and resources are allocated.
Example: You click on an application icon.
2. Ready State
- The process is ready to execute.
- It is waiting for CPU allocation.
Example: Multiple applications waiting for CPU time.
3. Running State
- The CPU is executing the process instructions.
Example: Typing in Microsoft Word.
4. Waiting (Blocked) State
- The process waits for an event such as:
- Keyboard input
- File reading
- Printer response
- Network communication
Example: Waiting for a file to load.
5. Terminated (Exit) State
- The process has completed execution or has been terminated.
Example: Closing an application.
Process State Diagram
+------+
| New |
+------+
|
v
+---------+
| Ready |
+---------+
|
CPU Allocated
|
v
+---------+
| Running |
+---------+
/ \
I/O Request / \ Process Finished
v v
+---------+ +-----------+
| Waiting | |Terminated |
+---------+ +-----------+
|
I/O Completed
|
v
+---------+
| Ready |
+---------+
State Transitions
| From State | To State | Reason |
|---|---|---|
| New | Ready | Process created |
| Ready | Running | CPU allocated |
| Running | Waiting | I/O request or event wait |
| Waiting | Ready | Event completed |
| Running | Terminated | Process finishes execution |
Process Control Block (PCB)
Definition
The Process Control Block (PCB) is a data structure maintained by the operating system that stores all information about a process.
Information Stored in PCB
- Process ID (PID)
- Process State
- Program Counter
- CPU Registers
- CPU Scheduling Information
- Memory Management Information
- I/O Status Information
- Accounting Information
PCB Diagram
+----------------------------+
| Process Control Block (PCB)|
+----------------------------+
| Process ID |
| Process State |
| Program Counter |
| CPU Registers |
| Memory Information |
| Scheduling Information |
| I/O Status |
| Accounting Information |
+----------------------------+
Advantages of Process Management
- Efficient CPU utilization
- Supports multitasking
- Better resource allocation
- Improved system performance
- Enables concurrent execution of programs
Applications
- Web browsers
- Operating system services
- Database systems
- Office applications
- Multimedia software
- Mobile applications
Process Control Block (PCB)
Definition
A Process Control Block (PCB) is a data structure maintained by the Operating System (OS) that stores all the information related to a process. It is also known as the Task Control Block (TCB) in some operating systems.
Whenever a process is created, the operating system creates a PCB for it. The PCB helps the OS manage, schedule, and execute processes efficiently.
Definition: A Process Control Block (PCB) is a data structure used by the operating system to store information about a process, including its state, CPU registers, memory allocation, scheduling information, and I/O status.
Purpose of PCB
The PCB is used to:
- Identify each process uniquely.
- Keep track of the process state.
- Store CPU register values during context switching.
- Manage memory allocated to the process.
- Maintain scheduling information.
- Store input/output (I/O) status.
- Monitor process execution.
Components of Process Control Block
A PCB typically contains the following information:
1. Process ID (PID)
- A unique number assigned to each process.
- Used to identify the process.
Example: PID = 1050
2. Process State
Stores the current state of the process.
Possible states:
- New
- Ready
- Running
- Waiting (Blocked)
- Terminated
3. Program Counter (PC)
- Stores the address of the next instruction to be executed.
- Helps the CPU continue execution from the correct location.
4. CPU Registers
Stores the contents of CPU registers such as:
- General-purpose registers
- Stack Pointer (SP)
- Program Counter (PC)
- Status Register
These values are saved during context switching.
5. CPU Scheduling Information
Contains information required by the CPU scheduler, such as:
- Process priority
- Scheduling queue pointers
- CPU burst time
6. Memory Management Information
Stores details about the memory allocated to the process, including:
- Base address
- Limit registers
- Page tables
- Segment tables
7. Accounting Information
Maintains statistics about the process, such as:
- CPU time used
- Process creation time
- User ID
- Job number
8. I/O Status Information
Contains information related to input/output operations, including:
- Open files
- Allocated devices
- Pending I/O requests
PCB Structure Diagram
+----------------------------------+
| Process Control Block |
+----------------------------------+
| Process ID (PID) |
| Process State |
| Program Counter |
| CPU Registers |
| CPU Scheduling Information |
| Memory Management Information |
| Accounting Information |
| I/O Status Information |
+----------------------------------+
Role of PCB in Context Switching
Context Switching is the process of switching the CPU from one process to another.
Steps:
- Save the current process information in its PCB.
- Select the next process to execute.
- Load the new process information from its PCB.
- Resume execution of the new process.
Context Switching Diagram
+-----------+ Save Context +-----------+
| Process A | -----------------------> | PCB A |
+-----------+ +-----------+
CPU switches to another process
+-----------+ Load Context +-----------+
| Process B | <----------------------- | PCB B |
+-----------+ +-----------+
Advantages of PCB
- Efficient process management.
- Supports multitasking.
- Enables context switching.
- Stores complete process information.
- Helps in CPU scheduling.
- Improves resource utilization.
Applications
- CPU scheduling
- Process management
- Multitasking systems
- Memory management
- Operating system kernels
Difference Between Process and PCB
| Process | Process Control Block (PCB) |
|---|---|
| A program in execution | A data structure storing process information |
| Uses CPU and memory | Maintained by the operating system |
| Performs tasks | Helps the OS manage the process |
| Dynamic entity | Contains process-related information |
Process Scheduling, Schedulers, Context Switching, and Operations on Processes
1. Process Scheduling
Definition
Process Scheduling is the process by which the operating system selects one process from the Ready Queue and allocates the CPU to it for execution.
The main objective of process scheduling is to maximize CPU utilization and ensure efficient execution of multiple processes.
Definition: Process Scheduling is the activity of selecting a process from the ready queue and assigning the CPU to it.
Objectives of Process Scheduling
- Maximize CPU utilization
- Improve system throughput
- Reduce waiting time
- Reduce turnaround time
- Reduce response time
- Ensure fairness among processes
Process Scheduling Diagram
Ready Queue
+-------------------+
| P1 P2 P3 P4 |
+-------------------+
|
| CPU Scheduler
โผ
+-----------+
| CPU |
+-----------+
|
โผ
Running Process
2. Scheduling Queues
Processes move through different queues during execution.
a) Job Queue
- Contains all processes in the system.
- Every newly created process enters the job queue.
b) Ready Queue
- Contains processes that are ready to execute.
- Waiting for CPU allocation.
c) Device Queue (Waiting Queue)
- Contains processes waiting for an I/O device.
- After I/O completion, they return to the Ready Queue.
Queue Diagram
New Process
|
โผ
+-----------+
| Job Queue |
+-----------+
|
โผ
+-----------+
|Ready Queue|
+-----------+
|
โผ
+-----------+
| CPU |
+-----------+
|
โผ
+------------+
| Device Queue|
+------------+
|
โผ
Ready Queue
3. Schedulers
Definition
A Scheduler is an operating system component that selects processes from different queues and determines which process should execute next.
There are three types of schedulers.
A. Long-Term Scheduler (Job Scheduler)
Definition
The Long-Term Scheduler selects processes from the Job Queue and loads them into the Ready Queue.
Functions
- Controls the degree of multiprogramming.
- Decides which jobs enter the system.
- Invoked less frequently.
Diagram
Job Queue
|
Long-Term Scheduler
|
Ready Queue
B. Short-Term Scheduler (CPU Scheduler)
Definition
The Short-Term Scheduler selects a process from the Ready Queue and allocates the CPU.
Functions
- Executes very frequently.
- Responsible for CPU allocation.
- Must be fast.
Diagram
Ready Queue
|
CPU Scheduler
|
CPU
C. Medium-Term Scheduler
Definition
The Medium-Term Scheduler temporarily removes processes from memory (swapping) and later reloads them.
Functions
- Reduces memory usage.
- Controls the degree of multiprogramming.
- Improves system performance.
Diagram
Memory
|
Medium-Term Scheduler
|
Disk (Swap Area)
Comparison of Schedulers
| Scheduler | Function | Frequency |
|---|---|---|
| Long-Term | Moves jobs to Ready Queue | Low |
| Short-Term | Selects process for CPU | Very High |
| Medium-Term | Swaps processes in/out of memory | Medium |
4. Context Switching
Definition
Context Switching is the process of saving the state of the currently running process and loading the state of another process so that CPU execution can switch from one process to another.
The process state is stored in the Process Control Block (PCB).
Steps in Context Switching
- Save the current process state in its PCB.
- Select the next process.
- Load the new process state from its PCB.
- Resume execution.
Context Switching Diagram
Process A Running
|
Save Context in PCB
|
CPU Switches
|
Load Context from PCB
|
Process B Running
Advantages
- Supports multitasking.
- Improves CPU utilization.
- Enables concurrent execution.
Disadvantages
- Takes CPU time.
- Frequent switching reduces performance.
5. Operations on Processes
The operating system performs several operations during the life cycle of a process.
A. Process Creation
Definition
A new process is created when a user starts a program or when another process creates it.
Steps
- Assign Process ID (PID)
- Create PCB
- Allocate memory
- Place process in Ready Queue
Example
Opening a web browser creates a new process.
B. Process Execution
The scheduler allocates the CPU, and the process begins executing instructions.
C. Process Blocking (Waiting)
A running process may wait for:
- Keyboard input
- Disk access
- Printer
- Network communication
During this time, it moves to the Waiting state.
D. Process Wake-Up
When the required event or I/O operation completes, the process moves back to the Ready state.
E. Process Termination
A process terminates when:
- It completes execution.
- It encounters a fatal error.
- It is terminated by the operating system or user.
Process Life Cycle
+------+
| New |
+------+
|
โผ
+-----------+
| Ready |
+-----------+
|
โผ
+-----------+
| Running |
+-----------+
/ \
I/O Wait Finished
โผ โผ
+-----------+ +-----------+
| Waiting | |Terminated |
+-----------+ +-----------+
|
โผ
Ready
Advantages of Process Scheduling
- Efficient CPU utilization.
- Supports multitasking.
- Better resource allocation.
- Reduces response time.
- Improves overall system performance.
Threads in Operating System (OS)
1. Introduction
A Thread is the smallest unit of CPU execution within a process. A process can contain one or more threads that execute concurrently while sharing the same resources, such as memory and files.
A thread is also called a lightweight process (LWP) because it requires fewer resources than a full process.
2. Definition
Thread: A thread is the smallest sequence of instructions that can be scheduled and executed independently by the CPU. Multiple threads within the same process share the process’s code, data, and resources.
3. Process vs Thread
| Process | Thread |
|---|---|
| Program in execution | Smallest unit of execution within a process |
| Has its own memory space | Shares memory with other threads of the same process |
| More resource consumption | Less resource consumption |
| Creation is slower | Creation is faster |
| Context switching is slower | Context switching is faster |
| Communication is slower | Communication is faster through shared memory |
4. Components of a Thread
Each thread has its own:
- Thread ID (TID)
- Program Counter (PC)
- Register Set
- Stack
Threads of the same process share:
- Code (Text Section)
- Data Section
- Heap Memory
- Open Files
5. Thread Structure
Process
+--------------------------------------+
| Code (Shared) |
| Data (Shared) |
| Heap (Shared) |
| Open Files (Shared) |
| |
| +-----------+ +-----------+ |
| | Thread 1 | | Thread 2 | |
| | PC | | PC | |
| | Registers | | Registers | |
| | Stack | | Stack | |
| +-----------+ +-----------+ |
+--------------------------------------+
6. Types of Threads
A. User-Level Threads (ULT)
Definition
User-level threads are managed by a thread library in user space. The operating system kernel is not aware of these threads.
Advantages
- Fast thread creation.
- Fast context switching.
- Easy to implement.
Disadvantages
- If one thread blocks, the entire process may block.
- Cannot fully utilize multiple CPUs in many implementations.
Examples
- POSIX Threads (Pthreads) library
- Java Threads (managed by JVM)
B. Kernel-Level Threads (KLT)
Definition
Kernel-level threads are managed directly by the operating system kernel.
Advantages
- Better scheduling by the OS.
- One blocked thread does not block the entire process.
- Supports true parallel execution on multicore processors.
Disadvantages
- Thread creation is slower than user-level threads.
- Context switching requires kernel involvement.
Examples
- Windows Threads
- Linux Kernel Threads
7. Multithreading
Definition
Multithreading is the ability of a process to execute multiple threads simultaneously.
Example
A web browser performs multiple tasks at the same time:
- Downloading files
- Playing videos
- Loading web pages
- Checking spellings
Each task runs in a separate thread.
Multithreading Diagram
Process
|
---------------------
| | |
โผ โผ โผ
Thread1 Thread2 Thread3
| | |
---------------------
|
CPU
8. Benefits of Multithreading
- Responsiveness
- Applications remain responsive while performing background tasks.
- Resource Sharing
- Threads share memory and resources efficiently.
- Economy
- Creating threads is less expensive than creating processes.
- Scalability
- Improves performance on multicore processors.
- Improved CPU Utilization
- Multiple threads can execute simultaneously.
9. Advantages of Threads
- Faster execution.
- Better CPU utilization.
- Lower memory usage.
- Easy communication through shared memory.
- Improved application responsiveness.
- Supports parallel processing.
10. Disadvantages of Threads
- Synchronization becomes complex.
- Shared data may cause race conditions.
- Debugging multithreaded programs is difficult.
- One faulty thread can affect the entire process.
11. Applications of Threads
- Web browsers
- Web servers
- Database management systems
- Video games
- Multimedia applications
- Operating systems
- Office applications
12. Thread Life Cycle
A thread moves through the following states:
- New โ Thread is created.
- Ready โ Waiting for CPU allocation.
- Running โ Executing on the CPU.
- Blocked (Waiting) โ Waiting for an event or I/O operation.
- Terminated โ Execution completed.
Thread State Diagram
+------+
| New |
+------+
|
โผ
+---------+
| Ready |
+---------+
|
โผ
+---------+
| Running |
+---------+
/ \
โผ โผ
+---------+ +------------+
|Blocked | |Terminated |
+---------+ +------------+
|
โผ
Ready
Difference Between Multitasking and Multithreading
| Multitasking | Multithreading |
|---|---|
| Multiple processes execute simultaneously | Multiple threads execute within the same process |
| More memory required | Less memory required |
| Communication is slower | Communication is faster |
| Context switching is slower | Context switching is faster |
Threads in Operating System (OS)
1. Introduction
A Thread is the smallest unit of CPU execution within a process. A process can contain one or more threads that execute concurrently while sharing the same resources, such as memory and files.
A thread is also called a lightweight process (LWP) because it requires fewer resources than a full process.
2. Definition
Thread: A thread is the smallest sequence of instructions that can be scheduled and executed independently by the CPU. Multiple threads within the same process share the process’s code, data, and resources.
3. Process vs Thread
| Process | Thread |
|---|---|
| Program in execution | Smallest unit of execution within a process |
| Has its own memory space | Shares memory with other threads of the same process |
| More resource consumption | Less resource consumption |
| Creation is slower | Creation is faster |
| Context switching is slower | Context switching is faster |
| Communication is slower | Communication is faster through shared memory |
4. Components of a Thread
Each thread has its own:
- Thread ID (TID)
- Program Counter (PC)
- Register Set
- Stack
Threads of the same process share:
- Code (Text Section)
- Data Section
- Heap Memory
- Open Files
5. Thread Structure
Process
+--------------------------------------+
| Code (Shared) |
| Data (Shared) |
| Heap (Shared) |
| Open Files (Shared) |
| |
| +-----------+ +-----------+ |
| | Thread 1 | | Thread 2 | |
| | PC | | PC | |
| | Registers | | Registers | |
| | Stack | | Stack | |
| +-----------+ +-----------+ |
+--------------------------------------+
6. Types of Threads
A. User-Level Threads (ULT)
Definition
User-level threads are managed by a thread library in user space. The operating system kernel is not aware of these threads.
Advantages
- Fast thread creation.
- Fast context switching.
- Easy to implement.
Disadvantages
- If one thread blocks, the entire process may block.
- Cannot fully utilize multiple CPUs in many implementations.
Examples
- POSIX Threads (Pthreads) library
- Java Threads (managed by JVM)
B. Kernel-Level Threads (KLT)
Definition
Kernel-level threads are managed directly by the operating system kernel.
Advantages
- Better scheduling by the OS.
- One blocked thread does not block the entire process.
- Supports true parallel execution on multicore processors.
Disadvantages
- Thread creation is slower than user-level threads.
- Context switching requires kernel involvement.
Examples
- Windows Threads
- Linux Kernel Threads
7. Multithreading
Definition
Multithreading is the ability of a process to execute multiple threads simultaneously.
Example
A web browser performs multiple tasks at the same time:
- Downloading files
- Playing videos
- Loading web pages
- Checking spellings
Each task runs in a separate thread.
Multithreading Diagram
Process
|
---------------------
| | |
โผ โผ โผ
Thread1 Thread2 Thread3
| | |
---------------------
|
CPU
8. Benefits of Multithreading
- Responsiveness
- Applications remain responsive while performing background tasks.
- Resource Sharing
- Threads share memory and resources efficiently.
- Economy
- Creating threads is less expensive than creating processes.
- Scalability
- Improves performance on multicore processors.
- Improved CPU Utilization
- Multiple threads can execute simultaneously.
9. Advantages of Threads
- Faster execution.
- Better CPU utilization.
- Lower memory usage.
- Easy communication through shared memory.
- Improved application responsiveness.
- Supports parallel processing.
10. Disadvantages of Threads
- Synchronization becomes complex.
- Shared data may cause race conditions.
- Debugging multithreaded programs is difficult.
- One faulty thread can affect the entire process.
11. Applications of Threads
- Web browsers
- Web servers
- Database management systems
- Video games
- Multimedia applications
- Operating systems
- Office applications
12. Thread Life Cycle
A thread moves through the following states:
- New โ Thread is created.
- Ready โ Waiting for CPU allocation.
- Running โ Executing on the CPU.
- Blocked (Waiting) โ Waiting for an event or I/O operation.
- Terminated โ Execution completed.
Thread State Diagram
+------+
| New |
+------+
|
โผ
+---------+
| Ready |
+---------+
|
โผ
+---------+
| Running |
+---------+
/ \
โผ โผ
+---------+ +------------+
|Blocked | |Terminated |
+---------+ +------------+
|
โผ
Ready
Difference Between Multitasking and Multithreading
| Multitasking | Multithreading |
|---|---|
| Multiple processes execute simultaneously | Multiple threads execute within the same process |
| More memory required | Less memory required |
| Communication is slower | Communication is faster |
| Context switching is slower | Context switching is faster |
