O.S - Unit-2&3
O.S - Unit-2&3
S-----UNIT -2
Process scheduling allows OS to allocate a time interval of CPU execution for each
process. Another important reason for using a process scheduling system is that it
keeps the CPU busy all the time. This allows you to get the minimum response
time for programs.
Process
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.
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 –
S.N. Component & Description
A Program In execution
An instance of running program
The entity that can be assigned to and executed as a process
1
Stack
The process Stack contains the temporary data such as method/function parameters, return
address and local variables.
2
Heap
This is dynamically allocated memory to a process during its run time.
3
Text-
This includes the current activity represented by the value of Program Counter and the contents of
the processor's registers.
4
Data
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");
return0;
}
In general, a process can have one of the following five states at a time.
1
Start
This is the initial state when a process is first started/created.
2
Ready
The process is waiting to be assigned to a processor. Ready processes are waiting to have the
processor allocated to them by the operating system so that they can run. Process may come into this
state after Start state or while running it by but interrupted by the scheduler to assign CPU to some
other process.
3
Running
Once the process has been assigned to a processor by the OS scheduler, the process state is set to
running and the processor executes its instructions.
4
Waiting
Process moves into the waiting state if it needs to wait for a resource, such as waiting for user input,
or waiting for a file to become available.
5
Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system, it is moved to the
terminated state where it waits to be removed from main memory.
A Process Control Block is a data structure maintained by the Operating System for every
process. The PCB is identified by an integer process ID (PID). A PCB keeps all the
information needed to keep track of a process as listed below in the table –
1
Process State
The current state of the process i.e., whether it is ready, running, waiting, or whatever.
2
Process privileges
This is required to allow/disallow access to system resources.
3
Process ID
Unique identification for each of the process in the operating system.
4
Pointer
A pointer to parent process.
5
Program Counter
Program Counter is a pointer to the address of the next instruction to be executed for this process.
6
CPU registers
Various CPU registers where process need to be stored for execution for running state.
7
CPU Scheduling Information
Process priority and other scheduling information which is required to schedule the process.
8
Memory management information
This includes the information of page table, memory limits, Segment table depending on memory
used by the operating system.
9
Accounting information
This includes the amount of CPU used for process execution, time limits, execution ID etc.
10
IO status information
This includes a list of I/O devices allocated to the process.
The architecture of a PCB is completely dependent on Operating System and may contain
different information in different operating systems. Here is a
The PCB is maintained for a process throughout its lifetime and is deleted once the process
terminates.
Definition
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 a 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.
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.
Two-State Process Model
Two-state process model refers to running and non-running states which are described
below −
1
Running
When a new process is created, it enters into the system as in the running state.
2
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
2 Speed is lesser than short term Speed is fastest among other Speed is in between both short and
scheduler two long term scheduler.
3 It controls the degree of It provides lesser control over It reduces the degree of
multiprogramming degree of multiprogramming multiprogramming.
4 It is almost absent or minimal in It is also minimal in time It is a part of Time sharing systems.
time sharing system sharing system
5 It selects processes from pool and It selects those processes which It can re-introduce the process into
loads them into memory for are ready to execute memory and execution can be
execution continued.
Program Counter
Scheduling information
Base and limit register value
Currently used register
Changed State
I/O State information
Accounting information
Operations on Processes
Process: A process is an activity of executing a program. Basically, it is a program
under execution. Every process needs certain resources to complete its task.
Operation on a Process:
The execution of a process is a complex activity. It involves various operations.
Following are the operations that are performed while execution of a process:
1. Creation: This the initial step of process execution activity. Process creation means
the construction of a new process for the execution. This might be performed by
system, user or old process itself. There are several events that leads to the process
creation. Some of the such events are following:
When we start the computer, system creates several background processes.
A user may request to create a new process.
A process can create a new process itself while executing.
Batch system takes initiation of a batch job.
2. Scheduling/Dispatching: The event or activity in which the state of the process is
changed from ready to running. It means the operating system puts the process from
ready state into the running state. Dispatching is done by operating system when the
resources are free or the process has higher priority than the ongoing process. There
are various other cases in which the process in running state is preempted and process
in ready state is dispatched by the operating system.
3. Blocking: When a process invokes an input-output system call that blocks the
process and operating system put in block mode. Block mode is basically a mode
where process waits for input-output. Hence on the demand of process itself, operating
system blocks the process and dispatches another process to the processor. Hence, in
process blocking operation, the operating system puts the process in ‘waiting’ state.
4. Preemption: When a timeout occurs that means the process hadn’t been terminated
in the allotted time interval and next process is ready to execute, then the operating
system preempts the process. This operation is only valid where CPU scheduling
supports preemption. Basically this happens in priority scheduling where on the
incoming of high priority process the ongoing process is preempted. Hence, in process
preemption operation, the operating system puts the process in ‘ready’ state.
5. Termination: Process termination is the activity of ending the process. In other
words, process termination is the relaxation of computer resources taken by the
process for the execution. Like creation, in termination also there may be several
events that may lead to the process termination. Some of them are:
Process completes its execution fully and it indicates to the OS that it has
finished.
Operating system itself terminates the process due to service errors.
There may be problem in hardware that terminates the process.
One process can be terminated by another process.
cooperating processes
Cooperating processes are those that can affect or are affected by other processes running on
the system. Cooperating processes may share data with each other.
Modularity
Modularity involves dividing complicated tasks into smaller subtasks. These subtasks
can completed by different cooperating processes. This leads to faster and more
efficient completion of the required tasks.
Information Sharing
Convenience
There are many tasks that a user needs to do such as compiling, printing, editing etc.
It is convenient if these tasks can be managed by cooperating processes.
Computation Speedup
Subtasks of a single task can be performed parallely using cooperating processes. This
increases the computation speedup as the task can be executed faster. However, this is
only possible if the system has multiple processing elements.
Methods of Cooperation
Cooperating processes can coordinate with each other using shared data or messages. Details
about these are given as follows −
Cooperation by Sharing
The cooperating processes can cooperate with each other using shared data such as
memory, variables, files, databases etc. Critical section is used to provide data
integrity and writing is mutually exclusive to prevent inconsistent data.
In the above diagram, Process P1 and P2 can cooperate with each other using shared
data such as memory, variables, files, databases etc.
Cooperation by Communication
The cooperating processes can cooperate with each other using messages. This may
lead to deadlock if each process is waiting for a message from the other to perform a
operation. Starvation is also possible if a process never receives a message.
Thread
What is a Thread?
A thread is a path of execution within a process. A process can contain multiple
threads.
Why Multithreading?
A thread is also known as lightweight process. The idea is to achieve parallelism by
dividing a process into multiple threads. For example, in a browser, multiple tabs can
be different threads. MS Word uses multiple threads: one thread to format the text,
another thread to process inputs, etc. More advantages of multithreading are discussed
below
Process vs Thread?
The primary difference is that threads within the same process run in a shared memory
space, while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads
share with other threads their code section, data section, and OS resources (like open
files and signals). But, like process, a thread has its own program counter (PC),
register set, and stack space.
Advantages of Thread over Process
1. Responsiveness: If the process is divided into multiple threads, if one thread
completes its execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to
process context switch. Process context switching requires more overhead from the
CPU.
3. Effective utilization of multiprocessor system: If we have multiple threads in a
single process, then we can schedule multiple threads on multiple processor. This will
make process execution faster.
4. Resource sharing: Resources like code, data, and files can be shared among all
threads within a process.
Note: stack and registers can’t be shared among the threads. Each thread has its own
stack and registers.
5. Communication: Communication between multiple threads is easier, as the threads
shares common address space. while in process we have to follow some specific
communication technique for communication between two process.
6. Enhanced throughput of the system: If a process is divided into multiple threads,
and each thread function is considered as one job, then the number of jobs completed
per unit of time is increased, thus increasing the throughput of the system.
Types of Threads
There are two types of threads.
User Level Thread
Kernel Level Thread
1. CPU utilisation –
The main objective of any CPU scheduling algorithm is to keep the CPU
as busy as possible. Theoretically, CPU utilisation can range from 0 to 100
but in a real-time system, it varies from 40 to 90 percent depending on the
load upon the system.
2. Throughput –
A measure of the work done by CPU is the number of processes being
executed and completed per unit time. This is called throughput. The
throughput may vary depending upon the length or duration of processes.
3. Turnaround time –
For a particular process, an important criteria is how long it takes to
execute that process. The time elapsed from the time of submission of a
process to the time of completion is known as the turnaround time. Turn-
around time is the sum of times spent waiting to get into memory, waiting
in ready queue, executing in CPU, and waiting for I/O.
4. Waiting time –
A scheduling algorithm does not affect the time required to complete the
process once it starts execution. It only affects the waiting time of a
process i.e. time spent by a process waiting in the ready queue.
5. Response time –
In an interactive system, turn-around time is not the best criteria. A
process may produce some output fairly early and continue computing
new results while previous results are being output to the user. Thus
another criteria is the time taken from submission of the process of request
until the first response is produced. This measure is called response time.
There are various algorithms which are used by the Operating System to schedule the
processes on the processor in an efficient way.
Or
There are the following algorithms which can be used to schedule the jobs.
2. Round Robin
In the Round Robin scheduling algorithm, the OS defines a time quantum (slice). All the
processes will get executed in the cyclic way. Each of the process will get the CPU for a
small amount of time (called time quantum) and then get back to the ready queue to wait for
its next turn. It is a preemptive type of scheduling.
Skip Ad
Scheduling algorithms
P0 0-0=0
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
P0 0 5 0
P1 1 3 5
P2 2 8 14
P3 3 6 8
P0 0-0=0
P1 5-1=4
P2 14 - 2 = 12
P3 8-3=5
P0 0 5 1 0
P1 1 3 2 11
P2 2 8 1 14
P3 3 6 3 5
P1 11 - 1 = 10
P2 14 - 2 = 12
P3 5-3=2
P1 (3 - 1) = 2
P3 (9 - 3) + (17 - 12) = 11
One approach is when all the scheduling decisions and I/O processing are handled by
a single processor which is called the Master Server and the other processors
executes only the user code. This is simple and reduces the need of data sharing. This
entire scenario is called Asymmetric Multiprocessing.
A second approach uses Symmetric Multiprocessing where each processor is self
scheduling. All processes may be in a common ready queue or each processor may
have its own private queue for ready processes. The scheduling proceeds further by
having the scheduler for each processor examine the ready queue and select a process
to execute.
Processor Affinity –
Processor Affinity means a processes has an affinity for the processor on which it is
currently running.
When a process runs on a specific processor there are certain effects on the cache
memory. The data most recently accessed by the process populate the cache for the
processor and as a result successive memory access by the process are often satisfied
in the cache memory. Now if the process migrates to another processor, the contents
of the cache memory must be invalidated for the first processor and the cache for the
second processor must be repopulated. Because of the high cost of invalidating and
repopulating caches, most of the SMP(symmetric multiprocessing) systems try to
avoid migration of processes from one processor to another and try to keep a process
running on the same processor. This is known as PROCESSOR AFFINITY.
There are two types of processor affinity:
1. Soft Affinity – When an operating system has a policy of attempting to
keep a process running on the same processor but not guaranteeing it will
do so, this situation is called soft affinity.
2. Hard Affinity – Hard Affinity allows a process to specify a subset of
processors on which it may run. Some systems such as Linux implements
soft affinity but also provide some system calls like sched_setaffinity() that
supports hard affinity.
Load Balancing –
Multicore Processors –
In multicore processors multiple processor cores are places on the same physical
chip. Each core has a register set to maintain its architectural state and thus appears to
the operating system as a separate physical processor. SMP systems that use
multicore processors are faster and consume less power than systems in which each
processor has its own physical chip.
However multicore processors may complicate the scheduling problems. When
processor accesses memory then it spends a significant amount of time waiting for the
data to become available. This situation is called MEMORY STALL. It occurs for
various reasons such as cache miss, which is accessing the data that is not in the cache
memory. In such cases the processor can spend upto fifty percent of its time waiting
for data to become available from the memory. To solve this problem recent hardware
designs have implemented multithreaded processor cores in which two or more
hardware threads are assigned to each core. Therefore if one thread stalls while
waiting for the memory, core can switch to another thread.
There are two ways to multithread a processor :
1. Coarse-Grained Multithreading – In coarse grained multithreading a
thread executes on a processor until a long latency event such as a memory
stall occurs, because of the delay caused by the long latency event, the
processor must switch to another thread to begin execution. The cost of
switching between threads is high as the instruction pipeline must be
terminated before the other thread can begin execution on the processor
core. Once this new thread begins execution it begins filling the pipeline
with its instructions.
2. Fine-Grained Multithreading – This multithreading switches between
threads at a much finer level mainly at the boundary of an instruction cycle.
The architectural design of fine grained systems include logic for thread
switching and as a result the cost of switching between threads is small.
In this type of multiple-processor scheduling even a single CPU system acts like a
multiple-processor system. In a system with Virtualization, the virtualization presents
one or more virtual CPU to each of virtual machines running on the system and then
schedules the use of physical CPU among the virtual machines. Most virtualized
environments have one host operating system and many guest operating systems. The
host operating system creates and manages the virtual machines. Each virtual machine
has a guest operating system installed and applications run within that guest.Each
guest operating system may be assigned for specific use cases,applications or users
including time sharing or even real-time operation. Any guest operating-system
scheduling algorithm that assumes a certain amount of progress in a given amount of
time will be negatively impacted by the virtualization. A time sharing operating
system tries to allot 100 milliseconds to each time slice to give users a reasonable
response time. A given 100 millisecond time slice may take much more than 100
milliseconds of virtual CPU time. Depending on how busy the system is, the time
slice may take a second or more which results in a very poor response time for users
logged into that virtual machine. The net effect of such scheduling layering is that
individual virtualized operating systems receive only a portion of the available CPU
cycles, even though they believe they are receiving all cycles and that they are
scheduling all of those cycles.Commonly, the time-of-day clocks in virtual machines
are incorrect because timers take no longer to trigger than they would on dedicated
CPU’s.
Virtualizations can thus undo the good scheduling-algorithm efforts of the operating
systems within virtual machines.
System call provides an interface between user program and operating system. The structure
of system call is as follows −
When the user wants to give an instruction to the OS then it will do it through system calls.
Or a user program can access the kernel which is a part of the OS through system calls.
It is a programmatic way in which a computer program requests a service from the kernel of
the operating system.
Types of system calls
The different system calls are as follows −
wait − Processes are supposed to wait for other processes to complete their work.
Types of System Calls Windows Linux
fork() − A parent process always uses a fork for creating a new child process. The child
process is generally called a copy of the parent. After execution of fork, both parent and child
execute the same program in separate processes.
exec() − This function is used to replace the program executed by a process. The child
sometimes may use exec after a fork for replacing the process memory space with a new
program executable making the child execute a different program than the parent.
exit() − This function is used to terminate the process.
wait() − The parent uses a wait function to suspend execution till a child terminates. Using
wait the parent can obtain the exit status of a terminated child.
Process Control CreateProcess() fork()
ExitProcess() exit()
WaitForSingleObject() wait()
There are many different system calls as shown above. Details of some of those system calls
are as follows −
exec()
This system call runs an executable file in the context of an already running process. It
replaces the previous executable file. This is known as an overlay. The original process
identifier remains since a new process is not created but data, heap, stack etc. of the process
are replaced by the new process.
fork()
Processes use the fork() system call to create processes that are a copy of themselves.
This is one of the major methods of process creation in operating systems. When a parent
process creates a child process and the execution of the parent process is suspended until the
child process executes. When the child process completes execution, the control is returned
back to the parent process.
exit()
The exit() system call is used by a program to terminate its execution. In a multithreaded
environment, this means that the thread execution is complete. The operating system reclaims
resources that were used by the process after the exit() system call.
kill()
The kill() system call is used by the operating system to send a termination signal to a
process that urges the process to exit.However, kill system call does not necessary mean
killing the process and can have various meanings.
The waitpid() system call suspends execution of the current process until a child specified
by pid argument has changed state. By default, waitpid() waits only for terminated children,
but this behaviour is modifiable via the options argument, as described below.
The value of pid can be: (child_pid, &status, options);
Tag Description
meaning wait for any child process whose process group
< -1
ID is equal to the absolute value of pid.
Process scheduling allows OS to allocate a time interval of CPU execution for each
process. Another important reason for using a process scheduling system is that it
keeps the CPU busy all the time. This allows you to get the minimum response
time for programs.
1. Job queue – It helps you to store all the processes in the system.
2. Ready queue – This type of queue helps you to set every process residing in
the main memory, which is ready and waiting to execute.
3. Device queues – It is a process that is blocked because of the absence of an
I/O device.
n the above-given Diagram,
1. Every new process first put in the Ready queue .It waits in the ready queue
until it is finally processed for execution. Here, the new process is put in the
ready queue and wait until it is selected for execution or it is dispatched.
2. One of the processes is allocated the CPU and it is executing
3. The process should issue an I/O request
4. Then, it should be placed in the I/O queue.
5. The process should create a new subprocess
6. The process should be waiting for its termination.
7. It should remove forcefully from the CPU, as a result interrupt. Once
interrupt is completed, it should be sent back to ready queue.
Running State
Not Running State
Running
In the Operating system, whenever a new process is built, it is entered into the
system, which should be running.
Not Running
The process that are not running are kept in a queue, which is waiting for their turn
to execute. Each entry in the queue is a point to a specific process.
Scheduling Objectives
Here, are important objectives of Process scheduling
However, the main goal of this type of scheduler is to offer a balanced mix of jobs,
like Processor, I/O jobs., that allows managing multiprogramming.
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.
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.
Interval Timer
Timer interruption is a method that is closely related to preemption. When a certain
process gets the CPU allocation, a timer may be set to a specified interval. Both
timer interruption and preemption force a process to return the CPU before its CPU
burst is complete.
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.
Scheduling Algorithms
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.
Characteristics of FCFS method:
It offers non-preemptive and pre-emptive scheduling algorithm.
Jobs are always executed on a first-come, first-serve basis
It is easy to implement and use.
However, this method is poor in performance, and the general wait time is
quite high.
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.
Characteristics of Round-Robin Scheduling
Round robin is a hybrid model which is clock-driven
Time slice should be minimum, which is assigned for a specific task to be
processed. However, it may vary for different processes.
It is a real time system which responds to the event within a specific time
limit.
Summary:
CPU scheduling is a process of determining which process will own CPU
for execution while another process is on hold.
In Preemptive Scheduling, the tasks are mostly assigned with their priorities.
In the Non-preemptive scheduling method, the CPU has been allocated to a
specific process.
The burst time is the time required for the process to complete execution. It
is also called running time.
CPU utilization is the main task in which the operating system needs to
ensure that the CPU remains as busy as possible.
The number of processes that finish their execution per unit time is known
Throughput.
Waiting time is an amount that a specific process needs to wait in the ready
queue.
It is the amount of time in which the request was submitted until the first
response is produced.
Turnaround time is the amount of time to execute a specific process.
Timer interruption is a method that is closely related to preemption.
A dispatcher is a module that provides control of the CPU to the process.
Six types of process scheduling algorithms are: First Come First Serve
(FCFS), 2) Shortest-Job-First (SJF) Scheduling, 3) Shortest Remaining
Time, 4) Priority Scheduling, 5) Round Robin Scheduling, 6) Multilevel
Queue Scheduling.
In the First Come First Serve method, the process which requests the CPU
gets the CPU allocation first.
In the Shortest Remaining time, the process will be allocated to the task
closest to its completion.
In Priority Scheduling, the scheduler selects the tasks to work as per the
priority.
Round robin scheduling works on the principle where each person gets an
equal share of something in turn.
In the Shortest job first, the shortest execution time should be selected for
execution next.
The multilevel scheduling method separates the ready queue into various
separate queues. In this method, processes are assigned to a queue based on
a specific property.
The CPU uses scheduling to improve its efficiency.
What is Inter Process Communication?
In general, Inter Process Communication is a type of mechanism usually provided by the
operating system (or OS). The main aim or goal of this mechanism is to provide
communications in between several processes. In short, the intercommunication allows a
process letting another process know that some event has occurred.
Let us now look at the general definition of inter-process communication, which will explain
the same thing that we have discussed above.
Definition
"Inter-process communication is used for exchanging useful information between numerous
threads in one or more processes (or programs)."
To understand inter process communication, you can consider the following given diagram
that illustrates the importance of inter-process communication:
47.5M
1.1K
Features of J
These are the following methods that used to provide the synchronization:
1. Mutual Exclusion
2. Semaphore
3. Barrier
4. Spinlock
Mutual Exclusion:-
It is generally required that only one process thread can enter the critical section at a time.
This also helps in synchronization and creates a stable state to avoid the race condition.
Semaphore:-
Semaphore is a type of variable that usually controls the access to the shared resources by
several processes. Semaphore is further divided into two types which are as follows:
1. Binary Semaphore
2. Counting Semaphore
Barrier:-
A barrier typically not allows an individual process to proceed unless all the processes does
not reach it. It is used by many parallel languages, and collective routines impose barriers.
Spinlock:-
Spinlock is a type of lock as its name implies. The processes are trying to acquire the
spinlock waits or stays in a loop while checking that the lock is available or not. It is known
as busy waiting because even though the process active, the process does not perform any
functional operation (or task).
1. Pipes
2. Shared Memory
3. Message Queue
4. Direct Communication
5. Indirect communication
6. Message Passing
7. FIFO
Pipe:-
The pipe is a type of data channel that is unidirectional in nature. It means that the data in this
type of data channel can be moved in only a single direction at a time. Still, one can use two-
channel of this type, so that he can able to send and receive data in two processes. Typically,
it uses the standard methods for input and output. These pipes are used in all types of POSIX
systems and in different versions of window operating systems as well.
Shared Memory:-
It can be referred to as a type of memory that can be used or accessed by multiple processes
simultaneously. It is primarily used so that the processes can communicate with each other.
Therefore the shared memory is used by almost all POSIX and Windows operating systems
as well.
Message Queue:-
In general, several different messages are allowed to read and write the data to the message
queue. In the message queue, the messages are stored or stay in the queue unless their
recipients retrieve them. In short, we can also say that the message queue is very helpful in
inter-process communication and used by all operating systems.
To understand the concept of Message queue and Shared memory in more detail, let's take a
look at its diagram given below:
Message Passing:-
It is a type of mechanism that allows processes to synchronize and communicate with each
other. However, by using the message passing, the processes can communicate with each
other without restoring the hared variables.
Usually, the inter-process communication mechanism provides two operations that are as
follows:
o send (message)
o received (message)
Direct Communication:-
In this type of communication process, usually, a link is created or established between two
communicating processes. However, in every pair of communicating processes, only one link
can exist.
Indirect Communication
Indirect communication can only exist or be established when processes share a common
mailbox, and each pair of these processes shares multiple communication links. These shared
links can be unidirectional or bi-directional.
FIFO:-
It acts as a type of endpoint for receiving or sending the data in a network. It is correct for
data sent between processes on the same computer or data sent between different computers
on the same network. Hence, it used by several types of operating systems.
o File:-
A file is a type of data record or a document stored on the disk and can be acquired on
demand by the file server. Another most important thing is that several processes can access
that file as required or needed.
o Signal:-
As its name implies, they are a type of signal used in inter process communication in a
minimal way. Typically, they are the massages of systems that are sent by one process to
another. Therefore, they are not used for sending data but for remote commands between
multiple processes.
Usually, they are not used to send the data but to remote commands in between several
processes.
Why we need interprocess communication?
There are numerous reasons to use inter-process communication for sharing the data. Here
are some of the most important reasons that are given below:
Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in front
of each other. A similar situation occurs in operating systems when there are two or
more processes that hold some resources and wait for resources held by other(s). For
example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
Deadlock can arise if the following four conditions hold simultaneously
(Necessary Conditions)
Mutual Exclusion: Two or more resources are non-shareable (Only one process can
use at a time)
Hold and Wait: A process is holding at least one resource and waiting for
resources.
No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
Circular Wait: A set of processes are waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance: The idea is to not let the system into a
deadlock state.
One can zoom into each category individually, Prevention is done by negating one
of above mentioned necessary conditions for deadlock.
Avoidance is kind of futuristic in nature. By using strategy of “Avoidance”, we have
to make an assumption. We need to ensure that all information about resources
which process will need are known to us prior to execution of the process. We use
Banker’s algorithm (Which is in-turn a gift from Dijkstra) in order to avoid
deadlock.
2) Deadlock detection and recovery: Let deadlock occur, then do preemption to
handle it once occurred.
3) Ignore the problem altogether: If deadlock is very rare, then let it happen and
reboot the system. This is the approach that both Windows and UNIX take.
OR
A deadlock happens in operating system when two or more processes need some resource to
complete their execution that is held by the other process.
In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly
process 2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in
deadlock as each of them needs the other’s resource to complete their execution but neither of
them is willing to relinquish their resources.
Coffman Conditions
A deadlock occurs if the four Coffman conditions hold true. But these conditions are not
mutually exclusive.
The Coffman conditions are given as follows −
Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the
diagram below, there is a single instance of Resource 1 and it is held by Process 1
only.
No Preemption
A resource cannot be preempted from a process by force. A process can only release a
resource voluntarily. In the diagram below, Process 2 cannot preempt Resource 1
from Process 1. It will only be released when Process 1 relinquishes it voluntarily
after its execution is complete.
Circular Wait
A process is waiting for the resource held by the second process, which is waiting for
the resource held by the third process and so on, till the last process is waiting for a
resource held by the first process. This forms a circular chain. For example: Process 1
is allocated Resource2 and it is requesting Resource 1. Similarly, Process 2 is
allocated Resource 1 and it is requesting Resource 2. This forms a circular wait loop.
Deadlock Detection
A deadlock can be detected by a resource scheduler as it keeps track of all the resources that
are allocated to different processes. After a deadlock is detected, it can be resolved using the
following methods −
All the processes that are involved in the deadlock are terminated. This is not a good
approach as all the progress made by the processes is destroyed.
Resources can be preempted from some processes and given to others till the deadlock is
resolved.
Deadlock Prevention
It is very important to prevent a deadlock before it can occur. So, the system checks each
transaction before it is executed to make sure it does not lead to deadlock. If there is even a
slight chance that a transaction may lead to deadlock in the future, it is never allowed to
execute.
Deadlock Avoidance
It is better to avoid a deadlock rather than take measures after the deadlock has occurred. The
wait for graph can be used for deadlock avoidance. This is however only useful for smaller
databases as it can get quite complex in larger databases.
System model
Overview:
A deadlock occurs when a set of processes is stalled because each process is holding
a resource and waiting for another process to acquire another resource. In the
diagram below, for example, Process 1 is holding Resource 1 while Process 2
acquires Resource 2, and Process 2 is waiting for Resource 1.
System Model :
For the purposes of deadlock discussion, a system can be modeled as a
collection of limited resources that can be divided into different categories
and allocated to a variety of processes, each with different requirements.
Memory, printers, CPUs, open files, tape drives, CD-ROMs, and other
resources are examples of resource categories.
By definition, all resources within a category are equivalent, and any of
the resources within that category can equally satisfy a request from that
category. If this is not the case (i.e. if there is some difference between the
resources within a category), then that category must be subdivided
further. For example, the term “printers” may need to be subdivided into
“laser printers” and “color inkjet printers.”
Some categories may only have one resource.
The kernel keeps track of which resources are free and which are
allocated, to which process they are allocated, and a queue of processes
waiting for this resource to become available for all kernel-managed
resources. Mutexes or wait() and signal() calls can be used to control
application-managed resources (i.e. binary or counting semaphores. )
When every process in a set is waiting for a resource that is currently
assigned to another process in the set, the set is said to be deadlocked.
Operations:
In normal operation, a process must request a resource before using it and release it
when finished, as shown below.
1. Request –
If the request cannot be granted immediately, the process must wait until
the resource(s) required to become available. The system, for example,
uses the functions open(), malloc(), new(), and request ().
2. Use –
The process makes use of the resource, such as printing to a printer or
reading from a file.
3. Release –
The process relinquishes the resource, allowing it to be used by other
processes.
NECESSARY CONDITIONS
There are four conditions that must be met in order to achieve deadlock as
follows.
1. Mutual Exclusion –
At least one resource must be kept in a non-shareable state; if another
process requests it, it must wait for it to be released.
4. Circular Wait –
There must be a set of processes P0, P1, P2,…, PN such that every
P[I] is waiting for P[(I + 1) percent (N + 1)]. (It is important to note that
this condition implies the hold-and-wait condition, but dealing with the
four conditions is easier if they are considered separately).
Deadlock Prevention:
Deadlocks can be avoided by avoiding at least one of the four necessary
conditions: as follows.
Condition-1:
Mutual Exclusion :
Read-only files, for example, do not cause deadlocks.
Unfortunately, some resources, such as printers and tape drives,
require a single process to have exclusive access to them.
Condition-2 :
Hold and Wait :
To avoid this condition, processes must be prevented from holding one or
more resources while also waiting for one or more others. There are a few
possibilities here:
Make it a requirement that all processes request all resources at
the same time. This can be a waste of system resources if a
process requires one resource early in its execution but does not
require another until much later.
Processes that hold resources must release them prior to
requesting new ones, and then re-acquire the released resources
alongside the new ones in a single new request. This can be a
problem if a process uses a resource to partially complete an
operation and then fails to re-allocate it after it is released.
If a process necessitates the use of one or more popular resources,
either of the methods described above can result in starvation.
Condition-3 :
No Preemption :
When possible, preemption of process resource allocations can help to avoid
deadlocks.
One approach is that if a process is forced to wait when requesting
a new resource, all other resources previously held by this process
are implicitly released (preempted), forcing this process to re-
acquire the old resources alongside the new resources in a single
request, as discussed previously.
Another approach is that when a resource is requested, and it is not
available, the system looks to see what other processes are
currently using those resources and are themselves blocked while
waiting for another resource. If such a process is discovered, some
of their resources may be preempted and added to the list of
resources that the process is looking for.
Either of these approaches may be appropriate for resources
whose states can be easily saved and restored, such as registers
and memory, but they are generally inapplicable to other devices,
such as printers and tape drives.
Condition-4 :
Circular Wait :
To avoid circular waits, number all resources and insist that
processes request resources is strictly increasing ( or decreasing)
order.
To put it another way, before requesting resource Rj, a process
must first release all Ri such that I >= j.
The relative ordering of the various resources is a significant
challenge in this scheme.
Deadlock Avoidance :
The general idea behind deadlock avoidance is to avoid deadlocks
by avoiding at least one of the aforementioned conditions.
This necessitates more information about each process AND
results in low device utilization. (This is a conservative approach.)
The scheduler only needs to know the maximum number of each
resource that a process could potentially use in some algorithms. In
more complex algorithms, the scheduler can also use the schedule
to determine which resources are required and in what order.
When a scheduler determines that starting a process or granting
resource requests will result in future deadlocks, the process is
simply not started or the request is denied.
The number of available and allocated resources, as well as the
maximum requirements of all processes in the system, define a
resource allocation state.
Deadlock Detection :
If deadlocks cannot be avoided, another approach is to detect them
and recover in some way.
Aside from the performance hit of constantly checking for
deadlocks, a policy/algorithm for recovering from deadlocks must
be in place, and when processes must be aborted or have their
resources preempted, there is the possibility of lost work.
Recovery From Deadlock :
There are three basic approaches to getting out of a bind:
1. Inform the system operator and give him/her permission to
intervene manually.
2. Stop one or more of the processes involved in the deadlock.
3. Prevent the use of resources.