Operating System Notes: Index
Operating System Notes: Index
Operating System Notes: Index
INDEX:
S.NO. TOPIC P.NO.
1 Introduction & Definition 2
2 Process state & Process State Transitions 4
3 Process Control Block 6
4 Operations on Process 7
5 Inter process Communication 12
6 Process Scheduling concepts and performing criteria 19
7 CPU Scheduling Algorithms 23
8 Synchronization 28
9 Critical Section Problem 29
10 Semaphores 31
11 Classical Problems of concurrency 34
12 CPU Scheduling Numerical 39
1
A process is basically a program in execution.
The execution of a process must progress in a sequential fashion.
A process is defined as an entity which represents the basic unit of work to be implemented in
the system.
To put it in simple terms, we write our computer programs in a text file and when we execute
this program, it becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into four
sections ─ stack, heap, text and data. The following image shows a simplified layout of a
process inside main memory −
1 Stack
The process Stack contains the temporary data such as method/function parameters,
return address and local variables.
2
Heap
2
This is dynamically allocated memory to a process during its run time.
Text
3 This includes the current activity represented by the value of Program Counter and
the contents of the processor's registers.
Data
4
This section contains the global and static variables.
Program
A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language. For
example, here is a simple program written in C programming language –
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
A computer program is a collection of instructions that performs a specific task when executed
by a computer. When we compare a program with a process, we can conclude that a process is a
dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as an algorithm. A
collection of computer programs, libraries and related data are referred to as software.
Attributes or Characteristics of a Process
A process has the following attributes.
3
3. CPU registers: Like the Program Counter (CPU registers must be saved and restored
when a process is swapped in and out of CPU)
4. I/O status information: For example, devices allocated to the process, open files, etc
5. CPU scheduling information: For example, Priority (Different processes may have
different priorities, for example a shorter process assigned high priority in the shortest job
first scheduling)
Process States
State Diagram
The process, from its creation to completion, passes through various states. The minimum
number of states is five.
4
The names of the states are not standardized although the process may be in one of the following
states during execution.
1. New
A program which is going to be picked up by the OS into the main memory is called a new
process.
2. Ready
Whenever a process is created, it directly enters in the ready state, in which, it waits for the CPU
to be assigned. The OS picks the new processes from the secondary memory and put all of them
in the main memory.
The processes which are ready for the execution and reside in the main memory are called ready
state processes. There can be many processes present in the ready state.
3. Running
One of the processes from the ready state will be chosen by the OS depending upon the
scheduling algorithm. Hence, if we have only one CPU in our system, the number of running
processes for a particular time will always be one. If we have n processors in the system then we
can have n processes running simultaneously.
4. Block or wait
From the Running state, a process can make the transition to the block or wait state depending
upon the scheduling algorithm or the intrinsic behavior of the process.
When a process waits for a certain resource to be assigned or for the input from the user then the
OS move this process to the block or wait state and assigns the CPU to the other processes.
5. Completion or termination
When a process finishes its execution, it comes in the termination state. All the context of the
process (Process Control Block) will also be deleted the process will be terminated by the
Operating system.
6. Suspend ready
A process in the ready state, which is moved to secondary memory from the main memory due to
lack of the resources (mainly primary memory) is called in the suspend ready state.
If the main memory is full and a higher priority process comes for the execution then the OS
have to make the room for the process in the main memory by throwing the lower priority
5
process out into the secondary memory. The suspend ready processes remain in the secondary
memory until the main memory gets available.
7. Suspend wait
Instead of removing the process from the ready queue, it's better to remove the blocked process
which is waiting for some resources in the main memory. Since it is already waiting for some
resource to get available hence it is better if it waits in the secondary memory and make room for
the higher priority process. These processes complete their execution once the main memory gets
available and their wait is finished.
The role of the PCBs is central in process management: they are accessed and/or modified by
most utilities, particularly those involved with scheduling and resource management.
In multitasking operating systems, the PCB stores data needed for correct and efficient process
management. Though the details of these structures are system-dependent, common elements fall
in three main categories:
• Process identification
• Process state
• Process control
Status tables exist for each relevant entity, like describing memory, I/O devices, files and
processes.
Memory tables, for example, contain information about the allocation of main and secondary
(virtual) memory for each process, authorization attributes for accessing memory areas shared
among different processes, etc. I/O tables may have entries stating the availability of a device or
its assignment to a process, the status of I/O operations, the location of memory buffers used for
them, etc.
Process identification data include a unique identifier for the process (almost invariably an
integer) and, in a multiuser-multitasking system, data such as the identifier of the parent process,
user identifier, user group identifier, etc. The process id is particularly relevant since it is often
used to cross-reference the tables defined above, e.g. showing which process is using which I/O
devices, or memory areas.
Process state data define the status of a process when it is suspended, allowing the OS to restart
it later. This always includes the content of general-purpose CPU registers, the CPU process
6
status word, stack and frame pointers, etc. During context switch, the running process is stopped
and another process runs. The kernel must stop the execution of the running process, copy out
the values in hardware registers to its PCB, and update the hardware registers with the values
from the PCB of the new process.
Process control information is used by the OS to manage the process itself. This includes:
• Process scheduling state–The state of the process in terms of "ready", "suspended", etc.,
and other scheduling information as well, such as priority value, the amount of time elapsed
since the process gained control of the CPU or since it was suspended. Also, in case of a
suspended process, event identification data must be recorded for the event the process is
waiting for.
• Process structuring information–the process's children id's, or the id's of other processes
related to the current one in some functional way, which may be represented as a queue, a
ring or other data structures
• Inter-process communication information–flags, signals and messages associated with the
communication among independent processes
• Process Privileges–allowed/disallowed access to system resources
• Process State–new, ready, running, waiting, dead
• Process Number (PID)–unique identification number for each process (also known
as Process ID)
• Program Counter (PC)–A pointer to the address of the next instruction to be executed for
this process
• CPU Registers–Register set where process needs to be stored for execution for running state
• CPU Scheduling Information–information scheduling CPU time
• Memory Management Information–page table, memory limits, segment table
• Accounting Information–amount of CPU used for process execution, time limits, execution
ID etc.
• I/O Status Information–list of I/O devices allocated to the process.
PCB must be kept in an area of memory protected from normal process access. In some
operating systems the PCB is placed at the beginning of the kernel stack of the process.
Once the process is created, it will be ready and come into the ready queue (main memory) and
will be ready for the execution.
7
2. Scheduling
Out of the many processes present in the ready queue, the Operating system chooses one process
and start executing it. Selecting the process which is to be executed next, is known as scheduling.
3. Execution
Once the process is scheduled for the execution, the processor starts executing it. Process may
come to the blocked or wait state during the execution then in that case the processor starts
executing the other processes.
4. Deletion/killing
Once the purpose of the process gets over then the OS will kill the process. The Context of the
process (PCB) will be deleted and the process gets terminated by the Operating system.
8
9
10
11
Inter Process Communication (IPC)
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:
1. Shared Memory
2. Message passing
Figure 1 below shows a basic structure of communication between processes via the shared
memory method and via the message passing method.
An operating system can implement both methods of communication. First, we will discuss the
shared memory methods of communication and then message passing.
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.
Let’s discuss an example of communication between processes using the shared memory
method.
12
i) Shared Memory Method
Ex: Producer-Consumer problem
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.
We will discuss 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.
13
Producer Process Code
item nextProduced;
while(1){
shared_buff[free_index] = nextProduced;
item nextConsumed;
while(1){
while((free_index == full_index);
nextConsumed = shared_buff[full_index];
14
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.
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.
15
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.
While implementing the link, there are some questions that need to be kept in mind like:
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.
In 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.
16
Message Passing through Exchanging the Messages
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 sends keep on failing, the receiver will have to wait
indefinitely. That is why we also consider the other possibility of message passing.
In Direct message passing, the process which wants to communicate must explicitly name
the recipient or sender of the communication.
17
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
▫ Allow a link to be associated with at most two processes
▫ Allow only one process at a time to execute a receive operation
▫ Allow the system to select arbitrarily the receiver. Sender is notified who the
receiver was.
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.
18
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.
• Pipe
• Socket
• Remote Procedural calls (RPCs)
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.
Process scheduling is an essential part of Multiprogramming operating systems. Such operating
systems allow more than one process to be loaded into the executable memory at a time and the
loaded process shares the CPU using time multiplexing.
19
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; in the above diagram, it has been
merged with the CPU.
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
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.
20
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 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.
2 Speed is lesser than short Speed is fastest among Speed is in between both short
term scheduler other two and long term scheduler.
5 It selects processes from It selects those processes It can re-introduce the process
pool and loads them into which are ready to execute into memory and execution can
memory for execution be continued.
21
Context Switch
A context switch 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. When the process is switched, the following
information is stored for later use.
• Program Counter
• Scheduling information
• Base and limit register value
• Currently used register
• Changed State
• I/O State information
• Accounting information
22
CPU Scheduling Algorithms
What is CPU Scheduling?
CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold. The main task of CPU scheduling is to make sure that whenever the
CPU remains idle, the OS at least select one of the processes available in the ready queue for
execution. The selection process will be carried out by the CPU scheduler. It selects one of the
processes in memory that are ready for execution.
Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is
important to run a task with a higher priority before another lower priority task, even if the lower
priority task is still running. The lower priority task holds for some time and resumes when the
higher priority task finishes its execution.
Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific process. The process
that keeps the CPU busy will release the CPU either by switching context or terminating. It is the
only method that can be used for various hardware platforms. That’s because it doesn’t need
special hardware (for example, a timer) like preemptive scheduling.
23
When scheduling is Preemptive or Non-Preemptive?
24
Maximize:
CPU utilization: CPU utilization is the main task in which the operating system needs to make
sure that CPU remains as busy as possible. It can range from 0 to 100 percent. However, for the
RTOS, it can be range from 40 percent for low-level and 90 percent for the high-level system.
Throughput: The number of processes that finish their execution per unit time is known
Throughput. So, when the CPU is busy executing the process, at that time, work is being done,
and the work completed per unit time is called Throughput.
Minimize:
Waiting time: Waiting time is an amount that specific process needs to wait in the ready queue.
Response time: It is an amount to time in which the request was submitted until the first
response is produced.
Turnaround Time: Turnaround time is an amount of time to execute a specific process. It is the
calculation of the total time spent waiting to get into the memory, waiting in the queue and,
executing on the CPU. The period between the time of process submission to the completion
time is the turnaround time.
What is Dispatcher?
It is a module that provides control of the CPU to the process. The Dispatcher should be fast so
that it can run on every context switch. Dispatch latency is the amount of time needed by the
CPU scheduler to stop one process and start another.
• Context Switching
• Switching to user mode
• Moving to the correct location in the newly loaded program.
25
Scheduling Algorithms
First Come First Serve
First Come First Serve is the full form of FCFS. It is the easiest and most simple CPU scheduling
algorithm. In this type of algorithm, the process which requests the CPU gets the CPU allocation
first. This scheduling method can be managed with a FIFO queue.
As the process enters the ready queue, its PCB (Process Control Block) is linked with the tail of
the queue. So, when CPU becomes free, it should be assigned to the process at the beginning of
the queue.
26
Shortest Remaining Time
The full form of SRT is Shortest remaining time. It is also known as SJF preemptive scheduling.
In this method, the process will be allocated to the task, which is closest to its completion. This
method prevents a newer ready state process from holding the completion of an older process.
• This method is mostly applied in batch environments where short jobs are required to be
given preference.
• This is not an ideal method to implement it in a shared system where the required CPU
time is unknown.
• Associate with each process as the length of its next CPU burst. So that operating system
uses these lengths, which helps to schedule the process with the shortest possible time.
Priority scheduling is a method of scheduling processes based on priority. In this method, the
scheduler selects the tasks to work as per the priority.
Priority scheduling also helps OS to involve priority assignments. The processes with higher
priority should be carried out first, whereas jobs with equal priorities are carried out on a round-
robin or FCFS basis. Priority can be decided based on memory requirements, time requirements,
etc.
Round-Robin Scheduling
Round robin is the oldest, simplest scheduling algorithm. The name of this algorithm comes from
the round-robin principle, where each person gets an equal share of something in turn. It is
mostly used for scheduling algorithms in multitasking. This algorithm method helps for
starvation free execution of processes.
SJF is a full form of (Shortest job first) is a scheduling algorithm in which the process with the
shortest execution time should be selected for execution next. This scheduling method can be
preemptive or non-preemptive. It significantly reduces the average waiting time for other
processes awaiting execution.
27
Characteristics of SJF Scheduling
Process Synchronization
On the basis of synchronization, processes are categorized as one of the following two types:
• Independent Process: Execution of one process does not affects the execution of other
processes.
• Cooperative Process: Execution of one process affects the execution of other processes.
Process synchronization problem arises in the case of Cooperative process also because
resources are shared in Cooperative processes.
Race Condition:
When more than one processes are 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, 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.
28
Critical Section Problem
Critical section is a code segment that can be accessed by only one process at a time. Critical
section contains shared variables which need to be synchronized to maintain consistency of data
variables.
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 in the critical section next, and the
selection cannot 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.
The general structure for a two Process Solution for Critical Section Problem- Algorithm 1 is:
turn = 0;
do
{
while (turn != 0) ; //if not P0's turn , wait indefinitely
//remainder section
} while (1); //loop again
29
The problem with this Algorithm is that it doesn't support the necessary requirement of Progress.
It forces the critical section to be owned in equal turns by P0 -> P1 -> P0 -> P1 -> ... To get over
this problem Algorithm 2 is used where variable turn is replaced by an array flag[]. The general
structure of algorithm 2 is:
do
{
flag[0] = T ;
while (flag[1]);//if flag[1] is true wait indefinitely
//remainder section
} while (1); //loop again
Here, a process can execute its critical section repeatedly if it needs. (Although this algorithm too
doesn't support’s progress)
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:
30
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.
Semaphore
a semaphore is a variable or abstract data type used to control access to a common resource by
multiple threads and avoid critical section problems in a concurrent system such as
a multitasking operating system. Semaphores are a type of synchronization primitive. A trivial
semaphore is a plain variable that is changed (for example, incremented or decremented, or
toggled) depending on programmer-defined conditions.
A useful way to think of a semaphore as used in a real-world system is as a record of how many
units of a particular resource are available, coupled with operations to adjust that
record safely (i.e., to avoid race conditions) as units are acquired or become free, and, if
necessary, wait until a unit of the resource becomes available.
31
Semaphores are a useful tool in the prevention of race conditions; however, their use is not a
guarantee that a program is free from these problems. Semaphores which allow an arbitrary
resource count are called counting semaphores, while semaphores which are restricted to the
values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores and are
used to implement locks.
Semaphores are integer variables that are used to solve the critical section problem by using two
atomic operations, wait and signal that are used for process synchronization.
The definitions of wait and signal are as follows −
Wait
The wait operation decrements the value of its argument S, if it is positive. If S is negative or
zero, then no operation is performed.
wait(S)
{
while (S<=0);
S--;
}
Signal
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary semaphores.
Details about these are given as follows −
Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These semaphores
are used to coordinate the resource access, where the semaphore count is the number of available
resources. If the resources are added, semaphore count automatically incremented and if the
resources are removed, the count is decremented.
32
Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0 and 1. The
wait operation only works when the semaphore is 1 and the signal operation succeeds when
semaphore is 0. It is sometimes easier to implement binary semaphores than counting
semaphores.
Advantages of Semaphores
• Semaphores allow only one process into the critical section. They follow the
mutual exclusion principle strictly and are much more efficient than some other
methods of synchronization.
• There is no resource wastage because of busy waiting in semaphores as processor
time is not wasted unnecessarily to check if a condition is fulfilled to allow a
process to access the critical section.
• Semaphores are implemented in the machine independent code of the microkernel.
So they are machine independent.
Disadvantages of Semaphores
33
Example of Semaphore
The below-given program is a step by step implementation, which involves usage and
declaration of semaphore.
These problems are used for testing nearly every newly proposed synchronization scheme. The
following problems of synchronization are considered as classical problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
4. Sleeping Barber Problem
These are summarized, for detailed explanation, you can view the linked articles for each.
• Bounded-buffer (or Producer-Consumer) Problem:
Bounded Buffer problem is also called producer consumer problem. This problem is
generalized in terms of the Producer-Consumer problem. Solution to this problem
is, creating two counting semaphores “full” and “empty” to keep track of the current
number of full and empty buffers respectively. Producers produce a product and
consumers consume the product, but both use of one of the containers each time.
• Dining-Philosophers Problem:
34
The Dining Philosopher Problem states that K philosophers seated around a circular
table with one chopstick between each pair of philosophers. There is one chopstick
between each philosopher. A philosopher may eat if he can pickup the two
chopsticks adjacent to him. One chopstick may be picked up by any one of its
adjacent followers but not both. This problem involves the allocation of limited
resources to a group of processes in a deadlock-free and starvation-free manner.
• Once a writer is ready, it performs its write. Only one writer may write at
a time.
35
• Sleeping Barber Problem:
Barber shop with one barber, one barber chair and N chairs to wait in. When no
customers the barber goes to sleep in barber chair and must be woken when a
customer comes in. When barber is cutting hair new customers take empty seats to
wait, or leave if no vacancy.
36
37
38
CPU Scheduling Numerical
39
40
41
Q4 SRTF or Preemptive SJF
Q5.
42
Q6
Suppose that the following processes arrive for execution at the times indicated. Each process
will run for the amount of time listed. In answering the questions, use non-preemptive
scheduling, and base all decisions on the information you have at the time the decision must be
made.
43
a. What is the average turnaround time for these processes with the FCFS
scheduling algorithm?
b. b. What is the average turnaround time for these processes with the SJF
scheduling algorithm?
c. c. The SJF algorithm is supposed to improve performance, but notice that we
chose to run process P1 at time 0 because we did not knothat two shorter
processes would arrive soon. Compute what the average turnaround time will be
if the CPU is left idle for the rst 1 unit and then SJF scheduling is used.
Remember that processes P1 and P2 are waiting during this idle time, so their
waiting time may increase. This algorithm could be known as future-knowledge
scheduling.
Answer:
a. 10.53
b. 9.53
c. 6.86
Remember that turnaround time is finishing time minus arrival time, so you have to subtract the
arrival times to compute the turnaround times. FCFS is 11 if you forget to subtract arrival time.
44