Module 2-1
Module 2-1
Module 2-1
MODULE 2
MULTITHREADED PROGRAMMING
A thread is a basic unit of CPUutilization.
It consists of
thread ID
PC
register-set and
stack.
It shares with other threads belonging to the same process its code-section &data-section.
A traditional (or heavy weight) process has a single thread of control.
If a process has multiple threads of control, it can perform more than one task at a time.
such a process is called multithreaded process
1
Operating Systems 18CS43
2. In some situations, a single application may be required to perform several similar tasks. For
ex: A web-server may create a separate thread for each client requests. This allows the server to
service several concurrent requests.
Resource Sharing by default, threads share the memory (and resources) of the
process to which they belong. Thus, an application is allowed to have several
different threads of activity within the same address-space.
Economy Allocating memory and resources for process-creation is costly. Thus, it is
more economical to create and context-switch threads.
Utilization of Multiprocessor Architectures in a multiprocessor architecture,
threads may be running in parallel on different processors. Thus, parallelism will be
increased.
MULTITHREADING MODELS
2
Operating Systems 18CS43
Many-to-One Model
Many user-level threads are mapped to one kernel thread.
Advantages:
Thread management is done by the thread library in user space, so it is efficient.
Disadvantages:
The entire process will block if a thread makes a blocking system-call.
Multiple threads are unable to run in parallel on multiprocessors.
For example:
Solaris green threads
GNU portable threads.
One-to-One Model
Each user thread is mapped to a kernel thread.
Advantages:
It provides more concurrency by allowing another thread to run when a thread
makes a blocking system-call.
Multiple threads can run in parallel on multiprocessors.
Disadvantage:
Creating a user thread requires creating the corresponding kernel thread.
For example:
Windows NT/XP/2000, Linux
3
Operating Systems 18CS43
Many-to-Many Model
Many user-level threads are multiplexed to a smaller number of kernel threads.
Advantages:
Developers can create as many user threads as necessary
The kernel threads can run in parallel on a multiprocessor.
When a thread performs a blocking system-call, kernel can schedule another thread
for execution.
Two Level Model
A variation on the many-to-many model is the two level-model
Similar to M: N, except that it allows a user thread to be bound to kernel thread.
for example:
HP-UX
Tru64 UNIX
Thread Libraries
It provides the programmer with an API for the creation and management of threads.
4
Operating Systems 18CS43
3. Java.
Pthreads
This is a POSIX standard API for thread creation and synchronization.
This is a specification for thread-behavior, not an implementation.
OS designers may implement the specification in any way they wish.
Commonly used in: UNIX and Solaris.
5
Operating Systems 18CS43
Win32 threads
Implements the one-to-one mapping
Each thread contains
A threaded
Register set
Separate user and kernel stacks
Private data storage area
The register set, stacks, and private storage area are known as the context of the
threads The primary data structures of a thread include:
ETHREAD (executive thread block)
KTHREAD (kernel thread block)
TEB (thread environment block)
Java Threads
Threads are the basic model of program-execution in
Java program and
Java language.
The API provides a rich set of features for the creation and management of threads.
All Java programs comprise at least a single thread of control.
Two techniques for creating threads:
1. Create a new class that is derived from the Thread class and override its run () method.
2. Define a class that implements the Runnable interface. The Runnable interface is
defined as follows:
6
Operating Systems 18CS43
THREADING ISSUES
7
Operating Systems 18CS43
THREAD POOLS
The basic idea is to
create a no. of threads at process-startup and
place the threads into a pool (where they sit and wait for work).
Procedure:
1. When a server receives a request, it awakens a thread from the pool.
2. If any thread is available, the request is passed to it for service.
3. Once the service is completed, the thread returns to the pool.
Advantages:
Servicing a request with an existing thread is usually faster than waiting to
create a thread.
The pool limits the no. of threads that exist at any one point.
No. of threads in the pool can be based on actors such as
no. of CPUs
amount of memory and
expected no. of concurrent client-requests.
SCHEDULER ACTIVATIONS
Both M:M and Two-level models require communication to maintain the
appropriate number of kernel threads allocated to the application.
Scheduler activations provide up calls a communication mechanism from
thekernel to the thread library
This communication allows an application to maintain the correct number kernel
threads
One scheme for communication between the user-thread library and the kernel is
known as scheduler activation.
9
Operating Systems
PROCESS SCHEDULING
Basic Concepts
In a single-processor system,
Only one process may run at a time.
Other processes must wait until the CPU is rescheduled.
Objective of multiprogramming:
To have some process running at all times, in order to maximize CPU
utilization.
10
Operating Systems
CPU Scheduler
This scheduler
selects a waiting-process from the ready-queue and
allocates CPU to the waiting-process.
The ready-queue could be a FIFO, priority queue, tree and list.
The records in the queues are generally process control blocks (PCBs) of the processes.
CPU Scheduling
Four situations under which CPU scheduling decisions take place:
1. When a process switches from the running state to the waiting state. For ex; I/O
request.
2. When a process switches from the running state to the ready state. For ex:
when an interrupt occurs.
3. When a process switches from the waiting state to the ready state. For ex:
completion of I/O.
4. When a process terminates.
Scheduling under 1 and 4 is non- preemptive. Scheduling under 2 and 3 is preemptive.
Preemptive Scheduling
This is driven by the idea of prioritized computation.
Processes that are runnable may be temporarily suspended
Disadvantages:
1. Incurs a cost associated with access to shared-data.
2. Affects the design of the OSkernel.
11
Operating Systems
Dispatcher
It gives control of the CPU to the process selected by the short-term scheduler.
The function involves:
1. Switching context
2. Switching to user mode&
3. Jumping to the proper location in the user program to restart that program
It should be as fast as possible, since it is invoked during every process switch.
Dispatch latency means the time taken by the dispatcher to
stop one process and
start another running.
SCHEDULING CRITERIA:
In choosing which algorithm to use in a particular situation, depends upon the properties
of the various algorithms. Many criteria have been suggested for comparing CPU-
scheduling algorithms. The criteria include the following:
1. 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 used
system).
2. Throughput: If the CPU is busy executing processes, then work is being done.
One measure of work is the number of processes that are completed per time unit,
called throughput. For long processes, this rate may be one process per hour; for
short transactions, it may be ten processes per second.
3. Turnaround time. This is the important criterion which tells how long it takes to
execute that process. The interval from the time of submission of a process to the
time of completion is the turnaround time. 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/0.
4. Waiting time: The CPU-scheduling algorithm does not affect the amount of time
during which a process executes or does I/0, it affects only the amount of time that
a process spends waiting in the ready queue. Waiting time is the sum of the periods
spent waiting in the ready queue.
5. Response time: In an interactive system, turnaround time may not be the best
criterion. Often, a process can produce some output fairly early and can continue
computing new results while previous results are being output to the user. Thus,
another measure is the time from the submission of a request until the first response
is produced. This measure, called response time, is the time it takes to start
responding, not the time it takes to output the response. The turnaround time is
generally limited by the speed of the output device.
12
Operating Systems
SCHEDULING ALGORITHMS
CPU scheduling deals with the problem of deciding which of the processes in
the ready-queue is to be allocated the CPU.
Following are some scheduling algorithms:
1. FCFS scheduling (First Come First Served)
2. Round Robin scheduling
3. SJF scheduling (Shortest Job First)
4. SRT scheduling
5. Priority scheduling
6. Multilevel Queue scheduling and
7. Multilevel Feedback Queue scheduling
FCFS Scheduling
The process that requests the CPU first is allocated the CPU first.
The implementation is easily done using a FIFO queue.
Procedure:
1. When a process enters the ready-queue, its PCB is linked onto the tail of
the queue.
2. When the CPU is free, the CPU is allocated to the process at the queue’s head.
3. The running process is then removed from the queue.
Advantage:
1. Code is simple to write & understand.
Disadvantages:
1. Convoy effect: All other processes wait for one big process to get off the CPU.
2. Non-preemptive (a process keeps the CPU until it releases it).
3. Not good for time-sharing systems.
4. The average waiting time is generally not minimal.
Example: Suppose that the processes arrive in the order P1, P2,P3.
The Gantt Chart for the schedule is as follows:
Suppose that the processes arrive in the order P2, P3, P1.
13
Operating Systems
SJF Scheduling
The CPU is assigned to the process that has the smallest next CPU burst.
If two processes have the same length CPU burst, FCFS scheduling is used to break
the tie.
For long-term scheduling in a batch system, we can use the process time limit
specified by the user, as the ‘length’
SJF can't be implemented at the level of short-term scheduling, because there is
no way to know the length of the next CPU burst
Advantage:
1. The SJF is optimal, i.e. it gives the minimum average waiting time for a
given set of processes.
Disadvantage:
1. Determining the length of the next CPU burst.
14
Operating Systems
preemptive SJF/SRTF: Consider the following set of processes, with the length
Priority Scheduling
A priority is associated with each process.
The CPU is allocated to the process with the highest priority.
Equal-priority processes are scheduled in FCFS order.
Priorities can be defined either internally or externally.
1. Internally-defined priorities.
Use some measurable quantity to compute the priority of a process.
For example: time limits, memory requirements, no. f open files.
2. Externally-defined priorities.
Set by criteria that are external to the OS for
example:
importance of the process, political factors
Priority scheduling can be either preemptive or non-preemptive.
1.Preemptive
The CPU is preempted if the priority of the newly arrived process is
higher than the priority of the currently running process.
2. Non Preemptive
The new process is put at the head of the ready-queue
Advantage:
Higher priority processes can be executed first.
Disadvantage:
Indefinite blocking, where low-priority processes are left waiting
indefinitely for CPU. Solution: Aging is a technique of increasing
priority of processes that wait in system for a long time.
15
Operating Systems
17
Operating Systems
19
Operating Systems
20
Operating Systems
Symmetric Multithreading
The basic idea:
1. Create multiple logical processors on the same physical processor.
2. Present a view of several logical processors to the OS.
Each logical processor has its own architecture state, which includes general-
purpose and machine-state registers.
Each logical processor is responsible for its own interrupt handling.
SMT is a feature provided in hardware, not software.
THREAD SCHEDULING
On OSs, it is kernel-level threads but not processes that are being scheduled by the OS.
User-level threads are managed by a thread library, and the kernel is unaware of them.
To run on a CPU, user-level threads must be mapped to an associated kernel-level
thread.
Contention Scope
Two approaches:
1. Process-Contention scope
On systems implementing the many-to-one and many-to-many models, the
thread library schedules user-level threads to run on an available LWP.
Competition for the CPU takes place among threads belonging to thesame
process.
2. System-Contention scope
The process of deciding which kernel thread to schedule on the CPU.
Competition for the CPU takes place among all threads in the system.
Systems using the one-to-one model schedule threads using only SCS.
Pthread Scheduling
Pthread API that allows specifying either PCS or SCS during thread creation.
Pthreads identifies the following contention scope values:
1. PTHREAD_SCOPEJPROCESS schedules threads using PCS scheduling.
2. PTHREAD-SCOPE_SYSTEM schedules threads using SC scheduling.
Pthread IPC provides following two functions for getting and setting the contention
scope policy:
1. pthread_attr_setscope(pthread_attr_t *attr, intscope)
2. pthread_attr_getscope(pthread_attr_t *attr, int*scop)
21