CSE2008 – Operating Systems
Dr. Venkata Rami Reddy Ch
[Link] Professor
School of Computer Science & Engineering
VIT-AP University
Module-2: Process & Threads
• Process and programs, process states, process concept, process
scheduling, IPC, multithreading models.
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Process
Program
• It is a set of instructions written in a programming language
• Stored on disk.
• A program is a passive entity.
• It does not perform any action by itself.
Process
• A process is a program in execution.
• A process is not just the program code but the entire environment necessary for
execution.
• A process will need certain resources—such as CPU time, memory, files, and I/O
devices to accomplish its task.
• It includes: Program code, Current activity (value of Program Counter, CPU
registers),Process state and Memory (stack, heap, data, code)
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Process in Memory
• When a program becomes a process, it is loaded into main memory (RAM) by the operating
system.
• The memory layout of a process is divided into several segments, each with a specific
purpose.
Text Section:
•Contains the program's code (instructions).
Stack:
•Used to store temporary data:
•Function parameters
•Return addresses
•Local variables
Data Section:
•Stores global variables.
Heap:
•Dynamically allocated memory at runtime (e.g., via malloc() in C).
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Memory Layout of a C Program
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Process State
• A process moves through various states during its lifetime.
• These states help the operating system manage and schedule processes
effectively.
New: The process is being created
Running: Instructions are being executed
Waiting: The process is waiting for some I/O event (e.g., keyboard input, file
read).
Ready: The process is waiting to be assigned to a processor
Terminated: The process has finished execution
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Process Control Block
• The Process Control Block (PCB) is a data structure used by the operating system
to store all information about a process.
• Each process has its own PCB, which the OS uses to manage and schedule that
process.
• It is also called task control block.
Process State
•Describes the current status of the process.
•Common states: New,Ready ,Running ,Waiting ,Terminated
Program Counter
• Contains the address of the next instruction to be executed for
the process.
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Process Control Block
CPU Registers
• Includes all processor-specific registers that the process is using: Accumulators, Index
registers, Stack pointers, General-purpose registers
CPU-scheduling information
• This information includes a process priority, pointers to scheduling queues, and any
other scheduling parameters.
Memory Management Information
• May include: Base and limit registers, Page tables, Segment tables
Accounting information
• It includes the amount of CPU and real time used, time limits and so on.
I/O status information
• It includes the list of I/O devices allocated to the process, a list of open files, and so
on.
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
CPU/Process Scheduling
• CPU or process scheduling is the process by which the operating system selects the
next process to be executed on the CPU
Key components in Process Scheduling
• Scheduling Queues
• Job Queue
• Ready Queue
• Device (I/O) Queue
• Schedulers
• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler
• Context Switch
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Scheduling Queues
Job Queue
• Contains all processes that enter the system.
• As soon as a process is created, it is put into this queue.
Ready Queue
• The processes that are residing in main memory and are ready and waiting to
execute are kept in ready queue.
Device Queue
• The list of processes waiting for a particular I/O device is called a device queue.
• Each I/O device (like disk, printer, keyboard) has its own separate device queue.
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Queueing-diagram representation of process
scheduling
• It shows how processes move between different queues and components of
the CPU scheduling system.
A new process is initially put in the ready queue.
While process is executing on the CPU, one of
several events could occur:
• The process could issue an I/O request and then
be placed in an I/O queue.
• The process could create a new child process and
wait for the child’s termination.
• The process could be removed forcibly from the
CPU, as a result of an interrupt, and be put back
in the ready queue.
Schedulers
Long-Term Scheduler (Job Scheduler)
• Selects processes from the job pool (newly created processes) and loads them into
main memory (ready queue).
• Runs less frequently.
• Controls the degree of multiprogramming (number of processes in memory).
Short-Term Scheduler (CPU Scheduler)
• The short-term scheduler selects one of the processes that are in the ready queue
and assigns the CPU to that process for execution
• Runs very frequently
• Very fast
Medium-term scheduler
•A medium-term scheduler swaps out processes from main memory to secondary
storage to reduce memory load, and swaps them back in when sufficient memory
becomes available, allowing the process to resume execution.
Context Switch
Context Switch
• A context switch happens when the CPU
switches from one process to another.
Steps involved:
• Save the context of the currently running
process into its PCB.
• Load the context of the next scheduled
process from its PCB.
• When an interrupt occurs (e.g., due to I/O,
timer, or system call), the operating system
must pause the currently running process.
• To do this safely, it needs to save the current
state (context) of that process so it can resume
it later from the exact same point.
Scheduling Criteria
• CPU scheduling algorithms are evaluated based on several performance criteria.
• These determine how efficiently the CPU and system resources are used.
CPU utilization
• We want to keep the CPU as busy as possible.
• Conceptually, CPU utilization can range from 0 to 100 percent.
• In a real system, it should range from 40 percent (for a lightly loaded system) to 90
percent (for a heavily loaded system).
Throughput
• Number of processes that are completed per time unit.
• For long processes ,this rate may be one process per hour; for short ,it may be ten
processes per second
Turnaround Time
• Total time taken from process submission to completion.
• Turnaround time is the sum of the periods spent waiting to get into memory, waiting
in the ready queue, executing on the CPU, and doing I/O
Scheduling Criteria
Waiting time
• Waiting time is the sum of the periods spent waiting in the ready queue.
Response Time
• Time from process submission to the first response (not completion).
• Responsetime,isthetimeittakestostartresponding,notthetimeittakes to output the
response.
• It is desirable to maximize CPU utilization and throughput and to minimize
turnaround time, waiting time, and response time.
Types of CPU Scheduling
Preemptive Scheduling
• Preemptive Scheduling allows the operating system to interrupt a currently running
process and allocate the CPU to another process.
When Preemption Occurs:
• A higher-priority process arrives.
• The current process exceeds its time slice (e.g., in Round Robin).
• A process with shorter remaining time becomes ready (e.g., in SRTF).
Ex: Round Robin (RR), Shortest Remaining Time First (SRTF) ,Preemptive Priority
Scheduling
Non-Preemptive Scheduling
• Non-Preemptive Scheduling is a CPU scheduling method where once a process
starts executing, it runs to completion or until it voluntarily releases the CPU (e.g.,
by requesting I/O or terminating).
Ex: First-Come, First-Served (FCFS), Non-Preemptive Shortest Job First (SJF),
Non-Preemptive Priority Scheduling
CPU Scheduling Algorithms
Non-Preemptive Scheduling Algorithms:
• First-Come, First-Served (FCFS)
• Shortest Job First (SJF) – Non-Preemptive
• Priority Scheduling – Non-Preemptive
Preemptive Scheduling Algorithms:
• Shortest Remaining Time First (SRTF) (Preemptive version of SJF)
• Priority Scheduling – Preemptive
• Round Robin (RR)
• Multilevel Queue Scheduling
• Multilevel Feedback Queue Scheduling
Formulas
Turnaround Time (TAT):
TAT=Completed Time-Arrival Time(AT)
TAT=CT-AT
Waiting Time (WT):
WT=TAT-Burst Time
=TAT-BT
Response Time(RT):
Response Time=Start Time−Arrival Time
Average TAT=Total TAT/No of process
Average WT= Total WT/No of process
Average RT=Total RT/No of process
First-Come, First-Served (FCFS)
• First-Come, First-Served (FCFS) is the simplest CPU scheduling algorithm.
• In FCFS, the process that arrives first in the ready queue is executed first, and so on.
• The CPU is assigned to the process in the order of arrival time.
• Once a process starts executing, it runs to completion.
First-Come, First-Served (FCFS)
First-Come, First-Served (FCFS)
Gantt Chart :
P1 P2 P3
0 24 27 30
Suppose that the processes arrive in the order:P2 , P3 , P1
Gantt Chart :
P2 P3 P1
0 3 6 30
Problem-3
Consider the following set of processes, each with their arrival time and burst time:
Process Arrival Time (AT) Burst Time (BT)
P1 0 5
P2 1 3
P3 2 8
P4 3 6
•Construct the Gantt chart.
•Calculate the following for each process:
• Completion Time (CT)
• Turnaround Time (TAT)
• Waiting Time (WT)
• Response Time (RT)
•Compute the average turnaround time, average waiting time, and average
response time.
Gantt Chart : Problem-3
Process AT BT Start Time CT TAT = CT - AT WT = TAT - BT RT = Start - AT
P1 0 5 0 5 5 0 0
P2 1 3 5 8 7 4 4
P3 2 8 8 16 14 6 6
P4 3 6 16 22 19 13 13
convoy effect
• when a long (CPU-bound) process occupies the CPU, and all other shorter processes
must wait for it to complete.
• This effect results in lower CPU and device utilization.
• If shorter processes were allowed to execute first, overall performance,
responsiveness, and resource utilization would be significantly improved.
Shortest Job First (SJF)/SJN
• This algorithm associates with each process the length of the process’s next CPU
burst.
• Shortest Job First (SJF), also called Shortest Job Next (SJN), is a non-preemptive
scheduling algorithm in which the process with the shortest burst time (execution
time) is selected next for execution.
• When the CPU is available, it is assigned to the process that has the smallest next
CPU burst.
• If the next CPU bursts of two processes are the same, FCFS scheduling is used to
break the tie.
Shortest Job First (SJF)/SJN
Gantt Chart : Shortest Job First (SJF)/SJN
P4 P1 P3 P2
0 3 9 16 24
Shortest Job First (SJF)/SJN
Gantt Chart :
shortest-remaining-time-first
• The SJF algorithm can be either preemptive or non preemptive.
• The choice arises when a new process arrives at the ready queue while a previous
process is still executing.
• The next CPU burst of the newly arrived process may be shorter than remaining
time of currently executing process.
• A preemptive SJF algorithm will preempt the currently executing process, whereas a
non preemptive SJF algorithm will allow the currently running process to finish its
CPU burst. Preemptive SJF scheduling is sometimes called shortest-remaining-time-
first scheduling
shortest-remaining-time-first
Gantt Chart
Gantt Chart shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
shortest-remaining-time-first
Consider the following set of processes with their respective arrival times and burst times.
Using Shortest Remaining Time First (SRTF) scheduling, calculate:
•Completion Time (CT)
•Turnaround Time (TAT)
•Waiting Time (WT)
•Draw the Gantt chart.
Priority Scheduling Algorithm
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority (smallest
integer highest priority)
• Equal priority processes follow FCFS.
• Can be preemptive or non-preemptive.
Priority Scheduling Algorithm
Non-Preemptive Priority Scheduling
Gantt Chart
Preemptive Priority Scheduling
Rule (preemptive): at any time run the ready process with the smallest priority
number; new higher-priority arrivals preempt the running process.
Preemptive Priority Scheduling
Gantt Chart
Time Event
t=0 P1 arrives → CPU runs P1 (priority 2).
P2 arrives (priority 1 — higher than P1’s priority 2) → P1 is preempted → CPU
t=1 switches to P2.
t=2 P3 arrives (priority 3 — lower than P2’s priority 1) → No preemption, P2 continues.
P4 arrives (priority 2 — still lower than P2’s priority 1) → No preemption, P2
t=3 continues.
P2 finishes → CPU picks next highest priority process (P1, priority 2 and FCFS) and
t=5 runs it.
Preemptive Priority Scheduling
Gantt Chart
WT = TAT–
Process AT BT Priority ST CT TAT = CT–AT RT = ST–AT
Burst
P1 0 8 2 0 12 12 − 0 = 12 12 − 8 = 4 0−0=0
P2 1 4 1 1 5 5−1=4 4−4=0 1−1=0
P3 2 9 3 17 26 26 − 2 = 24 24 − 9 = 15 17 − 2 = 15
P4 3 5 2 12 17 17 − 3 = 14 14 − 5 = 9 12 − 3 = 9
•Average Turnaround Time (TAT) = (12 + 4 + 24 + 14) / 4 = 13.5
•Average Waiting Time (WT) = (4 + 0 + 15 + 9) / 4 = 7.0
•Average Response Time (RT) = (0 + 0 + 15 + 9) / 4 = 6.0
Preemptive Priority Scheduling
Preemptive Priority Scheduling
Gantt Chart
| P2 | P5 | P4 | P1 | P3 |
0 4 9 12 18 26
Process AT BT PR ST CT TAT = CT−AT WT = TAT−BT RT = ST− AT
P1 0 6 3 12 18 18 − 0 = 18 18 − 6 = 12 12 − 0 = 12
P2 0 4 1 0 4 4−0=4 4−4=0 0−0=0
P3 5 8 4 18 26 26 − 5 = 21 21 − 8 = 13 18 − 5 = 13
P4 5 3 2 9 12 12 − 5 = 7 7−3=4 9−5=4
P5 2 5 1 4 9 9−2=7 7−5=2 4−2=2
•Average Turnaround Time (TAT) = (18 + 4 + 21 + 7 + 7) / 5 = 11.4
•Average Waiting Time (WT) = (12 + 0 + 13 + 4 + 2) / 5 = 6.2
•Average Response Time (RT) = (12 + 0 + 13 + 4 + 2) / 5 = 6.2
Priority Scheduling
Problem
Starvation (Indefinite Blocking):
• low priority processes may never execute
• Low priority processes may suffer from starvation if higher priority processes keep
arriving continuously, causing the low priority ones to never get executed.
Solution
Aging
• Aging is a technique used to prevent starvation in priority scheduling.
• It works by gradually increasing the priority of a process the longer it waits in the
ready queue.
• For each unit of time a process waits, its priority is increased
Round Robin (RR) Scheduling
• Round Robin (RR) is a preemptive CPU scheduling algorithm designed mainly for
time-sharing systems.
• In RR, each process is assigned a fixed time unit called a time quantum or time
slice. The CPU scheduler goes through the ready queue in a circular manner,
allocating CPU to each process for one quantum at a time.
How does RR work?
• All processes are placed in a ready queue.
• The CPU scheduler picks the first process from the ready queue and allocates the
CPU for a time interval equal to the time quantum (q).
• If the process completes within this time, it leaves the system.
• If the process does not complete, it is preempted after the time quantum expires
and placed at the back of the ready queue.
• The CPU scheduler then picks the next process in the queue and repeats the
cycle.
Round Robin (RR) Scheduling
Gantt Chart
Process B CT TAT WT
P1 24 30 30 6
P2 3 7 7 4
P3 3 10 10 7
Gantt Chart Ready Queue: P1 P2 P3 P1 P4 P5 P2 P1 P5
Turnaround Waiting Response
Arrival Time Burst Time Completion Time
Process Time (TAT = CT Time (WT = Time (RT =
(AT) (BT) (CT)
- AT) TAT - BT) ST - AT)
P1 0 5 13 13 - 0 = 13 13 - 5 = 8 0 -0=0
P2 1 3 12 12 - 1 = 11 11 - 3 = 8 2-1=1
P3 2 1 5 5-2=3 3-1=2 4-2=2
P4 3 2 9 9-3=6 6-2=4 7-3=4
P5 4 3 14 14 - 4 = 10 10 - 3 = 7 9-4=5
•Average Turnaround Time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6
•Average Waiting Time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8
•Average Response Time = (0 + 1 + 2 + 4 + 5) / 5 = 12 / 5 = 2.4
Gantt Chart Ready Queue: P1 P2 P3 P1 P4 P5 P2
Process AT BT CT TAT = CT - AT WT = TAT - BT RT=ST- AT
P1 0 4 9 9-0=9 9-4=5 0-0=0
P2 1 5 15 15 - 1 = 14 14 - 5 = 9 3-1=2
P3 3 2 8 8-3=5 5-2=3 6-3=3
P4 4 1 10 10 - 4 = 6 6-1=5 9-4=5
P5 6 3 13 13 - 6 = 7 7-3=4 10-6=4
•Avg Turnaround Time = (9 + 14 + 5 + 6 + 7) / 5 = 41 / 5 = 8.2
•Avg Waiting Time = (5 + 9 + 3 + 5 + 4) / 5 = 26 / 5 = 5.2
•Avg Response Time = (0 + 2 + 3 + 5 + 4) / 5 = 14 / 5 = 2.8
Round Robin (RR) Scheduling
Advantages
Fairness: Each process gets an equal share of CPU time.
Responsive: Good for time-sharing systems, allowing interactive users to get
CPU time regularly.
No starvation: Because of the cyclic order, every process will eventually get
CPU time.
Disadvantages
Context Switching Overhead: If time quantum is too small, frequent context
switches happen, which can degrade performance.
Choosing Quantum: The time quantum value is critical — if it’s too large, RR
behaves like FCFS (First-Come-First-Served); if too small, excessive context
switching occurs.
Multilevel Queue Scheduling
• Multilevel Queue Scheduling is a CPU scheduling algorithm that partitions the
ready queue into several separate queues.
• Each queue has its own scheduling algorithm and priority level.
• Processes are permanently assigned to one queue based on some characteristic
like process type, priority, memory requirements, or process behavior.
Scheduling
Process Type Algorithm Priority
System
processes Round Robin Highest
Interactive
processes Round Robin Medium
Batch
processes FCFS Lowest
Multilevel Queue Scheduling
How It Works
• The CPU scheduler always selects processes from the highest priority queue that
is not empty.
• If that queue is empty, the scheduler looks for processes in the next lower priority
queue.
• No process in the batch queue, could run unless the queues for system
processes, and interactive processes were all empty.
• If an interactive process entered the ready queue while a batch process was
running, the batch process would be preempted.
Multilevel Feedback Queue Scheduling
• The multilevel feedback queue scheduling algorithm allows a process to
move between queues.
• Consider a multilevel feedback queue
scheduler with three queues, numbered from 0
to 2.
• The scheduler first executes all processes in
queue 0.
• Only when queue 0 is empty will it execute
processes in queue 1.
• Similarly, processes in queue 2 will be executed
only if queues 0 and 1 are empty.
• A process that arrives for queue 1 will preempt
a process in queue 2.
• A process in queue 1 will in turn be preempted
by a process arriving for queue 0.
Multilevel Feedback Queue Scheduling
• A process entering the ready queue is put in
queue 0.
• A process in queue 0 is given a time quantum of
8 milliseconds.
• If it does not finish within this time, it is moved
to the tail of queue 1.
• If queue 0 is empty, the process at the head of
queue 1 is given a quantum of 16 milliseconds.
• If it does not complete, it is preempted and is put
into queue 2.
• Processes in queue 2 are run on an FCFS basis
but are run only when queues 0 and 1 are empty.
Multilevel Feedback Queue Scheduling
• In general, a multilevel feedback queue scheduler is defined by the following
parameters:
• The number of queues
• The scheduling algorithm for each queue
• The method used to determine when to upgrade a process to a higher
priority queue
• The method used to determine when to demote a process to a lower
priority queue
Inter process Communication
• Processes within a system may be independent or cooperating
• Any process that does not share data with any other process is
independent.
• Any process that shares data with other processes is a cooperating process.
• Inter process Communication (IPC) refers to the mechanisms that allow
processes to exchange data and coordinate their actions.
Why IPC is Needed
• Data Sharing
• Computation Speedup
• Modularity
• Convenience
IPC models
Two models of IPC
Shared memory
Message passing
IPC – Shared Memory
• A region of memory is shared between cooperating processes.
• One process creates a shared memory segment.
• a shared-memory region resides in the address space of the process
creating the shared-memory segment.
• Other processes that want to communicate must attach the shared
memory segment to their address space.
• Once shared memory is established, processes can read and write directly
in that region.
Producer-Consumer Problem
Paradigm for cooperating processes:
• producer process produces information that is consumed by a consumer
process
Two variations:
unbounded-buffer -places no practical limit on the size of the buffer:
• Producer never waits
• Consumer waits if there is no buffer to consume
bounded-buffer- assumes that there is a fixed buffer size
• Producer must wait if all buffers are full
• Consumer waits if there is no buffer to consume
Producer-Consumer Problem
• One solution to the producer–consumer problem uses shared memory.
• To allow producer and consumer processes to run concurrently, we must have
available a buffer of items that can be filled by the producer and emptied by the
consumer.
• This buffer will reside in a region of memory that is shared by the producer and
consumer processes.
• A producer can produce one item while the consumer is consuming another
item.
• We can do so by having an integer counter that keeps track of the number of
full buffers.
• Initially, counter is set to 0.
Bounded-Buffer – Shared-Memory Solution
• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Producer Process – Shared Memory
while (true) {
/* produce an item in next produced */
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer Process – Shared Memory
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
IPC – Message Passing
• Message passing allows processes to communicate and synchronize their actions by
sending and receiving messages.
• Unlike shared memory, processes do not share the same address space. Instead, the
OS provides mechanisms (system calls) for safe data exchange.
• It is particularly useful in a distributed environment, where the communicating
processes may reside on different computers connected by a network.
• A message-passing facility provides two operations:
• send(message)
• receive(message)
• If processes P and Q wish to communicate, they need to:
• Establish a communication link between them
• Exchange messages via send/receive
Implementation of Communication Link
• Here are several methods for logically implementing a link and the send()/receive()
operations:
• Direct or indirect communication
• Synchronous or asynchronous communication
• Automatic or explicit buffering
Direct Communication
• Each process that wants to communicate must explicitly name the recipient or
sender of the communication.
• In this scheme, the send() and receive() primitives are defined as:
• send(P, message)—Send a message to process P.
• receive(Q, message)—Receive a message from process Q.
Indirect Communication
• With indirect communication, the messages are sent to and received from mail
boxes, or ports.
• A mailbox can be an object into which messages can be placed by processes and
from which messages can be removed
• Each mailbox has a unique id
Operations:
• Create a new mailbox (port)
• Send and receive messages through mailbox
• Delete a mailbox
• Primitives are defined as:
• send(A, message) – send a message to mailbox A
• receive(A, message) – receive a message from mailbox A
Synchronization
• Message passing may be either blocking or nonblocking— also known as
synchronous and asynchronous.
• Blocking is considered synchronous
• Blocking send -- the sender is blocked until the message is received
• Blocking receive -- the receiver is blocked until a message is available
• Non-blocking is considered asynchronous
• Non-blocking send -- the sender sends the message and continue
• Non-blocking receive -- the receiver receives:
• A valid message, or
• Null message
When both send() and receive() are blocking, we have a rendezvous between
the sender and the receiver.
Buffering
• Whether communication is direct or indirect, messages exchanged by
communicating processes reside in a temporary queue.
• Basically, such queues can be implemented in three ways
Zero capacity
• The queue has a maximum length of zero; thus, the link cannot have any
messages waiting in it.
• In this case, the sender must block until the recipient receives the message.
Bounded capacity.
• The queue has finite length n; thus, at most n messages can reside in it.
• If the link is full, the sender must block until space is available in the queue.
Unbounded capacity –
• infinite length
• Sender never waits
Producer-Consumer: Message Passing
• Producer:
message next_produced;
while (true) {
/* produce an item in next_produced */
send(next_produced);
}
• Consumer:
message next_consumed;
while (true) {
receive(next_consumed)
/* consume the item in next_consumed */
}
Threads in OS
• A thread is the smallest unit of execution in an operating system.
• A thread is a lightweight execution unit inside a process that allows multitasking
and faster performance.
• It is like a lightweight process.
• A process can have one or many threads.
• Each thread has its own:
• Program Counter (PC) – tells which instruction to execute next
• Registers – hold intermediate data
• Stack – used for function calls and local variables
• But threads of the same process share:
• Code section
• Data section (global variables, heap memory)
• Open files and resources
Process VS Thread
Aspect Process Thread
Definition Independent program in execution Smallest execution unit inside a process
Memory Own memory (code, data, heap, stack) Shares process memory; own stack &
registers
Creation heavy-weight Light-weight
Communication Needs IPC Easy via shared memory
Isolation Processes are isolated from each other Threads are not isolated (if one thread
(one crash doesn’t affect others). crashes, the whole process may crash).
Within a single Chrome tab: one thread
Example Chrome browser = one process per tab. for rendering, one for JavaScript
Single and Multithreaded Processes
1. Single-Threaded Process
• Has only one thread of control.
• The process executes one task at a time
• Example:
• A simple text editor that allows only typing (no background saving, no spell-checking).
Single and Multithreaded Processes
2. Multi-Threaded Process
• Has two or more threads within the same process.
• If a process has multiple threads of control, it can perform more than one task at
a time.
• All threads share the same code, data, and resources but have separate stacks
and registers.
• Most software applications that run on modern computers are multithreaded.
• For example, A word processor(doc) may have a thread for displaying
graphics, another thread for responding to keystrokes from the user, and a
third thread for performing spelling and grammar checking in the
background.
Types of Threads
user threads:
• Managed by the user-level threads library, not the OS.
• Faster to create and switch.
kernel threads
• Managed by the OS.
• OS scheduler handles them.
• Slower to create/switch than user threads, but more powerful.
Multithreading Models
• To connect user threads to kernel threads, different multithreading
models are used.
1. Many-to-One
2. One-to-One
3. Many-to-Many
Many-to-One
• The many-to-one model maps many user-level threads to one kernel thread.
• One thread blocking causes the entire process to block
• Multiple threads may not run in parallel because the kernel can schedule only
one thread at a time.
• Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
•Pros: Efficient, less overhead.
•Cons: If one thread blocks → all threads in the process block (no true parallelism).
One-to-One
• The one-to-one model maps each user thread to a kernel thread.
• Creating a user thread requires creating the corresponding kernel thread.
• More concurrency than many-to-one Because if one thread makes a blocking
system call, other threads can still run.
• Multiple threads can run simultaneously on multiprocessors
• Number of threads per process sometimes restricted due to overhead of
creating kernel threads
Examples
• Windows
• Linux
Many-to-Many Model
• The many-to-many model maps many user-level threads to a smaller or equal
number of kernel threads.
• The number of kernel threads may be specific to either a particular application
or a particular machine
• Developers can create as many user threads as necessary, and the
corresponding kernel threads can run in parallel on a multiprocessor.
• When a thread performs a blocking system call, the kernel can schedule another
thread for execution.
• Example: Solaris (older versions), Windows ThreadPool.