2. WHAT IS 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
● Concurrency results in sharing of resources result in problems like deadlocks and
resources starvation.
eating and talking with the same source mouth
we can only do one work at a time
3. WHAT IS PARALLELISM
Parallelism:
Parallelism is related to an application where tasks are divided into smaller sub-tasks that are
processed seemingly simultaneously or parallel. It is used to increase the throughput and
computational speed of the system by using multiple processors. It enables single sequential
CPUs to do lot of things “seemingly” simultaneously.
singing and dancing at the same time
we can do both work
4. WHAT IS CONCURRENCY
● Concurrency increase the context switching on shared resource or memory
● it encompasses a host of design issue including communication ,sharing resources like
memory ,i/o files
● It arises in three different context
Structured applications: As an extension of the principles of modular design and
structured programming, some applications can be effectively programmed as a set of
concurrent processes.
Multiple applications: Multiprogramming was invented to allow processing time to be
dynamically shared among a number of active applications
Operating system structure: The same structuring advantages apply to systems
programs, and we have seen that operating systems are themselves often implemented
as a set of processes or threads.
5. PRINCIPAL OF CONCURRENCY
● the activities of other process
● way OS handles interrupts
● scheduling process of the OS
● Interaction between multiple process running on one CPU
● interaction between multiple thread running in one process(multithreading )
● interaction between multiple processes
● interaction between distributed process
6. PROBLEM OF CONCURRENCY
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.
Optimal allocation of resources –
It is difficult for the operating system to manage the allocation of resources optimally.
Locating programming errors –
It is very difficult to locate a programming error because reports are usually not reproducible.
Locking the channel –
It may be inefficient for the operating system to simply lock the channel and prevents its use
by other processes.
7. ADVANTAGE
● Running of multiple applications –
It enable to run multiple applications at the same time.
● Better resource utilization –
It enables that the resources that are unused by one application can be used for other
applications.
● Better average response time –
Without concurrency, each application has to be run to completion before the next one can be run.
● Better performance –
It enables the better performance by the operating system. When one application uses only the
processor and another application uses only the disk drive then the time to run both application
DRAWBACKS
● It is required to protect multiple applications from one another.
● It is required to coordinate multiple applications through additional mechanisms.
● Additional performance overheads are required for switching among applications.
● Sometimes running too many applications concurrently leads to severely degraded performance.
8. ISSUE OF CONCURRENCY
1.Non-atomic
Operations that are non-atomic but interruptible by several processes may happen issues. A non-
atomic operation depends on other processes, and an atomic operation runs independently of
other processes.
2. Deadlock
In concurrent computing, it occurs when one group member waits for another member, including
itself, to send a message and release a lock. Software and hardware locks are commonly used to
arbitrate shared resources and implement process synchronization in parallel computing,
distributed systems, and multiprocessing.
3. Blocking
A blocked process is waiting for some event, like the availability of a resource or completing an I/O
operation. Processes may block waiting for resources, and a process may be blocked for a long
time waiting for terminal input. If the process is needed to update some data periodically, it will be
very undesirable.
9. ISSUE OF CONCURRENCY
4. Race Conditions
A race problem occurs when the output of a software application is determined by the timing or
sequencing of other uncontrollable events. Race situations can also happen in multithreaded
software, runs in a distributed environment, or is interdependent on shared resources.
5. Starvation
A problem in concurrent computing is where a process is continuously denied the resources it
needs to complete its work. It could be caused by errors in scheduling or mutual exclusion
algorithm, but resource leaks may also cause it.
Concurrent system design frequently requires developing dependable strategies for coordinating
their execution, data interchange, memory allocation, and execution schedule to decrease
response time and maximize throughput.
10. 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.
� A race condition is an undesirable situation that occurs
when a device or system attempts to perform two or
more operations at the same time.
� But, because of the nature of the device or system, the
operations must be done in the proper sequence to be
done correctly.
� To prevent race conditions, concurrent processes must
be synchronized.
Print spooler directory example :
Two processes want to access shared
memory at the same time.
11. Example of Race Condition
• Process A
next_free_slot = in
Write file name at slot (7)
next_free_slot += 1
in = next_free_slot (8)
Context Switch
• Process B
next_free_slot = in
Write file name at slot (8)
next_free_slot += 1
in = next_free_slot (9)
• Process A
next_free_slot = in (7)
Context Switch
• Process B
next_free_slot = in (7)
Write file name at slot (7)
next_free_slot += 1
in = next_free_slot (8)
Context Switch
• Process A
Write file name at slot (7)
next_free_slot += 1
in = next_free_slot (8)
12. Race Condition (2)
� Withdraw money from bank account
� two request for withdrawal from the same
account comes to a bank from two different
ATM machines
� A thread for each request is created
� Assume a balance of $1000
� if both $600 withdraw is request from the account
13. Critical Section
T1 T2 T3 T4
Process A
Process B
A enters in critical region
B attempt to
enter
A leaves critical region
B enters in
critical region
B leaves
critical region
B Blocked
Critical Section: The part of program where the
shared resource is accessed is called critical section
or critical region.
only one process can execute in its critical section at
a time .All other processes have to wait to execute in
its critical section
14. Critical Section
� Each process takes permission from the OS to enter into critical
section
� ENTRY SECTION : Block of code executed in preparation for entering
critical section .It acts as a gateway for a process to enter inside the
critical section.It ensures that only one process is present inside the
critical section at any time.It does not allow any other process to enter
inside the critical section if one process is already present inside it.
� EXIT SECTION : The code executed upon leaving the critical section It
acts as an exit gate for a process to leave the critical section.When a
process takes exit from the critical section, some changes are made
so that other processes can enter inside the critical section.
� REMAINDER SECTION : Rest of code
15. Critical Section Problem
● If multiple processes access the critical section concurrently, then results produced might be inconsistent.
● This problem is called as critical section problem
SYNCHRONIZATION MECHANISMS:Synchronization mechanisms allow the processes to access critical
section in a synchronized manner to avoid the inconsistent results.
16. 1. Mutual Exclusion-
● The processes access the critical section in a mutual exclusive manner.
● Only one process is present inside the critical section at any time.
● No other process can enter the critical section until the process already present inside it completes.
2. Progress-
● An entry of a process inside the critical section is not dependent on the entry of another process inside
the critical section.
● A process can freely enter inside the critical section if there is no other process present inside it.
● A process enters the critical section only if it wants to enter.
● A process is not forced to enter inside the critical section if it does not want to enter.
3. Bounded Wait-
● The wait of a process to enter the critical section is bounded.
● A process gets to enter the critical section before its wait gets over.
4. Architectural Neutral-
● It can run on any architecture without any problem.
● There is no dependency on the architecture.
17. Mutual Exclusion
Mutual Exclusion: Way of making sure that if one
process is using a shared variable or file; the other
process will be excluded (stopped) from doing the
same thing.
Conditions
We need four conditions to hold to have a good
solution for the critical section problem
1. No two processes may be at the same moment
inside their critical section.
2. No assumptions are made about relative speed
of processes or number of CPUs.
3. No Process should be outside its critical section
and should block other processes.
4. No Process should wait arbitrarily long to enter
its critical section.
18. Real life example to explain mechanisms for achieving mutual exclusion with busy waiting
19. Mutual exclusion with busy waiting
�Mechanisms for achieving mutual exclusion with busy waiting
⮩ Disabling interrupts (Hardware approach)
⮩ Shared lock variable (Software approach)
⮩ Strict alternation (Software approach)
⮩ TSL (Test and Set Lock) instruction (Hardware approach)
⮩ Dekker’s solution (Software approach)
⮩ Peterson’s solution (Software approach)
20. Disabling interrupts (Hardware approach)
1) INTERRUPT HANDLING
● simplest solution for mutual exclusion is to have each
process disable all interrupts just after entering its
critical section.
● The process can re-enable these interrupts just before
leaving the critical section.
● Thus once a process has disable interrupts, it can use
the shared memory without the fear that any other
process will interfere.
T1 T3 T4
Process A
Process B
< disable interrupts > < enable interrupts >
< critical section > < remainder section >
�while (true)
{
< disable interrupts >;
< critical section >;
< enable interrupts >;
< remainder section >;
}
21. Problems in Disabling interrupts (Hardware approach)
� Unattractive or unwise to give user processes the power to turn off interrupts.
� What if one of the process did it (disable interrupt) and never turned them on (enable interrupt)
again? That could be the end of the system.
� If the system is a multiprocessor, with two or more CPUs, disabling interrupts affects only the
CPU that executed the disable instruction. The other ones will continue running and can access
the shared memory.
� It works only in uniprocessor system.
� Perform degrade as multiprogramming is not utilize
22. TSL (Test and Set Lock) instruction (Hardware approach)
�Algorithm
enter_region: (Before entering its critical region, process calls enter_region)
TSL REGISTER, LOCK |copy lock variable to register set lock to 1
CMP REGISTER, #0 |was register variable 0?
JNE enter_region |if it was nonzero, lock was set, so loop
RET |return to caller: critical region entered
leave_region: (When process wants to leave critical region, process calls leave_region)
MOVE LOCK, #0 |store 0 in lock variable
RET |return to caller
T1 T2 T3
Process 0
Process 1
Register 0
23. TSL (Test and Set Lock) instruction (Hardware approach)
�Test and Set Lock Instruction
⮩ TSL REGISTER, LOCK
⮩ It reads the contents of the memory word lock into register RX and then stores a nonzero value at the
memory address lock.
⮩ The operations of reading the word and storing into it are guaranteed to be indivisible—no other processor
can access the memory word until the instruction is finished.
⮩ The CPU executing the TSL instruction locks the memory bus to prohibit other CPUs from accessing memory
until it is done.
T1 T2 T3
Process 0
Process 1
Register 0
24. Shared lock variable (Software approach)
A lock variable provides the simplest synchronization mechanism for processes.
Its a software mechanism implemented in user mode, i.e. no support required from the Operating
System.
1. It is a busy waiting solution (keeps the CPU busy even when it’s technically waiting).
2. It can be used for more than two processes.
� A shared variable lock having value 0 or 1.
� Before entering into critical region a process checks a shared variable lock’s value.
⮩ If the value of lock is 0 then set it to 1 before entering the critical section and enters into critical
section and set it to 0 immediately after leaving the critical section.
⮩ If the value of lock is 1 then wait until it becomes 0 by some other process which is in critical
section.
25. Shared lock variable (Software approach)
� while (true)
{ < set shared variable to 1 >;
< critical section >;
< set shared variable to 0 >;
< remainder section >;
}
T1 T3 T4
Process A
Process B
< set lock to 1 > < set lock to 0 >
< critical section > < remainder section >
Problem:
• If process-A sees the value of lock variable 0 and before it can set it to
1 context switch occurs.
• Now process-B runs and finds value of lock variable 0, so it sets value
to 1, enters critical region.
• At some point of time process-A resumes, sets the value of lock
variable to 1, enters critical region.
• Now two processes are in their critical regions accessing the same
shared memory, which violates the mutual exclusion condition.
26. Strict alternation (Software approach)
� Integer variable 'turn' keeps track of whose turn is to enter the critical section.
� Initially turn=0. Process 0 inspects turn, finds it to be 0, and enters in its critical section.
� Process 1 also finds it to be 0 and therefore sits in a loop continually testing 'turn' to see when it
becomes 1.
� Continuously testing a variable waiting for some event to appear is called the busy waiting.
� When process 0 exits from critical region it sets turn to 1 and now process 1 can find it to be 1
and enters in to critical region.
� In this way, both the processes get alternate turn to enter in critical region.
27. Strict alternation (Software approach)
T1 T2 T3 T4
Process 0
Process 1
0 enters in critical region
1 attempt to
enter
0 leaves critical region
1 enters in
critical region
1 leaves
critical region
1 Busy Wait
0 1 0
0 0
1 attempt
to enter
T5
0
1 Busy Wait
Process 0
while (TRUE)
{
while (turn != 0) /* loop */ ;
critical_region();
turn = 1;
noncritical_region();
}
Process 1
while (TRUE)
{
while (turn != 1) /* loop */ ;
critical_region();
turn = 0;
noncritical_region();
}
28. Disadvantages of Strict alternation (Software approach)
�Taking turns is not a good idea when one of the processes is much slower than the other.
�Consider the following situation for two processes P0 and P1.
⮩ P0 leaves its critical region, set turn to 1, enters non critical region.
⮩ P1 enters and finishes its critical region, set turn to 0.
⮩ Now both P0 and P1 in non-critical region.
⮩ P0 finishes non critical region, enters critical region again, and leaves this region, set turn to 1.
⮩ P0 and P1 are now in non-critical region.
T1 T2 T3 T4
Process 0
Process 1
0 enters in critical region
1 attempt to
enter
0 leaves critical region
1 enters in
critical region
1 leaves
critical region
1 Busy Wait
0 1 0
0 0
T5
0
T6 T7
1
29. Disadvantages of Strict alternation (Software approach)
�Taking turns is not a good idea when one of the processes is much slower than the other.
�Consider the following situation for two processes P0 and P1.
⮩ P0 finishes non critical region but cannot enter its critical region because turn = 1 and it is turn of P1 to enter
the critical section.
⮩ Hence, P0 will be blocked by a process P1 which is not in critical region. This violates one of the conditions
of mutual exclusion.
⮩ It wastes CPU time, so we should avoid busy waiting as much as we can.
T1 T2 T3 T4
Process 0
Process 1
0 enters in critical region
1 attempt to
enter
0 leaves critical region
1 enters in
critical region
1 leaves
critical region
1 Busy Wait
0 1 0
0 0
T5
0
T6 T7
1 0 attempt to
enter
1 Busy Wait
30. Dekker’s solution (Software approach)
● Dekker’s algorithm is the first known solution to the mutual exclusion problem in concurrent
programming.
● It allows two threads to share a single use resource without conflict, only shared memory
for communication.
● If two processes attempt to enter the critical section at the same time, the algorithm will
allow only one process based on whose turn it is.
● If one process is already in the critical section, the other process will wait for the first
process to exit.
● This is done by two flags and one variable turn
● Flags used are wants_to_enter[0] and wants_to_enter[1], which indicate an intention to
enter the critical section on the part of the processes 0 and 1 respectively.
● A variable turn indicates who has the priority between the two processes.
31. Dekker’s solution (Software approach)
Algorithm
1. Consider two process P0 and P1
2. When P0 wants to enter the critical section it sets the flag to true.
3. It then checks for the flag P1.
4. If False P0 enters the critical section.
5. Else P0 checks turn.
6. If turn = 0, It knows that it is its turn to insist and periodically check P1’s flag.
7. P1 at some instance will note that it times to defer and set its flag to false which will allow
P0 to proceed.
8. After P0 has used the critical section , it sets flag to false to free the critical section and
sets turn to 1 to give P1 the right to insist.
35. Peterson’s solution (Software approach)
● provides a good algorithmic description of solving the critical-section problem and illustrates
some of the complexities involved in designing software that addresses the requirements of
mutual exclusion, progress, and bounded waiting.
● It’s for 2 processes which alternate execution between the critical section and remainder section.
Say, P1 is the first process and P2 is the second process.
● The 2 processes should share 2 data items with each other.
int turn
Boolean flag [2]
● Turn – It indicates the process who should enter into its critical section.
● Flag Array – It tells whether a process is ready to enter its critical section. Let flag[0] indicate
process Pi. If flag[0] = true , then Process Pi is ready to execute in its critical section. flag[1]
indicates process Pj. If flag[1] = true, then Process Pj is ready to execute in its critical section.
36. Peterson’s solution (Software approach)
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j);
/* critical section */
flag[i] = false;
/* remainder section */
}
while (true);
do {
flag[j] = true;
turn = i;
while (flag[i] && turn == i);
/* critical section */
flag[j] = false;
/* remainder section */
}
while (true);
pi pj
● First , pi sets flag[0] true,
then sets turn to j . So that if
Pj wants to enter the Critical
Section, it can do so.
● If Pi , Pj try to execute at the
same time, then turn is first
changed to i, then j or it
could be vice-versa. But, the
important point is, only one
of these 2 processes is
allowed to enter its critical
section. The second value
gets overwritten.
37. Semaphore
A semaphore is a variable that provides an abstraction for controlling the access of a shared
resource by multiple processes in a parallel programming environment.
Semaphore is defined as an integer variable which is used to solve the problem of the critical
section in process synchronization. In semaphore, we use two types of atomic operations
Characteristic of Semaphore
● It is a mechanism that can be used to provide synchronization of tasks.
● It is a low-level synchronization mechanism.
● Semaphore will always hold a non-negative integer value.
● Semaphore can be implemented using test operations and interrupts, which should be executed
using file descriptors.
● this is machine independent
38. Semaphore
Signal() / Up / V() / Release: a process
performs a signal operation to release the
critical section
This type of Semaphore operation is used
to control the exit of a task from a critical
section. It helps to increase the value of the
argument by 1, which is denoted as V(S).
Copy CodeP(S)
{
while (S>=0);
S++;
}
Wait() / Down / P(): a process performs a wait
operation to tell the semaphore that it wants to enter
the critical section
helps you to control the entry of a task into the
critical section. However, If the value of wait is
positive, then the value of the wait argument X is
decremented. In the case of negative or zero value,
no operation is executed. It is also called P(S)
operation.
Copy CodeP(S)
{
while (S<=0);
S--;
}
39. Type of Semaphore
1) Binary semaphores :It is a special form of semaphore used for implementing mutual
exclusion, hence it is often called a Mutex. the wait operation works only if the value of
semaphore = 1, and the signal operation succeeds when the semaphore= 0. it is easier to
implement
▪ Binary semaphores can take only 2 values (0/1).
▪ Binary semaphores have 2 methods associated with it (up, down / lock, unlock).
▪ They are used to acquire locks.
2) Counting semaphores :- These are used to implement bounded concurrency. These can be
used to control access to a given resource that consists of a finite number of Instances. Here
the semaphore count is used to indicate the number of available resources. If the resources are
added then the semaphore count automatically gets incremented and if the resources are
removed, the count is decremented. Counting Semaphore has no mutual exclusion.
▪ Counting semaphore can have possible values more than two.
41. Semaphore
Disadvantage of semaphores
● One of the biggest limitations of a semaphore is priority inversion.
● The operating system has to keep track of all calls to wait and signal semaphore.
● Their use is never enforced, but it is by convention only.
● In order to avoid deadlocks in semaphore, the Wait and Signal operations require to be executed
in the correct order.
● Semaphore programming is a complicated, so there are chances of not achieving mutual
exclusion.
● It is also not a practical method for large scale use as their use leads to loss of modularity.
● Semaphore is more prone to programmer error.
● It may cause deadlock or violation of mutual exclusion due to programmer error.
42. Monitor
� Monitors are used for process synchronization. With the help of programming languages, we
can use a monitor to achieve mutual exclusion among the processes.
� Example : Java Synchronized methods such as Java offers notify() and wait()
constructs.
� The Monitor is a module or package which encapsulates shared data structure, procedures,
and the synchronization between the concurrent procedure invocations.
� A monitor is a collection of procedures, variables, and data structures that are all grouped
together in a special kind of module or package.
43. Monitor
� Processes may call the procedures in a monitor whenever they want to, but they cannot directly
access the monitor’s internal data structures from procedures declared outside the monitor.
� Monitors have an important property for achieving mutual exclusion: only one process can be
active in a monitor at any instant.
� When a process calls a monitor procedure, the first few instructions of the procedure will check to
see if any other process is currently active within the monitor.
� If so, the calling process will be suspended until the other process has left the monitor. If no other
process is using the monitor, the calling process may enter.
� If the process is running outside the monitor, then it cannot access the monitor’s internal variable.
But a process can call the procedures of the monitor.
44. Monitor
Monitor data is accessible only
within the monitor .A shared data
structure can be protected by
placing it in a monitor. If the data in
a monitor represents some
resource then the monitor provides
a mutual exclusion facility for
accessing only those variable
declared locally within the monitor
and its formal parameter
cwait(condition variable)- suspend
execution of the calling process on
condition
csignal(condition variable):resume
execution of some process blocked
after a event on the same condition
45. Monitor
Components of Monitor
1. Initialization
2. Private data
3. Monitor procedure
4. Monitor entry queue
Initialization: - Initialization comprises the code, and when the monitors are created, we use this
code exactly once.
Private Data: - Private data is another component of the monitor. It comprises all the private data,
and the private data contains private procedures that can only be used within the monitor. So,
outside the monitor, private data is not visible.
Monitor Procedure: - Monitors Procedures are those procedures that can be called from outside
the monitor.
Monitor Entry Queue: - Monitor entry queue is another essential component of the monitor that
includes all the threads, which are called procedures.
46. Difference between Monitors and Semaphore
Features Semaphore Monitor
Definition A semaphore is an integer variable that
allows many processes in a parallel system
to manage access to a common resource like
a multitasking OS.
It is a synchronization process that
enables threads to have mutual
exclusion and the wait() for a given
condition to become true.
Syntax // Wait Operation
wait(Semaphore S) {
while (S<=0);
S--;
}
// Signal Operation
signal(Semaphore S) {
S++;
}
monitor {
//shared variable declarations
data variables;
Procedure P1() { ... }
Procedure P2() { ... }
.
.
.
Procedure Pn() { ... }
}
Condition
Variable
No condition variables. It has condition variables.
47. Difference between Monitors and Semaphore
Features Semaphore Monitor
Basic Integer variable Abstract data type
Access When a process uses shared resources, it
calls the wait() method on S, and when it
releases them, it uses the signal() method on
S.
When a process uses shared resources
in the monitor, it has to access them via
procedures.
Action The semaphore's value shows the number of
shared resources available in the system.
The Monitor type includes shared
variables as well as a set of procedures
that operate on them.
48. Pipes
� Pipe is a communication medium between two or more related or interrelated processes usually
between parent and child process.
� Communication is achieved by one process write into the pipe and other process reads from the
pipe.
� It performs one-way communication only means we can use a pipe such that one process write
into the pipe, and other process reads from the pipe.
� It opens a pipe, which is an area of main memory that is treated as a “virtual file”.
� It is bounded buffer means we can send only limited data through pipe.
49. Pipes
�Accessed by two associated file descriptors:
⮩ fd[0] for reading from pipe
⮩ fd[1] for writing into the pipe
�This system call would create a pipe for one-way
communication i.e., it creates two descriptors, first one is
connected to read from the pipe and other one is connected to
write into the pipe.
� This method will use two primitives
⮩ Send: It is used to send message.
▪ Send (destination, &message)
▪ In above syntax destination is the process to which sender want to send message
and message is what the sender wants to send.
⮩ Receive: It is used to receive message.
▪ Receive (source, &message)
▪ In above syntax source is the process that has send message and message is what
the sender has sent.
50. PARENTS AND CHILD SHARING A PIPE
for a pipe from parent to child ,the parent closes the
read end of the pipe (fd[0]) and child closes the write
end fd[1]
for a pipe from child to parent ,the parent closes the
write end of the pipe (fd[1]) and child closes the read
end fd[0]
two rules apply
If we read from a pipe whose write end has been closed
read returns 0 to indicate an end of file after all the data
has been read
If we write to a pipe whose read end has been closed
the SIGPIPE is generated .
fork
51. Pipes
LIMITATION OF PIPES
� reading data removes it from the pipe cannot be used to broadcast data to multiple receivers
� Data in a pipe is treated as a byte stream and no knowledge of message boundaries
� if there are multiple readers on a pipe a writer cannot direct data to a specific reader
52. MESSAGE PASSING
Allow multiple processes to read and write data to the message queue without being
connected to each other
it store in the queue until their recipient retrieves them .
useful for interprocess communication.
Message passing provides two operations which are as follows −
● Send message
● Receive message
If process P1 and P2 want to communicate they need to send a message to and receive a
message from each other that means here a communication link exists between them.
Step 5 − Methods for logically implementing a link and the send() and receive() operations.