0% found this document useful (0 votes)
16 views49 pages

Module 1-Os

This document provides an overview of operating system concepts, focusing on processes and process management. It explains the differences between programs and processes, describes the roles of the shell and kernel, and outlines process states, operations, and inter-process communication methods. Additionally, it covers context switching and client-server communication mechanisms.

Uploaded by

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

Module 1-Os

This document provides an overview of operating system concepts, focusing on processes and process management. It explains the differences between programs and processes, describes the roles of the shell and kernel, and outlines process states, operations, and inter-process communication methods. Additionally, it covers context switching and client-server communication mechanisms.

Uploaded by

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

MODULE 1: OPERATING SYSTEM

1. PROCESS & PROCESS MANAGEMENT


 What is mean by a program?
 A program is a set of instructions designed to complete a specific task.
 When we execute a program that was just compiled, the OS will
generate a process to execute the program.

 What is mean by a process?


 A program under execution is known as process (an instance of an
executing program)
 A program becomes a process when loaded into memory.

 What are the differences between program and process?


SI.
Program Process
NO
Passive entity as it resides in the Active entity as it is created during
1 secondary memory. execution and loaded into the main
memory.
Exists at a single place and Exists for a limited span of time as it
2 continues to exist until it is gets terminated after the completion
deleted. of task.
3 Static entity Dynamic entity

Does not have any resource Has a high resource requirement, it


4 requirement, it only requires needs resources like CPU, memory
memory space for storing the address, I/O during its lifetime.
instructions.

5 Does not have any control block. Has its own control block called
Process Control Block.
Has two logical components: In addition to program data, a
6 code and data. process also requires additional
information required for the
management and execution.
 Differences between Shell &Kernel
 Shell:-
A shell is an environment or a special user program which provide an
interface to user to use operating system services. It executes programs
based on the input provided by the user.

 Kernel:-
Kernel is the heart and core of an Operating System that manages
operations of computer and hardware. It acts as a bridge between the user
and the resources of the system by accessing various computer resources
like the CPU, I/O devices and other resources.

 Differences:-
SI.
Shell Kernel
NO
Shell allows the users to Kernel controls all the tasks of
1
communicate with the kernel. the system.
It is the interface between It is the core of the operating
2
kernel and user. system.
It is a command line interpreter It is a low level program
(CLI). interfacing with the hardware
3
(CPU, RAM, disks) on top of
which applications are running.
Shell commands like ls, mkdir It performs process management.
and many more can be used to
4
request to complete the specific
operation to the OS.
5 It is the outer layer of OS. It is the inner layer of OS.
It interacts with user and Kernel directly interacts with the
interprets to machine hardware by accepting machine
6
understandable language. understandable language from
the shell.
Its types are – Bourne Shell, C Its types are – Monolithic
7 shell, Korn Shell, etc. Kernel, Micro kernel, Hybrid
kernel, etc.
Process Control Block (PCB)
 PCB is a data structure in the operating system kernel containing the
information needed to manage a particular process.
 Thus, the PCB is the data structure that defines a process to the
operating systems.

 The PCB contains important information about the specific process including
i. Process State: The current state of the process i.e., whether it is ready,
running, waiting, or whatever.

ii. Process Number: Unique identification of the process in order to track


information.
iii. Program Counter: This register stores the next instruction to be
executed.

iv. CPU Registers: MAR, MBR, PC, IR, GPR

v. Memory limits: process stored location in memory

vi. List of files:

vii. The priority of process

viii. A pointer to parent process.

ix. A pointer to child process (if it exists).


2. PROCESS STATES & STATE TRANSITION
As a process executes, it changes state. The following are the possible states:-
 New: The process is being created
 Running: Instructions are being executed
 Waiting: The process is waiting for some event to occur
 Ready: The process is waiting to be assigned to a processor
 Terminated: The process has finished execution
Only one process is running and all other processes are in ready or
waiting.

3. OPERATION ON PROCESS

Process
Creation

Process
Process Scheduling or
Termination Dispatching

Operations
on
Process

Process Process
Suspend or Waiting or
Resume Blocking

Process
Preemption
1. Process Creation

Initial step to process creation


Implies the creation of a new process for execution
A process may be created by another process using system fork(). In this case
the creating process is called the Parent Process and the created process is
called the Child Process.

P1 fork() P2

2. Process Scheduling or Dispatching

It refers to the event where the OS puts the process from ready to
running state.

It is done by the Process Scheduler or Process Dispatcher when there are


free resources or there is a process of higher priority than the on-going
process.

3. Process Waiting or Blocking

When a process needs an I/O event during its execution, then the
process moves from running state to waiting state and dispatches and
dispatches another process to CPU.
When the blocked process has completed the I/O event it will moves
from waiting state to ready state.

4. Process Preemption

It means the ability of the OS to pre-empt (or pause) a currently


running process in favor of a higher priority process.
Then the pre-empted process moves from running state to ready state
and CPU loads the priority from ready state to running state.
In other words, it is the act of temporarily interrupting an executing
task, with the intention of resuming it at a later time when the priority
process has finished its execution.
5. Process Suspend & Resume

A suspended process is indefinitely removed from contention for time


on a processor without being destroyed.
This is useful for detecting security threats (such as malicious code
execution) & for software debugging purposes.
Two new states have been added:-
1) Suspended Ready
2) Suspended Blocked
A suspension may be initiated by the process being suspended or by
another process.
A process suspends itself only when it is in the running state. In such
a situation, the process makes the transition from running to
suspended ready.
When a process suspends a ready process then the ready process
transition from ready to suspended ready.
A suspended ready process may be made ready, or resumed by
another process causing the first process to transition from suspended
ready to ready.
A blocked process will make the transition from blocked to

suspended blocked when it is suspended by another process.

A suspended blocked process may be resumed by another process &


make the transition from suspended blocked to blocked.

6. Process Termination

Ending a process is known as process termination.

There are many events that may lead to process termination, some of

them are:-

1) One process terminating the other process: A parent may terminate


execution of one of its children for the following reasons:
i. The child has exceeded its usage of some of the resources
that it has been allocated.
ii. The task assigned to the child is no longer required.
iii. Parent is exiting, and the operating system does not allow a
child to continue if its parent terminates.

2) A problem in hardware

3) The process is fully executed, implying that the OS is finished

4) OS might terminate itself due to service errors

Process State Transition With Suspend & Resume

Context Switching

Context switching refers to a technique/method used by the OS to switch

processes from a given state to another one for the execution of its function

using the CPUs present in the system.

There are three major triggers for context switching:-

1) Multitasking

2) Interrupt Handling

3) User and Kernel Mode Switching


The steps involved in context switching are as follows :-

1. Firstly, the context of the process P1 i.e. the process present in the

running state will be saved in the Process Control Block of process

P1 i.e. PCB1.

2. Now, you have to move the PCB1 to the relevant queue i.e. ready

queue, waiting queue, etc.

3. From the ready state, select the new process that is to be executed i.e.

the process P2.

4. Now, update the Process Control Block of process P2 i.e. PCB2 by

setting the process state to running. If the process P2 was earlier

executed by the CPU, then you can get the position of last executed

instruction so that you can resume the execution of P2.

5. Similarly, if you want to execute the process P1 again, then you have

to follow the same steps as mentioned above (from step 1 to 4).


4. INTER-PROCESS COMMUNICATION (IPC)

A process can be of two types:


1) Independent process.
2) 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.

Processes can communicate with each other using two ways:


1) Shared Memory
2) Message Passing
Shared Memory is faster once it is set up, because no system calls are
required and access occurs at normal memory speeds. However it is more
complicated to set up, and doesn't work as well across multiple computers.
Shared memory is generally preferable when large amounts of information
must be shared quickly on the same computer.
Message Passing requires system calls for every message transfer, and is
therefore slower, but it is simpler to set up and works well across multiple
computers. Message passing is generally preferable when the amount and/or
frequency of data transfer is small, or when multiple computers are involved.
It is particularly useful in distributed environment.
 IPC By Shared Memory

Multiple processes can access a common shared memory. That is process can
exchange information by reading and writing data to the shared region.
In general the memory to be shared in a shared-memory system is initially
within the address space of a particular process, which needs to make system
calls in order to make that memory publicly available to one or more other
processes.
Other processes which wish to use the shared memory must then make their
own system calls to attach the shared memory area onto their address space.
Once shared memory is established, all accesses are treated as routine
memory accesses and no assistance from kernel is required.

 IPC By Message-Passing Systems

Message passing systems must support at a minimum system calls for "send
(message)" and "receive (message)".
A communication link must be established between the cooperating processes
before messages can be sent.
The following are the methods for logically implementing a link and the send()
or receive() operations:
1. Direct or indirect communication ( naming )
2. Synchronous or asynchronous communication
3. Automatic or explicit buffering.
Direct or indirect communication ( naming )
1) Direct communication: Symmetry and Asymmetry
 A communication link has the following properties:
 Links are established automatically between every pair of
processes that want to communicate.
 The processes need to know only each other’s identity to
communicate.
 A link is associated with exactly two processes.
 Between each pair of processes, there exists exactly one link.
In case of symmetry:
 Each process that wants to communicate must explicitly name the
recipient or sender of the communication.
 In this scheme, the send() and receive() primitives are defined as:
 send(P, message):- Send a message to process P
 receive(Q, message):- Receive a message from process Q
In case of asymmetry:
 Only the sender names the recipient; the recipient is not required to
name the sender.
 In this scheme, the send() and receive() primitives are defined as:
 send(P, message):- Send a message to process P
 receive(id, message):- Receive a message from any process,
the variable ‘id’ is set to the name of the process with which
the communication has taken place.
Disadvantages:
 It is tightly related with the names of the processes and changing the
names cause changes in programs.
2) Indirect communication
 Processes can communicate only if they share a mailbox or port
(with unique identification).
 In this scheme, the send() and receive() primitives are defined as:
 send(A, message):- Send a message to mailbox A
 receive(A, message):- Receive a message from mailbox A
 A communication link has the following properties:
 A link is established between a pair of processes only if both
members of the pair have a shared mailbox.
 A link may be associated with more than two processes.
 A number of different links may exist between each pair of
communicating processes, with each link corresponding to one
mailbox.
 The link may be unidirectional or bidirectional.
Synchronous or asynchronous communication
 Message passing communication between processes implemented as
calls to the send() and receive() primitives may be either blocking
(or synchronous) or non-blocking (or asynchronous) primitives.
 Based on this, different combinations of send() and receive()
primitives are possible. They are as follows;
1) Blocking Send:- The sending process is blocked until the message
is received.
2) Non-Blocking Send:- The sending process resumes the operation.
3) Blocking Receive:- The receiver blocks until a message is
available.
4) Non-Blocking Receive:- The receiver retrieves either a valid
message or null.

Automatic or explicit buffering


 Messages exchanged by communicating processes resides in temporary queue
implemented in one of the three ways:
1) Zero Capacity:- 0 messages; Sender must wait for receiver
2) Bounded Capacity:- Finite length of ‘n’ messages; Sender must
wait if link is full
3) Unbounded Capacity:- Infinite length; Sender never waits.
5. COMMUNICATION IN CLIENT-SERVER SYSTEMS

Client-Server communication involves two components:


1) Client
2) Server
They are usually multiple clients in communication with a single server.
The clients send requests to the server and the server responds to the client
requests.
There are three main methods to client/server communication. These are given as
follows:
1) Sockets
2) Remote Procedure Calls (RPC)
3) Pipes
 Sockets
 A socket is an endpoint for communication.
 Two processes communicating over a network often use a pair of connected
sockets as a communication channel.
 A socket is identified by an IP address concatenated with a port number, e.g.
200.100.50.5:80 (where 200.100.50.5 is IP address and 80 is port number).
 Port numbers below 1024 are considered to be well-known, and are generally
reserved for common Internet services. For example, telnet servers listen to
port 23, ftp servers to port 21, and web servers to port 80.

 When server is running on a host, it is listening to its port and handles all
requests sent by clients to the port on the host (server socket).
 A client must know IP and port of the server (server socket) to send a request
to it.
 Client's port is often provided by OS kernel when client starts
communication with the server and is freed when communication
is over.
 Although communication using sockets is common and efficient, it
is considered low level, because sockets only allow to transfer
unstructured stream of bytes between processes.
 It is up to client and server applications to impose a structure on the data
passed as byte stream.
 Remote Procedure Calls (RPC)

 RPC is a higher level communication method.


 It was designed to mimic procedure call mechanism, but execute it
over network.
 In contrast to IPC messages, RPC messages are well structured.
 RPC hides the details of communication by providing a stub on the
client side.
 The stub marshals (Marshaling is the packing of procedure parameters into a
message packet) the parameters and sends a message to RPC daemon running
on the server.
 RPC daemon (a similar stub on the server side) receives the message and
invokes the procedure on the server. Return values are passed back to the
client using the same technique.
 This is a form of client-server interaction implemented via a request-response
message-passing system.

 Pipes
 Pipe is one of the oldest and simplest IPC methods that appeared in
early UNIX systems.
 A pipe is an IPC abstraction with two endpoints, similar to a
physical pipe.
 Usually one process puts data to one end of the pipe and another
process consumes them from the other one.

 The two different types of pipes are ordinary pipes and named pipes.
Ordinary pipes:-
 Ordinary pipes are unidirectional, i.e. allow only one-way communication.
 They implement standard producer-consumer mechanism, where one
process writes to the pipe and another one reads from it.
 For two-way communication two pipes are needed.
 Ordinary pipes require a parent–child relationship between communicating
processes, because a pipe can only be accessed from process that created or
inherited it.
 Parent process creates a pipe and uses it to communicate with a child created
via fork().
 Once communication is over and processes terminated, the ordinary pipe
ceases to exist.
Named pipes:-
 Named pipes are more powerful.
 They do not require parent– child relationship and can be
bidirectional.
 Once a named pipe is created, multiple non related processes can
communicate over it.
 Two separate processes can access the pipe by name:- one process can open
it as a reader, and the other as a writer.
 Named pipe continues to exist after communicating processes
have terminated.
 It must be explicitly deleted when not required anymore.

6. BASIC CONCEPTS OF THREADS


A thread is a flow of execution through the process code, with;
 Program Counter that keeps track of which instruction to execute next
 System Registers which hold its current working variables
 Stack which contains the execution history.
A thread is also called a lightweight process.
Threads provide a way to improve application performance through parallelism.
For example, in a browser, multiple tabs can be different threads.
Each thread belongs to exactly one process and no thread can exist outside a
process.

Difference between Process and Thread

Advantages of Thread
1) Minimize the context switching time
2) Provides concurrency within a process
3) Efficient communication
4) Resource sharing
5) Enhanced throughput of the system
6) Allow utilization of multiprocessor architectures to a greater scale and
efficiency
Thread States

1) Born State: A thread that has just been created.


2) Ready State: The thread is waiting for the processor (CPU).
3) Running: The System assigns the processor to the thread means that the
thread is being executed.
4) Blocked State: The thread is waiting for an event to occur or waiting for an
I/O device.
5) Waiting State: When a thread must wait for an event (eg; mouse movement
or a signal from another thread), it can enter the waiting state.
6) Sleep: A sleeping thread becomes ready after the designated sleep time
expires.
7) Dead: The execution of the thread is finished.
Types of Thread
Threads are implemented in following two ways:-
1) User Level Threads− User managed threads.
2) Kernel Level Threads− Operating System managed threads acting
on kernel, an operating system core.
User-Level Threads
 The user-level threads are implemented by users and the kernel is not aware of
the existence of these threads.
 User-Level threads are managed entirely by the run-time system (user-level
library). It handles them as if they were single-threaded processes.
 User-level threads are small and much faster than kernel level threads.
 They are represented by a program counter (PC), stack, registers and a small
process control block.
 Also, there is no kernel involvement in synchronization for user-level threads.
 Advantages of User-Level Threads:-
1) Easier and faster to create than kernel-level threads.
2) They can also be more easily managed.
3) They can be run on any operating system.
4) Thread switching does not require Kernel mode privileges.
 Disadvantages of User-Level Threads:-
1) Multithreaded applications in user-level threads cannot use
multiprocessing to their advantage.
2) The entire process is blocked if one user-level thread performs blocking
operation.
Kernel-Level Threads
 Kernel-level threads are handled by the operating system directly and the
thread management is done by the kernel.
 The context information for the process as well as the process threads is all
managed by the kernel. Because of this, kernel-level threads are slower than
user-level threads.
 Advantages of Kernel-Level Threads:-
1) Multiple threads of the same process can be scheduled on different
processors in kernel-level threads.
2) The kernel routines can also be multithreaded.
3) If a kernel-level thread is blocked, another thread of the same process
can be scheduled by the kernel.

 Disadvantages of Kernel-Level Threads:-


1) Kernel threads are generally slower to create and manage than the user
threads.
2) Transfer of control from one thread to another within the same process
requires a mode switch to the Kernel.
Difference between User-Level & Kernel-Level Thread

Multithreading Models
 Some operating system provide a combined user level thread and Kernel level
thread facility
 Multithreading allows the application to divide its task into individual threads.
 In multi-threads, the same process or task can be done by the number of
threads, or we can say that there is more than one thread to perform the task in
multithreading.
 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:
1) Many to many relationship.
2) Many to one relationship.
3) One to one relationship.
Many to many relationship
 In this, the OS multiplexes 'n' number of threads of user onto kernel thread
of equal or small numbers. The diagram represents the many-to-many
relationship thread model.
 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 relationship


 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 relationship
 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.

7. CONCURRENCY
Concurrency is the execution of the multiple instruction sequences at the same
time.
It happens in the operating system when there are several process threads running in
parallel.
The running process threads always communicate with each other through shared
memory or message passing.
There are several reasons for providing an environment that allows concurrency:-
1) Information sharing
2) Computational speedup
3) Modularity
4) Convenience
Concurrent execution that requires cooperation among the processes requires
mechanisms to allow processes to communicate with each other and to synchronize
their action.
Thus, when processes interact with one another, two fundamental requirements
must be satisfied:
1) Inter-Process Communication
2) Process Synchronization
Principles of Concurrency:-
 Both interleaved and overlapped processes can be viewed as examples of
concurrent processes, they both present the same problems.
 The relative speed of execution cannot be predicted. It depends on the
following:
1) The activities of other processes
2) The way operating system handles interrupts
3) The scheduling policies of the operating system
Problems in Concurrency:-
1) Sharing global resources:-
 Sharing of global resources safely is difficult.
 If two processes both make use of a global variable and both perform read
and write on that variable, then the order in which various read and write
are executed is critical.
2) Optimal allocation of resources:-
 It is difficult for the operating system to manage the allocation of resources
optimally.
3) Locating programming errors:-
 It is very difficult to locate a programming error because reports are usually
not reproducible.
4) Locking the channel:-
 It may be inefficient for the operating system to simply lock the channel and
prevents its use by other processes.
Advantages of Concurrency
1) Running of multiple applications
2) Better resource utilization
3) Better average response time
4) Better performance
Disadvantages of Concurrency
1) It is required to protect multiple applications from one another.
2) It is required to coordinate multiple applications through additional
mechanisms.
3) Additional performance overheads and complexities in operating systems are
required for switching among applications.
4) Sometimes running too many applications concurrently leads to severely
degraded performance.
Issues of Concurrency
1) Non-atomic:-
 Operations that are non-atomic but interruptible by multiple processes can
cause problems.
2) Race Condition:-
 Several process access and manipulate the same data concurrently and the
outcome of the execution depends on the particular order in which access
takes place.
3) Blocking:-
 Processes can block waiting for resources.
 A process could be blocked for a long period of time waiting for input from
a terminal.
 If the process is required to periodically update some data, this would be
very undesirable.
4) Starvation:-
 Starvation is the problem that occurs when high priority processes keep
executing and low priority processes get blocked for indefinite time.
5) Deadlock:-
 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.
Process Synchronization

A process can be of two types:


1) Independent process.
2) Co-operating process.
When two or more process cooperates with each other, their order of
execution must be preserved otherwise there can be conflicts in their
execution and inappropriate outputs can be produced.

Process synchronization problem arises in the case of Cooperative process


also because resources are shared in Cooperative processes. The problems
includes:-

1) Race Condition

2) Critical Section Problem


8. 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.

Entry Section: It is part of the process which decides the entry of a particular
process.
Critical Section: This part allows one process to enter and modify the shared
variable.
Exit Section: Exit section allows the other processes that are waiting in the
Entry Section, to enter into the Critical Sections. It also checks that a process
that finished its execution should be removed through this Section.
Remainder Section: All other parts of the Code, which is not in Critical, Entry,
and Exit Section, are known as the Remainder Section.
In the entry section, the process requests for entry in the Critical Section.
Any solution to the critical section problem must satisfy three requirements:
1) Mutual Exclusion:- If process Pi is executing in its critical section, then
no other processes can be executing in their critical sections.
2) Progress:- If no process is executing in its critical section and some
processes wish to enter their critical sections, then only those processes
that are not executing in their remainder section can participate in the
decision on which will enter its critical section next, and this selection
cannot be postponed indefinitely.
3) 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.
Three basic approaches are available for the implementation of solution to the
critical section problem:
1) Software Solution
2) Hardware Solution
3) As Mutual Exclusive Primitives (Semaphore)

Software Solution
Peterson’s Solution is a classical software-based solution to the critical section
problem.
In Peterson’s solution, we have two shared variables:
1) Boolean flag:-
 The array variable flag is used to indicate if a process is ready to enter
its critical section.
 For eg; if flag[0] is true, P0 is ready to enter its critical section.
2) Int turn:-
 The variable turn indicates whose turn it is to enter its critical section.
 That is, if turn==0, then the process P0 is allowed to execute in its
critical section.
Entrance to the critical section is granted for process P0 if P1 does not want to
enter its critical section or if P1 has given priority to P0 by setting turn to 0.
bool flag[2] = [false, false];
int turn;

Process P0: Process P1:


flag[0] = true; flag[1] = true;
turn = 1; turn = 0;

// busy wait loop // busy wait loop


while(flag[1] == true && turn == 1); while(flag[0] == true && turn == 0);

// critical section // critical section


........................... ...........................
........................... ...........................
// end of critical section // end of critical section

flag[0] = false; flag[1] = false;

This algorithm satisfies the three essential criteria:-


1. Mutual Exclusion
2. Progress
3. Bounded Waiting
Disadvantages of Peterson’s Solution:-
1) Limited to 2 process
2) Involves busy waiting
3) Crashing or terminating of one process within or outside of its critical
section can lead to deadlock.
Hardware Solution
There are three algorithms in the hardware approach of solving Process
Synchronization problem:-
1) Test and Set
2) Compare & Swap
Test and Set
 It is an instruction that returns the old value of a memory location and sets
the memory location value to 1 as a single atomic operation.
 It uses a boolean variable 'lock' which is initially initialized to false.
 lock = 0 means the critical section is currently vacant and no process is
present inside it.
 lock = 1 means the critical section is currently occupied and a process is
present inside it.
 Definition of Test and Set:-
boolean test-and-set(boolean *target){
boolean returnValue = *target;
*target = true;
return returnValue;
}

 Mutual-exclusion implementation with test and set():-


bool lock = false;
Process P0: Process P1:

// busy wait loop // busy wait loop


while(test-and-set(lock)); while(test-and-set(lock));

// critical section // critical section


........................... ...........................
........................... ...........................
// end of critical section // end of critical section

lock = false; lock = false;

Compare and Swap()


 It uses two boolean variables lock and key. Both lock and key variables
are initially initialized to false.
 It uses a temporary variable temp to set the lock to true when a process
enters the critical section of the program.
 Definition of compare and swap:-
int compare_and_swap(int *lock, int expected, int newValue){
int temp = *lock;
if (*lock == expected)
* lock = newValue;
return temp;
}
 Mutual-exclusion implementation with compare and swap:-
int lock = 0;

Process P0: Process P1:

// busy wait loop // busy wait loop


while(compare_and_swap (lock,0,1) != 0); while(compare_and_swap(lock,0,1) != 0);

// critical section // critical section


........................... ...........................
........................... ...........................
// end of critical section // end of critical section

lock = 0; lock = 0;

Advantages of Hardware Solution:-


1) It ensures mutual exclusion.
2) It ensures progress.
3) It is simple and easy to verify.
4) It can be used to support multiple critical sections.
Disadvantages of Hardware Solution:-
1) It does not guarantee bounded waiting.
2) Starvation is possible
3) Deadlock is possible
Semaphore
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.
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++;
}

Semaphore implementation of critical section problem between two process is


shown below:
semaphore mutex = 1;

Process P0: Process P1:

wait(mutex) wait(mutex)
// critical section // critical section
........................... ...........................
........................... ...........................
// end of critical section // end of critical section

signal(mutex) signal(mutex)

Types of Semaphores:
1) Counting Semaphores
2) Binary Semaphores
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.
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:-
1) Ensures Mutual Exclusion
2) Applicable for more than two processes
Disadvantages of Semaphores:-
1) Semaphores are complicated so the wait and signal operations must be
implemented in the correct order to prevent deadlocks.
2) Semaphores may lead to a priority inversion.

9. CLASSIC PROBLEMS OF SYNCHRONIZATION

1) Producer-Consumer Problem (Bounded-Buffer Problem)

2) Readers-and-Writers Problem

3) Dining-Philosophers Problem

 Producer-Consumer Problem (Bounded-Buffer Problem)


Producer:- a process which is able to produce data/item.
Consumer:- a Process that is able to consume the data/item produced by the
Producer.
Both Producer and Consumer share a common memory buffer.
The producer produces the data into the buffer and the consumer consumes the
data from the buffer.
Producer-Consumer Problem:-
 Producer Process should not produce any data when the shared buffer is full.
 Consumer Process should not consume any data when the shared buffer is
empty.
 The access to the shared buffer should be mutually exclusive i.e. at a time
only one process should be able to access the shared buffer and make
changes to it.
To implement these conditions, the following three semaphores are required:-
Semaphore Purpose Initial Value
free Mutual exclusion for buffer access 1
space Space available in buffer N (bytes)
data Data available in buffer 0

The structure of the Producer is given below:-


while(true){
// produce item
wait(space); // If buffer is full, wait for space signal
wait(free); // If buffer is being used, wait for free signal
// add item to buffer
signal(free); // Signal that buffer is no longer in use
signal(data); // Signal that data has been put in buffer
}

The structure of the Consumer is given below:-


while(true){
wait(data); // Wait until at least one item in buffer
wait(free); // If buffer is being used, wait for free signal
// get item from buffer
signal(free); // Signal that buffer is no longer in use
signal(space); // Signal that at least one space exist in buffer
}
 Readers-and-Writers Problem
One set of data is shared among a number of processes.
Once a writer is ready, it performs its write. Only one writer may write at a time.
If a process is writing, no other process can read it.
If at least one reader is reading, no other process can write. To control this, a
shared variable rcount (initially 0) is used to count the number of active readers.
Readers may not write and only read.

Process 1 Process 2 Allowed

To implement these conditions, the following three semaphores are required:-

Semaphore Purpose Initial Value


readsem To prevent write access when a reader is already active 1
writesem To prevent read/write access when a writer is already active 1
The structure of the Reader is given below:-
while(true){

wait(readsem); // Wait before modifying rcount


rcount++;

if(rcount == 1) // If first reader


wait(writesem); // Wait for writer to finish

signal(readsem); // Free readsem access


// read the data
wait(readsem); // Wait before modifying rcount
rcount--;

if(rcount == 0) // If no reader left


signal(writesem); // Allow writer now

signal(readsem);
}

The structure of the Writer is given below:-


while(true){
wait(writesem); // Wait on other write or read
// write the data
signal(writesem); // Free writesem access
}

 Dining-Philosophers Problem
Five philosophers sit around a circular table and alternate between thinking and
eating.
A bowl of noodles and five chopsticks for each philosopher are placed at the
center of the table.
Philosopher Chopsticks Used
P0 C0 & C1
P1 C1 & C2
P2 C2 & C3
P3 C3 & C4
P4 C4 & C0

There are certain conditions a philosopher must follow:


1) A philosopher must use both their right and left chopsticks to eat.
2) A philosopher can only eat if both of his or her immediate left and right
chopsticks are available.
3) If the philosopher's immediate left or right chopstick is not available, the
philosopher places their (either left or right) chopsticks on the table and
resumes thinking.
Scenario:-
1) Deadlock:- Each of the philosopher pick up a chopstick, no one can eat,
hence a deadlock.
2) Starvation:- If a philosopher is not able to eat for a long period of time,
then it is starvation.
A solution of the Dining Philosophers Problem is to use a semaphore to represent
a chopstick. A chopstick can be picked up by executing a wait() operation on the
semaphore and released by executing a signal() operation on the semaphore.
The structure of the philosopher i is given below:-
Initially the elements of the chopstick are initialized to 1 as the chopsticks are on
the table and not picked up by a philosopher. That is chopstick [5] = [1, 1, 1, 1, 1]

semaphore chopstick [5];


while(true) {
wait(chopstick[i]);
wait(chopstick[ (i+1) % 5] );

//EATING THE RICE

signal(chopstick[i]);
signal(chopstick[ (i+1) % 5] );

//THINKING
}

10.DEADLOCKS
A process in operating system uses resources in the following way.
1) Requests a resource
2) Use the resource
3) Releases the resource
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.

Deadlock can arise if the following four conditions hold simultaneously


(Necessary Conditions):-
1) Mutual Exclusion: Only one process can use a resource at any given
time i.e. the resources are non-sharable.
2) Hold and wait: A process is holding at least one resource at a time and is
waiting to acquire other resources held by some other process.
3) No Preemption: A resource cannot be taken from a process unless the
process releases the resource.
4) Circular Wait: A set of processes are waiting for each other in a circular
fashion. For example, let’s say there are a set of processes {P0, P1, P2, P3}
such that P0 depends on P1, P1 depends on P2, P2 depends on P3 and P3
depends on P0. This creates a circular relation between all these processes
and they have to wait forever to be executed.
Deadlock Handling
1) Deadlock Prevention
2) Deadlock Avoidance
3) Deadlock Detection and Recovery
Deadlock Prevention
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption
and circular wait holds simultaneously. If it is possible to violate one of the four
conditions at any time then the deadlock can never occur in the system. The
mutual exclusion condition cannot be disallowed, so it is customary to prevent
one or more of the remaining three conditions.
Denying Hold and Wait
There are basically two possible implementation of this strategy:-
1) The process requests all needed resource at one time prior to the
commencement of the execution. Disadvantage is low resource utilization.
2) The process requests resources incrementally in the course of execution but
releases all its resource holding upon encountering a denial. This avoids the
disadvantage of the first approach but there is a possibility of starvation.

Denying No Preemption
Preemption of process resource allocations can prevent this condition of
deadlocks, when it is possible. There are basically two possible implementation
of this strategy:-
1) One approach is that if a process is forced to wait when requesting a new
resource, then all other resources previously held by this process are
implicitly released (preempted), forcing this process to re-acquire the old
resources along with the new resources in a single request.
2) Another approach is that when a resource is requested and not available,
then the system looks to see what other processes currently have those
resources and are themselves blocked waiting for some other resource. If
such a process is found, then some of their resources may get preempted
and added to the list of resources for which the process is waiting.
Either of these approaches may be applicable for resources whose states are easily
saved and restored, such as registers and memory, but are generally not applicable
to other devices such as printers and tape drives.
Denying Circular Wait
 One way to avoid circular wait is to number all resources, and to require that
processes request resources only in strictly increasing (or decreasing) order.
 In other words, in order to request resource Ri, a process must first release
all Rj such that j >= i.
 For eg:- A system could order the following resources as shown:
1. Printer
2. Tape Drive
3. Disk Drive
 Each process would first have to acquire all printers, then all tape
drives and finally all the disk drives.
 If a process holds the printer, it can be allocated the tape driver or disk
drive.
 If a process holds the disk drive, it cannot request any other resources:
to do so, it would be required to relinquish the drive, then request the
printer and disk drive in that order.
 One big challenge in this scheme is determining the relative ordering of the
different resources.
Deadlock Avoidance
The general idea behind deadlock avoidance is examining the resource allocation
state to ensure that deadlock never occurs.
A resource allocation state is defined by the number of available and allocated
resources and the maximum requirements of all processes in the system.
Safe State:-
 A state is safe if the system can allocate all resources requested by all
processes (up to their stated maximums) without entering a deadlock state.
 More formally, a state is safe if there exists a safe sequence of processes {P0,
P1, P2, ..., PN} such that all of the resource requests for Pj can be granted
using the resources currently allocated to Pj and all processes Pi where i < j.
(i.e. if all the processes prior to Pj finish and free up their resources, then Pj
will be able to finish also, using the resources that they have freed up.)
 If a safe sequence does not exist, then the system is in an unsafe state, which
MAY lead to deadlock. (All safe states are deadlock free, but not all unsafe
states lead to deadlocks.)

 For example, consider a system with 12 tape drives, allocated as follows. Is


this a safe state? What is the safe sequence?
Maximum Current
Process
Needs Allocation
P0 10 5
P1 4 2
P2 9 2

 <P0, P1, P2> leads to Deadlock (Unsafe State)


 <P1, P0, P2> avoids Deadlock (Safe State)
Methods of Deadlock Avoidance
1) Resource Allocation Graph Algorithm
 Resource Allocation Graph
 In some cases deadlocks can be understood more clearly through the
use of Resource-Allocation Graphs, having the following properties:
1) A set of resource categories, {R1, R2, R3, . . ., RN}, which appear
as square nodes on the graph. Dots inside the resource nodes
indicate specific instances of the resource. (E.g. two dots might
represent two laser printers.)

2) A set of processes, {P1, P2, P3, . . ., PN}


3) Request Edges - A set of directed arcs from Pi to Rj, indicating
that process Pi has requested Rj, and is currently waiting for that
resource to become available. That is, (Pi -> Rj).
4) Assignment Edges - A set of directed arcs from Rj to Pi indicating
that resource Rj has been allocated to process Pi, and that Pi is
currently holding resource Rj. That is, (Pi <- Rj).
 If a resource-allocation graph contains no cycles, then the system is not
deadlocked. The below graph is an example for the resource-allocation
graph with no deadlock.

 If a resource-allocation graph does contain cycles AND each resource


category contains only a single instance, then a deadlock exists.

 If a resource category contains more than one instance, then the


presence of a cycle in the resource-allocation graph indicates the
possibility of a deadlock, but does not guarantee one.

 Resource-Allocation Graph Algorithm


 If resource categories have only single instances of their resources, then
deadlock states can be detected by cycles in the resource-allocation
graphs.
 In this case, unsafe states can be recognized and avoided by
augmenting the resource-allocation graph with claim edges, noted by
dashed lines, which point from a process to a resource that it may
request in the future.
 In order for this technique to work, all claim edges must be added to
the graph for any particular process before that process is allowed to
request any resources. (Alternatively, processes may only make
requests for resources for which they have already established claim
edges, and claim edges cannot be added to any process that is currently
holding resources).

 When a process makes a request, the claim edge Pi->Rj is converted to


a request edge. Similarly when a resource is released, the assignment
reverts back to a claim edge.
 This approach works by denying requests that would produce cycles in
the resource-allocation graph, taking claim edges into effect.
 Consider for example what happens when process P2 requests resource
R2. If the request is granted, then the resource-allocation graph is like
as follows; An unsafe state in a resource allocation graph:

 The resulting resource-allocation graph would have a cycle in it, and so


the request cannot be granted.
2) Banker's Algorithm
 For resource categories that contain more than one instance the resource-
allocation graph method does not work.
 Banker’s algorithm is named so because it is used in banking system to
check whether loan can be sanctioned to a person or not.
 In other words, the bank would never allocate its money in such a way that
it can no longer satisfy the needs of all its customers. The bank would try
to be in safe state always.
 When a process starts up, it must state in advance the maximum allocation
of resources it may request, up to the amount available on the system.
 When a request is made, the scheduler determines whether granting the
request would leave the system in a safe state. If not, then the process must
wait until the request can be granted safely.
 Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of
resources types.
1) Available[m]:- indicates how many resources are currently available
of each type.
2) Max[n][m]:- indicates the maximum demand of each process of
each resource.
3) Allocation[n][m]:- indicates the number of each resource category
allocated to each process.
4) Need[n][m]:- indicates the remaining resources needed of each type
for each process.
Need[ i ][ j ] = Max[ i ][ j ] - Allocation[ i ][ j ] for all i, j
Where i -> process Pi
j -> resource Rj

Banker's Algorithm consists of two algorithms:-


1) Safety Algorithm
2) Resource request algorithm
I. Safety Algorithm
 In order to apply the Banker's algorithm, we first need an algorithm
for determining whether or not a particular state is safe.
 This algorithm determines if the current state of a system is safe,
according to the following steps:
1) Let Work and Finish be array of length m and n respectively.
Initialize as follows:-
Work = Available
Finish[i] = false , where i=0,1,2,3,....n

2) Find an i such that both;


Finish[i] = false , where i=0,1,2,3,....n
Need[i] <= Work[i]
This process has not finished, but could with the given available
working set. If no such i exists, go to step 4.

3) Set as follows:
Work = Work + Allocation[ i ]
Finish[i] = true
Go to step 2 to check the status of resource availability for the
next process.
4) If finish[i] == true for all i, then the state is a safe state, because a
safe sequence has been found.
II. Resource-Request Algorithm
 This algorithm checks if the request can be safely granted or not to the
process requesting for resources.
 If a process Pi request for c instances for resource of type j then it can
be represented as Request[i,j]=c. This algorithm has the following
steps:-
1) If Request[i,j] <= Need[i,j], then go to step2. Otherwise an error
flag can be raised stating that process is requesting for
resources beyond its need.
2) If Request [i,j] <= Available[i,j], then proceed to step-3 else,
the process has to wait for the available resource to be greater
than equal to the requested resources.
3) Now, if Request[i,j] <= Available[i,j] then resource are
allocated to the process Pi and the Available[i,j],
Allocation[i,j]and Need[i,j] will updated as given below:
Available[i,j] = Available[i,j] - Request[i,j]
Allocation[i,j] = Allocation[i,j] + Request[i,j]
Need[i,j] = Need[i,j] - Request[i,j]

 For example, consider a system with 3 process (P1, P2, and P3) and 10
resources of same type (eg;- magnetic tape). The maximum resource
requirements and the current allocations are shown below:
Maximum Current
Process
Need Usage
P1 8 3
P2 5 1
P3 8 2
Available Resources = 4 ie, (10 – (3+1+2)) = 4
 This State is Safe because, still possible for all 3 processes to
finish.
 In other words, the key to a state being safe is that there is at least
one way for all processes to finish.
 The safe sequence is: <P2, P1, P3>
 Now, consider the following system state:
Maximum Current
Process
Need Usage
P1 9 3
P2 5 1
P3 9 2
 This state is unsafe. Process P2 could use the 4 available resources
and finish its execution. This would release 5 resources. But the
available resource at this point cannot meet the maximum needs of
either P1 or P3. So this state is unsafe.
 An unsafe state does not mean that a deadlock will happen, since
the process may not in fact require their declared maximum needs,
or some resources may be temporarily released.
 An unsafe state does imply is simply that some unfortunate
sequence of events might lead to a deadlock.
Advantages of Deadlock Avoidance:-
1) It does not require the acquisition of all resources at once.
2) It will not force preemption of resources.
3) It eliminates the problem of resource idling due to premature
acquisition.
Disadvantages of Deadlock Avoidance:-
1) Each process has to specify its maximum resource requirement and it
has the tendency to overestimate the resource requirements.
2) Avoidance algorithm must be executed every time a resource request
is made. This will lead to processing overhead.
Deadlock Detection and Recovery
If deadlocks are not avoided, then another approach is to detect when they
have occurred and recover somehow.
In addition to deadlock detection a policy or algorithm must be in place for
recovering from deadlocks.
Single Instance of Each Resource Type
 If each resource category has a single instance, then we can use a
variation of the resource-allocation graph known as a wait-for graph.
 A wait-for graph can be constructed from a resource-allocation graph by
eliminating the resources and collapsing the associated edges.
 An arc from Pi to Pj in a wait-for graph indicates that process Pi is
waiting for a resource that process Pj is currently holding.
 Consider the following Resource allocation graph:
Its corresponding wait-for graph is given below:

 This algorithm must maintain the wait-for graph, and periodically search
it for cycles.
 Cycles in the wait-for graph indicate deadlocks.
Several Instance of a Resource Type (Using Banker’s Algorithm)
1) Let Work and Finish be array of length m and n respectively. Initialize as
follows:-
Work = Available
If Allocation[i] != 0:
Finish[i] = false
Else:
Finish[i] = true

2) Find an i such that both;


Finish[i] = false , where i=0,1,2,3,....n
Need[i] <= Work[i]
This process has not finished, but could with the given available
working set. If no such i exists, go to step 4.

3) Set as follows:
Work = Work + Allocation[ i ]
Finish[i] = true
Go to step 2 to check the status of resource availability for the
next process.
4) If finish[i] == false for any process Pi, then that process is specifically
involved in the deadlock which has been detected.
Detection-Algorithm Usage
i. How often a deadlock is likely to occur?
ii. How many processes will need to be rolled back?
Recovery from Deadlock
1) Process Termination
 Two basic approaches, both of which recover resources allocated to
terminated processes:-
i. Terminate all processes involved in the deadlock (Expensive).
ii. Terminate processes one by one until the deadlock is broken
(Overhead).
 Criteria for Process Termination:-
i. Process priorities.
ii. How long the process has been running, and how close it is to
finishing.
iii. How many and what type of resources is the process holding
(Are they easy to preempt and restore?)
iv. How many more resources does the process need to complete.
v. How many processes will need to be terminated
vi. Whether the process is interactive or batch.
2) Resource Preemption
 When preempting resources to relieve deadlock, there are three
important issues to be addressed:
i. Selecting a victim - Deciding which resources to preempt from
which processes involves many of the same decision criteria
outlined above.
ii. Rollback - Ideally one would like to roll back a preempted
process to a safe state prior to the point at which that resource
was originally allocated to the process. Unfortunately it can be
difficult or impossible to determine what such a safe state is, and
so the only safe rollback is to roll back all the way back to the
beginning. (i.e. abort the process and make it start over. )
iii. Starvation - How do you guarantee that a process won't starve
because its resources are constantly being preempted? One
option would be to use a priority system, and increase the
priority of a process every time its resources get preempted.
Eventually it should get a high enough priority that it won't get
preempted any more.

You might also like