0% found this document useful (0 votes)
8 views

OS UNIT-II MATERIAL

This document covers key concepts in operating systems, focusing on processes, threads, and CPU scheduling. It explains the process lifecycle, including creation, termination, and interprocess communication, as well as the advantages of multithreading and various scheduling strategies. Additionally, it discusses the differences between long-term, short-term, and medium-term schedulers, along with context switching and models of interprocess communication.

Uploaded by

csewarriors2k23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

OS UNIT-II MATERIAL

This document covers key concepts in operating systems, focusing on processes, threads, and CPU scheduling. It explains the process lifecycle, including creation, termination, and interprocess communication, as well as the advantages of multithreading and various scheduling strategies. Additionally, it discusses the differences between long-term, short-term, and medium-term schedulers, along with context switching and models of interprocess communication.

Uploaded by

csewarriors2k23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

OPERATING SYSTEMS

UNIT-II
Syllabus: Processes: Process Concept, Process scheduling, Operations on processes, Inter-process
communication. Threads and Concurrency: Multithreading models, Thread libraries, Threading issues.
CPU Scheduling: Basic concepts, Scheduling criteria, Scheduling algorithms, Multiple processor scheduling.

2.1. PROCESS:
A process can be thought of as a program in execution. 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. A process is the unit of work in most
systems. Systems consist of a collection of processes: Operating-system processes execute system
code, and user processes execute user code. All these processes may execute concurrently.
2.1.1. PROCESS CONCEPT:
Process – a program in execution; process execution must progress in sequential fashion. No parallel
execution of instructions of a single process
A process includes:
o The program code, also called text section
o Current activity including program counter, processor
registers
o Stack containing temporary data
 Function parameters, return addresses, local
variables
o Data section containing global variables
o Heap containing memory dynamically allocated during run
time
 Program is passive entity stored on disk (executable file); process
is active
o Program becomes process when an executable file is
loaded into memory
 Execution of program started via GUI mouse clicks, command line entry of its name, etc.
 One program can be several processes
o Consider multiple users executing the same program
 Compiler
 Text editor
2.1.2. PROCESS STATE:
As a process executes, it changes state. The state of a process is defined in part by the current activity
of that process. Each process may be in one of the following states:
 new: The process is being created
 running: Instructions are being executed
 waiting: The process is waiting for some event to occur
 ready: The process is waiting to be assigned to a processor
 terminated: The process has finished execution
Diagram of Process State
2.1.3. PROCESS CONTROL BLOCK (PCB):
• A process control block (PCB) is a data structure used by
computer operating systems to store all the information about
a process. It is also known as a process descriptor.
• Information associated with each process (also called task
control block)
• Process state – running, waiting, etc.
• Program counter – location of instruction to next execute
• CPU registers – contents of all process-centric registers
• CPU scheduling information- priorities, scheduling queue
pointers
• Memory-management information – memory allocated to the
process
• Accounting information – CPU used, clock time elapsed since
start, time limits
• I/O status information – I/O devices allocated to process, list of
open files
2.1.4. CPU SWITCH FROM PROCESS TO PROCESS:
2.1.5. Threads:
A thread is a lightweight process that can be managed independently by a
scheduler. It improves the application performance using parallelism. A thread
shares information like data segment, code segment, files etc. with its peer
threads while it contains its own registers, stack, counter etc.
A thread is a basic unit of CPU utilization, consisting of a program counter, a
stack, and a set of registers, ( and a thread ID. ) Traditional ( heavyweight )
processes have a single thread of control - There is one program counter, and
one sequence of instructions that can be carried out at any given time. As shown in below Figure,
multi-threaded applications have multiple threads within a single process, each having their own
program counter, stack and set of registers, but sharing common code, data, and certain structures
such as open files.

2.1.6. ADVANTAGES OF THREAD:


 Threads minimize the context switching time (is a time spent between two processes (i.e.,
bringing a waiting process into execution and sending an executing process into a waiting
for state).
 Use of threads provides concurrency within a process.
 Efficient communication.
 It is more economical to create and context switch threads.
 Threads are very useful in modern programming whenever a process has multiple tasks
to perform independently of the others.
 This is particularly true when one of the tasks may block, and it is desired to allow the
other tasks to proceed without blocking.
 For example, in a word processor, a background thread may check spelling and grammar
while a foreground thread processes user input (keystrokes), while yet a third thread loads
images from the hard drive, and a fourth does periodic automatic backups of the file being
edited.
 Another example is a web server - Multiple threads allow for multiple requests to be
satisfied simultaneously, without having to service requests sequentially or to fork off
separate processes for every incoming request.

2.2. PROCESS SCHEDULING:


The process scheduling is the activity of the process manager that handles the removal of the running
process from the CPU and the selection of another process on the basis of a particular strategy. The
objective of multiprogramming is to have some process running at all times, to maximize CPU
utilization. Process scheduling is an essential part of a Multiprogramming operating systems. Such
operating systems allow more than one process to be loaded into the executable memory at a time.
2.2.1. CATEGORIES OF SCHEDULING:
There are two categories of scheduling:
 Non-preemptive: Here the resource can’t be taken from a
process until the process completes execution. The switching
of resources occurs when the running process terminates and
moves to a waiting state.
 Preemptive: Here the OS allocates the resources to a process
for a fixed amount of time. During resource allocation, the
process switches from running state to ready state or from
waiting state to ready state. This switching occurs as the CPU
may give priority to other processes and replace the process
with higher priority with the running process.

2.2.2. PROCESS SCHEDULING QUEUES:


 The OS maintains all Process Control Blocks (PCBs) in
Process Scheduling Queues.
 The OS maintains a separate queue for each of the process states and PCBs of all
processes in the same execution state are placed in the same queue.
 When the state of a process is changed, its PCB is unlinked from its current queue and
moved to its new state queue.
 The Operating System maintains the following important process scheduling queues −
 Job queue − This queue keeps all the
processes in the system.
 Ready queue − This queue keeps a set of all
processes residing in main memory, ready
and waiting to execute. A new process is
always put in this queue.
 Device queues − The processes which are
blocked due to unavailability of an I/O
device constitute this queue.
The stages a process goes through are:
 A new process first goes in the Ready queue, where it waits for execution or to be
dispatched.
 The CPU gets allocated to one of the processes for execution.
 The process issues an I/O request, after which an OS places it in the I/O queue.
 The process then creates a new subprocess and waits for its termination.
 If removed forcefully, the process creates an interrupt. Thus, once this interrupt
completes, the process goes back to the ready queue.
2.2.3. SCHEDULERS:
Schedulers are special system software which handle process scheduling in various ways. Their main
task is to select the jobs to be submitted into the system and to decide which process to run.
Schedulers are of three types −
• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler
2.2.3.1. LONG TERM SCHEDULER:
 It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing.
 It selects processes from the queue and loads them into memory for execution.
 Process loads into the memory for CPU scheduling.
 The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O
bound and processor bound.
o I/O-bound process – spends more time doing I/O than computations, many short CPU
bursts
o CPU-bound process – spends more time doing computations; few very long CPU
bursts
 It also controls the degree of multiprogramming.
 Degree of multiprogramming describes the maximum number of processes that a single-
processor system can accommodate efficiently.
2.2.3.2. SHORT TERM SCHEDULER:
 It is also called as CPU scheduler.
 Its main objective is to increase system performance in accordance with the chosen set of
criteria.
 It is the change of ready state to running state of the process.
 CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
 Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next.
 Short-term schedulers are faster than long-term schedulers.
2.2.3.3. MEDIUM TERM SCHEDULER:
 Medium-term scheduling is a part of swapping. It removes the processes from the
memory.
 It reduces the degree of multiprogramming.
 The medium-term scheduler is in-charge of handling the swapped out-processes.
 A running process may become suspended if it makes an I/O request. A suspended
processes cannot make any progress towards completion. In this condition, to remove the
process from memory and make space for other processes, the suspended process is
moved to the secondary storage. This process is called swapping, and the process is said
to be swapped out or rolled out. Swapping may be necessary to improve the process mix.
 The task of moving from main memory to secondary memory is called swapping out. The
task of moving back a swapped-out process from secondary memory to main memory is
known as swapping in.

COMPARISON AMONG SCHEDULER:


S.N. Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler

1 It is a job scheduler It is a CPU scheduler It is a process swapping


scheduler.
2 Speed is lesser than short Speed is fastest among Speed is in between both
term scheduler other two short- and long-term
scheduler.
3 It controls the degree of It provides lesser control It reduces the degree of
multiprogramming over degree of multiprogramming.
multiprogramming
4 It is almost absent or It is also minimal in time It is a part of Time-sharing
minimal in time sharing system systems.
sharing system
5 It selects processes from It selects those processes It can re-introduce the
pool and loads them which are ready to process into memory
into memory for execute and execution can be
execution continued.
2.2.4. CONTEXT SWITCHING:
When CPU switches to another process, the system must save the
state of the old process and load the saved state for the new
process via a context switch. A context switching is the mechanism
to store and restore the state or context of a CPU in Process
Control block so that a process execution can be resumed from the
same point at a later time. Using this technique, a context switcher
enables multiple processes to share a single CPU. Context
switching is an essential part of a multitasking operating system
features. When the scheduler switches the CPU from executing one
process to execute another, the state from the current running
process is stored into the process control block. After this, the state
for the process to run next is loaded from its own PCB and used to
set the PC, registers, etc. At that point, the second process can start
executing. Context switches are computationally intensive since
register and memory state must be saved and restored. To avoid
the amount of context switching time, some hardware systems
employ two or more sets of processor registers.

2.3. OPERATIONS ON PROCESSES:


The processes in most systems can execute concurrently, and they may be created and deleted
dynamically. Thus, these systems must provide a mechanism for process creation and termination.
2.3.1. PROCESS CREATION:
A process may create several new processes, via a create-process system call, during the course of
execution. The creating process is called a parent process, and the new processes are called the
children of that process. Each of these new processes may in turn create other processes, forming a
tree of processes. Most operating systems identify processes according to a unique process identifier
(or pid),
In general, a process will need certain resources (CPU time, memory, files, and I/O devices) to
accomplish its task.
 Resource sharing options
o Parent and children share all resources
o Children share subset of parent’s resources
o Parent and child share no resources
 Execution options
o Parent and children execute concurrently
o Parent waits until children terminate
init
pid = 1

Figure: A Tree of Processes in Linux

2.3.2. PROCESS TERMINATION:


A process terminates when it finishes executing its final statement and asks the operating system to
delete it by using the exit ( ) system call. At that point, the process may return a status value (typically
an integer) to its parent process. All the resources of the process—including physical and virtual
memory, open files and I/O buffers—are de-allocated by the operating system.
A parent may terminate the execution of one of its children for a variety of reasons, such as these:
• The child has exceeded its usage of some of the resources that it has been allocated.
• The task assigned to the child is no longer required.
• The parent is exiting, and the operating system does not allow a child to continue if its parent
terminates.

2.4. INTERPROCESS COMMUNICATION:


Interprocess communication is the mechanism provided by the operating system that allows
processes to communicate with each other. This communication could involve a process letting
another process know that some event has occurred or the transferring of data from one process to
another.

Processes executing concurrently in the operating system may be either independent processes or
cooperating processes.
A process is independent if it cannot affect or be affected by the other processes executing in the
system. Any process that does not share data with any other process is independent.
A process is cooperating if it can affect or be affected by the other processes executing in the system.
Clearly, any process that shares data with other processes is a cooperating process.
There are several reasons for providing an environment that allows process cooperation:
 Information sharing. Since several users may be interested in the same piece of information (for
instance, a shared file), we must provide an environment to allow concurrent access to such
information.
 Computation speedup. If we want a particular task to run faster, we must break it into subtasks,
each of which will be executing in parallel with the others. Notice that such a speedup can be
achieved only if the computer has multiple processing cores.
 Modularity. We may want to construct the system in a modular fashion, dividing the system
functions into separate processes or threads
 Convenience. Even an individual user may work on many tasks at the same time. For instance, a
user may be editing, printing, and compiling in parallel.
2.4.1. DIFFERENT MODELS OF INTERPROCESS COMMUNICATION:
a) Shared Memory Model
b) Message Passing Model
a) SHARED MEMORY MODEL:
Shared memory is the memory that can be simultaneously accessed by multiple processes. This is
done so that the processes can communicate with each other. All POSIX systems, as well as Windows
operating systems use shared memory.
Advantages of Shared Memory Model
 Memory communication is faster on the shared memory model as compared to the message
passing model on the same machine.
Disadvantages of Shared Memory Model
 All the processes that use the shared memory model need to make sure that they are not writing
to the same memory location.
 Shared memory model may create problems such as synchronization and memory protection that
need to be addressed.
b) MESSAGE PASSING MODEL:
Multiple processes can read and write data to the message queue without being connected to each
other. Messages are stored on the queue until their recipient retrieves them. Message queues are
quite useful for Interprocess communication and are used by most operating systems.
Advantage of Messaging Passing Model
 The message passing model is much easier to implement than the shared memory model.
Disadvantage of Messaging Passing Model
 The message passing model has slower communication than the shared memory model because
the connection setup takes time.

(a) Message passing. (b) shared memory.


2.4.2. SHARED MEMORY METHOD:
PRODUCER-CONSUMER PROBLEM:
• There are two processes: Producer and Consumer.
• The producer produces some items and the Consumer consumes that item.
• The two processes share a common space or memory location known as a buffer where the
item produced by the Producer is stored and from which the Consumer consumes the item if
needed.
• There are two versions of this problem:
• the first one is known as the unbounded buffer problem in which the Producer can
keep on producing items and there is no limit on the size of the buffer
• the second one is known as the bounded buffer problem in which the Producer can
produce up to a certain number of items before it starts waiting for Consumer to
consume it.
2.4.3. BOUNDED BUFFER PROBLEM (CONSUMER PRODUCER PROBLEM):
• First, the Producer and the Consumer will share some common memory, then the producer
will start producing items.
• If the total produced item is equal to the size of the buffer, the producer will wait to get it
consumed by the Consumer.
• Similarly, the consumer will first check for the availability of the item.
• If no item is available, the Consumer will wait for the Producer to produce it.
• If there are items available, Consumer will consume them.

The pseudo-code to demonstrate is provided below:


Shared Data between the two Processes
#define buff_max 25
#define mod %
struct item{
// different member of the produced data
// or consumed data

}
// An array is needed for holding the items.
// This is the shared place which will be
// access by both process
// item shared_buff [ buff_max ];
// Two variables which will keep track of
// the indexes of the items produced by producer
// and consumer The free index points to
// the next free index. The full index points to
// the first full index.
int free_index = 0;
int full_index = 0;
Producer Process Code
item nextProduced;

while(1){

// check if there is no space


// for production.
// if so keep waiting.
while((free_index+1) mod buff_max == full_index);

shared_buff[free_index] = nextProduced;
free_index = (free_index + 1) mod buff_max;
}
Consumer Process Code
item nextConsumed;
while(1){

// check if there is an available


// item for consumption.
// if not keep on waiting for
// get them produced.
while((free_index == full_index);
nextConsumed = shared_buff[full_index];
full_index = (full_index + 1) mod buff_max;
}
In the above code, the Producer will start producing again when the (free_index+1) mod buff max will
be free because if it it not free, this implies that there are still items that can be consumed by the
Consumer so there is no need to produce more. Similarly, if free index and full index point to the same
index, this implies that there are no items to consume.
2.4.4. INTERPROCESS COMMUNICATION – MESSAGE PASSING:
• Message system allows processes to communicate with one another without the need to resort
to shared data.
• Services are provided as ordinary user processes operate outside the kernel.
• Communication among the user processes is accomplished through the passing of messages.
• An IPC facility provides at least two operations: send (message) and receive (message).
• Messages sent by a process can be of either fixed or variable size.
• If processes P and Q want to communicate, they must send messages to send and receive from
each other; a communication link must exist between them. There are several methods for logical
implementation of a link as follows:
• Direct or indirect communication
• Symmetric or asymmetric communication
• Synchronous or asynchronous communication
• Automatic or explicit buffering
2.4.4.1. Direct Communication:
 Processes must name each other explicitly:
o send (P, message) – send a message to process P
o receive (Q, message) – receive a message from process Q
 Properties of communication link
o Links are established automatically
o A link is associated with exactly one pair of communicating processes
o Between each pair there exists exactly one link
o The link may be unidirectional, but is usually bi-directional
Indirect Communication:
 The messages are sent to and received from mailboxes, or ports.
 Each mailbox has a unique identification.
 Two processes can communicate only if they share a mailbox.
 The send and receive primitives are defined as follows:
o Send (A, message)- Send a message to mailbox A.
o receive (A, message)– Receive a message from mailbox A.
In this scheme, a communication link has the following properties:
 A link is established between a pair of processes only if both members of the pair have a
shared mailbox.
 A link may be associated with more than two processes.
 A number of different links may exist between each pair of communicating processes, with
each link corresponding to one mailbox.
 If processes P1, P2 and P3 all share mailbox A. Process P1 sends a message to A, while P2 and
P3 each execute and receive from A.
 The process to receive the message depends on one of the scheme that:
o Allows a link to be associated with at most two processes.
o Allows utmost one process at a time to execute a receive operation.
o Allows the system to select arbitrarily which process will receive the message (that is
either P2 or P3, but not both, will receive the message). The system may identify the
receiver to the sender.
2.4.4.2. Symmetric or asymmetric communication:
 In Symmetric Communication both the sender process and receiver process must name the other
to communicate.
 In Asymmetric Communication only the sender names the recipient; the recipient is not required
to name the sender. In this, the sender and receiver primitives are as follows.
o send (P, message)– Send a message to process P.
o receive (id, message)– Receive a message from any process; the variable id is set to the
name of the process with which communication has taken place.
2.4.4.3. Synchronization:
 The send and receive system calls are used to communicate between processes but there are
different design options for implementing these calls.
 Message passing may be either blocking or non-blocking - also known as synchronous and
asynchronous.
 Blocking is considered synchronous
o Blocking send -- the sender is blocked until the message is received
o Blocking receive -- the receiver is blocked until a message is available
 Non-blocking is considered asynchronous
o Non-blocking send -- the sender sends the message and continue
o Non-blocking receive -- the receiver receives:
 A valid message, or
 Null message
2.4.4.4. Buffering:
 During direct or indirect communication, messages exchanged between communicating processes
reside in a temporary queue.
 which are implemented in the following three ways:
o Zero capacity: The queue has maximum length 0; thus, the link cannot have any message
waiting in it. In this case, the sender must block until the recipient receives the message.
This is referred to as no buffering.
o Bounded capacity: The queue has finite length n; thus, at most n messages can reside in
it. If the queue is not full when a new message is sent, the latter is placed in the queue
(either the message is copied or a pointer to the message is kept ), and the sender can
continue execution without waiting. If the link is full, the sender must block until space is
available in the queue. This is referred to as auto buffering
o Unbounded capacity: The queue has potentially infinite length; thus, any number of
messages can wait in it. The sender never blocks. This also referred to as auto buffering.
o Zero capacity is known as explicit buffering and the other two are known as automatic
buffering.

2.5. MULTITHREADED PROGRAMMING


2.6.1 Thread:
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set,
and a stack. It shares with other threads belonging to the same process its code section, data section,
and other operating-system resources, such as open files and signals. A traditional (or heavyweight)
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.
2.6.2. Single and Multithreaded Processes:

 Most modern applications are multithreaded


 Threads run within application
 Multiple tasks with the application can be implemented by separate threads
o Update display
o Fetch data
o Spell checking
o Answer a network request
 Process creation is heavy-weight while thread creation is light-weight
 Can simplify code, increase efficiency
 Kernels are generally multithreaded
Benefits of Multithreading:
 Responsiveness: Multithreading in an interactive application enables a program to continue
running even if a section is blocked or executing a lengthy process, increasing user responsiveness.
 Resource Sharing: Processes can only share resources through techniques such as shared memory
and message passing. Such techniques must be explicitly arranged by the programmer. However,
threads share the memory and the resources of the process to which they belong by default. The
benefit of sharing code and data is that it allows an application to have several different threads
of activity within the same address space.
 Economy: Allocating memory and resources for process creation is costly. Because threads share
the resources of the process to which they belong, it is more economical to create and context-
switch threads.
 Scalability: The benefits of multithreading can be even greater in a multiprocessor architecture,
where threads may be running in parallel on different processing cores. A single-threaded process
can run on only one processor, regardless how many are available.

2.6. MULTI-THREADING :
2.6.1. Types of Threads:
Threads are implemented in following two ways −
 User Level Threads − User managed threads.
 Kernel Level Threads − Operating System managed
threads acting on kernel, an operating system core.
Kernel Level Threads: Kernel-level threads are handled by the
operating system directly and the thread management is done
by the kernel. The context information for the process as well
as the process threads is all managed by the kernel. Because
of this, kernel-level threads are slower than user- level
threads.
User Level Threads: The user-level threads are implemented by users and the kernel is not aware of
the existence of these threads. It handles them as if they were single-threaded processes. User-level
threads are small and much faster than kernel level threads. They are represented by a program
counter (PC), stack, registers and a small process control block. Also, there is no kernel involvement
in synchronization for user-level threads.
2.6.2. Difference between User-Level & Kernel-Level Thread:
S.No User-Level Threads Kernel-Level Thread

User-level threads are faster to create and Kernel-level threads are slower to create and
1
manage. manage.

Implementation is by a thread library at the Operating system supports creation of Kernel


2
user level. threads.

User-level thread is generic and can run on Kernel-level thread is specific to the operating
3
any operating system. system.

Multi-threaded applications cannot take Kernel routines themselves can be


4
advantage of multiprocessing. multithreaded.

2.7. MULTITHREADING MODELS:


In a specific implementation, the user threads must be mapped to kernel threads, using one of the
following strategies.
 Many-to-One
 One-to-One
 Many-to-Many
a) Many-to-One:
 In the many-to-one model, many user-level threads are all mapped onto a
single kernel thread.
 Thread management is handled by the thread library in user space, which
is very efficient.
 However, if a blocking system call is made, then the entire process blocks,
even if the other user threads would otherwise be able to continue.
 Because a single kernel thread can operate only on a single CPU, the many-to-
one model does not allow individual processes to be split across multiple CPUs.
 Green threads for Solaris and GNU Portable Threads implement the many-to-one
model in the past, but few systems continue to do so today.
b) One-to-One:
 The one-to-one model maps each user thread to a kernel thread.
 It provides more concurrency than the many-to-one model by
allowing another thread to run when a thread makes a
blocking system call; it also allows multiple threads to run in
parallel on multiprocessors.
 The only drawback to this model is that creating a user thread
requires creating the corresponding kernel thread.
 Because the overhead of creating kernel threads can burden
the performance of an application, most implementations of
this model restrict the number of threads supported by the system.
 Linux, along with the family of Windows operating systems, implement the one-to-one model.
c) Many-to-Many Model:
 The many-to-many model multiplexes any number of user
threads onto an equal or smaller number of kernel threads,
combining the best features of the one-to-one and many-to-one
models.
 Users have no restrictions on the number of threads created.
 Blocking kernel system calls do not block the entire process.
 Processes can be split across multiple processors.
 Individual processes may be allocated variable numbers of kernel
threads, depending on the number of CPUs present and other
factors.
d) Two-level Model:
The many-to-many model still multiplexes many user level
threads to a smaller or equal number of kernel threads but also
allows a user-level thread to be bound to a kernel thread.
Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier

2.8. THREAD LIBRARIES:


 A thread library provides the programmer an API for creating and managing threads. There are
two primary ways of implementing a thread library.
 The first approach is to provide a library entirely in user space with no kernel support. All code
and data structures for the library exist in user space. This means that invoking a function in the
library results in a local function call in user space and not a system call.
 The second approach is to implement a kernel-level library supported directly by the operating
system. In this case, code and data structures for the library exist in kernel space. Invoking a
function in the API for the library typically results in a system call to the kernel.
Types of thread libraries:
 There are three main thread libraries in use today:
 POSIX Pthreads(Portable Operating System Interface) - may be provided as either a user or kernel
library, as an extension to the POSIX standard.
 Windows threads - provided as a kernel-level library on Windows systems.
 Java threads - Since Java generally runs on a Java Virtual Machine, the implementation of threads
is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or Win32
threads depending on the system.

2.9. THREADING ISSUES:


a. The fork() and exec() are the system calls.
The fork() call is used to create a duplicate child process. During a fork() call the issue that arises is
whether the whole process should be duplicated or just the thread which made the fork() call should
be duplicated. The exec() call replaces the whole process that called it including all the threads in
the process with a new program.
b. Thread Cancellation:
The termination of a thread before its completion is called thread cancellation and the
terminated thread is termed as target thread. Thread cancellation is of two types:
 Asynchronous Cancellation: In asynchronous cancellation, one thread immediately
terminates the target thread.
 Deferred Cancellation: In deferred cancellation, the target thread periodically checks if
it should be terminated.
c. Signal Handling:
In UNIX systems, a signal is used to notify a process that a particular event has happened. Based on
the source of the signal, signal handling can be categorized as:
1. Asynchronous Signal: The signal which is generated outside the process which receives it.
2. Synchronous Signal: The signal which is generated and delivered in the same process.
d. Thread-Local Storage:
 Thread-local storage (TLS) allows each thread to have its own copy of data
 Useful when you do not have control over the thread creation process (i.e., when using a thread
pool)
 Different from local variables
o Local variables visible only during single function invocation
o TLS visible across function invocations
 Similar to static data
o TLS is unique to each thread
e. Scheduler Activations:
 A final issue to be considered with multithreaded programs concerns
communication between the kernel and the thread library, which may be
required by the many-to-many and two-level models.
 Many systems implementing either the many-to-many or the two-level
model place an intermediate data structure between the user and kernel
threads. This data structure—typically known as a lightweight process, or
LWP.
 To the user-thread library, the LWP appears to be a virtual processor on
which the application can schedule a user thread to run.
 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. Furthermore, the kernel
must inform an application about certain events. This procedure is known as an upcall. Upcalls are
handled by the thread library with an upcall handler, and upcall handlers must run on a virtual
processor.

2.10. Process VS thread:


Comparison Basis Process Thread
A process is a program under A thread is a lightweight
execution i.e an active program. process that can be managed
Definition
independently by a
scheduler.
Processes require more time for Threads require less time for
Context switching
context switching as they are context switching as they are
time
more heavy. lighter than processes.
Processes are totally independent A thread may share some
Memory Sharing and don’t share memory. memory with its peer
threads.
Communication between Communication between
Communication processes requires more time threads requires less time
than between threads. than between processes.
If a process gets blocked, If a user level thread gets
Blocked remaining processes can continue blocked, all of its peer
execution. threads also get blocked.
Resource Processes require more resources Threads generally need less
Consumption than threads. resources than processes.
Individual processes are Threads are parts of a
Dependency independent of each other. process and so are
dependent.
Processes have independent data A thread shares the data
Data and Code
and code segments. segment, code segment, files
sharing
etc. with its peer threads.
All the different processes are All user level peer threads
Treatment by OS treated separately by the are treated as a single task by
operating system. the operating system.
Processes require more time for Threads require less time for
Time for creation
creation. creation.
Processes require more time for Threads require less time for
Time for termination
termination. termination.
2.11. PROCESS SCHEDULING (OR) CPU SCHEDULING:
2.11.1. Basic Concepts:
In the uniprogrammming systems like MS DOS, when a process waits for any I/O operation to be
done, the CPU remains idol. This is an overhead since it wastes the time and causes the problem of
starvation. However, In Multiprogramming systems, the CPU doesn't remain idle during the waiting
time of the Process and it starts executing other processes. Operating System has to define which
process the CPU will be given. In Multiprogramming systems, the Operating system schedules the
processes on the CPU to have the maximum utilization of it and this procedure is called CPU
scheduling. The Operating System uses various scheduling algorithm to schedule the processes.

2.11.2. CPU–I/O Burst Cycle: Process execution consists of a cycle of


CPU execution and I/O wait. The state of process under
execution is called CPU burst and the state of process under
I/O request & its handling is called I/O burst. Processes
alternate between these two states. Process execution
begins with a CPU burst. That is followed by an I/O burst,
which is followed by another CPU burst, then another I/O
burst, and so on. Eventually, the final CPU burst ends with a
system request to terminate execution as shown in the
figure
2.11.3. CPU Scheduler:
Scheduling of processes/work is done to finish the work on time. CPU Scheduling is a process that
allows one process to use the CPU while another process is delayed (in standby) due to unavailability
of any resources such as I / O etc, thus making full use of the CPU. The purpose of CPU Scheduling is
to make the system more efficient, faster, and fairer. Whenever the CPU becomes idle, the operating
system must select one of the processes in the line 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.
2.11.3.1. Preemptive Scheduling:
 CPU-scheduling decisions may take place under the following four circumstances:
o Switches from running to waiting state
o Switches from running to ready state
o Switches from waiting to ready
o Terminates
 Scheduling under 1 and 4 is nonpreemptive
 All other scheduling is preemptive
o Consider access to shared data
o Consider preemption while in kernel mode
o Consider interrupts occurring during crucial OS activities
2.11.3.2. Dispatcher:
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this
involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running

2.12. SCHEDULING CRITERIA:


 CPU utilization – keep the CPU as busy as possible
 Throughput – number of processes that complete their execution per time unit
 Turnaround time – amount of time to execute a particular process. 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.
The formula to calculate Turn Around Time = Compilation Time – Arrival Time.
 Waiting time – amount of time a process has been waiting in the ready queue
The formula for calculating Waiting Time = Turnaround Time – Burst Time.
 Response time – amount of time it takes from when a request was submitted until the first
response is produced, not output (for time-sharing environment)
 Response Time = CPU Allocation Time(when the CPU was allocated for the first) – Arrival Time
Scheduling Algorithm Optimization Criteria:
 Max CPU utilization
 Max throughput
 Min turnaround time
 Min waiting time
 Min response time
2.12.1. FIRST- COME, FIRST-SERVED (FCFS) SCHEDULING:
Simplest CPU scheduling algorithm that schedules according to arrival times of processes. First come
first serve scheduling algorithm states that the process that requests the CPU first is allocated the
CPU first. It is implemented by using the FIFO queue. 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. FCFS is a non-preemptive
scheduling algorithm.
Characteristics of FCFS:
• Tasks are always executed on a First-come, First-serve concept.
• FCFS is easy to implement and use.
• This algorithm is not much efficient in performance, and the wait time is quite high.
Example:
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
The turn around time for P1 = 24
The turn around time for P2 = 27
The turn around time for P3 = 30
The average turnaround time = (24+27+30)/3= 27 milliseconds.

Generalized Activity Normalization Time Table (GANTT) chart is a type of bar chart that is used to
visually display the schedule of a project.
Convoy Effect in FCFS:
 FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all.
As in the real life, if a convoy is passing through the road then the other persons may get blocked
until it passes completely. This can be simulated in the Operating System also.
 If the CPU gets the processes of the higher burst time at the front end of the ready queue then
the processes of lower burst time may get blocked which means they may never get the CPU if
the job in the execution has a very high burst time. This is called convoy effect or starvation.
 Convoy effect - short process behind long process

Advantages of FCFS:
• Easy to implement
• First come, first serve method
Disadvantages of FCFS:
• FCFS suffers from Convoy effect.
• The average waiting time is much higher than the other algorithms.
• FCFS is very simple and easy to implement and hence not much efficient.
2.12.2. SHORTEST-JOB-FIRST (SJF) SCHEDULING:
This algorithm associates with each process the length of the process’s next CPU burst. When the CPU
is available, it is assigned to the process that has the smallest next CPU burst. This is also known as
shortest-next-CPU-burst algorithm.
Characteristics of SJF:
 Shortest Job first has the advantage of having a minimum average waiting time among all
operating system scheduling algorithms.
 It is associated with each task as a unit of time to complete.
 It may cause starvation if shorter processes keep coming.
Advantages of Shortest Job first:
• As SJF reduces the average waiting time thus, it is better than the first come first serve
scheduling algorithm.
• SJF is generally used for long term scheduling
Disadvantages of SJF:
• One of the demerits SJF has been starvation.
• Many times, it becomes complicated to predict the length of the upcoming CPU request

SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24
Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
2.12.3. SHORTEST-REMAINING-TIME-FIRST:
The Preemptive version of Shortest Job First (SJF) scheduling is known as Shortest Remaining Time
First (SRTF). With the help of the SRTF algorithm, the process having the smallest amount of time
remaining until completion is selected first to execute. So basically in SRTF, the processes are
scheduled according to the shortest remaining time. However, the SRTF algorithm involves more
overheads than the shortest job first (SJF)scheduling, because in SRTF OS is required frequently in
order to monitor the CPU time of the jobs in the READY queue and to perform context switching. In
the SRTF scheduling algorithm, the execution of any process can be stopped after a certain amount
of time. On arrival of every process, the short-term scheduler schedules those processes from the list
of available processes & running processes that have the least remaining burst time. After all the
processes are available in the ready queue, then, no preemption will be done and then the algorithm
will work the same as SJF scheduling. In the Process Control Block, the context of the process is saved,
when the process is removed from the execution and when the next process is scheduled. The PCB is
accessed on the next execution of this process.
Advantages of SRTF
 The main advantage of the SRTF algorithm is that it makes the processing of the jobs faster than
the SJF algorithm, mentioned it’s overhead charges are not counted.
Disadvantages of SRTF
 In SRTF, the context switching is done a lot more times than in SJN due to more consumption of
the CPU's valuable time for processing. The consumed time of CPU then adds up to its processing
time and which then diminishes the advantage of fast processing of this algorithm.
Explanation
• At the 0th unit of the CPU, there is only one process that is P1, so P1 gets executed for the 1-
time unit.
• At the 1st unit of the CPU, Process P2 arrives. Now, the P1 needs 6 more units more to be
executed, and the P2 needs only 3 units. So, P2 is executed first by preempting P1.
• At the 3rd unit of time, the process P3 arrives, and the burst time of P3 is 4 units which is more
than the completion time of P2 that is 1 unit, so P2 continues its execution.
• Now after the completion of P2, the burst time of P3 is 4 units that means it needs only 4 units
for completion while P1 needs 6 units for completion.
• So, this algorithm picks P3 above P1 due to the reason that the completion time of P3 is less
than that of P1
• P3 gets completed at time unit 8, there are no new processes arrived.
• So again, P1 is sent for execution, and it gets completed at the 14th unit.
• As Arrival Time and Burst time for three processes P1, P2, P3 are given in the above diagram.
Let us calculate Turnaround time, completion time, and waiting time.
Turn Around Time = Waiting Time = Turn
Arrival Burst Completion
Process Completion Time – Around Time –
Time Time time
Arrival Time Burst Time

P1 0 7 14 14-0=14 14-7=7

P2 1 3 4 4-1=3 3-3=0

P3 3 4 8 8-3=5 5-4=1
average waiting time = waiting for time of all processes/ no.of processes
average waiting time=7+0+1=8/3 = 2.66ms
2.12.4. PRIORITY SCHEDULING:
The SJF algorithm is a special case of the general priority-scheduling algorithm. 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. SJF is priority scheduling where priority is the inverse of
predicted next CPU burst time. A major problem with priority scheduling algorithms is indefinite
blocking, or starvation. A process that is ready to run but waiting for the CPU can be considered
blocked. A priority scheduling algorithm can leave some low priority processes waiting indefinitely. A
solution to the problem of indefinite blockage of low-priority processes is aging. Aging involves
gradually increasing the priority of processes that wait in the system for a long time.
Example
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart

Average waiting time = 8.2 msec


2.12.5. ROUND ROBIN (RR):
The round-robin (RR) scheduling algorithm is designed especially for timesharing systems. It 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. In the RR scheduling
algorithm, no process is allocated the CPU for more than 1 time quantum in a row. If a process’s CPU
burst exceeds 1 time quantum, that process is preempted and is put back in the ready queue. The RR
scheduling algorithm is thus preemptive. If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units. Each
process must wait no longer than (n -1)× q time units until its next time quantum.
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

Characteristics of Round robin:


• It’s simple, easy to use, and starvation-free as all processes get the balanced CPU allocation.
• One of the most widely used methods in CPU scheduling as a core.
• It is considered preemptive as the processes are given to the CPU for a very limited time.
Advantages of Round robin:
• Round robin seems to be fair as every process gets an equal share of CPU.
• The newly created process is added to the end of the ready queue.
2.12.6. MULTILEVEL QUEUE SCHEDULING:
Another class of scheduling algorithms has been created for situations in which processes are easily
classified into different groups. For example, A common division is made between foreground (or
interactive) processes and background (or batch) processes. These two types of processes have
different response-time requirements, and so might have different scheduling needs. In addition,
foreground processes may have priority over background processes. A multi-level queue scheduling
algorithm partitions the ready queue into several separate queues. The processes are permanently
assigned to one queue, generally based on some property of the process, such as memory size,
process priority, or process type. Each queue has its own scheduling algorithm. For example,
separate queues might be used for foreground and background processes. The foreground queue
might be scheduled by the Round Robin algorithm, while the background queue is scheduled by an
FCFS algorithm.
Let us consider an example of a multilevel queue-scheduling algorithm with five queues:
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue, for
example, could run unless the queues for system processes, interactive processes, and interactive
editing processes were all empty. If an interactive editing process entered the ready queue while
a batch process was running, the batch process will be preempted. If there are no processes on
the higher priority queue only then the processes on the low priority queues will run.
The Description of the processes in the above diagram is as follows:
• System Process The Operating system itself
has its own process to run and is termed as
System Process.
• Interactive Process The Interactive Process
is a process in which there should be the
same kind of interaction (basically an online
game).
• Batch Processes Batch processing is
basically a technique in the Operating
system that collects the programs and data
together in the form of the batch before the
processing starts.
• Student Process The system process always
gets the highest priority while the student
processes always get the lowest priority.
2.12.7. MULTILEVEL FEEDBACK QUEUE:
• In a multilevel queue-scheduling algorithm, processes are permanently assigned to a queue on
entry to the system. Processes do not move between queues. This setup has the advantage of low
scheduling overhead, but the disadvantage of being inflexible.
• Multilevel feedback queue scheduling, however, allows a process to move between queues. The
idea is to separate processes with different CPU-burst characteristics. If a process uses too much
CPU time, it will be moved to a lower-priority queue. Similarly, a process that waits too long in a
lower-priority queue may be moved to a higher-priority queue. This form of aging prevents
starvation.
• In general, the Multilevel-feedback-queue scheduler defined by the following parameters:
o number of queues
o scheduling algorithms for each queue
o method used to determine when to upgrade a process
o method used to determine when to demote a process
o method used to determine which queue a process will enter when that process needs
service

Explanation
First of all, suppose that queues 1 and 2 follow round robin with
time quantum 8 and 16 respectively and queue 3 follows FCFS.
One of the implementations of Multilevel Feedback Queue
Scheduling is as follows:
1. If any process starts executing then firstly it enters queue 1.
2. In queue 1, the process executes for 8 unit and if it
completes in these 8 units or it gives CPU for I/O operation
in these 8 units unit than the priority of this process does not
change, and if for some reasons it again comes in the ready queue than it again starts its execution
in the Queue 1.
3. If a process that is in queue 1 does not complete in 8 units then its priority gets reduced and it
gets shifted to queue 2.
4. Above points 2 and 3 are also true for processes in queue 2 but the time quantum is 16 units.
Generally, if any process does not complete in a given time quantum then it gets shifted to the
lower priority queue.
5. After that in the last queue, all processes are scheduled in an FCFS manner.
6. It is important to note that a process that is in a lower priority queue can only execute only when
the higher priority queues are empty.
7. Any running process in the lower priority queue can be interrupted by a process arriving in the
higher priority queue.
Advantages of MFQS
• This is a flexible Scheduling Algorithm
• This scheduling algorithm allows different processes to move between different queues.
• In this algorithm, A process that waits too long in a lower priority queue may be moved to a higher
priority queue which helps in preventing starvation.
Disadvantages of MFQS
• This algorithm is too complex.
• As processes are moving around different queues which leads to the production of more CPU
overheads.
• In order to select the best scheduler this algorithm requires some other means to select the values

2.13. MULTIPLE-PROCESSOR SCHEDULING:


In multiple-processor scheduling multiple CPUs are available and hence Load Sharing becomes
possible. However multiple processor scheduling is more complex as compared to single processor
scheduling.
• CPU scheduling more complex when multiple CPUs are available
• Homogeneous processors within a multiprocessor
• Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating
the need for data sharing
• Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes in common
ready queue, or each has its own private queue of ready processes
• Currently, most common
• Processor affinity – process has affinity for processor on which it is currently running
o soft affinity: When an operating system has a policy of keeping a process running on
the same processor but not guaranteeing it will do so, this situation is called soft
affinity.
o hard affinity: Hard Affinity allows a process to specify a subset of processors on which
it may run.
• Variations including processor sets
• If SMP, need to keep all CPUs loaded for efficiency
• Load balancing attempts to keep workload evenly distributed
• Push migration – periodic task checks load on each processor, and if found pushes task from
overloaded CPU to other CPUs
• Pull migration – idle processors pull waiting task from busy processor
2.14. THREAD SCHEDULING:
 Scheduling of threads involves two boundary scheduling,
 Scheduling of user level threads (ULT) to kernel level threads (KLT) via lightweight process (LWP)
by the application developer.
 Scheduling of kernel level threads by the system scheduler to perform different unique os
functions.
Lightweight Process (LWP): Light-weight process are threads in the user
space that acts as an interface for the ULT to access the physical CPU
resources. Thread library schedules which thread of a process to run on
which LWP and how long. The number of LWP created by the thread
library depends on the type of application.
In real-time, the first boundary of thread scheduling is beyond specifying
the scheduling policy and the priority. It requires two controls to be
specified for the User level threads: Contention scope, and Allocation
domain.
Contention Scope: The word contention here refers to the competition
or fight among the User level threads to access the kernel resources. Thus,
this control defines the extent to which contention takes place. It is
defined by the application developer using the thread library. Depending
upon the extent of contention it is classified as Process Contention Scope
and System Contention Scope.
1. Process Contention Scope (PCS) – The contention takes place among threads within a same
process. The thread library schedules the high-prioritized PCS thread to access the resources via
available LWPs (priority as specified by the application developer during thread creation).
2. System Contention Scope (SCS) – The contention takes place among all threads in the system. In
this case, every SCS thread is associated to each
LWP by the thread library and are scheduled by
the system scheduler to access the kernel
resources.
Allocation Domain: The allocation domain is a set of
one or more resources for which a thread is
competing. In a multicore system, there may be one
or more allocation domains where each consists of
one or more cores. One ULT can be a part of one or
more allocation domain. Due to this high complexity
in dealing with hardware and software architectural
interfaces, this control is not specified. But by
default, the multicore system will have an interface
that affects the allocation domain of a thread.
• ce.

You might also like