Module 1-Os
Module 1-Os
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.
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
P1 fork() P2
It refers to the event where the OS puts the process from ready to
running state.
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
6. Process Termination
There are many events that may lead to process termination, some of
them are:-
2) A problem in hardware
Context Switching
processes from a given state to another one for the execution of its function
1) Multitasking
2) Interrupt Handling
1. Firstly, the context of the process P1 i.e. the process present in the
P1 i.e. PCB1.
2. Now, you have to move the PCB1 to the relevant queue i.e. ready
3. From the ready state, select the new process that is to be executed i.e.
executed by the CPU, then you can get the position of last executed
5. Similarly, if you want to execute the process P1 again, then you have
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.
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.
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)
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.
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
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.
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
1) Race Condition
A critical section is a code segment that can be accessed by only one process at
a time.
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;
lock = 0; lock = 0;
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.
2) Readers-and-Writers Problem
3) Dining-Philosophers Problem
signal(readsem);
}
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
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.
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.)
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
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.