UNIT 2 – PROCESS AND THREAD
2.1. The process model, process state, process control block, context switch
🔹 The Process Model
• In an operating system, a process is defined as a program in execution.
• A process is more than just the program code. It includes:
• The text section, which contains the actual code of the program.
• The program counter, which tells the current position of execution.
• The CPU registers, which hold temporary values needed during execution.
• A stack, which stores function calls, parameters, return addresses, and local
variables.
• A data section, which holds global variables and dynamically allocated
memory.
• In a batch operating system, the word “job” is used for a process.
• In a time-sharing system, the word “task” or “user program” is commonly used.
• Each process needs system resources like CPU time, memory, files, and I/O devices
🔹 Process States
As a process runs, it goes through different stages called process states. These are:
1. New:
The process is just being created and is not yet ready to run.
2. Ready:
The process is prepared to run and is waiting to be assigned to the CPU.
3. Running:
The process is currently being executed by the CPU.
4. Waiting:
The process is waiting for some event to occur, such as input/output completion.
5. Terminated:
The process has finished its execution and is being removed from the system.
📝 Note: At any time, only one process can be in the Running state on a single-core
CPU.
🔹 Process Control Block (PCB)
• Every process is represented inside the operating system using a special data
structure called a Process Control Block (PCB).
• The PCB contains all important information needed to manage the process.
Here are the main components of a PCB:
1. Process State:
It shows the current status of the process (new, ready, running, etc.).
2. Program Counter:
This tells the address of the next instruction the process will execute.
3. CPU Registers:
These are the values stored in CPU registers which the process was using.
4. CPU Scheduling Information:
This includes the process’s priority, scheduling queue pointers, etc.
5. Memory Management Information:
This includes details like base and limit registers, page tables, etc.
6. Accounting Information:
This includes the amount of CPU time used, process ID, job ID, etc.
7. I/O Status Information:
This shows which files and I/O devices the process is using or waiting for.
📝 The PCB helps the OS keep track of all the details of a process
🔹 Context Switch
• A context switch is the process of saving the current state of a running process so that
another process can be run on the CPU.
• This happens when the CPU moves from one process to another.
Steps in a Context Switch:
1. The OS saves the state of the current process into its PCB.
2. Then it loads the state of the next process from its PCB.
3. The CPU starts executing the new process from where it left off.
🔹 Context switches introduce some overhead because CPU time is used for saving and
loading process states, not for actual work.
2.2 – Process Scheduling: Scheduling Queues and Schedulers
📝 What is Process Scheduling?
• Process scheduling is the job of the operating system that decides which process should use
the CPU next.
• When a process finishes or moves to waiting state, the OS chooses another process from the
ready queue to run on the CPU.
• This choice is made based on some rules or scheduling algorithms.
• During its life, a process moves between different queues, like:
• Ready queue (waiting for CPU),
• Waiting queue (waiting for I/O),
• Device queue (waiting for a specific device).
📝 What are Scheduling Queues?
• In an operating system, processes are placed in different queues depending on their current
state.
Here are the main types of scheduling queues:
1. Job Queue
• This is the main list of all processes that have been submitted to the system.
• It contains both running and waiting processes.
2. Ready Queue
• This queue stores all the processes that are ready to run but are waiting for the CPU.
• The short-term scheduler picks processes from this queue.
3. Waiting (or I/O) Queue
• When a process needs to do some I/O (like reading a file or printing), it is moved to
this queue.
• The process stays here until the I/O is complete.
4. Device Queue
• Every input/output device (like a printer or disk) has its own device queue.
• Processes wait here for that particular device to become available.
📝 How processes move between queues:
• A new process starts in the ready queue.
• If it needs to wait for input/output, it moves to the I/O queue.
• After the I/O is done, it comes back to the ready queue.
• When selected by the short-term scheduler, it goes to the CPU.
• After completion, it is removed from all queues.
✨ Why are scheduling queues important?
• They help the operating system manage many processes at once.
• They ensure the CPU and I/O devices are used efficiently.
• They make the process scheduling system organized and fair.
📝 Types of Process Schedulers
The operating system uses three types of schedulers to manage processes:
1. Long-Term Scheduler (Job Scheduler)
• The long-term scheduler picks some processes from the job pool (stored in secondary
memory like hard disk) and moves them to the ready queue in main memory (RAM).
• Its main goal is to control the number of processes in memory. This is called
multiprogramming.
• It selects a good mix of:
• I/O-bound processes (spend more time on input/output),
• and CPU-bound processes (spend more time on CPU).
• If it picks too many I/O-bound processes, the CPU may stay idle.
• If it picks too many CPU-bound processes, I/O devices may stay idle.
• So, the job of this scheduler is very important for system performance.
2. Short-Term Scheduler (CPU Scheduler)
• The short-term scheduler chooses one process from the ready queue and gives it to the CPU.
• This scheduler works very often, because it must pick a new process whenever:
• A process finishes,
• Or goes into waiting state,
• Or a higher priority process arrives (in preemptive systems).
• If it picks a process with a very long CPU time, other processes may wait too long, causing
starvation.
• So, it must choose carefully to give all processes a fair chance.
3. Medium-Term Scheduler
• The medium-term scheduler helps in managing memory usage.
• When the system is full, it removes some processes from memory (this is called swapping).
• These removed processes are stored on disk until there is enough space to bring them back.
• When the time comes, it restores the process into memory, and it continues from where it
stopped.
• This helps the system free up memory and allows more active processes to run smoothly
Comparison of All Schedulers
Feature Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler
Other Name Job Scheduler CPU Scheduler Swapper
Picks ready jobs for Swaps jobs in/out of
Job Type Picks jobs from disk
CPU memory
How Often It
Very rarely Very frequently Sometimes
Works
How many processes in
Controls Who gets CPU next Free memory when needed
RAM
Role in System Increases efficiency Improves CPU use Manages memory usage
📝 Types of Scheduling
1. Non-Preemptive Scheduling
• In this type, once a process gets the CPU, it keeps running until it finishes or waits for I/O.
• The CPU is not taken away from the process.
2. Preemptive Scheduling
• In this type, the operating system can take the CPU away from a running process.
• This is done if another process has higher priority or needs urgent CPU time.
📝 Other Schedulers in Operating System
✅ I/O Scheduler
• These schedulers manage input and output devices.
• They decide which process will use I/O next.
• Common algorithms used are:
• FCFS (First Come First Serve)
• Round Robin
✅ Real-Time Scheduler
• Used in real-time systems where tasks must be completed within a deadline.
• They make sure important tasks are done on time.
• Algorithms used:
• RM (Rate Monotonic) – higher frequency = higher priority.
• EDF (Earliest Deadline First) – task with the nearest deadline gets priority
Point of Difference Preemptive Scheduling Non-Preemptive Scheduling
The operating system can take the
The process keeps the CPU until it
CPU Control CPU away from a running process
finishes or moves to the waiting state.
at any time.
A running process can be interrupted A running process is not interrupted by
Interruptions
if a higher priority process arrives. any other process.
High-priority processes have to wait if a
Process Priority High-priority processes are quickly
lower-priority process is already
Handling given the CPU.
running.
It gives better response time, Response time may be poor if a long
Response Time
especially in time-sharing systems. process is already running.
Implementation It is more complex to implement. It is simple and easy to implement.
It increases CPU efficiency in multi- It may cause delays for important tasks
Efficiency
tasking systems. and affect efficiency.
Used in modern operating systems Used in simpler systems or basic
Example
like Windows, Linux. embedded systems.
2.3 Inter process communication- Introduction, shared memory system& message passing
system, critical section problem, semaphore
📝 Introduction to IPC:
• In an operating system, different processes may need to communicate with each other.
• This communication between processes is called Interprocess Communication (IPC).
• IPC is mainly needed when processes are cooperating (i.e., they affect or are affected by
other processes).
• Cooperating processes often need to share information, speed up computations, improve
modularity, or improve convenience.
📝 Why Do Processes Need to Cooperate?
1. Information Sharing – Multiple processes may need to access the same data.
2. Computation Speed-up – Tasks can be divided among processes to run in parallel and
finish faster.
3. Modularity – Complex systems can be built by dividing them into simpler processes.
4. Convenience – It is easier to solve problems using multiple cooperating processes.
📝 Two Main Models of IPC:
There are two basic ways in which processes can communicate:
1. Shared Memory System:
• A section of memory is shared between the processes that want to communicate.
• All processes can read and write in this shared memory.
• The operating system only sets up the shared memory but does not manage how data is
shared.
• It is the responsibility of the processes to make sure they do not access memory at the same
time, which can lead to errors.
• To prevent this, synchronization techniques are used, such as semaphores (discussed in later
chapters).
• This model is fast because data doesn’t need to be copied from one process to another.
2. Message Passing System:
• In this model, processes send and receive messages to communicate with each other.
• No shared memory is used.
• It is useful when processes are on different machines or in distributed systems.
• IPC provides two operations:
• send(message) – A process sends a message to another.
• receive(message) – A process receives a message.
• Messages can be of fixed or variable size.
• Message passing is easier to implement, but can be slower than shared memory.
•
✅Critical Section Problem
🔹 What is a Critical Section?
• A critical section is a part of a program where a process accesses shared data or resources
(like files, memory, or printers).
• If more than one process enters their critical sections at the same time, it can lead to data
inconsistency or errors.
🔹 Critical section Problem:
• The goal is to make sure that only one process uses the shared resource at a time.
• This is called the critical section problem.
🔹 Conditions
to Solve the
Problem:
A good solution must satisfy:
Condition Meaning
Mutual
Only one process is allowed in the critical section at a time.
Exclusion
If no process is in the critical section, one of the waiting processes should be
Progress
allowed to enter.
Bounded
Each process should get a fair chance without waiting forever.
Waiting
📝 Example:
Two processes trying to update the same bank balance together → wrong total.
Solution: Use proper control so only one process updates at a time.
✅ Semaphore (Simple & Exam-Ready)
🔹 What is a Semaphore?
• A semaphore is a special variable used to control access to shared resources.
• It helps in solving the critical section problem and ensures synchronization between
processes.
🔹 Types:
Type Description
Binary Semaphore Has only two values: 0 and 1 (like a lock).
Counting Semaphore Can have values 0, 1, 2, etc. (used when multiple resources are available).
🔹 Semaphore Operations:
• wait(S): If S > 0, decrease by 1 and enter. If S == 0, the process waits.
• signal(S): Increases the value of S. Allows waiting processes to enter.
📝 Example:
If a printer is shared by two processes:
• Set S = 1.
• A process must wait(S) before printing.
• After printing, it will signal(S) to allow the next process.
2.4 – Threads and Multithreading Models
🔹What is a Thread?
• A thread is the smallest unit of execution in a process.
• A process can have multiple threads running at the same time.
• All threads of a process share the same memory space but have separate registers, stack,
and program counters.
• Threads allow different parts of a program to run independently.
• They help increase performance, especially on multi-core systems.
🔹Benefits of Using Threads
1. Thread creation and context switching is faster than processes.
2. Threads within the same process share memory, which makes communication easier.
3. Threads improve the responsiveness of applications. For example, one thread can handle
user input while another performs calculations.
4. They help in achieving parallel execution when running on multi-core processors.
5. Threads save memory and resources compared to multiple processes.
6. They allow modular program design by dividing tasks into separate threads.
🔹Types of Threads
1. User-Level Threads
• These threads are managed by the user program, not the operating system.
• The OS sees only the main process, not the individual threads.
• They are faster to create and manage.
• A major drawback is that if one thread is blocked (for example, due to I/O), all threads are
blocked.
• They are portable across different operating systems.
2. Kernel-Level Threads
• These threads are managed by the operating system.
• Each thread is known to the OS and scheduled
individually.
• If one thread is blocked, others can still run.
• Thread creation and management are slower due to OS
involvement.
• They are better for systems that need real multitasking.
🔹Multithreading Models
These models define how user-level threads are mapped to kernel-level threads.
1. Many-to-One Model
• Many user threads are mapped to a single kernel thread.
• Only one thread can execute at a time, even on multi-core systems.
• If one thread is blocked, all others are also blocked.
• This model is simple and uses less system resources.
• It does not support true parallelism.
• Example: Some early implementations of Java threads.
•
2. One-to-One Model
• Each user thread is mapped to one kernel thread.
• Multiple threads can run in parallel on multi-core systems.
• If one thread is blocked, others can continue running.
• This model provides true concurrency.
• It uses more system resources and is slower to manage.
• Example: Windows, Linux (using pthreads).
3. Many-to-Many Model
• Many user threads are mapped to many kernel threads.
• The OS manages which user thread runs on which kernel thread.
• This model allows better use of CPU and true concurrency.
• It avoids the disadvantages of the other two models.
• It is more complex to implement.
• Example: Solaris, and some advanced operating systems.
Summary Table
Feature Many-to-One One-to-One Many-to-Many
Mapping Many user to one kernel One user to one kernel Many user to many kernel
Parallel Execution Not supported Supported Supported
Blocking Effect All threads blocked Others can run Others can run
Resource Usage Low High Moderate
Used In Early Java implementations Windows, Linux Solaris, modern OS