0% found this document useful (0 votes)
15 views

OS UNIT 3 NOTES

Uploaded by

Shlok Gaikwad
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)
15 views

OS UNIT 3 NOTES

Uploaded by

Shlok Gaikwad
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/ 17

Unit 3

Process Coordination
Q. Explain Synchronization: Principles of Concurrency.
 Concurrency is the execution of the multiple instruction sequences at the same
time.
 It is different than parallelism wherein tasks are running in parallel, but use of
multi-processor machine is must.
 Concurrency require multi-processor machine.
 Processes are concurrent if they exist at the same time.
There are two types of concurrent processes.
a) Independent Process
 The process that does not affect or is affected by the other process while its
execution then the process is called Independent Process.
 Example The process that does not share any shared variable, database, files, etc.
b) Cooperative Process
 Cooperating Process in the operating system is a process that gets affected by
other processes under execution or can affect any other process under execution.
For synchronization purpose: -
 The task or process must communicate with each other through IPC mechanism
Successful use of concurrency among processes requires the ability to define critical
section/region and enforce mutual exclusion.
 Concurrent access to shared data may result in data inconsistency.
 Maintaining data consistency requires mechanisms to ensure the orderly execution
of cooperating processes.
 Shared-memory solution to bounded-buffer problem allows at most n – 1 item in
buffer at the same time.
Synchronization problems
1. Readers-writer problem
2. Producer Consumer problem
3. Dining philosopher problem

Q. Explain Inter-Process Communication.


 Process frequently needs to communicate.
1
 For Ex. The output of first process must be passed to the second process.
 No two or more processes should get in each other’s way.
 For Ex. Airline Reservation system. Proper sequencing when dependencies are
present.
 For Ex. Read from ADC then write to Display.
"Inter-process communication is used for exchanging useful information between
numerous threads in one or more processes (or programs)."

Processes can communicate with each other through both:


 Shared Memory
 Message passing
 Figure 1 below shows a basic structure of communication between processes via
the shared memory method and via the message passing method.
 An operating system can implement both methods of communication.
 First, we will discuss the shared memory methods of communication and then
message passing.
 Communication between processes using shared memory requires processes to
share some variable, and it completely depends on how the programmer will implement
it.
 One way of communication using shared memory can be imagined like this:
Suppose process1 and process2 are executing simultaneously, and they share some
resources or use some information from another process.
 Process1 generates information about certain computations or resources being used
and keeps it as a record in shared memory.
 When process2 needs to use the shared information, it will check in the record
stored in shared memory and take note of the information generated by process1 and
act accordingly.
 Processes can use shared memory for extracting information as a record from
another process as well as for delivering any specific information to other processes.

2
I) Shared Memory Method
 Ex: Producer-Consumer problem
 There are two processes: Producer and Consumer.
 The producer produces some items and the Consumer consumes that item.
 The two processes share a common space or memory location known as a buffer
where the item produced by the Producer is stored and from which the Consumer
consumes the item if needed.
 There are two versions of this problem: the first one is known as the unbounded
buffer problem in which the Producer can keep on producing items and there is no
limit on the size of the buffer, the second one is known as the bounded buffer
problem in which the Producer can produce up to a certain number of items before
it starts waiting for Consumer to consume it.
 We will discuss the bounded buffer problem.
 First, the Producer and the Consumer will share some common memory, then the
producer will start producing items.
 If the total produced item is equal to the size of the buffer, the producer will wait
to get it consumed by the Consumer.
 Similarly, the consumer will first check for the availability of the item.
 If no item is available, the Consumer will wait for the Producer to produce it.
 If there are items available, Consumer will consume them.
II) Messaging Passing Method
 Now, we will start our discussion of the communication between processes via
message passing.
 In this method, processes communicate with each other without using any kind of
shared memory.
 If two processes p1 and p2 want to communicate with each other, they proceed as
follows:
 Establish a communication link (if a link already exists, no need to establish it
again.)
 Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)
 The message size can be of fixed size or of variable size.
 If it is of fixed size, it is easy for an OS designer but complicated for a programmer
and if it is of variable size then it is easy for a programmer but complicated for the
OS designer.
 A standard message can have two parts: header and body.
The header part is used for storing message type, destination id, source id,
message length, and control information.
3
 The control information contains information like what to do if runs out of buffer
space, sequence number, priority. Generally, message is sent using FIFO style.
Role of Synchronization in Inter Process Communication
 It is one of the essential parts of inter process communication.
 Typically, this is provided by interprocess communication control mechanisms, but
sometimes it can also be controlled by communication processes.
These are the following methods that used to provide the synchronization:

1. Mutual Exclusion
2. Semaphore
3. Barrier
4. Spinlock

Q. Explain Mutual Exclusion in detail?

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).
 Approaches to Interprocess Communication
4
These are a few different approaches for Inter- Process Communication:

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

Message Passing: -

5
 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:

send (message)
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.i.e.,
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 is a type of general communication between two unrelated processes.
 It can also be considered as full duplex, which means that one process can
communicate with another process and vice versa.
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:
 It helps to speedup modularity.
 Computational, Privilege separation, Convenience helps operating system to
communicate with each other and synchronize their actions as well.

Q. What is Race Condition?


 Race Condition or Race Hazard is an undesirable situation of software, electronics, or
other systems.
 When the output of the system or program depends on the sequence or timing of other
uncontrolled events, this condition is called Race Condition.
6
 This condition occurs mainly in the logic circuits, distributed and multithreaded
software programs.
 A race condition is categorized as either a critical or non-critical race condition.
 The critical race conditions are those conditions that occur when the order of the
internal variable regulates the last state of the machine.
 On the other hand, the non-critical race conditions are those conditions which occur
when the order of internal variables does not regulate the last state of the machine.
 Situation where two or more processes are reading or writing some shared data and the
final result depends on who runs precisely when are called race condition.
 Race condition: The situation where several processes access – and manipulate shared
data concurrently.
 The final value of the shared data depends upon which process finishes last.
 To prevent race conditions, concurrent processes must be synchronized.
 A race condition occurs in the software when the computer program depends on the
threads or processes of a program.
 In software, we cannot debug the race condition because the final result is non-
deterministic and based on the timing of multiple threads.

To avoid RACE condition


a) No two processes may be simultaneously inside their critical regions.
b) No assumptions may be made about speeds or the number of CPUs.
c) No process running outside its critical region may block other processes.
d) No process should have to wait forever to enter its critical region.

Q. What is Mutual Exclusion?

 It is a way of a making sure that if one process is using a shared variable or file, the
other process will be excluded from doing the same thing.
 It is a program object that prevents simultaneous access to a shared resource.
 Concept used in concurrent programming by taking care of CRITICAL SECTION ( it
is a piece of code in which processes or threads access a shared resource)
Arrange resources such that NO TWO PROCESSES EVER ENTER IN THEIR
CRITICAL SECTION / REGION (at the same time).
 Mutual exclusion is a mechanism to ensure that only one process is doing certain things
at one time, this avoids data inconsistency.
 Mutual exclusion algorithm ensures that if a process is already performing write
operation on a data object no other process/thread is allowed to access the same object
7
until the first process has finished writing upon the data object and released the object
for other processes to read and write upon.
 All others should be prevented from modifying shared data until the current process
finishes.
 There is thus exclusion of one process by another. In certain regions of
an operating system.
 The requirement of mutual exclusion was first Solution of a problem in concurrent
programming control, and is credited as the first topic in the study of concurrent
algorithms.

Mutual exclusion Algorithm:


1) No Starvation:
Any site should not wait indefinitely to execute critical section while other site are
repeatedly executing critical section
2) No Deadlock:
Two or more site should not endlessly wait for any message that will never arrive.
3) Fault Tolerance:
In case, it should be able to recognize it by itself in order to continue functioning
without any disruption.
4) Fairness:
Any request to execute critical section must be executed in the order they are made.

Distributed algorithm:

 A distributed algorithm is an algorithm designed to run on computer hardware


constructed from interconnected processors.
 Distributed algorithms are used in different application areas of distributed computing.
 Such as telecommunications, scientific computing, distributed information processing,
and real-time process control.

1) Token Based Algorithm:

 If a site possesses the unique token, it is allowed to enter its critical section.
 This approach ensures Mutual exclusion as the token is unique.
 Each request for critical section contains a sequence number.
 This sequence number is used to distinguish old and current requests.
 This approach ensures Mutual exclusion as the token is unique.
 Example: Suzuki-Kasami’s Broadcast Algorithm

8
2) Non-token-based approach:

 All algorithm which follows non-token-based approach maintains a logical clock.


 Logical clocks get updated according to Lamport’s scheme.
 A site communicates with other sites in order to determine which sites should execute
critical section next.
 This requires exchange of two or more successive round of messages among sites.
 Example: Lamport's algorithm, Ricart–Agrawala algorithm.

3) Quorum based approach:

 Instead of requesting permission to execute the critical section from all other sites, Each
site requests only a subset of sites which is called a quorum.
 Any two subsets of sites or Quorum contains a common site.
 This common site is responsible to ensure mutual exclusion.
 Example: Maekawa’s Algorithm.

Q. Explain Mutual Exclusion HARDWARE Support in detail?


 "Mutual exclusion: Hardware support", it states for a process to guarantee mutual
exclusion it is sufficient to block all interrupts, and this can be done through interrupt
disabling.
 However, the cost is high since the processor is limited in its ability to interleave.

Interrupt Disabling
 A process runs until it invokes an operating system service or until it is interrupted.
 Disabling interrupts guarantees mutual exclusion.
 Processor is limited in its ability to interleave programs.
 Multiprocessing
• disabling interrupts on one processor will not guarantee mutual exclusion.
Special Machine Instructions
 At a hardware level, access to a memory location excludes any other access to the same
location.
 Performed in a single instruction cycle (Not subject to interference from other
instructions)
 Reading and writing
 Reading and testing

9
Q. Explain Mutual Exclusion: OS Support (Semaphores and MUTEX)?

 It consists of
1. Semaphores
2. Monitors
3. Message Passing
Semaphores
 Two or more processes can cooperate by means of simple signals
 A process is forced to stop at a specified place until it has received a specific signal
 Special variable called a semaphore is used for signaling
 To transmit a signal via a semaphore s, a process executes primitive signal(s) (V -
increment in dutch)
 To receive a signal via semaphore s, a process executes primitive wait(s) (P - test in
dutch)
 If a process is waiting for a signal, it is suspended until that signal is sent.
 Wait and signal operations cannot be interrupted.
 Queue is used to hold processes waiting on the semaphore.

Proposals for achieving mutual exclusion


Disabling interrupts
Lock variables
Strict alternation
Peterson's solution
The TSL instruction

Mutual Exclusion:
 Disabling interrupts: Each process disables all interrupts just after entering in its critical
section and reenable all interrupts just before leaving critical section.
 With interrupts turned off the CPU could not be switched to other process.
 Hence, no other process will enter its critical and mutual exclusion achieved.
 Disable interrupt before entering in critical region What will happen if process forget to
enable interrupt?

10
Q. What will happen in multicore environment?

 Mutual Exclusion: Lock variables


1. Use single, shared Lock variable.
2. Lock variable will be initially 0.
3. When process want to enter in critical region
4. It first test the lock.
5. If lock is 0,process set it to 1 and enters in
Critical region.
6. If lock is 1, process wait until it becomes 0.
7. Race around.
8. LOCK = 0 no process in critical region
9. LOCK = 1 process in critical region

 Mutual Exclusion: Strict Alternation

 In this proposed solution, the integer variable 'turn' keeps track of whose turn is to enter
the critical section.
 Initially, process A inspect turn, finds it to be 0, and enters in its critical section.
 Process B also finds it to be 0 and sits in a loop continually testing 'turn' to see when it
becomes 1.
 Continuously testing a variable waiting for some value to appear is called the Busy-
Waiting.
 Spin lock problem ,wastage of CPU cycles.
 A lock that is uses busy waiting is called a spin lock.

Q. What is Semaphore?
 Semaphore was proposed by Dijkstra in 1965 which is a very significant technique to
manage concurrent processes by using a simple integer value, which is known as a
semaphore.
 A semaphore is simply an integer variable that is shared between threads.
 This variable is used to solve the critical section problem and to achieve process
synchronization in the multiprocessing environment.
 Semaphores are of two types:
Binary Semaphore –
 This is also known as mutex lock.

11
 It can have only two values – 0 and 1.
 Its value is initialized to 1. It is used to implement the solution of critical section
problems with multiple processes.
Counting Semaphore –
 Its value can range over an unrestricted domain.
 It is used to control access to a resource that has multiple instances.

Semaphores : Limitations / drawback:


 Priority inversion is big limitation.
 Its use is not enforced but is by convention only.
 With improper use a process may block indefinitely.
 Access to semaphore can come from anywhere in a program.
 No control or guarantee of proper usage.
 No linguistic connection between semaphore and data to which semaphore controls
some access.

Q. What is Mutex?
 Mutex ensures that the code in the critical section (which has shared resources) being
controlled will only be used by a single thread at a time.
 Easy and efficient for implementation.
 Can be one of two states: Locked or Unlocked.
 A thread having the lock of mutex can use the critical section while other threads must
wait till the lock is released.
 Mutex tries to solve the famous 'Producer Consumer Problem'.
 As it ensures mutual exclusion, either the producer or consumer has their key(mutex),
so at a given time either the producer fills the buffer or the consumer consumes it but
both cannot occur at once.

Advantages of Mutex
 Mutexes are nothing more than basic locks that must be obtained prior to entry in a
critical section and subsequently released.
 Absence of race situations since a single thread is in the crucial region at a particular
time, and data consistency is there.

Disadvantages of Mutex
 Use of mutex can lead to Starvation.

12
 eg: A thread on gaining lock enters the critical section, but if this thread gets preempted
or goes to sleep, then the other process cannot enter the critical section as the other
process will wait till the lock is released.
 Locking or unlocking cannot be done from a different context.
 A single thread is allowed in the critical section at any instance.
 The standard implementation may result in a busy waiting state, wasting CPU time.

Q. Explain Programming language support ( Monitors)?

 It is object that contains both data and procedures needed to perform allocation of
shared resource.
 It is PL construct that support both data access synchronization & control
synchronization.
 Implemented in Pascal, JAVA, C++.
 Monitor is an abstract data type for which only one process may be executing a
procedure at any given time.
 The processes running outside the monitor can’t access the internal variable of the
monitor but can call procedures of the monitor.
 JAVA makes extensive use of monitors to implement Mutual Exclusion.
 It is the collection of condition variables and procedures combined together in a special
kind of module or a package.
 cwait : it suspends execution of calling process on condition.
 The suspended processes are placed in block queue of that condition variable
csignal : resume execution of some process blocked after a cwait on same condition.
 When a process performs signal operation on condition variable, one of the blocked
processes is given chance
13
Advantages of Monitor:
 Monitors have the advantage of making parallel programming easier and less error
prone than using techniques such as semaphore.

Disadvantages of Monitor:
 Monitors have to be implemented as part of the programming language.
 The compiler must generate code for them.
 This gives the compiler the additional burden of having to know
what operating system facilities are available to control access to critical sections in
concurrent processes.
 Some languages that do support monitors are Java,C#,Visual Basic,Ada and concurrent
Euclid.

Q. Explain Classical Synchronization Problems?


 In this problem there are some processes (called readers) that only read the shared data,
and never change it, and there are other processes(called writers) who may change the
data in addition to reading, or instead of reading it.
 Synchronization problems are present as example of a large class of concurrency-
control problems.
 These are used for testing newly proposed synchronization scheme. We use semaphores
for synchronization.

1. Readers – Writers
2. Producer Consumer Problem (bounded buffer)

Reader’s Writers problem


 Database is shared among several concurrent processes.
 Some pro may want only to read the database while others may need to update the
same.
Former: Reader
Latter: Writer
 If two readers access the shared data simultaneously, no effect but if one or two writers
access the database , it may create problem.
 Many readers may access the object concurrently , but if a writer is accessing the object
no other process may access the object.

First Readers Writers Problem


 Readers have priority over writers.
14
 Here writer may need to wait for a long time also.

Second Readers Writers Problem


 Writers have priority over readers.
 When a writer wants to access the object , only readers which have already obtained
permission to access the object are allowed to complete their access.
 Any reader requesting access after writer must wait until writer is finishing the task.
Reader may need to wait.

 Producer Consumer Problem


 Two processors:
ONE is PRODUCER
OTHER is CONSUMER
 Both run concurrently and share a common buffer.
 Producer generated items are consumed by customer.
 One or more producers are generating data and placing these in a buffer.
 A single consumer is taking items out of the buffer one at time.
 Only one producer or consumer may access the buffer at any one time

Q. What are Conditions for Resource Deadlocks?


 Different conditions for resource deadlock are:
1. Mutual exclusion condition
Each resource is either currently assigned to exactly one process or is available.
2. Hold and wait condition.
Process currently holding resources that were granted earlier can request new resources.
3. No preemption condition.
Resources previously granted cannot be forcibly taken away from the process.
4. Circular wait condition.
each one is waiting for a resource held by the next member.

Strategies for dealing with deadlocks:


1. Just ignore the problem.
2. Detection and recovery. Let deadlocks occur, detect them, take action.
3. Dynamic avoidance by careful resource allocation.
4. Prevention, by structurally negating one of the four required conditions.

Recovery from Deadlock


Recovery through preemption
15
Recovery through rollback (check point)
Recovery through killing processes

For Resource

Preempt the resource


 The ability to take a resource away from a process, have another process use it, and then
give it back without the process noticing It is highly dependent on the nature of the
resource.
 Deadlock recovery through preemption is too difficult or sometimes impossible.
 We can snatch one of the resources from the owner of the resource (process) and give it
to the other process with the expectation that it will complete the execution and will
release this resource sooner.
 Well, choosing a resource which will be snatched is going to be a bit difficult.

Rollback to a safe state


 In this case of deadlock recovery through rollback, whenever a deadlock is detected, it
is easy to see which resources are needed.
 To recover from deadlock, a process that owns a needed resource is rolled back to a
point in time before it acquired some other resource just by starting one of its earlier
checkpoints.
 System passes through various states to get into the deadlock state.
 he operating system can rollback the system to the previous safe state.
 For this purpose, OS needs to implement check pointing at every state.
 The moment, we get into deadlock, we will rollback all the allocations to get into the
previous safe state.

For Process

Kill a process
 This method of deadlock recovery via killing processes is the most basic method of
deadlock recovery.
 Sometimes it is best to kill a process that can be restarted from the beginning with no ill
effects.
 Assume we have opened an app, say "Google Chrome," but while browsing, the
browser may crash and stop working.
 And our system has reached a point where we are unable to do anything; thus, restarting
your app is the best way to resolve this issue, followed by simply opening the task
manager to terminate the process.
16
 Killing a process can solve our problem but the bigger concern is to decide which
process to kill.
 Generally, the Operating system kills a process which has done least amount of work
until now.

Kill all process


 This is not a suggestible approach but can be implemented if the problem becomes very
serious.
 Killing all process will lead to inefficiency in the system because all the processes will
execute again from starting.

17

You might also like