SlideShare a Scribd company logo
1 of 89
Download to read offline
Operating Systems
Chapter 6: Process Synchronization
Dr. Ahmed Hagag
Scientific Computing Department,
Faculty of Computers and Artificial Intelligence
Benha University
2019
Ch. 6: Process Synchronization
ā€¢ Introduction.
ā€¢ The Critical Section Problem.
ā€¢ Solution to The Critical Section Problem.
āž¢ Software Solution.
āž¢ Hardware Solution.
2
Ā©Ahmed Hagag Operating Systems
ā€¢ The operating system supports multiple processes
running in parallel that are the results from the
following:
āž¢ Multiple applications: Multi-programming allows
processor time to be shared among a number of active
applications.
3
Ā©Ahmed Hagag Operating Systems
Introduction (1/10)
ā€¢ The operating system supports multiple processes
running in parallel that are the results from the
following:
āž¢ Structured application: some applications can be
implemented as a set of concurrent processes.
4
Ā©Ahmed Hagag Operating Systems
Introduction (2/10)
ā€¢ A cooperating process is one that can affect or be
affected by other processes executing in the system.
ā€¢ Cooperating processes can either directly share a logical
address space (that is, both code and data) or be allowed
to share data only through files or messages. The former
case is achieved through the use of threads, discussed in
Chapter 4.
5
Ā©Ahmed Hagag Operating Systems
Introduction (3/10)
ā€¢ Concurrent access to shared data may result in data
inconsistency, however. In this chapter, we discuss
various mechanisms to ensure the orderly execution of
cooperating processes that share a logical address space,
so that data consistency is maintained.
6
Ā©Ahmed Hagag Operating Systems
Introduction (4/10)
Types of Process Cooperation
ā€¢ Processes interact with each other can be:
7
Ā©Ahmed Hagag Operating Systems
Introduction (5/10)
Processes unaware of each other (competition) (1/3)
ā€¢ Those are independent processes that are not intended to
work together.
ā€¢ For example, in a single user environment, two
independent applications may both want to access the
same disk, file or printer.
8
Ā©Ahmed Hagag Operating Systems
Introduction (6/10)
Processes unaware of each other (competition) (2/3)
ā€¢ In a multi-user environment, users are concurrent, use
the same web server and need access to the same web
page. A person can handle several tasks at once (have
you every listened to music while doing other work and
heard the phone ring?).
9
Ā©Ahmed Hagag Operating Systems
Introduction (6/10)
Processes unaware of each other (competition) (3/3)
ā€¢ The OS must resolve the competition for resources.
āž¢ I/O, memory, printer, tape drive, etc.
ā€¢ Each process should leave the state of any resource that
it uses unaffected.
10
Ā©Ahmed Hagag Operating Systems
Introduction (6/10)
Processes indirectly aware of each other (cooperation by
sharing) (1/2)
ā€¢ These are processes that are not necessarily aware of
each other but they share the same resources. Like for
example, I/O buffers.
11
Ā©Ahmed Hagag Operating Systems
Introduction (7/10)
Processes indirectly aware of each other (cooperation by
sharing) (2/2)
ā€¢ Types of interactions could be in the form of shared
access to some object:
āž¢ shared variables, files, or databases
ā€¢ Processes may use and update the shared data without
reference to other process, but know that other processes
may have access to the same data.
12
Ā©Ahmed Hagag Operating Systems
Introduction (7/10)
Process directly aware of each other(cooperation by
communication) (1/2)
ā€¢ These are processes that can work jointly on the same
activity and can communicate with each other through
their process ID.
13
Ā©Ahmed Hagag Operating Systems
Introduction (8/10)
Process directly aware of each other(cooperation by
communication) (2/2)
ā€¢ For example, when we have a process that produces an
output that is considered an input for other processes,
Interpose communication exists.
ā€¢ This can be accomplished through sending and receiving
of messages: This can be viewed when a compiler
generates the intermediate code for the assemble to be
processed and transferred to machine language during
the compilation process.
14
Ā©Ahmed Hagag Operating Systems
Introduction (8/10)
Definition of concurrency problem (1/4)
ā€¢ Traditionally, the expression of a task in the form of multiple,
possibly interacting subtasks, that may potentially be
executed at the same time constitutes concurrency.
āž¢ Uniprocessor: interleaving.
āž¢ Multiprocessor: interleaving and overlapping.
15
Ā©Ahmed Hagag Operating Systems
Introduction (9/10)
Definition of concurrency problem (2/4)
16
Ā©Ahmed Hagag Operating Systems
Introduction (9/10)
Definition of concurrency problem (3/4)
ā€¢ For the uni-processor environments, concurrent access to
shared data appears when the task is split between three
processes each of them use the process in a series which may
result in data inconsistency.
17
Ā©Ahmed Hagag Operating Systems
Introduction (9/10)
Definition of concurrency problem (3/4)
ā€¢ For the uni-processor environments, concurrent access to
shared data appears when the task is split between three
processes each of them use the process in a series which may
result in data inconsistency.
18
Ā©Ahmed Hagag Operating Systems
Introduction (9/10)
Definition of concurrency problem (3/4)
ā€¢ For the uni-processor environments, concurrent access to
shared data appears when the task is split between three
processes each of them use the process in a series which may
result in data inconsistency.
19
Ā©Ahmed Hagag Operating Systems
Introduction (9/10)
Definition of concurrency problem (4/4)
ā€¢ Also in multi-processor environment, tasks are run on
different processors in parallel which may lead to
overlapping and data inconsistency.
20
Ā©Ahmed Hagag Operating Systems
Introduction (9/10)
Definition of concurrency problem (4/4)
ā€¢ Also in multi-processor environment, tasks are run on
different processors in parallel which may lead to
overlapping and data inconsistency.
21
Ā©Ahmed Hagag Operating Systems
Introduction (9/10)
The producer-consumer problem (1/10)
ā€¢ The producer-consumer problem illustrates the need for
synchronization in systems where many processes share a
resource. In this problem, two processes share a fixed-size
(bounded) buffer.
22
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
The producer-consumer problem (2/10)
ā€¢ One process produces information and puts it in the buffer,
while the other process consumes information from the
buffer. These processes do not take turns accessing the buffer,
they both work concurrently.
23
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
Producer Consumer
The producer-consumer problem (3/10)
ā€¢ Here in lays the problem:
āž¢ What happens if the producer tries to put an item into a
full buffer?
āž¢ What happens if the consumer tries to take an item from
an empty buffer?
24
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
The producer-consumer problem (4/10)
ā€¢ Suppose that we want to provide a solution to the consumer-
producer problem that fills all the cells of the buffer:
āž¢ We introduce an integer counter that keeps track of the
number of full buffers.
25
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
The producer-consumer problem (5/10)
ā€¢ Initially, count is set to 0. It is incremented by the producer
after it produces a new item and is decremented by the
consumer after it consumes an item.
26
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
The producer-consumer problem (6/10)
27
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
while (true) {
/* produce an item in next produced */
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Producer
The producer-consumer problem (7/10)
28
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
Consumer
The producer-consumer problem (8/10)
29
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
Consider this execution interleaving with ā€œcount = 5ā€ initially:
S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 ā€“ 1 {register2 = 4}
S4: producer execute counter = register1 {counter = 6}
S5: consumer execute counter = register2 {counter = 4}
The producer-consumer problem (9/10)
ā€¢ The value of count should be 5 ā€¦ but is 4 (after the
Consumer process updates count). It could also be 6, if the
execution order of the last two steps is reversed.
ā€¢ This will cause "problems" with the data, since the Producer
process thinks that there are more free cells than there really
are.
30
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
The producer-consumer problem (10/10)
ā€¢ We can identify two critical sections, one being count++ (in
the Producer) the other countā€“ ā€“ (in the Consumer). Both
must be "protected", in order to ensure correct execution.
31
Ā©Ahmed Hagag Operating Systems
Introduction (10/10)
ā€¢ The problem has a Spooler directory with slots; index
variable; two processes attempt concurrent access.
ā€¢ According to the figure, at a certain time, slot 0 to 3 are
empty and slot 4 to 7 are full. Process A and B
simultaneously want to queue a file for printing.
32
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (1/14)
ā€¢ Process A reads "in" with the value 7 and is switched to
Process B.
33
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (2/14)
ā€¢ Process B reads "in", update it to 8 puts its product in the 'in'
slot, increases its local in by one and sets in.
34
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (3/14)
ā€¢ Process A gets the control again. Enters its product to the 'in'
slot, increments "its in" and sets in.
ā€¢ Result: "in" has a correct value but the printing job of B is
lostā€¦.
ā€¢ In this situation, the value of the shared variable depends on
who runs first and update the value which is called: Race
condition.
35
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (4/14)
Description of Race Condition
36
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (5/14)
Description of Race Condition
37
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (5/14)
Race Condition
ā€¢ Race condition is: A situation in which multiple processes
read and write a shared data item and the final result depends
on the relative timing of their execution.
ā€¢ Generally, the race conditions may occur whenever resources
and/or data are shared (by processes unaware of each other or
processes indirectly aware of each other).
38
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (6/14)
39
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (7/14)
Consider system of n processes {p0 , p1 , ā€¦ pn-1}
Each process has critical section segment of code
Process may be changing common variables, updating
table, writing file, etc.
When one process in critical section, no other may be in
its critical section.
40
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (8/14)
41
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (8/14)
42
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (8/14)
43
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (8/14)
44
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (9/14)
45
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (9/14)
46
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (10/14)
Critical section problem is to design protocol to solve this
Each process must ask permission to enter critical section in entry
section, may follow critical section with exit section, then
remainder section.
Solutions to the critical section problem
ā€¢ A solution to the critical section problem must satisfy the
following requirements:
47
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (11/14)
Mutual Exclusion
ā€¢ Mutual Exclusion: If process Pi is executing in its
critical section, then no other processes can be executing
in their critical sections.
48
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (12/14)
Progress
ā€¢ Progress: If no process is executing in its critical section
and there exist some processes that wish to enter their
critical section, then the selection of the processes that
will enter the critical section next cannot be postponed
indefinitely.
49
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (13/14)
Bonded Waiting (1/3)
ā€¢ Bonded Waiting: There exists a bound, or limit, 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. (Starvation).
āž¢ Assume that each process executes at a nonzero speed.
āž¢ No assumption concerning relative speed of the n
processes.
50
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (14/14)
Bonded Waiting (2/3)
51
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (14/14)
Bonded Waiting (3/3)
52
Ā©Ahmed Hagag Operating Systems
Critical Section Problem (14/14)
Types of Solutions
ā€¢ A software solution is a code that should be run before
entering the critical section to avoid race condition.
ā€¢ A hardware solution is accomplished by utilizing one
of the system components.
53
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (1/9)
ā€¢ In order to solve the critical region problem, a use of
external software and/or hardware could be used as
shown in the figure:
54
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (2/9)
ā€¢ In order to solve the critical region problem, a use of
external software and/or hardware could be used as
shown in the figure:
ā€¢ When process A enters critical region, a flag is
highlighted to indicate that no other process is allowed to
enter this critical region.
55
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (3/9)
ā€¢ In order to solve the critical region problem, a use of
external software and/or hardware could be used as
shown in the figure:
ā€¢ Then after process A is finished its task, the flag is off
allowing process B to continue in the critical region.
56
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (4/9)
Types of Hardware solutions
ā€¢ Many systems provide hardware support for critical
section code.
ā€¢ The same concept is used to solve the critical section
problem but with the help of hardware.
57
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (5/9)
Disable interrupt in uni-processor (1/7)
58
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Disable interrupt in uni-processor (2/7)
59
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Disable interrupt in uni-processor (3/7)
60
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Disable interrupt in uni-processor (4/7)
61
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Disable interrupt in uni-processor (5/7)
62
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Disable interrupt in uni-processor (6/7)
63
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Disable interrupt in uni-processor (6/7)
64
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Disable interrupt in uni-processor (7/7)
65
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (6/9)
Software Solutions (1/7)
ā€¢ One of the simplest software solutions is based on
having a simple common, shared (lock) variable.
66
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (7/9)
Software Solutions (2/7)
ā€¢ One of the simplest software solutions is based on
having a simple common, shared (lock) variable.
67
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (7/9)
Software Solutions (3/7)
ā€¢ One of the simplest software solutions is based on
having a simple common, shared (lock) variable.
68
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (7/9)
Software Solutions (4/7)
ā€¢ One of the simplest software solutions is based on
having a simple common, shared (lock) variable.
69
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (7/9)
Software Solutions (5/7)
ā€¢ One of the simplest software solutions is based on
having a simple common, shared (lock) variable.
70
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (7/9)
Software Solutions (6/7)
71
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (7/9)
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
Software Solutions (7/7)
Disadvantage of Software Solution
72
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (7/9)
Petersonā€™s Solution (software solutions) (1/6)
ā€¢ The two processes share two variables:
int turn;
Boolean flag[2]
ā€¢ The variable turn indicates whose turn it is to enter the
critical section.
ā€¢ The variable turn indicates whose turn it is to enter the
critical section
ā€¢ The flag array is used to indicate if a process is ready to
enter the critical section. flag[i] = true implies that
process Pi is ready!
73
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (8/9)
Petersonā€™s Solution (software solutions) (2/6)
74
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (8/9)
ā€¢ Initially, both processes are not in their critical region, and the
array flag has all its elements set to false.
Petersonā€™s Solution (software solutions) (3/6)
75
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (8/9)
1. Pi arrives first, sets its flag, pushes the turn to the other flag and
may enter.
Petersonā€™s Solution (software solutions) (4/6)
76
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (8/9)
2. Then, Pj also sets its flag & pushes the turn, but must wait until
Piā€™s flag is reset Process Pj will only be allowed to continue
when flag[0] is set to false which can only come about from
process Pi.
Petersonā€™s Solution (software solutions) (5/6)
77
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (8/9)
3. Pi exits the CR and resets its flag [i]; Pj may now enter.
Petersonā€™s Solution (software solutions) (6/6)
78
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (8/9)
3. Pi exits the CR and resets its flag [i]; Pj may now enter.
Semaphore (1/10)
79
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
ā€¢ Semaphore S ā€“ integer variable
ā€¢ Can only be accessed via two indivisible (atomic) operations
ā€“ wait() and signal()
ā€¢ Originally called P() and V()
ā€¢ Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
ā€¢ Definition of the signal() operation
signal(S) {
S++;
}
Semaphore (2/10)
80
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
ā€¢ To overcome the problem of busy waiting in semaphore,
we can modify the defining of the wait and signal operation.
ā€¢ When a process executes the wait operation and find the
value of semaphore is not positive, it must wait. However
rather than engaging in a busy waiting, the process can block
itself in waiting queue associated with each semaphore.
ā€¢ In this case the state of the process is waiting in the
semaphore queue.
Semaphore (3/10)
81
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
Semaphore (4/10)
82
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
Semaphore (5/10)
83
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
Semaphore (6/10)
84
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
Semaphore (7/10)
85
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
Semaphore (8/10)
86
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
Semaphore (9/10)
87
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
Semaphore (10/10)
88
Ā©Ahmed Hagag Operating Systems
Solution to The CS Problem (9/9)
ā€¢ Deadlock ā€“ two or more processes are waiting indefinitely for an
event that can be caused by only one of the waiting processes.
ā€¢ Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
Dr. Ahmed Hagag
ahagag@fci.bu.edu.eg
https://www.youtube.com/channel/UCzYgAyyZTLfnLFjQexOKxbQ
https://www.facebook.com/ahmed.hagag.71/

More Related Content

Similar to OS_Ch06.pdf

slide 4 systemsch1-great slide 4 systems.pptx
slide 4 systemsch1-great slide 4 systems.pptxslide 4 systemsch1-great slide 4 systems.pptx
slide 4 systemsch1-great slide 4 systems.pptxthanhan858423
Ā 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfAmanuelmergia
Ā 
ch3_smu.ppt
ch3_smu.pptch3_smu.ppt
ch3_smu.pptJubayerPial
Ā 
Introduction_to_OperatingSystems_ch1.ppt
Introduction_to_OperatingSystems_ch1.pptIntroduction_to_OperatingSystems_ch1.ppt
Introduction_to_OperatingSystems_ch1.pptpaautomation11
Ā 
Operating System - Unit I - Introduction
Operating System - Unit I - IntroductionOperating System - Unit I - Introduction
Operating System - Unit I - Introductioncscarcas
Ā 
ch01.ppt
ch01.pptch01.ppt
ch01.pptemomani1
Ā 
ch1.pdfsystem programiming for engineeering in gct coimbator4e
ch1.pdfsystem programiming for engineeering in gct  coimbator4ech1.pdfsystem programiming for engineeering in gct  coimbator4e
ch1.pdfsystem programiming for engineeering in gct coimbator4eEsakyS
Ā 
Performance tuning Grails applications SpringOne 2GX 2014
Performance tuning Grails applications SpringOne 2GX 2014Performance tuning Grails applications SpringOne 2GX 2014
Performance tuning Grails applications SpringOne 2GX 2014Lari Hotari
Ā 
3 processes
3 processes3 processes
3 processesBaliThorat1
Ā 
PROCESS Management presentation processins.pdf
PROCESS Management presentation processins.pdfPROCESS Management presentation processins.pdf
PROCESS Management presentation processins.pdfPDhivyabharathi2
Ā 
chapter 1 of Operating system by gagne.ppt
chapter 1 of Operating system by gagne.pptchapter 1 of Operating system by gagne.ppt
chapter 1 of Operating system by gagne.ppt51b48265a74f87
Ā 

Similar to OS_Ch06.pdf (20)

slide 4 systemsch1-great slide 4 systems.pptx
slide 4 systemsch1-great slide 4 systems.pptxslide 4 systemsch1-great slide 4 systems.pptx
slide 4 systemsch1-great slide 4 systems.pptx
Ā 
ch1.pptx
ch1.pptxch1.pptx
ch1.pptx
Ā 
OS UNIT2.ppt
OS UNIT2.pptOS UNIT2.ppt
OS UNIT2.ppt
Ā 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
Ā 
ch3_smu.ppt
ch3_smu.pptch3_smu.ppt
ch3_smu.ppt
Ā 
ch1.pptx
ch1.pptxch1.pptx
ch1.pptx
Ā 
Introduction_to_OperatingSystems_ch1.ppt
Introduction_to_OperatingSystems_ch1.pptIntroduction_to_OperatingSystems_ch1.ppt
Introduction_to_OperatingSystems_ch1.ppt
Ā 
Operating System - Unit I - Introduction
Operating System - Unit I - IntroductionOperating System - Unit I - Introduction
Operating System - Unit I - Introduction
Ā 
Operating System
Operating SystemOperating System
Operating System
Ā 
ch01.ppt
ch01.pptch01.ppt
ch01.ppt
Ā 
ch1.pdfsystem programiming for engineeering in gct coimbator4e
ch1.pdfsystem programiming for engineeering in gct  coimbator4ech1.pdfsystem programiming for engineeering in gct  coimbator4e
ch1.pdfsystem programiming for engineeering in gct coimbator4e
Ā 
Performance tuning Grails applications SpringOne 2GX 2014
Performance tuning Grails applications SpringOne 2GX 2014Performance tuning Grails applications SpringOne 2GX 2014
Performance tuning Grails applications SpringOne 2GX 2014
Ā 
3 processes
3 processes3 processes
3 processes
Ā 
PROCESS Management presentation processins.pdf
PROCESS Management presentation processins.pdfPROCESS Management presentation processins.pdf
PROCESS Management presentation processins.pdf
Ā 
ch1.ppt
ch1.pptch1.ppt
ch1.ppt
Ā 
chapter 1 of Operating system by gagne.ppt
chapter 1 of Operating system by gagne.pptchapter 1 of Operating system by gagne.ppt
chapter 1 of Operating system by gagne.ppt
Ā 
ch1.ppt
ch1.pptch1.ppt
ch1.ppt
Ā 
ch1.ppt
ch1.pptch1.ppt
ch1.ppt
Ā 
ch1.ppt
ch1.pptch1.ppt
ch1.ppt
Ā 
ch1.ppt
ch1.pptch1.ppt
ch1.ppt
Ā 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
Ā 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜RTylerCroy
Ā 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
Ā 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
Ā 
Scaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationScaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationRadu Cotescu
Ā 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
Ā 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
Ā 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
Ā 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
Ā 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
Ā 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
Ā 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
Ā 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
Ā 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
Ā 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
Ā 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
Ā 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
Ā 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
Ā 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Ā 
Scaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationScaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organization
Ā 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Ā 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Ā 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Ā 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Ā 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
Ā 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Ā 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
Ā 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
Ā 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Ā 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Ā 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Ā 

OS_Ch06.pdf

  • 1. Operating Systems Chapter 6: Process Synchronization Dr. Ahmed Hagag Scientific Computing Department, Faculty of Computers and Artificial Intelligence Benha University 2019
  • 2. Ch. 6: Process Synchronization ā€¢ Introduction. ā€¢ The Critical Section Problem. ā€¢ Solution to The Critical Section Problem. āž¢ Software Solution. āž¢ Hardware Solution. 2 Ā©Ahmed Hagag Operating Systems
  • 3. ā€¢ The operating system supports multiple processes running in parallel that are the results from the following: āž¢ Multiple applications: Multi-programming allows processor time to be shared among a number of active applications. 3 Ā©Ahmed Hagag Operating Systems Introduction (1/10)
  • 4. ā€¢ The operating system supports multiple processes running in parallel that are the results from the following: āž¢ Structured application: some applications can be implemented as a set of concurrent processes. 4 Ā©Ahmed Hagag Operating Systems Introduction (2/10)
  • 5. ā€¢ A cooperating process is one that can affect or be affected by other processes executing in the system. ā€¢ Cooperating processes can either directly share a logical address space (that is, both code and data) or be allowed to share data only through files or messages. The former case is achieved through the use of threads, discussed in Chapter 4. 5 Ā©Ahmed Hagag Operating Systems Introduction (3/10)
  • 6. ā€¢ Concurrent access to shared data may result in data inconsistency, however. In this chapter, we discuss various mechanisms to ensure the orderly execution of cooperating processes that share a logical address space, so that data consistency is maintained. 6 Ā©Ahmed Hagag Operating Systems Introduction (4/10)
  • 7. Types of Process Cooperation ā€¢ Processes interact with each other can be: 7 Ā©Ahmed Hagag Operating Systems Introduction (5/10)
  • 8. Processes unaware of each other (competition) (1/3) ā€¢ Those are independent processes that are not intended to work together. ā€¢ For example, in a single user environment, two independent applications may both want to access the same disk, file or printer. 8 Ā©Ahmed Hagag Operating Systems Introduction (6/10)
  • 9. Processes unaware of each other (competition) (2/3) ā€¢ In a multi-user environment, users are concurrent, use the same web server and need access to the same web page. A person can handle several tasks at once (have you every listened to music while doing other work and heard the phone ring?). 9 Ā©Ahmed Hagag Operating Systems Introduction (6/10)
  • 10. Processes unaware of each other (competition) (3/3) ā€¢ The OS must resolve the competition for resources. āž¢ I/O, memory, printer, tape drive, etc. ā€¢ Each process should leave the state of any resource that it uses unaffected. 10 Ā©Ahmed Hagag Operating Systems Introduction (6/10)
  • 11. Processes indirectly aware of each other (cooperation by sharing) (1/2) ā€¢ These are processes that are not necessarily aware of each other but they share the same resources. Like for example, I/O buffers. 11 Ā©Ahmed Hagag Operating Systems Introduction (7/10)
  • 12. Processes indirectly aware of each other (cooperation by sharing) (2/2) ā€¢ Types of interactions could be in the form of shared access to some object: āž¢ shared variables, files, or databases ā€¢ Processes may use and update the shared data without reference to other process, but know that other processes may have access to the same data. 12 Ā©Ahmed Hagag Operating Systems Introduction (7/10)
  • 13. Process directly aware of each other(cooperation by communication) (1/2) ā€¢ These are processes that can work jointly on the same activity and can communicate with each other through their process ID. 13 Ā©Ahmed Hagag Operating Systems Introduction (8/10)
  • 14. Process directly aware of each other(cooperation by communication) (2/2) ā€¢ For example, when we have a process that produces an output that is considered an input for other processes, Interpose communication exists. ā€¢ This can be accomplished through sending and receiving of messages: This can be viewed when a compiler generates the intermediate code for the assemble to be processed and transferred to machine language during the compilation process. 14 Ā©Ahmed Hagag Operating Systems Introduction (8/10)
  • 15. Definition of concurrency problem (1/4) ā€¢ Traditionally, the expression of a task in the form of multiple, possibly interacting subtasks, that may potentially be executed at the same time constitutes concurrency. āž¢ Uniprocessor: interleaving. āž¢ Multiprocessor: interleaving and overlapping. 15 Ā©Ahmed Hagag Operating Systems Introduction (9/10)
  • 16. Definition of concurrency problem (2/4) 16 Ā©Ahmed Hagag Operating Systems Introduction (9/10)
  • 17. Definition of concurrency problem (3/4) ā€¢ For the uni-processor environments, concurrent access to shared data appears when the task is split between three processes each of them use the process in a series which may result in data inconsistency. 17 Ā©Ahmed Hagag Operating Systems Introduction (9/10)
  • 18. Definition of concurrency problem (3/4) ā€¢ For the uni-processor environments, concurrent access to shared data appears when the task is split between three processes each of them use the process in a series which may result in data inconsistency. 18 Ā©Ahmed Hagag Operating Systems Introduction (9/10)
  • 19. Definition of concurrency problem (3/4) ā€¢ For the uni-processor environments, concurrent access to shared data appears when the task is split between three processes each of them use the process in a series which may result in data inconsistency. 19 Ā©Ahmed Hagag Operating Systems Introduction (9/10)
  • 20. Definition of concurrency problem (4/4) ā€¢ Also in multi-processor environment, tasks are run on different processors in parallel which may lead to overlapping and data inconsistency. 20 Ā©Ahmed Hagag Operating Systems Introduction (9/10)
  • 21. Definition of concurrency problem (4/4) ā€¢ Also in multi-processor environment, tasks are run on different processors in parallel which may lead to overlapping and data inconsistency. 21 Ā©Ahmed Hagag Operating Systems Introduction (9/10)
  • 22. The producer-consumer problem (1/10) ā€¢ The producer-consumer problem illustrates the need for synchronization in systems where many processes share a resource. In this problem, two processes share a fixed-size (bounded) buffer. 22 Ā©Ahmed Hagag Operating Systems Introduction (10/10)
  • 23. The producer-consumer problem (2/10) ā€¢ One process produces information and puts it in the buffer, while the other process consumes information from the buffer. These processes do not take turns accessing the buffer, they both work concurrently. 23 Ā©Ahmed Hagag Operating Systems Introduction (10/10) Producer Consumer
  • 24. The producer-consumer problem (3/10) ā€¢ Here in lays the problem: āž¢ What happens if the producer tries to put an item into a full buffer? āž¢ What happens if the consumer tries to take an item from an empty buffer? 24 Ā©Ahmed Hagag Operating Systems Introduction (10/10)
  • 25. The producer-consumer problem (4/10) ā€¢ Suppose that we want to provide a solution to the consumer- producer problem that fills all the cells of the buffer: āž¢ We introduce an integer counter that keeps track of the number of full buffers. 25 Ā©Ahmed Hagag Operating Systems Introduction (10/10)
  • 26. The producer-consumer problem (5/10) ā€¢ Initially, count is set to 0. It is incremented by the producer after it produces a new item and is decremented by the consumer after it consumes an item. 26 Ā©Ahmed Hagag Operating Systems Introduction (10/10)
  • 27. The producer-consumer problem (6/10) 27 Ā©Ahmed Hagag Operating Systems Introduction (10/10) while (true) { /* produce an item in next produced */ while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; counter++; } Producer
  • 28. The producer-consumer problem (7/10) 28 Ā©Ahmed Hagag Operating Systems Introduction (10/10) while (true) { while (counter == 0) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /* consume the item in next consumed */ } Consumer
  • 29. The producer-consumer problem (8/10) 29 Ā©Ahmed Hagag Operating Systems Introduction (10/10) counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 counter-- could be implemented as register2 = counter register2 = register2 - 1 counter = register2 Consider this execution interleaving with ā€œcount = 5ā€ initially: S0: producer execute register1 = counter {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = counter {register2 = 5} S3: consumer execute register2 = register2 ā€“ 1 {register2 = 4} S4: producer execute counter = register1 {counter = 6} S5: consumer execute counter = register2 {counter = 4}
  • 30. The producer-consumer problem (9/10) ā€¢ The value of count should be 5 ā€¦ but is 4 (after the Consumer process updates count). It could also be 6, if the execution order of the last two steps is reversed. ā€¢ This will cause "problems" with the data, since the Producer process thinks that there are more free cells than there really are. 30 Ā©Ahmed Hagag Operating Systems Introduction (10/10)
  • 31. The producer-consumer problem (10/10) ā€¢ We can identify two critical sections, one being count++ (in the Producer) the other countā€“ ā€“ (in the Consumer). Both must be "protected", in order to ensure correct execution. 31 Ā©Ahmed Hagag Operating Systems Introduction (10/10)
  • 32. ā€¢ The problem has a Spooler directory with slots; index variable; two processes attempt concurrent access. ā€¢ According to the figure, at a certain time, slot 0 to 3 are empty and slot 4 to 7 are full. Process A and B simultaneously want to queue a file for printing. 32 Ā©Ahmed Hagag Operating Systems Critical Section Problem (1/14)
  • 33. ā€¢ Process A reads "in" with the value 7 and is switched to Process B. 33 Ā©Ahmed Hagag Operating Systems Critical Section Problem (2/14)
  • 34. ā€¢ Process B reads "in", update it to 8 puts its product in the 'in' slot, increases its local in by one and sets in. 34 Ā©Ahmed Hagag Operating Systems Critical Section Problem (3/14)
  • 35. ā€¢ Process A gets the control again. Enters its product to the 'in' slot, increments "its in" and sets in. ā€¢ Result: "in" has a correct value but the printing job of B is lostā€¦. ā€¢ In this situation, the value of the shared variable depends on who runs first and update the value which is called: Race condition. 35 Ā©Ahmed Hagag Operating Systems Critical Section Problem (4/14)
  • 36. Description of Race Condition 36 Ā©Ahmed Hagag Operating Systems Critical Section Problem (5/14)
  • 37. Description of Race Condition 37 Ā©Ahmed Hagag Operating Systems Critical Section Problem (5/14)
  • 38. Race Condition ā€¢ Race condition is: A situation in which multiple processes read and write a shared data item and the final result depends on the relative timing of their execution. ā€¢ Generally, the race conditions may occur whenever resources and/or data are shared (by processes unaware of each other or processes indirectly aware of each other). 38 Ā©Ahmed Hagag Operating Systems Critical Section Problem (6/14)
  • 39. 39 Ā©Ahmed Hagag Operating Systems Critical Section Problem (7/14) Consider system of n processes {p0 , p1 , ā€¦ pn-1} Each process has critical section segment of code Process may be changing common variables, updating table, writing file, etc. When one process in critical section, no other may be in its critical section.
  • 40. 40 Ā©Ahmed Hagag Operating Systems Critical Section Problem (8/14)
  • 41. 41 Ā©Ahmed Hagag Operating Systems Critical Section Problem (8/14)
  • 42. 42 Ā©Ahmed Hagag Operating Systems Critical Section Problem (8/14)
  • 43. 43 Ā©Ahmed Hagag Operating Systems Critical Section Problem (8/14)
  • 44. 44 Ā©Ahmed Hagag Operating Systems Critical Section Problem (9/14)
  • 45. 45 Ā©Ahmed Hagag Operating Systems Critical Section Problem (9/14)
  • 46. 46 Ā©Ahmed Hagag Operating Systems Critical Section Problem (10/14) Critical section problem is to design protocol to solve this Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section.
  • 47. Solutions to the critical section problem ā€¢ A solution to the critical section problem must satisfy the following requirements: 47 Ā©Ahmed Hagag Operating Systems Critical Section Problem (11/14)
  • 48. Mutual Exclusion ā€¢ Mutual Exclusion: If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. 48 Ā©Ahmed Hagag Operating Systems Critical Section Problem (12/14)
  • 49. Progress ā€¢ Progress: If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. 49 Ā©Ahmed Hagag Operating Systems Critical Section Problem (13/14)
  • 50. Bonded Waiting (1/3) ā€¢ Bonded Waiting: There exists a bound, or limit, 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. (Starvation). āž¢ Assume that each process executes at a nonzero speed. āž¢ No assumption concerning relative speed of the n processes. 50 Ā©Ahmed Hagag Operating Systems Critical Section Problem (14/14)
  • 51. Bonded Waiting (2/3) 51 Ā©Ahmed Hagag Operating Systems Critical Section Problem (14/14)
  • 52. Bonded Waiting (3/3) 52 Ā©Ahmed Hagag Operating Systems Critical Section Problem (14/14)
  • 53. Types of Solutions ā€¢ A software solution is a code that should be run before entering the critical section to avoid race condition. ā€¢ A hardware solution is accomplished by utilizing one of the system components. 53 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (1/9)
  • 54. ā€¢ In order to solve the critical region problem, a use of external software and/or hardware could be used as shown in the figure: 54 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (2/9)
  • 55. ā€¢ In order to solve the critical region problem, a use of external software and/or hardware could be used as shown in the figure: ā€¢ When process A enters critical region, a flag is highlighted to indicate that no other process is allowed to enter this critical region. 55 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (3/9)
  • 56. ā€¢ In order to solve the critical region problem, a use of external software and/or hardware could be used as shown in the figure: ā€¢ Then after process A is finished its task, the flag is off allowing process B to continue in the critical region. 56 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (4/9)
  • 57. Types of Hardware solutions ā€¢ Many systems provide hardware support for critical section code. ā€¢ The same concept is used to solve the critical section problem but with the help of hardware. 57 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (5/9)
  • 58. Disable interrupt in uni-processor (1/7) 58 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 59. Disable interrupt in uni-processor (2/7) 59 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 60. Disable interrupt in uni-processor (3/7) 60 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 61. Disable interrupt in uni-processor (4/7) 61 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 62. Disable interrupt in uni-processor (5/7) 62 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 63. Disable interrupt in uni-processor (6/7) 63 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 64. Disable interrupt in uni-processor (6/7) 64 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 65. Disable interrupt in uni-processor (7/7) 65 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (6/9)
  • 66. Software Solutions (1/7) ā€¢ One of the simplest software solutions is based on having a simple common, shared (lock) variable. 66 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (7/9)
  • 67. Software Solutions (2/7) ā€¢ One of the simplest software solutions is based on having a simple common, shared (lock) variable. 67 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (7/9)
  • 68. Software Solutions (3/7) ā€¢ One of the simplest software solutions is based on having a simple common, shared (lock) variable. 68 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (7/9)
  • 69. Software Solutions (4/7) ā€¢ One of the simplest software solutions is based on having a simple common, shared (lock) variable. 69 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (7/9)
  • 70. Software Solutions (5/7) ā€¢ One of the simplest software solutions is based on having a simple common, shared (lock) variable. 70 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (7/9)
  • 71. Software Solutions (6/7) 71 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (7/9) do { acquire lock critical section release lock remainder section } while (TRUE);
  • 72. Software Solutions (7/7) Disadvantage of Software Solution 72 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (7/9)
  • 73. Petersonā€™s Solution (software solutions) (1/6) ā€¢ The two processes share two variables: int turn; Boolean flag[2] ā€¢ The variable turn indicates whose turn it is to enter the critical section. ā€¢ The variable turn indicates whose turn it is to enter the critical section ā€¢ The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready! 73 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (8/9)
  • 74. Petersonā€™s Solution (software solutions) (2/6) 74 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (8/9) ā€¢ Initially, both processes are not in their critical region, and the array flag has all its elements set to false.
  • 75. Petersonā€™s Solution (software solutions) (3/6) 75 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (8/9) 1. Pi arrives first, sets its flag, pushes the turn to the other flag and may enter.
  • 76. Petersonā€™s Solution (software solutions) (4/6) 76 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (8/9) 2. Then, Pj also sets its flag & pushes the turn, but must wait until Piā€™s flag is reset Process Pj will only be allowed to continue when flag[0] is set to false which can only come about from process Pi.
  • 77. Petersonā€™s Solution (software solutions) (5/6) 77 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (8/9) 3. Pi exits the CR and resets its flag [i]; Pj may now enter.
  • 78. Petersonā€™s Solution (software solutions) (6/6) 78 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (8/9) 3. Pi exits the CR and resets its flag [i]; Pj may now enter.
  • 79. Semaphore (1/10) 79 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9) ā€¢ Semaphore S ā€“ integer variable ā€¢ Can only be accessed via two indivisible (atomic) operations ā€“ wait() and signal() ā€¢ Originally called P() and V() ā€¢ Definition of the wait() operation wait(S) { while (S <= 0) ; // busy wait S--; } ā€¢ Definition of the signal() operation signal(S) { S++; }
  • 80. Semaphore (2/10) 80 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9) ā€¢ To overcome the problem of busy waiting in semaphore, we can modify the defining of the wait and signal operation. ā€¢ When a process executes the wait operation and find the value of semaphore is not positive, it must wait. However rather than engaging in a busy waiting, the process can block itself in waiting queue associated with each semaphore. ā€¢ In this case the state of the process is waiting in the semaphore queue.
  • 81. Semaphore (3/10) 81 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9)
  • 82. Semaphore (4/10) 82 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9)
  • 83. Semaphore (5/10) 83 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9)
  • 84. Semaphore (6/10) 84 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9)
  • 85. Semaphore (7/10) 85 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9)
  • 86. Semaphore (8/10) 86 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9)
  • 87. Semaphore (9/10) 87 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9)
  • 88. Semaphore (10/10) 88 Ā©Ahmed Hagag Operating Systems Solution to The CS Problem (9/9) ā€¢ Deadlock ā€“ two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. ā€¢ Let S and Q be two semaphores initialized to 1 P0 P1 wait(S); wait(Q); wait(Q); wait(S); ... ... signal(S); signal(Q); signal(Q); signal(S);