Module 4
Module 4
5 Process has its own control Program does not have any
system known as Process control system. It is just called
Control Block when specified and it executes the
whole program when called
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.
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
– Ready queue
– Device 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 OS can use different policies to manage each queue
(FIFO, Round Robin, Priority, etc.).
• The OS scheduler determines how to move processes between
the ready and run queues which can only have one entry per
processor core on the system.
Two-State Process Model
• Two-state process model refers to running and non-running states
which are
– Running
– Not Running
Running
• When a new process is created, it enters into the system as in the
running state.
Not Running
• Processes that are not running are kept in queue, waiting for their
turn to execute.
• Each entry in the queue is a pointer to a particular process. Queue
is implemented by using linked list.
• Use of dispatcher is as follows.
• When a process is interrupted, that process is transferred in the
waiting queue.
• If the process has completed or aborted, the process is discarded.
• In either case, the dispatcher then selects a process from the queue
to execute.
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
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.
• It also controls the degree of multiprogramming.
• If the degree of multiprogramming is stable, then the average rate
of process creation must be equal to the average departure rate of
processes leaving the system.
• On some systems, the long-term scheduler may not be available or
minimal.
• Time-sharing operating systems have no long term scheduler.
• When a process changes the state from new to ready, then there is
use of long-term scheduler.
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.
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.
Long-Term Short-Term Medium-Term
No.
Scheduler Scheduler Scheduler
It is a process
1 It is a job scheduler It is a CPU scheduler
swapping scheduler.
Speed is in between
Speed is lesser than Speed is fastest
2 both short and long
short term scheduler among other two
term scheduler.
It can re-introduce
It selects processes
It selects those the process into
from pool and loads
5 processes which are memory and
them into memory
ready to execute execution can be
for execution
continued.
Pre-emptive Scheduling
• Preemptive scheduling is used when a process switches from the
running state to the ready state or from the waiting state to the
ready state.
• The resources (mainly CPU cycles) are allocated to the process for
a limited amount of time and then taken away, and the process is
again placed back in the ready queue if that process still has CPU
burst time remaining.
• That process stays in the ready queue till it gets its next chance to
execute.
• Algorithms based on preemptive scheduling are Round Robin
(RR), Shortest Remaining Time First (SRTF), Priority (preemptive
version), etc.
Advantages
• Because a process may not monopolize the processor, it is a more
reliable method.
• Each occurrence prevents the completion of ongoing tasks.
• The average response time is improved.
• Utilizing this method in a multi-programming environment is
more advantageous.
• The operating system makes sure that every process using the
CPU is using the same amount of CPU time.
Disadvantages
• Limited computational resources must be used.
• Suspending the running process, change the context, and dispatch
the new incoming process all take more time.
• The low-priority process would have to wait if multiple
high-priority processes arrived at the same time.
Non-Preemptive Scheduling
• Non-preemptive Scheduling is used when a process terminates, or
a process switches from running to the waiting state.
• In this scheduling, once the resources (CPU cycles) are allocated
to a process, the process holds the CPU till it gets terminated or
reaches a waiting state.
• In the case of non-preemptive scheduling does not interrupt a
process running CPU in the middle of the execution. Instead, it
waits till the process completes its CPU burst time, and then it can
allocate the CPU to another process.
• Algorithms based on non-preemptive scheduling are: Shortest Job
First (SJF basically non preemptive) and Priority (nonpreemptive
version), etc.
Advantages
• It has a minimal scheduling burden.
• It is a very easy procedure.
• Less computational resources are used.
• It has a high throughput rate.
Disadvantages
• Its response time to the process is super.
• Bugs can cause a computer to freeze up.
Differences Between Preemptive and Non-Preemptive Scheduling
• In preemptive scheduling, the CPU is allocated to the processes for a
limited time whereas, in Non-preemptive scheduling, the CPU is
allocated to the process till it terminates or switches to the waiting
state.
• The executing process in preemptive scheduling is interrupted in the
middle of execution when a higher priority one comes whereas, the
executing process in non-preemptive scheduling is not interrupted in
the middle of execution and waits till its execution.
• In Preemptive Scheduling, there is the overhead of switching the
process from the ready state to the running state, vise-verse, and
maintaining the ready queue. Whereas in the case of non-preemptive
scheduling has no overhead of switching the process from running state
to ready state.
• In preemptive scheduling, if a high-prior. The process
non-preemptive low-priority process frequently arrives in the
ready queue then the process with low priority has to wait for a
long, and it may have to starve. , in non-preemptive scheduling, if
CPU is allocated to the process having a larger burst time then the
processes with a small burst time may have to starve.
• Preemptive scheduling attains flexibility by allowing the critical
processes to access the CPU as they arrive in the ready queue, no
matter what process is executing currently. Non-preemptive
scheduling is called rigid as even if a critical process enters the
ready queue the process running CPU is not disturbed.
• Preemptive Scheduling has to maintain the integrity of shared data
that’s why it is cost associative which is not the case with
Non-preemptive Scheduling.
Preemptive Non-Preemptive
Parameter
Scheduling Scheduling
Once resources(CPU Cycle)
In this resources(CPU
are allocated to a process, the
Cycle) are allocated to
Basic process holds it till it
a process for a limited
completes its burst time or
time.
switches to waiting state.
Process can not be
Process can be
Interrupt interrupted until it terminates
interrupted in between.
itself or its time is up.
Preemptive Non-Preemptive
Parameter
Scheduling Scheduling
If a process having If a process with a long burst
high priority frequently time is running CPU, then
Starvation arrives in the ready later coming process with
queue, a low priority less CPU burst time may
process may starve. starve.
It has overheads of
Overhead scheduling the It does not have overheads.
processes.
Flexibility flexible rigid
Preemptive Non-Preemptive
Parameter
Scheduling Scheduling
Cost cost associated no cost associated
In preemptive
CPU It is low in non preemptive
scheduling, CPU
Utilization scheduling.
utilization is high.
Waiting Preemptive scheduling Non-preemptive scheduling
Time waiting time is less. waiting time is high.
Response Preemptive scheduling Non-preemptive scheduling
Time response time is less. response time is high.
Preemptive Non-Preemptive
Parameter
Scheduling Scheduling
Decisions are made by Decisions are made by the
Decision the scheduler and are process itself and the OS just
making based on priority and follows the process’s
time slice allocation instructions
0 1 3 6 12 16 22
P0 P1 P3 P1 P0 P2
0 1 3 6 12 16 22
P0 P1 P3 P1 P0 P2
• Waiting time of each process is as follows −
• Waiting Time=Total waiting time-No of Seconds Process
Executed-Arrival Time
Process Waiting Time
P0 12-1-0=11
P1 6-2-1=3
P2 16-0-2=14
P3 3-0-3=0
0 4 8 14 22
P0 P1 P0 P3 P2
• Waiting time of each process is as follows −
• Waiting Time=Total waiting time-No of times Process
Executed-Arrival Time
Process Waiting Time
P0 4-1-0=3
P1 1-0-1=0
P2 14-0-2=12
P3 8-0-3=5
Disadvantages
• In a typical operating system, most system calls are blocking.
• Multithreaded application cannot take advantage of
multiprocessing.
Kernel Level Threads
• In this case, thread management is done by the Kernel.
• There is no thread management code in the application area.
• Kernel threads are supported directly by the operating system.
• Any application can be programmed to be multithreaded. All of
the threads within an application are supported within a single
process.
• The Kernel maintains context information for the process as a
whole and for individuals threads within the process.
• Scheduling by the Kernel is done on a thread basis.
• The Kernel performs thread creation, scheduling and management
in Kernel space.
• Kernel threads are generally slower to create and manage than the
user threads.
Advantages
• Kernel can simultaneously schedule multiple threads from the
same process on multiple processes.
• If one thread in a process is blocked, the Kernel can schedule
another thread of the same process.
• Kernel routines themselves can be multithreaded.
Disadvantages
• Kernel threads are generally slower to create and manage than the
user threads.
• Transfer of control from one thread to another within the same
process requires a mode switch to the Kernel.
Multithreading Models
• Some operating system provide a combined user level thread and
Kernel level thread facility.
• Solaris is a good example of this combined approach. In a
combined system, multiple threads within the same application
can run in parallel on multiple processors and a blocking system
call need not block the entire process.
• Multithreading models are three types
– Many to many relationship.
– Many to one relationship.
– One to one relationship.
Many to Many Model
• The many-to-many model multiplexes any number of user threads
onto an equal or smaller number of kernel threads.
• The following diagram shows the many-to-many threading model
where 6 user level threads are multiplexing with 6 kernel level
threads.
• In this model, developers can create as many user threads as
necessary and the corresponding Kernel threads can run in parallel
on a multiprocessor machine.
• This model provides the best accuracy on concurrency and when a
thread performs a blocking system call, the kernel can schedule
another thread for execution.
Many to One Model
• Many-to-one model maps many user level threads to one
Kernel-level thread.
• Thread management is done in user space by the thread library.
• When thread makes a blocking system call, the entire process will
be blocked. Only one thread can access the Kernel at a time, so
multiple threads are unable to run in parallel on multiprocessors.
• If the user-level thread libraries are implemented in the operating
system in such a way that the system does not support them, then
the Kernel threads use the many-to-one relationship modes.
One to One Model
• There is one-to-one relationship of user-level thread to the
kernel-level thread.
• This model provides more concurrency than the many-to-one
model.
• It also allows another thread to run when a thread makes a
blocking system call.
• It supports multiple threads to execute in parallel on
microprocessors.
• Disadvantage of this model is that creating user thread requires the
corresponding Kernel thread.
• OS/2, windows NT and windows 2000 use one to one relationship
model.
Difference between User-Level & Kernel-Level Thread
No. User-Level Threads Kernel-Level Thread
Multi-threaded applications
Kernel routines themselves can
4 cannot take advantage of
be multithreaded.
multiprocessing.
Inter-Process Communication
• A process can be of two types:
– Independent process.
– Co-operating process.
• An independent process is not affected by the execution of other
processes while a co-operating process can be affected by other
executing processes.
• Though one can think that those processes, which are running
independently, will execute very efficiently, in reality, there are
many situations when co-operative nature can be utilized for
increasing computational speed, convenience, and modularity.
• Inter-process communication (IPC) is a mechanism that allows
processes to communicate with each other and synchronize
their actions.
• The communication between these processes can be seen as a
method of co-operation between them.
• Processes can
communicate with
each other through
both:
• Shared Memory
• Message passing
• An operating system can implement both methods of communication.
• Communication between processes using shared memory requires
processes to share some variable, and it completely depends on how the
programmer will implement it.
• One way of communication using shared memory can be imagined like
this: Suppose process1 and process2 are executing simultaneously, and
they share some resources or use some information from another
process.
• Process1 generates information about certain computations or resources
being used and keeps it as a record in shared memory.
• When process2 needs to use the shared information, it will check in the
record stored in shared memory and take note of the information
generated by process1 and act accordingly.
• Processes can use shared memory for extracting information as a record
from another process as well as for delivering any specific information
to other processes.
Shared Memory Method
• Ex: 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.
• In the bounded buffer 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.
• Messaging Passing Method
• In this method, processes communicate with each other without
using any kind of shared memory.
• If two processes p1 and p2 want to communicate with each other,
they proceed as follows:
– Establish a communication link (if a link already exists, no
need to establish it again.)
– Start exchanging messages using basic primitives.
• We need at least two primitives:
• Send (message, destination) or
send (message)
• receive(message, host) or
receive(message)
• The message size can be of fixed size or of variable size.
• If it is of fixed size, it is easy for an OS designer but complicated
for a programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer.
• A standard message can have two parts: header and body.
• The header part is used for storing message type, destination id,
source id, message length, and control information.
• The control information contains information like what to do if
runs out of buffer space, sequence number, priority. Generally,
message is sent using FIFO style.
Message Passing through Communication Link.
• Direct and Indirect Communication link
• While implementing the link, there are some questions that need to
be kept in mind like :
• How are links established?
• Can a link be associated with more than two processes?
• How many links can there be between every pair of
communicating processes?
• What is the capacity of a link? Is the size of a message that the
link can accommodate fixed or variable?
• Is a link unidirectional or bi-directional?
• A link has some capacity that determines the number of messages
that can reside in it temporarily for which every link has a queue
associated with it which can be of zero capacity, bounded
capacity, or unbounded capacity.
• In zero capacity, the sender waits until the receiver informs the
sender that it has received the message.
• In non-zero capacity cases, a process does not know whether a
message has been received or not after the send operation.
• For this, the sender must communicate with the receiver explicitly.
• Implementation of the link depends on the situation, it can be
either a direct communication link or an in-directed
communication link.
• Direct Communication links are implemented when the processes
use a specific process identifier for the communication, but it is
hard to identify the sender ahead of time.
• For example the print server.
• In-direct Communication is done via a shared mailbox (port),
which consists of a queue of messages.
• The sender keeps the message in mailbox and the receiver picks
them up.
Message Passing through Exchanging the Messages.
• Synchronous and Asynchronous Message Passing:
• A process that is blocked is one that is waiting for some event,
such as a resource becoming available or the completion of an I/O
operation.
• IPC is possible between the processes on same computer as well
as on the processes running on different computer i.e. in
networked/distributed system.
• In both cases, the process may or may not be blocked while
sending a message or attempting to receive a message so message
passing may be blocking or non-blocking.
• Blocking is considered synchronous and blocking send means the
sender will be blocked until the message is received by receiver.
• Similarly, blocking receive has the receiver block until a message is
available.
• Non-blocking is considered asynchronous and Non-blocking send
has the sender sends the message and continue.
• Similarly, Non-blocking receive has the receiver receive a valid
message or null.
• After a careful analysis, we can come to a conclusion that for a
sender it is more natural to be non-blocking after message passing as
there may be a need to send the message to different processes.
• However, the sender expects acknowledgment from the receiver in
case the send fails. Similarly, it is more natural for a receiver to be
blocking after issuing the receive as the information from the
received message may be used for further execution.
• At the same time, if the message send keep on failing, the receiver
will have to wait indefinitely.
• That is why we also consider the other possibility of message
passing.
• There are basically three preferred combinations:
– Blocking send and blocking receive
– Non-blocking send and Non-blocking receive
– Non-blocking send and Blocking receive (Mostly used)
• In Direct message passing, the process which wants to communicate
must explicitly name the recipient or sender of the communication.
• e.g. send(p1, message) means send the message to p1.
• Similarly, receive(p2, message) means to receive the message from p2.
• In this method of communication, the communication link gets
established automatically, which can be either unidirectional or
bidirectional, but one link can be used between one pair of the sender
and receiver and one pair of sender and receiver should not possess
more than one pair of links.
• Symmetry and asymmetry between sending and receiving can also be
implemented i.e. either both processes will name each other for sending
and receiving the messages or only the sender will name the receiver for
sending the message and there is no need for the receiver for naming the
sender for receiving the message.
• The problem with this method of communication is that if the name of
one process changes, this method will not work.
• In Indirect message passing, processes use mailboxes (also
referred to as ports) for sending and receiving messages.
• Each mailbox has a unique id and processes can communicate
only if they share a mailbox.
• Link established only if processes share a common mailbox and a
single link can be associated with many processes.
• Each pair of processes can share several communication links and
these links may be unidirectional or bi-directional.
• Suppose two processes want to communicate through Indirect
message passing, the required operations are: create a mailbox, use
this mailbox for sending and receiving messages, then destroy the
mailbox.
• The standard primitives used are: send(A, message) which means
send the message to mailbox A.
• The primitive for the receiving the message also works in the same
way e.g. received (A, message).
• There is a problem with this mailbox implementation. Suppose
there are more than two processes sharing the same mailbox and
suppose the process p1 sends a message to the mailbox, which
process will be the receiver?
• This can be solved by either enforcing that only two processes can
share a single mailbox or enforcing that only one process is
allowed to execute the receive at a given time or select any process
randomly and notify the sender about the receiver.
• A mailbox can be made private to a single sender/receiver pair and
can also be shared between multiple sender/receiver pairs.
• Port is an implementation of such mailbox that can have multiple
senders and a single receiver.
• It is used in client/server applications (in this case the server is the
receiver).
• The port is owned by the receiving process and created by OS on
the request of the receiver process and can be destroyed either on
request of the same receiver processor when the receiver
terminates itself.
• Enforcing that only one process is allowed to execute the receive
can be done using the concept of mutual exclusion.
• Mutex mailbox is created which is shared by n process.
• The sender is non-blocking and sends the message.
• The first process which executes the receive will enter in the
critical section and all other processes will be blocking and will
wait.
Advantages of IPC:
• Enables processes to communicate with each other and share
resources, leading to increased efficiency and flexibility.
• Facilitates coordination between multiple processes, leading to
better overall system performance.
• Allows for the creation of distributed systems that can span
multiple computers or networks.
• Can be used to implement various synchronization and
communication protocols, such as semaphores, pipes, and sockets.
Disadvantages of IPC:
• Increases system complexity, making it harder to design, implement,
and debug.
• Can introduce security vulnerabilities, as processes may be able to
access or modify data belonging to other processes.
• Requires careful management of system resources, such as memory
and CPU time, to ensure that IPC operations do not degrade overall
system performance.
• Can lead to data inconsistencies if multiple processes try to access or
modify the same data at the same time.
• Overall, the advantages of IPC outweigh the disadvantages, as it is a
necessary mechanism for modern operating systems and enables
processes to work together and share resources in a flexible and
efficient manner. However, care must be taken to design and
implement IPC systems carefully, in order to avoid potential security
vulnerabilities and performance issues.
Process Synchronization
• Process Synchronization is the coordination of execution of
multiple processes in a multi-process system to ensure that they
access shared resources in a controlled and predictable manner.
• It aims to resolve the problem of race conditions and other
synchronization issues in a concurrent system.
• The main objective of process synchronization is to ensure that
multiple processes access shared resources without interfering
with each other and to prevent the possibility of inconsistent data
due to concurrent access.
• To achieve this, various synchronization techniques such as
semaphores, monitors, and critical sections are used.
• In a multi-process system, synchronization is necessary to ensure
data consistency and integrity, and to avoid the risk of deadlocks
and other synchronization problems.
• Process synchronization is an important aspect of modern
operating systems, and it plays a crucial role in ensuring the
correct and efficient functioning of multi-process systems.
• On the basis of synchronization, processes are categorized as one
of the following two types:
• Independent Process: The execution of one process does not
affect the execution of other processes.
• Cooperative Process: A process that can affect or be affected by
other processes executing in the system.
• Process synchronization problem arises in the case of Cooperative
processes also because resources are shared in Cooperative
processes.
Race Condition
• When more than one process is executing the same code or accessing the same
memory or any shared variable in that condition there is a possibility that the
output or the value of the shared variable is wrong so for that all the processes
doing the race to say that my output is correct this condition known as a race
condition.
• Several processes access and process the manipulations over the same data
concurrently, and then the outcome depends on the particular order in which
the access takes place.
• A race condition is a situation that may occur inside a critical section.
• This happens when the result of multiple thread execution in the critical
section differs according to the order in which the threads execute.
• Race conditions in critical sections can be avoided if the critical section is
treated as an atomic instruction.
• Also, proper thread synchronization using locks or atomic variables can
prevent race conditions.
• Let’s understand one example to understand the race condition
better:
• Let’s say there are two processes P1 and P2 which share a
common variable (shared=10), both processes are present in –
queue and waiting for their turn to be executed.
• Suppose, Process P1 first come under
execution, and the CPU store a common Process 1 Process 2
variable between them (shared=10) in the int X = int Y =
local variable (X=10) and increment it by shared shared
1(X=11), after then when the CPU read
line sleep(1),it switches from current X++ Y–
process P1 to process P2 present in sleep(1) sleep(1)
ready-queue.
• The process P1 goes in a waiting state for 1 shared = X shared = Y
second.
• Now CPU execute the Process P2 line by line and store common
variable (Shared=10) in its local variable (Y=10) and decrement Y
by 1(Y=9), after then when CPU read sleep(1), the current process
P2 goes in waiting for state and CPU remains idle for some time
as there is no process in ready-queue, after completion of 1 second
of process P1 when it comes in ready-queue, CPU takes the
process P1 under execution and execute the remaining line of code
(store the local variable (X=11) in common variable (shared=11) ),
CPU remain idle for sometime waiting for any process in
ready-queue,after completion of 1 second of Process P2, when
process P2 comes in ready-queue, CPU start executing the further
remaining line of Process P2(store the local variable (Y=9) in
common variable (shared=9) ).
• Note: We are assuming the final value of a common
variable(shared) after execution of Process P1 and Process P2 is
10 (as Process P1 increment variable (shared=10) by 1 and
Process P2 decrement variable (shared=11) by 1 and finally it
becomes shared=10).
• But we are getting undesired value due to a lack of proper
synchronization.
Actual meaning of race-condition
• If the order of execution of the process(first P1 -> then P2) then
we will get the value of common variable (shared) =9.
• If the order of execution of the process(first P2 -> then P1) then
we will get the final value of common variable (shared) =11.
• Here the (value1 = 9) and (value2=10) are racing, If we execute
these two processes in our computer system then sometime we
will get 9 and sometime we will get 10 as the final value of a
common variable(shared).
• This phenomenon is called race condition.
Critical Section Problem
• A critical section is a code segment that can be accessed by only
one process at a time. The critical section contains shared
variables that need to be synchronized to maintain the consistency
of data variables. So the critical section problem means designing
a way for cooperative processes to access shared resources without
creating data inconsistencies.
• In the entry section, the process requests for entry in the Critical
Section.
• Any solution to the critical section problem must satisfy three
requirements:
• Mutual Exclusion: If a process is executing in its critical section, then
no other process is allowed to execute in the critical section.
• Progress: If no process is executing in the critical section and other
processes are waiting outside the critical section, then only those
processes that are not executing in their remainder section can
participate in deciding which will enter the critical section next, and the
selection can not be postponed indefinitely.
• Bounded Waiting: A bound must exist on the number of times that
other processes are allowed to enter their critical sections after a process
has made a request to enter its critical section and before that request is
granted.
Peterson’s Solution
• Peterson’s Solution is a classical software-based solution to the
critical section problem.
• In Peterson’s solution, we have two shared variables:
• boolean flag[i]: Initialized to FALSE, initially no one is interested
in entering the critical section
• int turn: The process whose turn is to enter the critical section.
• Peterson’s Solution preserves all three conditions:
• Mutual Exclusion is assured as only one process can access the
critical section at any time.
• Progress is also assured, as a process outside the critical section
does not block other processes from entering the critical section.
• Bounded Waiting is preserved as every process gets a fair chance.
Disadvantages of Peterson’s Solution
• It involves busy waiting. (In the Peterson’s solution, the code
statement- “while(flag[j] && turn == j);” is responsible for this.
Busy waiting is not favored because it wastes CPU cycles that
could be used to perform other tasks.)
• It is limited to 2 processes.
• Peterson’s solution cannot be used in modern CPU architectures.
Semaphores
• A semaphore is a signaling mechanism and a thread that is waiting
on a semaphore can be signaled by another thread.
• This is different than a mutex as the mutex can be signaled only by
the thread that is called the wait function.
• A semaphore uses two atomic operations, wait and signal for
process synchronization.
• A Semaphore is an integer variable, which can be accessed only
through two operations wait() and signal().
• There are two types of semaphores: Binary Semaphores and
Counting Semaphores.
Binary Semaphores:
• They can only be either 0 or 1.
• They are also known as mutex locks, as the locks can provide
mutual exclusion.
• All the processes can share the same mutex semaphore that is
initialized to 1.
• Then, a process has to wait until the lock becomes 0.
• Then, the process can make the mutex semaphore 1 and start its
critical section.
• When it completes its critical section, it can reset the value of the
mutex semaphore to 0 and some other process can enter its critical
section.
Counting Semaphores:
• They can have any value and are not restricted to a certain domain.
• They can be used to control access to a resource that has a
limitation on the number of simultaneous accesses.
• The semaphore can be initialized to the number of instances of the
resource.
• Whenever a process wants to use that resource, it checks if the
number of remaining instances is more than zero, i.e., the process
has an instance available.
• Then, the process can enter its critical section thereby decreasing
the value of the counting semaphore by 1.
• After the process is over with the use of the instance of the
resource, it can leave the critical section thereby adding 1 to the
number of available instances of the resource.
Advantages of Process Synchronization
• Ensures data consistency and integrity
• Avoids race conditions
• Prevents inconsistent data due to concurrent access
• Supports efficient and effective use of shared resources
Banker’s Algorithm
• Bankers’s Algorithm is a resource allocation and deadlock avoidance
algorithm which test all the request made by processes for resources, it
checks for the safe state, and after granting a request system remains in
the safe state it allows the request, and if there is no safe state it doesn’t
allow the request made by the process.
Inputs to Banker’s Algorithm
• Max needs of resources by each process.
• Currently, allocated resources by each process.
• Max free available resources in the system.
• The request will only be granted under the below condition
– If the request made by the process is less than equal to the max
needed for that process.
– If the request made by the process is less than equal to the freely
available resource in the system.
Timeouts:
• To avoid deadlocks caused by indefinite waiting, a timeout mechanism
can be used to limit the amount of time a process can wait for a
resource.
• If the help is unavailable within the timeout period, the process can be
forced to release its current resources and try again later.
File Management
• A file management system is that set of system software that
provides services to users and applications in the use of files.
• Typically, the only way a user or application may access files is
through the file management system.
• This relieves the user or programmer of the necessity of
developing special-purpose software for each application and
provides the system with a consistent, well-defined means of
controlling its most important asset.
• [GROS86] suggests the following objectives for a file
management system:
• To meet the data management needs and requirements of the user,
which include storage of data and the ability to perform the
aforementioned operations.
• To guarantee, to the extent possible, that the data in the file are
valid.
• To optimize performance, both from the system point of view in
terms of overall throughput, and from the user’s point of view in
terms of response time.
• To provide I/O support for a variety of storage device types
• To minimize or eliminate the potential for lost or destroyed data.
• To provide a standardized set of I/O interface routines to user
processes.
• To provide I/O support for multiple users, in the case of
multiple-user systems
• To meet the data management needs and requirements of the user,
which include storage of data and the ability to perform the
aforementioned operations.
• To guarantee, to the extent possible, that the data in the file are
valid.
• To optimize performance, both from the system point of view in
terms of overall throughput, and from the user’s point of view in
terms of response time.
• To provide I/O support for a variety of storage device types
• To minimize or eliminate the potential for lost or destroyed data.
• To provide a standardized set of I/O interface routines to user
processes.
• To provide I/O support for multiple users, in the case of
multiple-user systems
• With respect to the first point, meeting user requirements, the extent of
such requirements depends on the variety of applications and the
environment in which the computer system will be used.
• For an interactive, general-purpose system, the following constitute a
minimal set of requirements:
– Each user should be able to create, delete, read, write, and modify
files.
– Each user may have controlled access to other users’ files.
– Each user may control what types of accesses are allowed to the
user’s files.
– Each user should be able to move data between files.
– Each user should be able to back up and recover the user’s files in
case of damage.
– Each user should be able to access his or her files by name rather
than by numeric identifier.
File System Architecture
• One way of getting a feel for the scope of file management is to
look at a depiction of a typical software organization.
• Of course, different systems will be organized differently, but this
organization is reasonably representative.
• At the lowest level, device drivers communicate directly with
peripheral devices or their controllers or channels.
• A device driver is responsible for starting I/O operations on a
device and processing the completion of an I/O request.
• For file operations, the typical devices controlled are disk and tape
drives.
• Device drivers are usually considered to be part of the operating
system.
• The next level is referred to as the basic file system, or the
physical I/O level.
• This is the primary interface with the environment outside of the
computer system.
• It deals with blocks of data that are exchanged with disk or tape
systems.
• Thus, it is concerned with the placement of those blocks on the
secondary storage device and on the buffering of those blocks in
main memory.
• It does not understand the content of the data or the structure of
the files involved.
• The basic file system is often considered part of the operating
system.
• The basic I/O supervisor is responsible for all file I/O
initiation and termination.
• At this level, control structures are maintained that deal with
device I/O, scheduling, and file status.
• The basic I/O supervisor selects the device on which file I/O is
to be performed, based on the particular file selected. It is also
concerned with scheduling disk and tape accesses to optimize
performance.
• I/O buffers are assigned and secondary memory is allocated at
this level.
• The basic I/O supervisor is part of the operating system.
• Logical I/O enables users and applications to access records.
• Thus, whereas the basic file system deals with blocks of data,
the logical I/O module deals with file records.
• Logical I/O provides a general-purpose record I/O capability
and maintains basic data about files.
• The level of the file system closest to the user is often termed
the access method.
• It provides a standard interface between applications and the
file systems and devices that hold the data.
• Different access methods reflect different file structures and
different ways of accessing and processing the data.
File Management Functions
• Let us follow this diagram from left to right.
• Users and application programs interact with the file system by
means of commands for creating and deleting files and for
performing operations on files.
• Before performing any operation, the file system must identify and
locate the selected file.
• This requires the use of some sort of directory that serves to
describe the location of all files, plus their attributes.
• In addition, most shared systems enforce user access control: Only
authorized users are allowed to access particular files in particular
ways.
• The basic operations that a user or an application may perform on
a file are performed at the record level.
• The user or application views the file as having some structure that
organizes the records, such as a sequential structure (e.g., personnel
records are stored alphabetically by last name).
• Thus, to translate user commands into specific file manipulation
commands, the access method appropriate to this file structure must be
employed.
• Whereas users and applications are concerned with records or fields,
I/O is done on a block basis.
• Thus, the records or fields of a file must be organized as a sequence of
blocks for output and unblocked after input.
• To support block I/O of files, several functions are needed.
• The secondary storage must be managed.
• This involves allocating files to free blocks on secondary storage and
managing free storage so as to know what blocks are available for new
files and growth in existing files.
• In addition, individual block I/O requests must be scheduled.
• Both disk scheduling and file allocation are concerned with optimizing
performance.
• As might be expected, these functions therefore need to be considered
together.
• Furthermore, the optimization will depend on the structure of the files
and the access patterns.
• Accordingly, developing an optimum file management system from the
point of view of performance is an exceedingly complicated task.
• Figure suggests a division between what might be considered the
concerns of the file management system as a separate system utility and
the concerns of the operating system, with the point of intersection
being record processing.
• This division is arbitrary; various approaches are taken in various
systems.
File Organization
• The term file organization to refer to the logical structuring of the
records as determined by the way in which they are accessed.
• In choosing a file organization, several criteria are important:
– Short access time
– Ease of update
– Economy of storage
– Simple maintenance
– Reliability
• The relative priority of these criteria will depend on the
applications that will use the file.
• For example, if a file is only to be processed in batch mode, with
all of the records accessed every time, then rapid access for
retrieval of a single record is of minimal concern.
• A file stored on CD-ROM will never be updated, and so ease of
update is not an issue.
• These criteria may conflict. For example, for economy of storage,
there should be minimum redundancy in the data.
• On the other hand, redundancy is a primary means of increasing
the speed of access to data.
• An example of this is the use of indexes.
• The number of alternative file organizations that have been
implemented or just proposed is unmanageably large, even for a
book devoted to file systems.
• Most structures used in actual systems either fall into one of these
categories, or can be implemented with a combination of these
organizations.
• The five organizations are
– The pile
– The sequential file
– The indexed sequential file
– The indexed file
– The direct, or hashed, file
The Pile
• The least complicated form of file organization may be termed the
pile.
• Data are collected in the order in which they arrive.
• Each record consists of one burst of data.
• The purpose of the pile is simply to accumulate the mass of data
and save it.
• Records may have different fields, or similar fields in different
orders.
• Thus, each field should be self-describing, including a field name
as well as a value.
• The length of each field must be implicitly indicated by delimiters,
explicitly included as a subfield, or known as default for that field
type.
• Because there is no structure to the pile file, record access is by
exhaustive search.
• That is, if we wish to find a record that contains a particular field with a
particular value, it is necessary to examine each record in the pile until
the desired record is found or the entire file has been searched.
• If we wish to find all records that contain a particular field or contain
that field with a particular value, then the entire file must be searched.
• Pile files are encountered when data are collected and stored prior to
processing or when data are not easy to organize.
• This type of file uses space well when the stored data vary in size and
structure, is perfectly adequate for exhaustive searches, and is easy to
update.
• However, beyond these limited uses, this type of file is unsuitable for
most applications.
The Sequential File
• The most common form of file structure is the sequential file. In
this type of file, a fixed format is used for records.
• All records are of the same length, consisting of the same number
of fixed-length fields in a particular order.
• Because the length and position of each field are known, only the
values of fields need to be stored; the field name and length for
each field are attributes of the file structure.
• One particular field, usually the first field in each record, is
referred to as the key field.
• The key field uniquely identifies the record; thus key values for
different records are always different.
• Further, the records are stored in key sequence: alphabetical order
for a text key, and numerical order for a numerical key.
• Sequential files are typically used in batch applications and are
generally optimum for such applications if they involve the
processing of all the records (e.g., a billing or payroll application).
• The sequential file organization is the only one that is easily stored
on tape as well as disk.
• For interactive applications that involve queries and/or updates of
individual records, the sequential file provides poor performance.
• Access requires the sequential search of the file for a key match.
• If the entire file, or a large portion of the file, can be brought into
main memory at one time, more efficient search techniques are
possible.
• Nevertheless, considerable processing and delay are
encountered to access a record in a large sequential file.
• Additions to the file also present problems.
• Typically, a sequential file is stored in simple sequential
ordering of the records within blocks.
• That is, the physical organization of the file on tape or disk
directly matches the logical organization of the file.
• In this case, the usual procedure is to place new records in a
separate pile file, called a log file or transaction file.
• Periodically, a batch update is performed that merges the log
file with the master file to produce a new file in correct key
sequence.
• An alternative is to organize the sequential file physically as a
linked list.
• One or more records are stored in each physical block. Each
block on disk contains a pointer to the next block.
• The insertion of new records involves pointer manipulation
but does not require that the new records occupy a particular
physical block position.
• Thus, some added convenience is obtained at the cost of
additional processing and overhead.
The Indexed Sequential File
• A popular approach to overcoming the disadvantages of the
sequential file is the indexed sequential file.
• The indexed sequential file maintains the key characteristic of the
sequential file: Records are organized in sequence based on a key
field.
• Two features are added: an index to the file to support random
access, and an overflow file.
• The index provides a lookup capability to reach quickly the
vicinity of a desired record.
• The overflow file is similar to the log file used with a sequential
file but is integrated so a record in the overflow file is located by
following a pointer from its predecessor record.
• In the simplest indexed sequential structure, a single level of
indexing is used.
• The index in this case is a simple sequential file.
• Each record in the index file consists of two fields: a key field,
which is the same as the key field in the main file, and
• a pointer into the main file.
• To find a specific field, the index is searched to find the highest
key value that is equal to or precedes the desired key value.
• The search continues in the main file at the location indicated by
the pointer.
• To see the effectiveness of this approach, consider a sequential file
with 1 million records.
• To search for a particular key value will require on average
one-half million record accesses.
• Now suppose an index containing 1,000 entries is constructed,
with the keys in the index more or less evenly distributed over the
main file.
• Now it will take on average 500 accesses to the index file followed
by 500 accesses to the mainfile to find the record.
• The average search length is reduced from 500,000 to 1,000.
• Additions to the file are handled in the following manner:
• Each record in the main file contains an additional field not visible
to the application, which is a pointer to the overflow file.
• When a new record is to be inserted into the file, it is added to the
overflow file.
• The record in the main file that immediately precedes the new
record in logical sequence is updated to contain a pointer to the
new record in the overflow file.
• If the immediately preceding record is itself in the overflow file,
then the pointer in that record is updated.
• As with the sequential file, the indexed sequential file is
occasionally merged with the overflow file in batch mode.
• The indexed sequential file greatly reduces the time required to
access a single record, without sacrificing the sequential nature of
the file.
• To process the entire file sequentially, the records of the main file
are processed in sequence until a pointer to the overflow file is
found, then accessing continues in the overflow file until a null
pointer is encountered, at which time accessing of the main file is
resumed where it left off.
• To provide even greater efficiency in access, multiple levels of
indexing can be used.
• Thus the lowest level of index file is treated as a sequential file
and a higher level index file is created for that file.
• Consider again a file with 1 million records.
• A lower-level index with 10,000 entries is constructed.
• A higher-level index into the lower-level index of 100 entries can
then be constructed.
• The search begins at the higher-level index (average length = 50
accesses) to find an entry point into the lower-level index.
• This index is then searched (average length = 50) to find an entry
point into the main file, which is then searched (average length =
50).
• Thus the average length of search has been reduced from 500,000
to 1,000 to 150.
The Indexed File
• The indexed sequential file retains one limitation of the sequential
file: Effective processing is limited to that which is based on a
single field of the file.
• For example, when it is necessary to search for a record on the
basis of some other attribute than the key field, both forms of
sequential file are inadequate.
• In some applications, the flexibility of efficiently searching by
various attributes is desirable.
• To achieve this flexibility, a structure is needed that employs
multiple indexes, one for each type of field that may be the subject
of a search.
• In the general indexed file, the concept of sequentiality and a
single key are abandoned.
• Records are accessed only through their indexes.
• The result is there is now no restriction on the placement of
records as long as a pointer in at least one index refers to that
record.
• Furthermore, variable-length records can be employed.
• Two types of indexes are used.
– Exhaustive index
– Partial index
Exhaustive index
• An exhaustive index contains one entry for every record in the
main file.
• The index itself is organized as a sequential file for ease of
searching.
Partial index
• A partial index contains entries to records where the field of
interest exists.
• With variable-length records, some records will not contain all
fields.
• When a new record is added to the main file, all of the index files
must be updated.
• Indexed files are used mostly in applications where timeliness of
information is critical and where data are rarely processed
exhaustively.
• Examples are airline reservation systems and inventory control
systems.
The Direct or Hashed File
• The direct, or hashed, file exploits the capability found on disks to
access directly any block of a known address.
• As with sequential and indexed sequential files, a key field is
required in each record.
• However, there is no concept of sequential ordering here.
• The direct file makes use of hashing on the key value.
• Direct files are often used where very rapid access is required,
where fixed length records are used, and where records are always
accessed one at a time.
• Examples are directories, pricing tables, schedules, and name lists.
I/O Management
• You can manage connectivity in your active I/O configurations
through I/O operations, which offers a centralized point of control.
• In addition to allowing you to view and change the paths between
a processor and an input/output device, which may involve using
dynamic switching, it actively participates in identifying unusual
I/O conditions.
How are I/O operations performed?
• Operating System has a certain portion of code that is dedicated to
managing Input/Output in order to improve the reliability and the
performance of the system.
• A computer system contains CPUs and more than one device
controller connected to a common bus channel, generally referred
to as the device driver.
• These device drivers provide an interface to I/O devices for
communicating with the system hardware promoting ease of
communication and providing access to shared memory.
I/O Requests in operating systems
• I/O Requests are managed by Device Drivers in collaboration
with some system programs inside the I/O device. The requests are
served by OS using three simple segments :
– I/O Traffic Controller: Keeps track of the status of all
devices, control units, and communication channels.
– I/O scheduler: Executes the policies used by OS to allocate
and access the device, control units, and communication
channels.
– I/O device handler: Serves the device interrupts and heads the
transfer of data.
I/O Scheduling in operating systems
• Scheduling is used for efficient usage of computer resources
avoiding deadlock and serving all processes waiting in the queue.
• I/O Traffic Controller has 3 main tasks:
– The primary task is to check if there’s at least one path
available.
– If there exists more than one path, it must decide which one to
select.
– If all paths are occupied, its task is to analyze which path will
be available at the earliest.
• Scheduling in computing is the process of allocating resources to
carry out tasks.
• Processors, network connections, or expansion cards are examples
of the resources.
• The tasks could be processes, threads, or data flows.
• A process referred to as a scheduler is responsible for scheduling.
• Schedulers are frequently made to keep all computer resources
active (as in load balancing), efficiently divide up system
resources among multiple users, or reach a desired level of service.
I/O Scheduler
• I/O Scheduler functions similarly to Process scheduler, it allocates
the devices, control units, and communication channels.
• However, under a heavy load of I/O requests, Scheduler must
decide what request should be served first and for that we multiple
queues to be managed by OS.
• The major difference between a Process scheduler< and an I/O
scheduler is that I/O requests are not preempted: Once the channel
program has started, it’s allowed to continue to completion.
Although it is feasible because programs are relatively short (50 to
100 ms).
• Some modern OS allows I/O Scheduler to serve higher priority
requests.
• In simpler words, If an I/O request has higher priority then they
are served before other I/O requests with lower priority.
• The I/O scheduler works in coordination with the I/O traffic
controller to keep track of which path is being served for the
current I/O request.
I/O Device Handler
• I/O Device Handler manages the I/O interrupts (if any) and
scheduling algorithms.
• A few I/O handling algorithms are :
– FCFS [First come first server].
– SSTF [Shortest seek time first].
– SCAN
– Look
• N-Step Scan
• C-SCAN
• C-LOOK
• Every scheduling algorithm aims to minimize arm movement,
mean response time, and variance in response time.
First Come First Serve [FCFS]:
• It is one of the simplest device-scheduling algorithms since it is
easy to program and essentially fair to users (I/O devices).
• The only barrier could be the high seek time, so any other
algorithm that can surpass the minimum seek time is suitable for
scheduling.
Shortest Seek Time First [SSTF]:
• It uses the same ideology as the Shortest Job First in process
scheduling, where the shortest processes are served first and
longer processes have to wait for their turn.
• Comparing the SJF concept in I/O scheduling, the request with the
track closest to the one being served (The one with the shortest
distance to travel on disk) is next to be satisfied.
• The main advantage over FCFS is that it minimizes overall seek
time.
• It favors easy-to-reach requests and postpones traveling to those
that are out of the way.
SCAN Algorithm:
• Scan uses a status flag that tells the direction of the arm, it tells
whether the arm is moving toward the center of the disk or to the
other side.
• This algorithm moves the arm from the end of the disk to the
center track servicing every request in its way.
• When it reaches the innermost track, it reverses the direction and
moves towards outer tracks on the disk, again servicing every
request in its path.
LOOK [Elevator Algorithm]:
• It’s a variation of the SCAN algorithm, here arm doesn’t
necessarily go all the way to either side on disk unless there are
requests pending.
• It looks ahead to a request before servicing it.
• The major advantage of LOOK over SCAN is that it discards the
indefinite delay of I/O requests.
N-Step Scan:
• It holds all the pending requests until the arm starts its way back.
• New requests are grouped for the next cycle of rotation.
C-SCAN [Circular SCAN] :
• It provides a uniform wait time as the arm serves requests on its
way during the inward cycle.
• To know more, refer Difference between SCAN and C-SCAN.
C-LOOK [Optimized version of C-SCAN] :
• Arm doesn’t necessarily return to the lowest-numbered track, it
returns from the lowest request to be served.
• It optimized the C-SCAN as the arm doesn’t move to the end of
the disk if not required.
Disk Scheduling
• As we know, a process needs two type of time, CPU time and IO
time.
• For I/O, it requests the Operating system to access the disk.
• However, the operating system must be fare enough to satisfy each
request and at the same time, operating system must maintain the
efficiency and speed of process execution.
• The technique that operating system uses to determine the request
which is to be satisfied next is called disk scheduling.
FCFS Scheduling
• First come first serve, as name suggest this algorithm entertains
the task in the order they arrived in the disk queue.
• It is the simplest and easy to understand disk scheduling
algorithm.
• In this the head or pointer moves in the direction in which the task
arrives and moves till all request is served.
• FCFS provides more average waiting time and response time.
• However, FCFS algorithm has more fair policy of handling
upcoming requests.
• Consider a disk with 200 tracks (0-199) and the disk queue having
I/O requests in the following order as follows: 98, 183, 40, 122,
10, 124, 65. The current head position of the Read\Write head is
53. Calculate the total number of track movements of Read/Write
head using FCFS algorithm.
=(98-53)+
(183-98)+
(183-40)+
(122-40)+
(122-10)+
(124-10)+
(124-65)
= 640
SSTF Disk Scheduling
• SSTF stands for Shortest Seek Time First, as the name suggests it
serves the request which is closest to the current position of head
or pointer.
• In this algorithm direction of the head pointer matters a lot.
• If somehow, we encounter a tie between requests then the head
will serve the request in its ongoing direction.
• SSTF algorithm is very efficient in seek time as compared to
FCFS.
• Consider a disk with 200 tracks (0-199) and the disk queue having
I/O requests in the following order as follows: 98, 183, 40, 122,
10, 124, 65. The current head position of the Read\Write head is
53 and will move in Right direction. Calculate the total number of
track movements of Read/Write head using SSTF algorithm.