Understanding Threads in Operating Systems
Understanding Threads in Operating Systems
Anum Kamal
Components of Threads
These are the basic components of the Operating System.
Stack Space: Stores local variables, function calls, and return addresses specific to the thread.
Register Set: Hold temporary data and intermediate results for the thread's execution.
Program Counter: Tracks the current instruction being executed by the thread.
7
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Both can be terminated: Threads and processes can be terminated by the operating system or by other
threads or processes. When a thread or process is terminated, all of its resources, including its execution
context, are freed up and made available to other threads or processes.
Differences Between Threads and Process
Resources: Processes have their own address space and resources, such as memory and file handles,
whereas threads share memory and resources with the program that created them.
Scheduling: Processes are scheduled to use the processor by the operating system, whereas threads are
scheduled to use the processor by the operating system or the program itself.
Creation: The operating system creates and manages processes, whereas the program or the operating
system creates and manages threads.
Communication: Because processes are isolated from one another and must rely on inter-process
communication mechanisms, they generally have more difficulty communicating with one another than
threads do. Threads, on the other hand, can interact with other threads within the same program directly.
Threads, in general, are lighter than processes and are better suited for concurrent execution within a single program.
Processes are commonly used to run separate program or to isolate resources between program.
Threads
8
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Threading Issues
The fork() and exec() System Calls : The semantics of the fork() and exec() system calls change in a
multithreaded program. If one thread in a program calls fork(), does the new process duplicate all threads,
or is the new process single-threaded? Some UNIX systems have chosen to have two versions of fork(),
one that duplicates all threads and another that duplicates only the thread that invoked the fork() system
call. The exec() system , That is, if a thread invokes the exec() system call , the program specified in the
parameter to exec() will replace the entire process—including all threads.
9
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Signal Handling : A signal is used in UNIX systems to notify a process that a particular event has
occurred. A signal may be received either synchronously or asynchronously depending on the source of and
the reason for the event being signaled. All signals, whether synchronous or asynchronous, follow the same
pattern:1. A signal is generated by the occurrence of a particular event.2. The signal is delivered to a
process.3. Once delivered, the signal must be handled. A signal may be handled by one of two possible
handlers: 1. A default signal handler .2. A user-defined signal handler. Every signal has a default signal
handler that the kernel runs when handling that signal. This default action can be overridden by a user-
defined signal handler that is called to handle the signal.
Thread Cancellation : Thread cancellation involves terminating a thread before it has completed. For
example, if multiple threads are concurrently searching through a database and one thread returns the result,
the remaining threads might be canceled. Another situation might occur when a user presses a button on a
web browser that stops a web page from loading any further. Often, a web page loads using several
threads—each image is loaded in a separate thread. When a user presses the stop button on the browser, all
threads loading the page are canceled. A thread that is to be canceled is often referred to as the target thread.
Cancellation of a target thread may occur in two different scenarios:1. Asynchronous cancellation. One
thread immediately terminates the target thread.2. Deferred cancellation. The target thread periodically
checks whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion.
Thread-Local Storage : Threads belonging to a process share the data of the process. Indeed, this data
sharing provides one of the benefits of multithreaded programming. However, in some circumstances, each
thread might need its own copy of certain data. We will call such data thread-local storage (or TLS.) For
example, in a transaction-processing system, we might service each transaction in a separate thread.
Furthermore, each transaction might be assigned a unique identifier. To associate each thread with its
unique identifier, we could use thread-local storage.
Scheduler Activations : One scheme for communication between the user-thread library and the kernel is
known as scheduler activation. It works as follows: The kernel provides an application with a set of virtual
processors (LWPs), and the application can schedule user threads onto an available virtual processor.
What is Multi-Threading?
A thread is also known as a lightweight process. The idea is to achieve parallelism by dividing a process into
multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads:
one thread to format the text, another thread to process inputs, etc. More advantages of multithreading are discussed
below.
Multithreading is a feature in operating systems that allows a program to do several tasks at the same time. Think of
it like having multiple hands working together to complete different parts of a job faster. Each "hand" is called a
thread, and they help make programs run more efficiently.
Multithreading is a technique used in operating systems to improve the performance and responsiveness of computer
systems. Multithreading allows multiple threads (i.e., lightweight processes) to share the same resources of a single
process, such as the CPU, memory, and I/O devices.
10
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Multithreading can be done without OS support, as seen in Java's multithreading model. In Java, threads are
implemented using the Java Virtual Machine (JVM), which provides its own thread management. These threads,
also called user-level threads, are managed independently of the underlying operating system.
Application itself manages the creation, scheduling, and execution of threads without relying on the operating
system's kernel. The application contains a threading library that handles thread creation, scheduling, and context
switching. The operating system is unaware of User-Level threads and treats the entire process as a single-threaded
entity.
11
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Basic Concepts
1. CPU-I/O Burst Cycle
The success of CPU scheduling depends on an observed property of processes. The Process execution consists
of a cycle of the CPU execution and I/O wait. Processes alternate between these two states. Process execution
begins with a CPU Burst. That is followed an I/O Burst, which is followed by another CPU burst, then another
I/O burst, and so on.
A process typically spends a period of time executing instructions on the CPU (a "CPU burst"), then transitions
to an I/O operation (an "I/O burst"), and then back to the CPU, and so on. This cycle continues until the process
completes its task.
2. 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). The scheduler
selects a process from the processes in memory that are ready to execute and allocates the CPU to that process.
12
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
3. Dispatcher
Another component involved in the CPU-scheduling function is the dispatcher. The dispatcher is the module
that gives control of the CPU to the process selected by the short-term scheduler.
This function involves the following:
Switching Context
Switching to user mode
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 is known as the dispatch latency.
13
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
CPU Scheduling
CPU SCHEDULING ALGORITHMS
Step-by-Step Execution:
1. P1 will start first and run for 5 units of time (from 0 to 5).
14
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
2. P2 will start next and run for 3 units of time (from 5 to 8).
3. P3 will run last, executing for 8 units (from 8 to 16).
Gant Chart:
15
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now, let's calculate average waiting time and turn around time:
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turnaround Time - Burst Time
AT : Arrival Time
BT : Burst Time or CPU Time
TAT : Turn Around Time
WT : Waiting Time
Processes AT BT CT TAT WT
P1 0 5 5 5-0 = 5 5-5 = 0
P2 0 3 8 8-0 = 8 8-3 = 5
P3 0 8 16 16-0 = 16 16-8 = 8
Average Turn around time = 9.67
Average waiting time = 4.33
Consider the following table of arrival time and burst time for three processes P1, P2 and P3
Process Burst Time (BT) Arrival Time (AT)
P1 5 ms 2 ms
P2 3 ms 0 ms
P3 4 ms 4 ms
Step-by-Step Execution:
P2 arrives at time 0 and runs for 3 units, so its completion time is:
Completion Time of P2=0+3=3
P1 arrives at time 2 but has to wait for P2 to finish. P1 starts at time 3 and runs for 5 units. Its completion
time is:
Completion Time of P1=3+5=8
P3 arrives at time 4 but has to wait for P1 to finish. P3 starts at time 8 and runs for 4 units. Its completion
time is:
Completion Time of P3=8+4=12
16
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Gantt Chart:
17
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now, lets calculate average waiting time and turn around time:
Process Completion Time (CT) Turnaround Time (TAT = CT - AT) Waiting Time (WT = TAT - BT)
P2 3 ms 3 ms 0 ms
P1 8 ms 6 ms 1 ms
P3 12 ms 8 ms 4 ms
Advantages of FCFS
The simplest and basic form of CPU Scheduling algorithm
Every process gets a chance to execute in the order of its arrival. This ensures that no process is arbitrarily
prioritized over another.
Easy to implement, it doesn't require complex data structures.
Since processes are executed in the order they arrive, there’s no risk of starvation
It is well suited for batch systems where the longer time periods for each process are often acceptable.
Disadvantages of FCFS
As it is a Non-preemptive CPU Scheduling Algorithm, FCFS can result in long waiting times, especially if
a long process arrives before a shorter one. This is known as the convoy effect, where shorter processes are
forced to wait behind longer processes, leading to inefficient execution.
The average waiting time in the FCFS is much higher than in the others
Since FCFS processes tasks in the order they arrive, short jobs may have to wait a long time if they arrive
after longer tasks, which leads to poor performance in systems with a mix of long and short tasks.
Processes that are at the end of the queue, have to wait longer to finish.
It is not suitable for time-sharing operating systems where each process should get the same amount of
CPU time.
18
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Estimation Formula
Tn+1=α⋅tn+(1−α)⋅TnTn+1=α⋅tn+(1−α)⋅Tn
Where:
Tn+1: Predicted burst time for the next process.
Tn: Previously predicted burst time.
tn: Actual burst time of the previous process.
α: Smoothing factor (0 ≤ α ≤ 1).
20
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
21
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
As we know,
Turn Around time = Completion time - arrival time
Waiting Time = Turn around time - burst time
Now, lets calculate average waiting time and turn around time:
Process Arrival Burst Completion Turn Around Waiting Time
Time Time Time (CT) Time (TAT) (WT)
(AT) (BT)
P1 0 6 6 6-0 = 6 6-6 = 0
P2 2 8 17 17-2 = 15 15-8 = 7
P3 4 3 9 9-4 = 5 5-3 = 2
Average Turn around time = (6 + 15 + 5)/3 = 8.6 ms
Average waiting time = ( 2 + 0 + 7 )/3 = 9/3 = 3 ms
22
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
26
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Step-by-Step Execution:
At Time 0: Only P1 has arrived. P1 starts execution as it is the only available process, and it will continue
executing till t = 4 because it is a non-preemptive approach.
At Time 4: P1 finishes execution. Both P2 and P3 have arrived. Since P2 has the highest priority (Priority
1), it is selected next.
At Time 6: P2 finishes execution. The only remaining process is P3, so it starts execution.
At Time 12: P3 finishes execution.
Gantt Chart:
31
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
32
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now, lets calculate average waiting time and turn around time:
Arrival Burst Completion Turnaround Time (CT - Waiting Time (TAT -
Process
Time Time Time AT) BT)
P1 0 4 4 4 0
P2 1 2 6 5 3
P3 2 6 12 10 4
Average Turnaround Time = 6.33
Average Waiting Time = 2.33
Step-by-Step Execution:
At Time 0: All processes arrive at the same time. P3 has the highest priority (Priority 3), so it starts
execution.
At Time 6: P3 completes execution. Among the remaining processes, P1 (Priority 2) has a higher priority
than P2, so P1 starts execution.
At Time 13: P1 completes execution. The only remaining process is P2 (Priority 1), so it starts execution.
At Time 17: P2 completes execution. All processes are now finished.
Gant Chart:
33
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
34
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now, lets calculate average waiting time and turn around time:
Arrival Burst Completion Turnaround Time (CT - Waiting Time (TAT -
Process
Time Time Time AT) BT)
P1 0 7 13 13 6
P2 0 4 17 17 13
P3 0 6 6 6 0
Average Turnaround Time = 12
Average Waiting Time = 6.33
Step-by-Step Execution:
At Time 0: Only P1 has arrived, so it starts execution.
At Time 1: P2 arrives with a higher priority (Priority 3) than P1. P1 is preempted, and P2 starts execution.
At Time 5: P2 completes execution. Both P1 and P3 are available. P1 has the higher priority (Priority 2), so
it starts execution.
At Time 10: P1 completes execution. P3 resumes execution to finish its remaining burst time.
At Time 15: P3 completes execution. All processes are now finished.
Gant Chart:
35
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
36
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now, lets calculate average waiting time and turn around time:
Arrival Burst Completion Turnaround Time (CT - Waiting Time (TAT -
Process
Time Time Time AT) BT)
P1 0 6 10 10 4
P2 1 4 5 4 0
P3 2 5 15 13 8
Average Turnaround Time = 9
Average Waiting Time = 4
37
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
How Does It Work? - Imagine you're at a busy restaurant with a group of friends, and there's only one waiter. The
waiter could spend a long time at one table, but instead, he choose to spend exactly one minute at each table before
moving to the next. Similarly, in Round Robin Scheduling, the CPU spends a predetermined slice of time on each
process. If a process hasn't finished its task by the time its slice is up, it's moved to the back of the queue, and the
CPU moves on to the next process.
42
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Step-by-Step Execution:
1. Time 0-2 (P1): P1 runs for 2 ms (total time left: 2 ms).
2. Time 2-4 (P2): P2 runs for 2 ms (total time left: 3 ms).
3. Time 4-6 (P3): P3 runs for 2 ms (total time left: 1 ms).
4. Time 6-8 (P1): P1 finishes its last 2 ms.
5. Time 8-10 (P2): P2 runs for another 2 ms (total time left: 1 ms).
6. Time 10-11 (P3): P3 finishes its last 1 ms.
7. Time 11-12 (P2): P2 finishes its last 1 ms.
Gantt Chart:
43
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
44
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
45
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now, lets calculate average waiting time and turn around time:
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turnaround Time - Burst Time
Processes AT BT CT TAT WT
P1 0 4 8 8-0 = 8 8-4 = 4
P2 0 5 12 12-0 = 12 12-5 = 7
P3 0 3 11 11-0 = 11 11-3 = 8
Average Turn around time = (8 + 12 + 11)/3 = 31/3 = 10.33 ms
Average waiting time = (4 + 7 + 8)/3 = 19/3 = 6.33 ms
Step-by-Step Execution:
Time 0-2 (P1 Executes):
o P1 starts execution as it arrives at 0 ms.
o Runs for 2 ms; remaining burst time = 5 - 2 = 3 ms.
o Ready Queue: [P1].
Time 2-4 (P1 Executes Again):
o P1 continues execution since no other process has arrived yet.
o Runs for 2 ms; remaining burst time = 3 - 2 = 1 ms.
o P2 arrive at 4 ms.
o Ready Queue: [P2, P1].
Time 4-6 (P2 Executes):
o P2 starts execution as it arrives at 4 ms.
o Runs for 2 ms; remaining burst time = 2 - 2 = 0 ms.
o P3 arrive at 5ms
o Ready Queue: [P1, P3].
Time 6-7 (P1 Executes):
o P1 starts execution.
o Runs for 1 ms; remaining burst time = 1 - 1 = 0 ms.
o Ready Queue: [P3].
Time 7-9 (P3 Executes):
o P3 starts execution.
o Remaining burst time = 4 - 2 = 2 ms.
o Ready Queue: [P3].
Time 9-11 (P3 Executes Again):
o P3 resumes execution and runs for 2 ms and complete its execution
o Remaining burst time = 2 - 2 = 0 ms.
o Ready Queue: [].
46
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Gantt Chart:
47
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
48
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now, lets calculate average waiting time and turn around time:
Process Completion Time (CT) Turnaround Time (TAT = CT - AT) Waiting Time (WT = TAT - BT)
P1 7 ms 7 ms 2 ms
P2 6 ms 2 ms 0 ms
P3 11 ms 6 ms 2 ms
Average Turn around time =7+2+6/3=15/3=5ms
Average waiting time = 2+0+2/3=1.33ms
49
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Here, all queues have their own scheduling algorithm, and process is chosen with highest priority. Then it is
executed preemptive or non-preemptively. No process in lower priority queue can get executed until higher process
queue are all empty.
For example, if batch process queue is running and interactive process comes in ready state batch process is
preempted and interactive process is allowed to execute.
50
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Fairness: MLQ scheduling provides a fair allocation of CPU time to different types of processes, based on
their priority and requirements.
Customizable: MLQ scheduling can be customized to meet the specific requirements of different types of
processes.
Ready Queue is divided into separate queues for each class of processes. For example, let us take three different
types of processes System processes, Interactive processes, and Batch Processes. All three processes have their own
queue. Now, look at the below figure.
51
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Batch Processes: Batch processing is generally a technique in the Operating system that collects the
programs and data together in the form of a batch before the processing starts.
All three different type of processes have their own queue. Each queue has its own Scheduling algorithm. For
example, queue 1 and queue 2 use Round Robin while queue 3 can use FCFS to schedule their processes.
Scheduling among the queues: What will happen if all the queues have some processes? Which process should get
the CPU? To determine this Scheduling among the queues is necessary. There are two ways to do so -
1. Fixed priority preemptive scheduling method: Each queue has absolute priority over the lower priority
queue. Let us consider the following priority order queue 1 > queue 2 > queue 3. According to this
algorithm, no process in the batch queue(queue 3) can run unless queues 1 and 2 are empty. If any batch
process (queue 3) is running and any system (queue 1) or Interactive process(queue 2) entered the ready
queue the batch process is preempted.
2. Time slicing: In this method, each queue gets a certain portion of CPU time and can use it to schedule its
own processes. For instance, queue 1 takes 50 percent of CPU time queue 2 takes 30 percent and queue 3
gets 20 percent of CPU time.
Example Problem:
Consider the below table of four processes under Multilevel queue scheduling. Queue number denotes the queue of
the process.
Priority of queue 1 is greater than queue 2. queue 1 uses Round Robin (Time Quantum = 2) and queue 2 uses FCFS.
Gantt Chart
Working
At starting, both queues have process so process in queue 1 (P1, P2) runs first (because of higher priority)
in the round-robin fashion and completes after 7 units
Then process in queue 2 (P3) starts running (as there is no process in queue 1) but while it is running P4
comes in queue 1 and interrupts P3 and start running for 5 seconds and
After its completion P3 takes the CPU and completes its execution.
52
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Multilevel feedback queue scheduling, however, allows a process to move between queues. Multilevel Feedback
QueueScheduling (MLFQ) keeps analyzing the behavior (time of execution) of processes and according to which it
changes its priority.
Now, look at the diagram and explanation below to understand it properly.
53
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Now let us suppose that queues 1 and 2 follow round robin with time quantum 4 and 8 respectively and queue 3
follow FCFS.
Problems in the above implementation: A process in the lower priority queue can suffer from starvation due to
some short processes taking all the CPU time.
Solution: A simple solution can be to boost the priority of all the processes after regular intervals and place them all
in the highest priority queue.
54
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
the process and then predicts its future behavior. This way it tries to run a shorter process first thus
optimizing turnaround time.
MFQS also reduces the response time.
Example 1: Now, let us consider multilevel feedback queue with three queues.
Example 2: Consider a system that has a CPU-bound process, which requires a burst time of 40 seconds. The
multilevel Feed Back Queue scheduling algorithm is used and the queue time quantum '2' seconds and in each level
it is incremented by '5' seconds. Then how many times the process will be interrupted and in which queue the
process will terminate the execution?
Solution:
Process P needs 40 Seconds for total execution.
At Queue 1 it is executed for 2 seconds and then interrupted and shifted to queue 2.
At Queue 2 it is executed for 7 seconds and then interrupted and shifted to queue 3.
At Queue 3 it is executed for 12 seconds and then interrupted and shifted to queue 4.
At Queue 4 it is executed for 17 seconds and then interrupted and shifted to queue 5.
At Queue 5 it executes for 2 seconds and then it completes.
Hence the process is interrupted 4 times and completed on queue 5.
55
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
UNIPROCESSOR SCHEDULING
Uniprocessor scheduling in operating systems refers to the management of processes on a single CPU. It involves
deciding which process gets to use the CPU next, aiming to optimize system performance based on various criteria
like response time, throughput, and resource utilization. Different scheduling algorithms are employed, each with its
own strengths and weaknesses, to achieve these goals.
Here's a breakdown of the key aspects:
1. Types of Scheduling:
Long-term scheduling: Determines which processes are admitted to the system for execution.
Medium-term scheduling: Manages the swapping of processes in and out of main memory to optimize
resource usage.
Short-term scheduling (CPU scheduling or dispatcher): The most frequent type, deciding which process
gets to use the CPU next.
I/O scheduling: Handles requests for input/output operations.
2. Scheduling Algorithms:
First-Come, First-Served (FCFS):
Processes are executed in the order they arrive. Simple but can lead to longer wait times for shorter processes if a
long process arrives first.
Round Robin (RR):
Each process gets a fixed time slice (quantum) of CPU time. If a process doesn't finish within its time slice, it's
moved to the back of the queue. Good for time-sharing but can have high overhead if the time slice is too short.
Shortest Process Next (SPN):
The process with the shortest estimated execution time is selected next. Efficient in terms of minimizing average
waiting time but requires accurate estimation of execution time and can lead to starvation of longer processes.
Shortest Remaining Time (SRT):
A preemptive version of SPN, where the process with the shortest remaining execution time is selected. Can preempt
a running process if a new process with a shorter remaining time arrives.
Highest Response Ratio Next (HRRN):
Calculates a ratio based on waiting time and estimated execution time to favor processes that have been waiting
longer or have shorter execution times.
Priority Scheduling:
Processes are assigned priorities, and the highest priority process is selected for execution.
Feedback Queues:
Processes can move between different priority queues based on their behavior, allowing for dynamic priority
adjustments.
56
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
4. Goals of Scheduling:
Maximize CPU utilization: Keep the CPU busy as much as possible.
Maximize throughput: Process as many jobs as possible in a given time.
Minimize response time: Provide quick responses to user requests.
Minimize waiting time: Reduce the amount of time processes spend waiting for the CPU.
Fairness: Ensure that all processes get a reasonable share of the CPU time.
Uniprocessor scheduling is a fundamental aspect of operating systems, and the choice of scheduling algorithm
significantly impacts system performance and user experience.
MULTIPLE-PROCESSOR SCHEDULING
In systems containing more than one processor, multiple-processor scheduling addresses task allocations to multiple
CPUs. This will involve higher throughputs since several tasks can be processed concurrently in separate processors.
It would also involve the determination of which CPU handles a particular task and balancing loads between
available processors.
1. Processor Affinity
Processor Affinity means a processes has an affinity for the processor on which it is currently running. When a
process runs on a specific processor there are certain effects on the cache memory. The data most recently accessed
by the process populate the cache for the processor and as a result successive memory access by the process are
often satisfied in the cache memory. Now if the process migrates to another processor, the contents of the cache
57
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
memory must be invalidated for the first processor and the cache for the second processor must be repopulated.
Because of the high cost of invalidating and repopulating caches, most of the SMP(symmetric multiprocessing)
systems try to avoid migration of processes from one processor to another and try to keep a process running on the
same processor. This is known as PROCESSOR AFFINITY. There are two types of processor affinity:
Soft Affinity: When an operating system has a policy of attempting to keep a process running on the same
processor but not guaranteeing it will do so, this situation is called soft affinity.
Hard Affinity: Hard Affinity allows a process to specify a subset of processors on which it may run. Some
systems such as Linux implements soft affinity but also provide some system calls
like sched_setaffinity() that supports hard affinity.
2. Load Balancing
Load Balancing is the phenomena which keeps the workload evenly distributed across all processors in
an SMP system. Load balancing is necessary only on systems where each processor has its own private queue of
process which are eligible to execute. Load balancing is unnecessary because once a processor becomes idle it
immediately extracts a runnable process from the common run queue. On SMP(symmetric multiprocessing), it is
important to keep the workload balanced among all processors to fully utilize the benefits of having more than one
processor else one or more processor will sit idle while other processors have high workloads along with lists of
processors awaiting the CPU. There are two general approaches to load balancing :
Push Migration: In push migration a task routinely checks the load on each processor and if it finds an
imbalance then it evenly distributes load on each processors by moving the processes from overloaded to
idle or less busy processors.
Pull Migration: Pull Migration occurs when an idle processor pulls a waiting task from a busy processor
for its execution.
3. Multicore Processors
In multicore processors multiple processor cores are places on the same physical chip. Each core has a register set
to maintain its architectural state and thus appears to the operating system as a separate physical processor. SMP
systems that use multicore processors are faster and consume less power than systems in which each processor has
its own physical chip. However multicore processors may complicate the scheduling problems. When processor
accesses memory then it spends a significant amount of time waiting for the data to become available. This situation
is called MEMORY STALL. It occurs for various reasons such as cache miss, which is accessing the data that is
not in the cache memory. In such cases the processor can spend upto fifty percent of its time waiting for data to
become available from the memory. To solve this problem recent hardware designs have implemented multithreaded
processor cores in which two or more hardware threads are assigned to each core. Therefore if one thread stalls
while waiting for the memory, core can switch to another thread. There are two ways to multithread a processor :
Coarse-Grained Multithreading: In coarse grained multithreading a thread executes on a processor until
a long latency event such as a memory stall occurs, because of the delay caused by the long latency event,
the processor must switch to another thread to begin execution. The cost of switching between threads is
high as the instruction pipeline must be terminated before the other thread can begin execution on the
processor core. Once this new thread begins execution it begins filling the pipeline with its instructions.
Fine-Grained Multithreading: This multithreading switches between threads at a much finer level mainly
at the boundary of an instruction cycle. The architectural design of fine grained systems include logic for
thread switching and as a result the cost of switching between threads is small.
58
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
environments have one host operating system and many guest operating systems. The host operating system creates
and manages the virtual machines. Each virtual machine has a guest operating system installed and applications run
within that guest. Each guest operating system may be assigned for specific use cases,applications or users including
time sharing or even real-time operation. Any guest operating-system scheduling algorithm that assumes a certain
amount of progress in a given amount of time will be negatively impacted by the virtualization. A time sharing
operating system tries to allot 100 milliseconds to each time slice to give users a reasonable response time. A given
100 millisecond time slice may take much more than 100 milliseconds of virtual CPU time. Depending on how busy
the system is, the time slice may take a second or more which results in a very poor response time for users logged
into that virtual machine. The net effect of such scheduling layering is that individual virtualized operating systems
receive only a portion of the available CPU cycles, even though they believe they are receiving all cycles and that
they are scheduling all of those [Link], the time-of-day clocks in virtual machines are incorrect because
timers take no longer to trigger than they would on dedicated CPU's. Virtualizations can thus undo the good
scheduling-algorithm efforts of the operating systems within virtual machines.
59
Principles of Operating System (CS303) Unit 2 Dr. Anum Kamal
Gaming ? In modern gaming systems, multiple processors are utilized to handle complex graphics
rendering, physics simulations, and AI computations. Effective scheduling ensures smooth gameplay,
minimizes lag, and maximizes the utilization of available processing power to deliver an immersive gaming
experience.
Embedded Systems ? Embedded systems with multiple processors, such as automotive systems, IoT
devices, or robotics, require efficient scheduling to ensure a timely response, real-time control, and
coordination of various tasks running on different processors. Scheduling algorithms prioritize critical tasks
and manage resource allocation to meet system requirements.
Extra
he major differences between single processor and multi-processor are as follows −
The name itself is saying that the single For this also the name itself indicates that the
Description processor system contains only one multiprocessor system contains two or more
processor for processing. processors for processing.
Single processor system is less reliable Multiprocessor system is more reliable because
Reliability of the
because failure in one processor will result failure of one processor does not halt the entire
system
in failure of the entire system. system but only speed will be slowed down.
60