0% found this document useful (0 votes)
13 views74 pages

OS Unit2

This document provides an overview of process and thread management in operating systems, detailing concepts such as process states, process control blocks, and scheduling algorithms. It covers the differences between programs and processes, the operations on processes, and the significance of CPU scheduling in maximizing efficiency. Additionally, it discusses various types of schedulers and their roles in managing process execution within a system.
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
0% found this document useful (0 votes)
13 views74 pages

OS Unit2

This document provides an overview of process and thread management in operating systems, detailing concepts such as process states, process control blocks, and scheduling algorithms. It covers the differences between programs and processes, the operations on processes, and the significance of CPU scheduling in maximizing efficiency. Additionally, it discusses various types of schedulers and their roles in managing process execution within a system.
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/ 74

1

Unit 2 : Process and Thread Management

Prepared by:
Dr. Khushboo Jain

School of Computer Science


UPES, Dehradun
India
Table of Contents
• Process: Program and Process concept, Process in memory, Process Control
Block, Process States, Process Context Switching, Process Scheduling Queues,
Process Schedulers, Process Context Switching,
• CPU Scheduling: Process Scheduling Criteria, Process Scheduling: Non-pre-
emptive and Pre-emptive Schedulers, FCFS, Shortest Job First, Shortest
Remaining Time First, Non -Pre-emptive Priority scheduling and Pre-emptive
Scheduling, Priority Round Robin, Multilevel Queue, Multiple Feedback Queue,
• Threads: Threads and its benefits, Multi-threading models, Kernel Level thread,
user level thread and hybrid threads, Thread Scheduling: Content Scope, Pthread
Scheduling, Threading Issues
• Case study: Process Management in Linux
Process Concept

• Process: A process can be thought of as a program in execution (or) A process is


the unit of work in a modern time-sharing system.
• A process will need certain resources such as CPU time, memory, files and I/O
devices to accomplish its task. These resources are allocated to the process either
when it is created or while it is executing.

• The process concept includes the following:


1. Process
2. Process state
3. Process Control Block
4. Threads
Process Concept(Continuing…)

The below figure shows the structure of process in memory:

The process contains several sections: Text, Data, Heap


and Stack.
• Text Section contains the program code. It also includes the
current activity, as represented by the value of the program
counter and the contents of the processor’s registers.
• Process stack contains temporary data such as function
parameters, return addresses and local variables.
• Data section contains global variables.
• Heap is memory that is dynamically allocated during process
run time.
Fig: Process in mem
ory
Difference between Program and Process
• A program is a passive entity, such as a file containing a list of instructions stored on
disk often called an executable file.
• A process is an active entity with a program counter specifying the next instruction to execute
and a set of associated resources.
• A program becomes a process when an executable file is loaded into memory.
• Two common techniques for loading executable files are double-clicking an icon
representing the executable file and entering the name of the executable file on the
command line as in prog.exe or a.out.
• Although two processes may be associated with the same program, they are
considered as two separate execution sequences. For instance, several users may be
running different copies of the mail program or the same user may invoke many
copies of the web browser program. Each of these is considered as a separate
process.
Process State
As a process executes, it changes state. The process state defines the current activity of that
process. A process may be in one of the following states:
• New: The process is being created.
• Ready: The process is waiting to be assigned to a processor.
• Running: Instructions are being executed.
• Waiting: The process is waiting for some event to occur such as an I/O completion or reception of a signal.
• Terminated: The process has finished execution.

Fig: Process State


Process Control Block
• Each process is represented in the operating system by a Process
Control Block (PCB). It is also called a Task Control Block.
• PCB serves as the repository for any information that may vary from
process to process.

The PCB contains information related to process such as:


1. Process state: The state may be new, ready, running, waiting and terminated.
2. Program counter: The counter indicates the address of the next instruction to be
executed for this process.
3. CPU registers: The registers vary in number and type, depending on the
computer architecture. They include accumulators, index registers, stack pointers
and general-Purpose registers etc. Along with the program counter, this state
information must be saved when an interrupt occurs, to allow the process to be
continued correctly afterward. Fig: Process Control Bloc
k
Process Control Block Information
4. CPU-scheduling information: This information includes a process
priority, pointers to scheduling queues and any other scheduling
parameters.

5. Memory-management information: This information includes the


base and limit registers values, the page tables or the segment tables
depending on the memory system used by the operating system.

6. Accounting information: This information includes the amount of CPU


and real time used, time limits, account numbers, job or process
numbers and so on.

7. I/O status information: This information includes the list of I/O


devices allocated to the process, a list of open files and so on.
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 for
program execution on the CPU.

Process scheduling involves three things:


1. Scheduling Queues
2. Schedulers
3. Context Switch
Scheduling Queues

There are several queues are implemented in operating system such as Job
Queue, Ready Queue, Device Queue.
• Job Queue: It consists of all processes in the system. As processes enter the system, they are
put into a job queue.
• Ready Queue: The processes that are residing in main memory and they are ready and
waiting to execute are kept on a list called the Ready Queue. Ready queue is generally stored
as a linked list. A ready-queue header contains pointers to the first and final PCBs in the list.
Each PCB includes a pointer field that points to the next PCB in the ready queue.
• Device Queue: Each device has its own device queue. It contains the list of processes waiting
for a particular I/O device.
Scheduling Queues(Continuing…)
Consider the given Queuing Diagram:
· Two types of queues are present: the Ready Queue
and a set of Device Queues.
· CPU and I/O are the resources that serve the queues.
· A new process is initially put in the ready queue. It
waits there until it is selected for execution or
dispatched.
• Once the process is allocated the CPU and is
executing, one of several events could occur:
1. The process could issue an I/O request and then be placed
in an I/O queue. Fig: Queuing diagram representation of process sche
2. The process could create a new child process and wait for
duling
the child’s termination.
3. The process could be removed forcibly from the CPU,
because of an interrupt and be put back in the ready
queue.
Schedulers
A process migrates among the various scheduling queues throughout its lifetime. For
scheduling purpose, the operating system must select processes from these queues. The
selection process is carried out by the Scheduler.
There are three types of Schedulers:
1. Long Term Scheduler
2. Short Term Scheduler
3. Medium Term Scheduler
Long Term Scheduler (New to ready state)
• Initially processes are spooled to a mass-storage device (i.e Hard disk), where they are kept for later
execution.
• Long-term scheduler or job scheduler selects processes from this pool and loads them into main
memory for execution. (i.e. from Hard disk to Main memory).
• The long-term scheduler executes much less frequently, there may be minutes of time between creation
of one new process to another process.
• The long-term scheduler controls the degree of multiprogramming (the number of processes in
memory).
Schedulers
Cont…

Fig: Process Scheduler


Schedulers
Cont…
Short Term Scheduler (Ready to Running)
• Short-term scheduler or CPU scheduler selects from among the processes that are ready to
execute and allocates the CPU to one of them. (i.e. a process that resides in main memory
will be taken by CPU for execution).
• The short-term scheduler must select a new process for the CPU frequently.
• The short-term scheduler must be very fast because of the short time between executions of
processes.

Medium Term Scheduler


Medium Term Scheduler does two tasks:
1. Swapping: Medium-term scheduler removes a process from main memory and stores it
into the secondary storage. After some time, the process can be reintroduced into main
memory and its execution can be continued where it left off. This procedure is called
Swapping.
2. Medium Term Scheduler moves a process from CPU to I/O waiting queue and I/O queue to
ready queue.
I/O bound and CPU bound
The processes can be described as two types:
1. I/O bound process is one that spends more of its time doing I/O than it spends doing computations.
2. CPU Bound process using more of its time doing computations and generates I/O requests
infrequently.

The long-term scheduler selects a good process mix of I/O-bound and CPU-bound
processes.
• If all processes are I/O bound, the ready queue will almost always be empty, and the CPU will remain
idle for long time because I/O device processing takes a lot of time.
• If all processes are CPU bound, the I/O waiting queue will almost always be empty. I/O devices will
be idle, and CPU is busy for most of the time.
• Thus, if the system maintains the combination of CPU bound and I/O bound processes then the system
performance will be increased.

Note: Time-sharing systems such as UNIX and Microsoft Windows systems often have no long-term scheduler but
simply put every new process in memory for the short-term scheduler.
Context Switching
• Switching the CPU from one process 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 a Context Switch.

• The context is represented in the PCB of the


process. It includes the value of the CPU registers,
the process state and memory-management
information.

• When a context switch occurs, the kernel saves the


context of the old process in its PCB and loads the
saved context of the new process scheduled to run.
Fig: CPU Switch from Process to Process
• Context-switch time is pure overhead, because the
system does no useful work while switching.
Context switch time may be in few milliseconds.
Operations on Processes
1. Process Creation
2. Process Termination

Process Creation
• During the execution of a process in its life time, a process may create several new processes.
• The creating process is called a parent process and the new processes are called children process.
• Each of these new processes may create other processes forming a tree of processes.
• Operating system identifies processes according to process identifier (pid).
• Pid provides an unique integer number for each process in the system.
• Pid can be used as an index to access various attributes of a process within the kernel.
Operations on Processes (Continuing…)
The below figure shows the process tree for the Linux OS that
shows the name of each process and its process ID (pid) . In Linux
process is called task.

Fig: Process Tree


Process System calls in Unix/ Linux: fork( ), exec( ), wait( ), exit( )
The key system calls in UNIX:

1. fork(): The fork() system call is used to create a new process (child process). The child process is a
duplicate of the parent process, sharing the same code, but with separate memory and execution.
After the fork, both the parent and child processes run the next instruction.

2. exec(): The exec() system call is used to replace the current process's memory space with a new
program. It loads and runs a new executable file within the same process, effectively transforming the
process into the new program. It is commonly used after fork().

3. wait(): The wait() system call allows a parent process to pause its execution until one or more of its
child processes finish. It ensures that the parent doesn't proceed until the child terminates, returning
control to the parent when the child process ends.

4. exit(): The exit() system call is used by a process to terminate its execution. It ends the process and
returns a status code to the operating system, signaling whether the process ended successfully or
encountered an error.
Continuing…
Zombie Process: A zombie process is a process that has terminated, but its
parent has not yet called wait(). Although its resources are deallocated, its entry
in the process table remains to hold the exit status. Once the parent calls wait(),
the process identifier and table entry are released.

Orphan Process: An orphan process occurs when the parent terminates without
calling wait(), leaving the child process without a parent. In Linux and UNIX,
orphan processes are adopted by the init process, which periodically calls wait()
to clean up the orphan’s exit status and process identifier.
Process Scheduling
Process scheduling is the basis of Multi-programmed operating
systems. By switching the CPU among processes, the operating
system can make the computer more productive
• In a single-processor system, only one process can run at a
time. Others must wait until the CPU is free and can be
rescheduled.
• The CPU will sit idle and waiting for a process that needs an I/O
operation to complete. If the I/O operation completes then only
the CPU will start executing the process. A lot of CPU time has
been wasted with this procedure.
• The objective of multiprogramming is to have some process
always running to maximize CPU utilization.
• When several processes are in main memory, if one processes
is waiting for I/O then the operating system takes the CPU away
from that process and gives the CPU to another process. Hence
there will be no wastage of CPU time.
Process Scheduling
Concepts of Process Scheduling
1. CPU–I/O Burst Cycle
2. CPU Scheduler
3. Pre-emptive Scheduling
4. Dispatcher

CPU–I/O Burst Cycle


Process execution consists of a cycle of CPU
execution and I/O wait.
• Process execution begins with a CPU burst. That is
followed by an I/O burst.
• Processes alternate between these two states.
Fig: Process Schedul
• The final CPU burst ends with a system request to er
terminate execution.
• Hence the First cycle and Last cycle of execution
must be CPU burst.
Process Scheduling
CPU Scheduler
Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried
out by the Short-Term Scheduler or CPU scheduler.

Preemptive Scheduling
CPU-scheduling decisions may take place under the following four cases:
1. When a process switches from the running state to the waiting state.
Example: as the result of an I/O request or an invocation of wait( ) for the
termination of a child process.
2. When a process switches from the running state to the ready state.
Example: when an interrupt occurs
3. When a process switches from the waiting state to the ready state.
Example: at completion of I/O.
4. When a process terminates. For situations 2 and 4 are considered as Pre-
emptive scheduling situations. Mach OS X,WINDOWS 95 and all
subsequent versions of WINDOWS are using Preemptive scheduling.
Process Scheduling
Dispatcher
The dispatcher is the module that gives control of the CPU to the
process selected by the short-term scheduler. Dispatcher
function involves:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart
that program.
The dispatcher should be as fast as possible, since it is invoked
during every process switch. The time it takes for the dispatcher
to stop one process and start another process running is known
as the Dispatch Latency.
Process Scheduling
Different CPU-scheduling algorithms have different properties, and the
choice of a particular algorithm may favor one class of processes over
another.
Many criteria have been suggested for comparing CPU-scheduling
algorithms:
• CPU utilization: CPU must be kept as busy as possible. CPU utilization can range
from 0 to 100 percent. In a real system, it should range from 40 to 90 percent.
• Throughput: The number of processes that are completed per time unit.
• Turn-Around Time: It is the interval from the time of submission of a process to
the time of 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.
• Waiting time: It is the amount of time that a process spends waiting in the ready
queue.
• Response time: It is the time from the submission of a request until the first
response is produced. Interactive systems use response time as its measure.

Note: It is desirable to maximize CPU utilization and Throughput and to minimize Turn- round
Process Scheduling Algorithms
Process/CPU scheduling deals with the problem of deciding which
of the processes in the ready queue is to be allocated the CPU.
Different CPU-scheduling algorithms are:
1. First-Come, First-Served Scheduling (FCFS)
2. Shortest-Job-First Scheduling (SJF)
3. Priority Scheduling
4. Round Robin Scheduling
5. Multilevel Queue Scheduling
6. Multilevel Feedback Queue Scheduling
First-Come, First-Served Scheduling (FCFS)
In FCFS, the process that requests the CPU first is allocated the CPU first.
• FCFS scheduling algorithm is Non-preemptive.
• Once the CPU has been allocated to a process, it keeps the CPU until it releases the
CPU.
• FCFS can be implemented by using FIFO queues.
• When a process enters the ready queue, its PCB is linked onto the tail of the queue.
• When the CPU is free, it is allocated to the process at the head of the queue.
· The running process is then removed from the queue.

Example:1 Consider the following set of processes that arrive at time 0. The
processes arearrived in the order P1, P2, P3, with
Process the length of the CPU burst given in
Burst
milliseconds. Time
P1 24
P2 3
Gantt Chart for P3 3
FCFS is:
First-Come, First-Served Scheduling (FCFS)
The average waiting time under the FCFS policy is often quite long.
• The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2 and
27 milliseconds for process P3.
• Thus, the average waiting time is (0 + 24 + 27)/3 = 17 milliseconds.

Convoy Effect in FCFS


Convoy effect means, when a big process is executing in CPU, all the smaller processes
must have to wait until the big process execution completes. This will affect the
performance of the system.
Example:2 Let us consider same example above but with the processes arrived in the
order
P2, P3, P1.

The processes coming at P2, P3, P1 the average waiting time (6 + 0 + 3)/3 = 3
milliseconds whereas the processes are coming in the order P1, P2, P3 the average
waiting time is 17 milliseconds.

Disadvantage of FCFS:
FCFS scheduling algorithm is Non-preemptive, it allows one process to keep CPU for
long time. Hence it is not suitable for time sharing systems.
Shortest-Job-First Scheduling(SJF)
Shortest-Job-First Scheduling (SJF)
SJF algorithm is defined as “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 between two processes.

SJF is also called as Shortest-Next CPU-Burst algorithm, because scheduling


depends on the length of the next CPU burst of a process, rather than its total length.
Proces Burst
s Time
Example: Consider the following processes and CPU burst in milliseconds:
P1 6
P2 8
Gantt Chart of SJF algorithm: P3 7
P4 3
Shortest-Job-First Scheduling(SJF)
Waiting Time for Processes:
• By looking at the table the average waiting time by using
Process Burst Waiting
SJF algorithm is 7ms. Time(ms) Time
• SJF gives the minimum average waiting time for a given
set of processes. SJF is optimal.
• The average waiting time decreases because moving a
short process before long process decrease the waiting
time of the short process more than it increases the P1 6 3
waiting time ofthe long process. P2 8 16
P3 7 9
Difficulty with SJF
The difficulty with the SJF algorithm is ―knowing the length P4 3 0
of the next CPU request. With Short-Term Scheduling, there is Average Waiting Time 7ms
no way to know the length of the next CPU burst. It is not
implemented practically.
Shortest-Job-First Scheduling(SJF)
Solution for the difficulty
One approach to this problem is to try to approximate SJF scheduling.
• We may not know the length of the next CPU burst, but we may be able to predict its
value. We expect that the next CPU burst will be similar in length to the previous
ones.
• By computing an approximation of the length of the next CPU burst, we can pick the
process with the shortest predicted CPU burst.
• The next CPU burst is generally predicted as an Exponential Average of the
measured lengths of previous CPU bursts.
The following formula defines the Exponential average:

tn be the length of the nth CPU burst (i.e. contains the most recent
information).
stores the history.
be our predicted value for the next CPU burst.
α controls the relative weight of recent and past history in our prediction (0
≤ α ≤1)
• If α=0, then , recent history has no effect
• If α=1 then , only the most recent CPU burst matters.
• If α = 1/2, so recent history and past history are equally weighted.
Shortest Remaining Time First Scheduling (SRTF)
SRTF is the pre-emptive SJF algorithm.
• 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 the currently
executing process.
• SRTF will preempt the currently executing
Process Arrival process
Time and executes the shortest job.
Burst Time(ms)
Consider the four processes with arrival times and burst times in milliseconds:
P1 0 8
P2 1 4
P3 2 9
P4 3 5

Gantt Chart for SRTF:


Shortest Remaining Time First Scheduling (SRTF)
Process P1 is started at time 0, since it is the only process in the queue.

 Process P2 arrives at time 1. The remaining time for process P1 (7 milliseconds) is larger
than the time required by process P2 (4 milliseconds), so process P1 is preempted and
process P2 is scheduled.

 The average waiting time = 26/4 = 6.5 milliseconds.


Priority Scheduling
A priority is associated with each process and the CPU is allocated to the process with
the highest priority. Equal-priority processes are scheduled in FCFS order.
• An SJF algorithm is special kind of priority scheduling algorithm where small CPU
burst will have higher priority.
• Priorities can be defined based on time limits, memory requirements, the number
of open files etc.
Example: Consider the following processes with CPU burst and Priorities. All the
processes are arrived at time t=0 in the same order. Low numbers are having higher
Process Burst Priority
priority. Time(ms)
P1 10 3
P2 1 1
P3 2 4
P4 1 5
Gantt chart for Priority Scheduling:
P5 5 2
Priority Scheduling
Priority scheduling can be either Preemptive or Non-preemptive.
Process Burst Waiting
A Preemptive Priority Scheduling algorithm will preempt the CPU Time(ms) Time
if the priority of the newly arrived process is higher than the priority
of the currently running process. P1 10 6
Problem: Starvation or Indefinite Blocking P2 1 0
• In priority Scheduling when there is a continuous
P3 2 16
flow of higher priority processes has come to ready
queue then all the lower priority processes must P4 1 18
have to wait for the CPU until the all the higher P5 5 1
• priority
This leads processes
to lowerexecution completes.
priority processes blocked from Average Waiting Time 8.2 ms
getting CPU for long period of time. This situation is
called Starvation or Indefinite blocking.
• In worst case indefinite blocking may take years to
execute the process.
Solution: Aging
Aging involves gradually increasing the priority of
processes that wait in the system for a long time.
Round Robin(RR) Scheduling
Round-Robin Scheduling (RR)
Round-Robin (RR) scheduling algorithm is designed especially for Timesharing
systems.
• RR is similar to FCFS scheduling, but preemption is added to enable the system to
switch between processes.
• A small unit of time called a Time Quantum or Time Slice is defined. A time
quantum is generally from 10 to 100 milliseconds in length.
• The ready queue is treated as a Circular queue. New processes are added to the
tail of the ready queue.
• The CPU scheduler goes around the ready queue by allocating the CPU to each
process for a time interval of up to 1 time quantum and dispatches the process.
• If a process CPU burst exceeds 1 time quantum, that process is preempted and is
put back in the ready queue.
Round Robin(RR) Scheduling
In RR scheduling one of two things will then happen:
1. The process may have a CPU burst of less than 1 time quantum. The process itself
will release the CPU voluntarily. The scheduler will then proceed to the next
process in the ready queue.
2. If the CPU burst of the currently running process is longer than 1 time quantum,
the timer will go off and will cause an interrupt to the operating system. A context
switch will be executed and the process will be put at the tail of the ready queue.
The CPU scheduler will then select the next process in the ready queue.

Consider the following set of processes that arrive at time 0 and the processes are
arrived in Process Burst Time
the order P1, P2, P3 and Time Quanta=4. (ms)
P1 24
P2 3
Gantt chart of Round Robin Scheduling: P3 3
Round Robin(RR) Scheduling
• If we use a time quantum of 4 milliseconds, then process P1 gets the first 4
milliseconds.
• Since it requires another 20 milliseconds, it is preempted after the first-time
quantum and the CPU is given to the next process in the queue, process P2.
• CPU burst of Process P2 is 3, so it does not need 4 milliseconds then it quits before
its time quantum expires. The CPU is then given to the next process P3.
• Once each process has received 1 time quantum, the CPU is returned to process P1
for an additional time quantum.

The average waiting time under the RR policy is often long.


• P1 waits for 6 milliseconds (10 - 4), P2 waits for 4 milliseconds and P3 waits for 7
milliseconds. Thus, the average waiting time is 17/3 = 5.66 milliseconds. The
performance of the RR algorithm depends on the size of the Time Quantum.
• If the time quantum is extremely large, the RR policy is the same as the FCFS policy.
• If the time quantum is extremely small (i.e. 1 millisecond) the RR approach can
result in many context switches.
• The time taken for context switch value should be a small fraction of Time quanta
then the performance of the RR will be increased.

Note: A rule of thumb is that 80 percent of the CPU bursts should be shorter than the
time
quantum.
Multilevel Queue
• Ready queue is partitioned into separate queues, eg:
• foreground (interactive)
• background (batch)
• Process permanently in a given queue
• Each queue has its own scheduling algorithm:
• foreground – RR
• background – FCFS
• Scheduling must be done between the queues:
• Fixed priority scheduling; (i.e., serve all from foreground then from background).
Possibility of starvation.
• Time slice – each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR and 20% to background in FCFS
Multilevel Queue Scheduling

Fig 3. Multilevel Queue Scheduling [1]


Multilevel Feedback Queue

• A process can move between the various queues; aging can be


implemented this way
• Multilevel-feedback-queue scheduler defined by the following
parameters:
• number of queues
• scheduling algorithms for each queue
• method used to determine when to upgrade a process
• method used to determine when to demote a process
• method used to determine which queue a process will enter when that process
needs service
Example of Multilevel Feedback Queue

Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS

Scheduling
A new job enters queue Q0 which is served FCFS
When it gains CPU, job receives 8 milliseconds
Fig 4. Example of multilevel
If it does not finish in 8 milliseconds, job is moved feedback queue [1]
to queue Q1
At Q1 job is again served FCFS and receives 16
additional milliseconds
If it still does not complete, it is preempted and
moved to queue Q2
Thread
• Definition: Threads are the smallest unit of execution within a process.

• Purpose: Allow for parallel execution of tasks within a single process.

• Benefits:
• Improved Performance: Allows concurrent execution, making better use of
CPU.
• Resource Sharing: Threads within the same process share memory and
resources.
• Responsiveness: Enhances the responsiveness of applications by performing
background tasks.
• Simplicity: Simplifies program design by breaking tasks into smaller threads.
Multicore Programming
• Multicore or multiprocessor systems putting pressure on programmers, challenges
include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging

• Parallelism implies a system can perform more than one task simultaneously

• Concurrency supports more than one task making progress


• Single processor / core, scheduler providing concurrency
Concurrency vs. Parallelism
Concurrent execution on single-core system:

Fig 2. Concurrency [1]


Parallelism on a multi-core system:

Fig 3. Parallelism [1]


Multicore Programming (Cont.)
• Types of parallelism
• Data parallelism – distributes subsets of the same data across multiple cores,
same operation on each
• Task parallelism – distributing threads across cores, each thread performing
unique operation
Multi-threading Models

• Single-threaded Process:
• Only one thread of execution.
• Simpler but less efficient for concurrent tasks.

• Multi-threaded Process :
• Multiple threads execute independently within a process.
• Provides better performance and responsiveness.

• Model Types:
• User-Level Threads (ULT): Managed by user-level libraries.
• Kernel-Level Threads (KLT): Managed by the operating system kernel.
• Hybrid Threads: Combination of ULT and KLT.
Single and Multithreaded Processes

Fig 4. Single and Multithreaded Processes [1]


Multi-threading Models
• User threads - management done by user-level threads library
• Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
• Kernel threads - Supported by the Kernel
• Examples – virtually all general-purpose operating systems, including:
• Windows
• Solaris
• Linux
• Tru64 UNIX
• Mac OS X
• Hybrid thread – Combination of both
User Level Threads (ULT)
• Definition: Threads that are managed by user-level libraries and not by the kernel.

• Features:
• Visibility: Kernel does not recognize ULTs, only the process.
• Context Switching: Faster and less costly.

• Advantages: Low overhead, easy to implement.

• Disadvantages: Cannot take full advantage of multi-core processors, limited by


single-core execution.
Kernel Level Threads (KLT)
• Definition: Threads that are managed directly by the operating system kernel.

• Features:
• Visibility: OS kernel is aware of each thread.
• Context Switching: More costly compared to ULT.

• Advantages: True parallelism, better support for multi-core processors.

• Use Cases: Systems requiring robust concurrency and multitasking.


Hybrid Threads
• Definition: Combination of user-level and kernel-level threads.

• Features:
• Management: Both user-level libraries and kernel support.

• Advantages: Efficient use of system resources, improved performance on multi-


core processors.

• Disadvantages: Increased complexity in thread management and synchronization.


User Threads, Kernel Threads and Hybrid Threads

Thread Type Managed By Characteristics Advantages Disadvantages

Limited to single-
Not visible to the OS, Lower overhead,
User-Level core processors,
User-level libraries managed by the faster context
Threads (ULT) less support from
thread library switching
OS

Full OS support, each Higher overhead


Kernel-Level Better multitasking,
Operating System thread is visible to the due to context
Threads (KLT) true parallelism
OS switching

Both user-level and Flexibility, improved Complexity in


Combines benefits of
Hybrid Threads kernel-level performance on implementation
both ULT and KLT
mechanisms multi-core systems and management
Multithreading Models
• Many operating systems support kernel thread and user thread in a combined
way.

• Example of such system is Solaris. Multi threading model are of three types.

• Many-to-One

• One-to-One

• Many-to-Many
Many-to-One Model
• Many user-level threads mapped to single
kernel thread
• One thread blocking causes all to block
• Multiple threads may not run in parallel on
muticore system because only one may be in
kernel at a time
• Few systems currently use this model
• Examples:
• Solaris Green Threads
• GNU Portable Threads

Fig 5. Many-to-One Model [1]


One-to-One Model
• Each user-level thread maps to
kernel thread
• Creating a user-level thread creates a
kernel thread
• More concurrency than many-to-one
• Number of threads per process
sometimes restricted due to
overhead
• Examples
• Windows Fig 6. One-to-One Model [1]
• Linux
• Solaris 9 and later
Many-to-many Model
• Allows many user level threads to be mapped
to many kernel threads
• Allows the operating system to create a
sufficient number of kernel threads
• Solaris prior to version 9
• Windows with the ThreadFiber package

Fig 7. Many-to-Many Model [1]


Two-Level Model
• Similar to M:M, except that it allows a user
thread to be bound to kernel thread
• Examples
• IRIX
• HP-UX
• Tru64 UNIX
• Solaris 8 and earlier

Fig 8. Two-Level Model


[1]
Difference between multithreading models
Aspect Many-to-One Model One-to-One Model Many-to-Many Model
Maps many user-level Maps many user-level
Maps each user-level thread to
Description threads to one kernel threads to many kernel
a kernel thread.
thread. threads.
Limited concurrency; only High concurrency; multiple Flexible concurrency; can
Concurrency one thread can access the threads can run in parallel on handle more threads than
kernel at a time. multiple processors. kernel threads.
Allows true parallelism on Avoids the limitations of
Simple implementation; multiprocessors; each thread both many-to-one and one-
Advantages
minimal overhead. can be independently to-one models; ent thread
scheduled. management.
Blocking of one thread Significant overhead for
Disadvantag More complex to implement
blocks all threads; no true creating and managing many
es than other models.
parallelism. kernel threads.
Suitable for applications
Suitable for applications Suitable for applications that
that need a balance
Suitability that do not require high require high concurrency and
between concurrency and
concurrency. true parallelism.
resource utilization.
Green threads in early Windows operating system,
Examples Solaris operating system.
versions of Java. Linux pthreads.
Thread Libraries

• Thread library provides programmer with API for creating and managing threads
• Two primary ways of implementing
• Library entirely in user space
• Kernel-level library supported by the OS
Pthread Scheduling

1. POSIX Threads (Pthreads):


• Overview: Standard API for thread management in Unix-like operating
systems.
• Thread Creation: Use pthread_create() to start a new thread.
• Thread Management: Includes functions like pthread_join() for
synchronizing threads and pthread_cancel() for terminating threads.

2. Scheduling Policies:
• SCHED_FIFO (First In, First Out): Threads are executed in the order they
are scheduled, without preemption.
• SCHED_RR (Round Robin): Threads are given equal time slices in a cyclic
manner.
• SCHED_OTHER: Default policy with time-sharing, preemptive scheduling.
Pthread Scheduling

3. Priority and Scheduling Attributes:


• Setting Priority: Use pthread_attr_setschedpolicy() and
pthread_attr_setschedparam() to define thread priorities and scheduling
policies.
• Real-Time Priorities: Higher priority for real-time threads, subject to policy
constraints.

4. Considerations for Pthreads:


• Portability: Scheduling behavior may vary between different Unix-like
systems.
• Real-Time Requirements: Special handling for real-time threads to ensure
deadlines are met.
Threading Issues
1. Race Conditions: Occurs when multiple threads access shared resources concurrently
without proper synchronization.
• Prevention: Use synchronization mechanisms such as mutexes, semaphores, and
condition variables.

2. Deadlocks: A situation where two or more threads are waiting indefinitely for resources
held by each other.
• Prevention: Use strategies like resource ordering, timeout mechanisms, or deadlock
detection and recovery.

3. Starvation: Occurs when a thread is perpetually denied necessary resources due to the
prioritization of other threads.
• Prevention: Implement fair scheduling policies to ensure all threads get a fair share of
resources.
Threading Issues
4. Thread Overhead: Managing threads incurs overhead in terms of memory and CPU usage.
• Mitigation: Use thread pooling and minimize the number of threads to optimize
performance.

5. Context Switching: The process of saving and restoring thread states, which can be costly
in terms of performance.
• Optimization: Reduce context switching by optimizing thread management and using
efficient scheduling algorithms.

6. Synchronization Issues: Improper use of synchronization can lead to deadlocks, race


conditions, or inefficiencies.
• Best Practices: Design and test synchronization strategies thoroughly to ensure
correctness and performance.
Case study: Process Management in Linux
Under Linux, process properties fall into three groups:

1. Process Identity:
• Process ID (PID): A unique identifier assigned to each process.
• Parent Process ID (PPID): The PID of the process that created the current process.
• User ID (UID) and Group ID (GID): Identifiers for the user and group that own the process, used for
permission checks and resource access.

2. Process Environment:
• Environment Variables: Key-value pairs that define the operating environment for the process. These
variables can influence the behavior of the process and its child processes.
• Command Line Arguments: Parameters passed to the process at startup, which can affect its execution.
• Current Working Directory: The directory in which the process operates, impacting file access and
relative paths.
Case study: Process Management in Linux
cont..
3. Process Context:
• Memory Management: Information about the process’s memory usage, including code, data, and stack segments.
• CPU State: The process’s current execution state, including the values of registers, program counter, and other
CPU-specific information.
• File Descriptors: References to open files or sockets that the process uses for input and output operations.
• Scheduling Information: Details related to process scheduling, such as priority and scheduling policies.
Process and Threads
• Linux uses the same internal representation for processes and threads; a thread is simply a
new process that happens to share the same address space as its parent
• A distinction is only made when a new thread is created by the clone
• system call
• fork creates a new process with its own entirely new process context
• clone creates a new process with its own identity, but that is allowed to share the data
structures of its parent
• Using clone gives an application fine-grained control over exactly what is shared between
two threads
Stages of a Process in Linux

Fig 1. Stages of a Process in Linux [1]


Stages of a Process in Linux
1. Created: A process is created when a program is executed. At this stage, the process is in a
"created" state, and its data structures are initialized.
2. Ready: The process enters the "ready" state when it is waiting to be assigned to a processor by
the Linux scheduler. At this stage, the process is waiting for its turn to execute.
3. Running: The process enters the "running" state when it is assigned to a processor and is
actively executing its instructions.
4. Waiting: The process enters the "waiting" state when it is waiting for some event to occur,
such as input/output completion, a signal, or a timer. At this stage, the process is not actively
executing its instructions.
5. Terminated: The process enters the "terminated" state when it has completed its execution or
has been terminated by a signal. At this stage, the process data structures are removed, and its
resources are freed.
6. Zombie: A process enters the "zombie" state when it has completed its execution but its parent
process has not yet read its exit status. At this stage, the process details still have an entry in
the process table, but it does not execute any instructions. The zombie process is removed from
the process table when its parent process reads its exit status.
Types of Process Management in Linux
• Foreground Processes:
• Interactive processes requiring user input.
• Executed in the foreground and associated with a terminal.
• Managed using:
• Shell commands (start, stop, pause, resume).
• "ps" command for process information.
• Signals (e.g., "Ctrl+C").
• Job control commands (e.g., "bg", "fg", "kill").
• Background Processes (Non-Interactive):
• Run in the background without user interaction.
• Used for system services, daemons, and long-running tasks.
• Managed using:
• "ps" command for process information.
• "top" command for real-time monitoring.
• Methods for starting (e.g., "&" symbol, "nohup" command).
• "kill" command for termination.
Commands Used to Manage Processes in Linux
1. ps: This command is used to display information about running processes. The "ps" command
can be used to list all processes or filter the list based on various criteria, such as the user who
started the process, the process ID (PID), and the process status.
2. top: This command is used to display a real-time view of system processes. The "top" command
provides information about the processes running on the system, including their resource usages,
such as CPU and memory.
3. kill: This command is used to terminate a process. The "kill" command can be used with the
process ID (PID) of the process or with a signal number to request a specific action.
4. nice: This command is used to adjust the priority of a process. Higher-priority processes get
more CPU time than lower-priority processes. The "nice" command can be used to increase or
decrease the priority of a process, which affects its CPU usage.
5. renice: This command is used to change or adjust the priority of a running process, which
affects its CPU usage.
Commands Used to Manage Processes in Linux
6. pkill: This command is used to send a signal to a process to request it to terminate. The "pkill"
command can be used with a current process name or a regular expression to match multiple
processes.
7. top: This command is used to display a real-time view of system processes. The "top"
command provides information about the processes running on the system, including their
resource usages, such as CPU and memory.
8. jobs: This command is used to display a list of background jobs running in the current shell
session.
9. fg: This command is used to move a background process to the foreground. The "fg" command
can be used with the job ID of the background process.
10. bg: This command is used to move a suspended process to the background. The "bg" command
can be used with the job ID of the suspended process.
Thank You

74

You might also like