s_ouatik@yahoo.com
ouatikelalaoui.said@uit.ac.ma
2019-2020 Process management 1
Translated from French to English - www.onlinedoctranslator.com
2019-2020 Process management 2
Part II
Communication and synchronization of
process
-How does a process pass information to another
process?
-How to avoid conflicts between competing
processes?
-How to synchronize dependent processes?
2019-2020 Process management 3
Part II
Communication and process synchronization
-Plan:
-The conditions of competition
-The criticism and mutual exclusion section
-Mutual exclusion with active expectation
-Disabling interrupts
-Lock variable
-Strict alternation
-Peterson's solution
-Semaphores
-The instructors
2019-2020 Process management 4
Conditions of competition
-Example: print spooler
-Principle
- A process that wants to print a file, enters its name in a special
directory: spooler directory
- The print daemon (spooler) periodically inspects its directory, if
there is a file it prints it and removes it from the directory
- The spooler directory has a large number of numbered entries: 0,
1, 2 … each of which can accommodate a file name.
- The spooler uses two shared variables: out and in
- Out: points to the next file to be printed (the head) In:
points to the next free input (the tail)
out and in variables can be stored in a file
-
-
2019-2020 Process management 5
Conditions of competition
Issue:Two processes A and B are trying to print at the same time
out
4
in
7
-Process A reads the value of the variable in
(7) and stores it in a local variable 4
5
6
7
Linux.pdf
-Before inserting his filename in entry 7,
his quantum was exhausted
Process A Prog.c
Script.sh
-the CPU switches to another process
-Process B which in turn gets the CPU reads
the value of in (always 7) and stores it in
its local variable. Then it inserts its file
name into input 7 and updates the
variable in(8)
B-process
2019-2020 Process management 6
Conditions of competition
Issue:Two processes A and B are trying to print at the same time
-When the CPU returns to the process
A, it resumes its execution at the
point where the CPU was removed
from it (ie for it the value of in is 7).
Then it inserts its file name into
input 7 and updates the variable
in(8)
out
4
in
7
4
5
6
7
Linux.pdf
Process A Prog.c
Script.sh
B-process
-For the spooler everything is coherent
-The file from process B will not be
printed, it has been overwritten by
the file from process A
2019-2020 Process management 7
critical section
-also calledcritical region, it is the set of instructions of a process
likely to cause the conflict with other processes.
-For example, access to memory, shared data, disk….
-Need: When a process is executing its critical section, no other process
should be allowed to execute its own.
-Situations where two (or more) processes access shared data are
called:the conditions of competition.
-To avoidthe conditions of competition,it must be avoided that the
processes concerned are at the same time in the critical section
--Ensure themutual exclusion
2019-2020 Process management 8
critical section
Example :Mutual exclusion
A enters his
critical section
Has left its critical
section
Process A
B tries to enter
in his section
critical
B enters his
critical section
B leaves his
critical section
B-process
B is blocked
t1 t2 t3 t4
time
2019-2020 Process management 9
critical section
-To avoid race conditions while ensuring cooperation and
proper use of shared resources,
Four conditions are necessary:
1.Two processes must not be simultaneously in their critical
sections(mutual exclusion)
2.No guesswork on speed or number of processors
3.No process running outside the critical section should block
other processes
4.No process should wait indefinitely to be able to enter its
critical section
2019-2020 Process management 10
mutual exclusion
-Several solutions to ensure mutual exclusion
-Mutual exclusion with active expectation
-
-
-
-
Disable Interrupts Lock
Variable
Strict alternation
Peterson's solution
-Semaphores
-The instructors
2019-2020 Process management 11
mutual exclusion with active wait
-The principle is that when a process is in its critical section,
other processes cannot enter it,
-But remainassetsand wait for the current process to
complete the critical region
-Several techniques are used:
-Disabling interrupts
-Lock variable
-Strict alternation
-Peterson's solution
2019-2020 Process management 12
Mutual exclusion with active expectation
Disabling interrupts:
-The process that enters its critical section, disables
interrupts
--the clock also could not send the interrupts, which implies
that the processor could not switch to another process
-The current process is safe now that no concurrent (or other)
process will be able to enter the critical section
-Other processes don't even get a chance to have the CPU
while interrupts are disabled
-The running process must re-enable interrupts at the end of
the critical section
2019-2020 Process management 13
Mutual exclusion with active expectation
Disabling interrupts:
-Issue!!
- Giving user processes the ability to block interrupts is not a good idea
-A process that completes its critical section and forgets to re-enable
interrupts
-A process that voluntarily does not re-enable interrupts to take over
the CPU exclusively
-Disabling interrupts can be interesting for kernel mode processes
-
-
In general, kernel-mode processes are very short
Some kernel processes (like the scheduler) should not be
preempted
2019-2020 Process management 14
Mutual exclusion with active expectation
-Lock variables:
-The process that wants to execute its critical section, tests a shared lock
variable (initialized to 0).
-
-
If this variable is at 0, then it resets it to 1 and enters the critical section
Otherwise, which means another process is in the critical section
- the process must wait for the lock to drop to 0
-The process resets the lock to 0 when it completes the critical section
Algorithm :
If lock=0 then /* critical section is free */
set lock to 1
critical section
reset lock to 0
Else wait for lock=0 /* critical section is busy */
2019-2020 Process management 15
Mutual exclusion with active expectation
Issue:
- The lock variable is shared - to access it, race conditions may
occur
- A process that has tested the lock and finds it at 0, loses the processor before
returning the lock to 1.
- A second process also tests the lock (still at 0) and resets it to 1 then
it enters its critical section
- The first process, which takes over the processor before the second
completes its critical section, resets the lock to 1 (it is already at 1) and
begins its critical section
- Result: both processes are in the critical section at the same time!!
- NB. A process can chain two (or more) executions of the critical
section, while the other process has not executed one.
2019-2020 Process management 16
Mutual exclusion with active expectation
Strict alternation
-Two processes strictly alternate their entry into the critical section
- Between two executions of the critical section by one of the processes, it is
imperative that the other process also executes the critical section
- Handy for representing the basic producer/consumer model
-Principle:
-
-
Both processes inspect a "turn" variable
Process 1 enters its critical sectionif turn is 0. Otherwise, it enters an
active waiting phase
- Unlike process 1, process 2 enters its critical sectionif turn is 1.
Otherwise it enters a waiting phase
2019-2020 Process management 17
Mutual exclusion with active expectation
-Strict alternation
-Each of the two processes alternates the turn variable at the end
of its critical section
-Process 1:
-Process 2:
-Entry code in the critical section of both processes
0-1
1-0
Process 1 Process 2
2019-2020 Process management 18
Mutual exclusion with active expectation
-Strict alternation
-Issue: A slow process outside its critical section would block the second
process
-Explanation: Suppose process P1 quickly executes its critical section and
sets turn to 1 and goes to its non_critical section.
- Process P2 is still in the non_critical section (and has been there for a
while).
- If the process P1 wants to enter its critical section it cannot because
turn =1.
2019-2020 Process management 19
Mutual exclusion with active
expectation
-Peterson's solution
-Combines strict alternation and lock variable.
-A process executes an "enter_region" function before
to enter the critical section
-And executes the "leave_region" function after its output
critical section.
2019-2020 Process management 20
Mutual exclusion with active expectation
-Peterson's solution code
# define n 2
int turn;
int interested[n];/*default initialized to false */
void enter_region(intprocess) /* Process number */
{
int other;/*number of other process */
other = 1 – process;
interested[process] = TRUE;/*manifests the need to be in critical section */
turn = process;
while(turn == process && interested[other] == TRUE);/*to wait for */ }
void leave_region(int process) {
inerested[process] = FALSE;/*notify the waiting process of its exit from the
critical section */
}
Mutual exclusion with active
expectation
-Restrictions:
-All previous solutions are based onactive waiting.
-Indeed, the process waiting to enter its critical section is obliged
to remain active.
-i.e. a process that cannot enter a critical section would use the
processor unnecessarily
-The process consumes, unnecessarily, its share of the processor
--This approach is very CPU intensive.
2019-2020 Process management 22
Semaphores
-A semaphore is a special integer variable on which two operations
can be performed:downAndup
-The operationdowntests if the semaphore is greater than 0. In this
case, it decrements it and the process can enter the critical
section. If the semaphore is null then the process goes to sleep
-The operationupincrements the semaphore.
- If a process is sleeping on this semaphore, then it will be
woken up
-Test, modify semaphore and put to sleep operations areindivisible
--this avoids race conditions
-A semaphore is a variable used to memorize and count the
number of pending wake-ups.
2019-2020 Process management 23
Semaphores
-A semaphore is used to block processes while waiting for a condition to
be fulfilled for their awakening. Blocked processes are placed in the
queue.
-A process executes the following code:
- Non-critical section
Down(S);
critical section
Up(S);
-
-
-
-Example: Producer/consumer
- Two processes share a buffer of fixed size. The first (producer) writes
information to the buffer, and the second (consumer) retrieves
information from it
-
-
Producer crashes when buffer becomes full
Consumer crashes when buffer empties
2019-2020 Process management 24
Semaphores
-Producer/consumer
-Principle
-
-
N being the fixed buffer size
The two processes share a “count” variable corresponding to the
number of elements in the buffer
- Consumer
-
-
If count = 0 - consumer goes to sleep If count = N-1 -
consumer wakes producer
- Producer
-
-
If count = N - producer goes to sleep If count = 1 -
producer wakes consumer
2019-2020 Process management 25
Semaphores
-Producer/consumer
-Two processes sharing a fixed size buffer. The first writes to the buffer
and the other consumes it
--Ensure mutual exclusion and synchronization of both processes
-Three semaphores are used in the solution
- Full:to count the number of full slots
Empty:the number of empty slots
Mutex:to ensure mutual exclusion when accessing the buffer
-
-
2019-2020 Process management 26
Semaphores
-Producer/consumer solution
# define n 100
semaphore mutex = 1;
semaphore empty = n;
semaphore full = 0; void
producer()
{
int item;
while (TRUE)
{
item = produce_item();
down(&empty);
down(&mutex);
insert_item(item);
up(&mutex);
up(&full);
}
voidconsumer()
{
int item;
while (TRUE)
{
down(&full);
down(&mutex);
item = remove_item();
up(&mutex);
up(&empty);
consume_item(item);
}
}
}
2019-20 above 27
Mutexes
-Simplified version of semaphores
-Particularly supports mutual exclusion
-A mutex can take two values
-1: unlocked
-0: locked
-Two functions for managing mutexes
-Mutex_lock: invoked to be able to enter a critical section
- If the mutex is 1 then it goes to 0 and the process could enter a
critical section
- If the mutex is 0 then the process would block until the mutex is
unblocked by another process (the one running its critical section)
-Mutex_unlock:resets the mutex to 1 at the end of the critical section,
and possibly wakes up a hung process.
2019-2020 Process management 28
Semaphores: risk of error
- The use of semaphores is relatively complex and the slightest error can
have fatal consequences on the overall functioning of the processes.
- Example: Producer/consumer
-What if, by mistake, the order of the two down statements in the
producer was reversed?
voidproducer()
{
int item;
while (TRUE)
{
item = produce_item();
down(&mutex);
down(&empty);
insert_item(item);
up(&mutex);
up(&full);
}
voidconsumer()
{
int item;
while (TRUE)
{
down(&full);
down(&mutex);
item = remove_item();
up(&mutex);
up(&empty);
consume_item(item);
}
} }
2019-2020 Process management 29
Semaphores: risk of error
-Example: Producer/consumer (continued)
-If mutex = 1 and empty = 0 - the producer will be able to perform the
down(mutex) operation, which means that the mutex goes to 0. On the
other hand, it will block after the down(empty) operation.
-The consumer will be stuck on down(mutex) because the
producer has not yet done the up(mutex) operation
-The consumer can never increment the value of empty and the
producer can never increment the value of mutex
--deadlock
--use of high-level synchronization primitives provided by a
programming language: theMonitors
2019-2020 Process management 30
Semaphores: risk of error
-Provide users with a way to define and specify part
of the code that requires mutual exclusion
without worrying about "complex" semaphore
management
-Minimize the risk of error when managing
semaphores
--use of high-level synchronization primitives
provided by a programming language
programming: Monitors
2019-2020 Process management 31
The instructors
-A monitor is a special module grouping functions,
variables and data structures
-Processes can call monitor functions, but they cannot
directly access its internal structures
-At a given time, only one process can be active in
the monitor
-The programmer does not have to worry about how
to ensure mutual exclusion
-Normally the compiler takes care of it.
2019-2020 Process management 32

Chap3_Les processus_Partie II_Ouatik.fr.en.pdf

  • 1.
    s_ouatik@yahoo.com ouatikelalaoui.said@uit.ac.ma 2019-2020 Process management1 Translated from French to English - www.onlinedoctranslator.com
  • 2.
  • 3.
    Part II Communication andsynchronization of process -How does a process pass information to another process? -How to avoid conflicts between competing processes? -How to synchronize dependent processes? 2019-2020 Process management 3
  • 4.
    Part II Communication andprocess synchronization -Plan: -The conditions of competition -The criticism and mutual exclusion section -Mutual exclusion with active expectation -Disabling interrupts -Lock variable -Strict alternation -Peterson's solution -Semaphores -The instructors 2019-2020 Process management 4
  • 5.
    Conditions of competition -Example:print spooler -Principle - A process that wants to print a file, enters its name in a special directory: spooler directory - The print daemon (spooler) periodically inspects its directory, if there is a file it prints it and removes it from the directory - The spooler directory has a large number of numbered entries: 0, 1, 2 … each of which can accommodate a file name. - The spooler uses two shared variables: out and in - Out: points to the next file to be printed (the head) In: points to the next free input (the tail) out and in variables can be stored in a file - - 2019-2020 Process management 5
  • 6.
    Conditions of competition Issue:Twoprocesses A and B are trying to print at the same time out 4 in 7 -Process A reads the value of the variable in (7) and stores it in a local variable 4 5 6 7 Linux.pdf -Before inserting his filename in entry 7, his quantum was exhausted Process A Prog.c Script.sh -the CPU switches to another process -Process B which in turn gets the CPU reads the value of in (always 7) and stores it in its local variable. Then it inserts its file name into input 7 and updates the variable in(8) B-process 2019-2020 Process management 6
  • 7.
    Conditions of competition Issue:Twoprocesses A and B are trying to print at the same time -When the CPU returns to the process A, it resumes its execution at the point where the CPU was removed from it (ie for it the value of in is 7). Then it inserts its file name into input 7 and updates the variable in(8) out 4 in 7 4 5 6 7 Linux.pdf Process A Prog.c Script.sh B-process -For the spooler everything is coherent -The file from process B will not be printed, it has been overwritten by the file from process A 2019-2020 Process management 7
  • 8.
    critical section -also calledcriticalregion, it is the set of instructions of a process likely to cause the conflict with other processes. -For example, access to memory, shared data, disk…. -Need: When a process is executing its critical section, no other process should be allowed to execute its own. -Situations where two (or more) processes access shared data are called:the conditions of competition. -To avoidthe conditions of competition,it must be avoided that the processes concerned are at the same time in the critical section --Ensure themutual exclusion 2019-2020 Process management 8
  • 9.
    critical section Example :Mutualexclusion A enters his critical section Has left its critical section Process A B tries to enter in his section critical B enters his critical section B leaves his critical section B-process B is blocked t1 t2 t3 t4 time 2019-2020 Process management 9
  • 10.
    critical section -To avoidrace conditions while ensuring cooperation and proper use of shared resources, Four conditions are necessary: 1.Two processes must not be simultaneously in their critical sections(mutual exclusion) 2.No guesswork on speed or number of processors 3.No process running outside the critical section should block other processes 4.No process should wait indefinitely to be able to enter its critical section 2019-2020 Process management 10
  • 11.
    mutual exclusion -Several solutionsto ensure mutual exclusion -Mutual exclusion with active expectation - - - - Disable Interrupts Lock Variable Strict alternation Peterson's solution -Semaphores -The instructors 2019-2020 Process management 11
  • 12.
    mutual exclusion withactive wait -The principle is that when a process is in its critical section, other processes cannot enter it, -But remainassetsand wait for the current process to complete the critical region -Several techniques are used: -Disabling interrupts -Lock variable -Strict alternation -Peterson's solution 2019-2020 Process management 12
  • 13.
    Mutual exclusion withactive expectation Disabling interrupts: -The process that enters its critical section, disables interrupts --the clock also could not send the interrupts, which implies that the processor could not switch to another process -The current process is safe now that no concurrent (or other) process will be able to enter the critical section -Other processes don't even get a chance to have the CPU while interrupts are disabled -The running process must re-enable interrupts at the end of the critical section 2019-2020 Process management 13
  • 14.
    Mutual exclusion withactive expectation Disabling interrupts: -Issue!! - Giving user processes the ability to block interrupts is not a good idea -A process that completes its critical section and forgets to re-enable interrupts -A process that voluntarily does not re-enable interrupts to take over the CPU exclusively -Disabling interrupts can be interesting for kernel mode processes - - In general, kernel-mode processes are very short Some kernel processes (like the scheduler) should not be preempted 2019-2020 Process management 14
  • 15.
    Mutual exclusion withactive expectation -Lock variables: -The process that wants to execute its critical section, tests a shared lock variable (initialized to 0). - - If this variable is at 0, then it resets it to 1 and enters the critical section Otherwise, which means another process is in the critical section - the process must wait for the lock to drop to 0 -The process resets the lock to 0 when it completes the critical section Algorithm : If lock=0 then /* critical section is free */ set lock to 1 critical section reset lock to 0 Else wait for lock=0 /* critical section is busy */ 2019-2020 Process management 15
  • 16.
    Mutual exclusion withactive expectation Issue: - The lock variable is shared - to access it, race conditions may occur - A process that has tested the lock and finds it at 0, loses the processor before returning the lock to 1. - A second process also tests the lock (still at 0) and resets it to 1 then it enters its critical section - The first process, which takes over the processor before the second completes its critical section, resets the lock to 1 (it is already at 1) and begins its critical section - Result: both processes are in the critical section at the same time!! - NB. A process can chain two (or more) executions of the critical section, while the other process has not executed one. 2019-2020 Process management 16
  • 17.
    Mutual exclusion withactive expectation Strict alternation -Two processes strictly alternate their entry into the critical section - Between two executions of the critical section by one of the processes, it is imperative that the other process also executes the critical section - Handy for representing the basic producer/consumer model -Principle: - - Both processes inspect a "turn" variable Process 1 enters its critical sectionif turn is 0. Otherwise, it enters an active waiting phase - Unlike process 1, process 2 enters its critical sectionif turn is 1. Otherwise it enters a waiting phase 2019-2020 Process management 17
  • 18.
    Mutual exclusion withactive expectation -Strict alternation -Each of the two processes alternates the turn variable at the end of its critical section -Process 1: -Process 2: -Entry code in the critical section of both processes 0-1 1-0 Process 1 Process 2 2019-2020 Process management 18
  • 19.
    Mutual exclusion withactive expectation -Strict alternation -Issue: A slow process outside its critical section would block the second process -Explanation: Suppose process P1 quickly executes its critical section and sets turn to 1 and goes to its non_critical section. - Process P2 is still in the non_critical section (and has been there for a while). - If the process P1 wants to enter its critical section it cannot because turn =1. 2019-2020 Process management 19
  • 20.
    Mutual exclusion withactive expectation -Peterson's solution -Combines strict alternation and lock variable. -A process executes an "enter_region" function before to enter the critical section -And executes the "leave_region" function after its output critical section. 2019-2020 Process management 20
  • 21.
    Mutual exclusion withactive expectation -Peterson's solution code # define n 2 int turn; int interested[n];/*default initialized to false */ void enter_region(intprocess) /* Process number */ { int other;/*number of other process */ other = 1 – process; interested[process] = TRUE;/*manifests the need to be in critical section */ turn = process; while(turn == process && interested[other] == TRUE);/*to wait for */ } void leave_region(int process) { inerested[process] = FALSE;/*notify the waiting process of its exit from the critical section */ }
  • 22.
    Mutual exclusion withactive expectation -Restrictions: -All previous solutions are based onactive waiting. -Indeed, the process waiting to enter its critical section is obliged to remain active. -i.e. a process that cannot enter a critical section would use the processor unnecessarily -The process consumes, unnecessarily, its share of the processor --This approach is very CPU intensive. 2019-2020 Process management 22
  • 23.
    Semaphores -A semaphore isa special integer variable on which two operations can be performed:downAndup -The operationdowntests if the semaphore is greater than 0. In this case, it decrements it and the process can enter the critical section. If the semaphore is null then the process goes to sleep -The operationupincrements the semaphore. - If a process is sleeping on this semaphore, then it will be woken up -Test, modify semaphore and put to sleep operations areindivisible --this avoids race conditions -A semaphore is a variable used to memorize and count the number of pending wake-ups. 2019-2020 Process management 23
  • 24.
    Semaphores -A semaphore isused to block processes while waiting for a condition to be fulfilled for their awakening. Blocked processes are placed in the queue. -A process executes the following code: - Non-critical section Down(S); critical section Up(S); - - - -Example: Producer/consumer - Two processes share a buffer of fixed size. The first (producer) writes information to the buffer, and the second (consumer) retrieves information from it - - Producer crashes when buffer becomes full Consumer crashes when buffer empties 2019-2020 Process management 24
  • 25.
    Semaphores -Producer/consumer -Principle - - N being thefixed buffer size The two processes share a “count” variable corresponding to the number of elements in the buffer - Consumer - - If count = 0 - consumer goes to sleep If count = N-1 - consumer wakes producer - Producer - - If count = N - producer goes to sleep If count = 1 - producer wakes consumer 2019-2020 Process management 25
  • 26.
    Semaphores -Producer/consumer -Two processes sharinga fixed size buffer. The first writes to the buffer and the other consumes it --Ensure mutual exclusion and synchronization of both processes -Three semaphores are used in the solution - Full:to count the number of full slots Empty:the number of empty slots Mutex:to ensure mutual exclusion when accessing the buffer - - 2019-2020 Process management 26
  • 27.
    Semaphores -Producer/consumer solution # definen 100 semaphore mutex = 1; semaphore empty = n; semaphore full = 0; void producer() { int item; while (TRUE) { item = produce_item(); down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); } voidconsumer() { int item; while (TRUE) { down(&full); down(&mutex); item = remove_item(); up(&mutex); up(&empty); consume_item(item); } } } 2019-20 above 27
  • 28.
    Mutexes -Simplified version ofsemaphores -Particularly supports mutual exclusion -A mutex can take two values -1: unlocked -0: locked -Two functions for managing mutexes -Mutex_lock: invoked to be able to enter a critical section - If the mutex is 1 then it goes to 0 and the process could enter a critical section - If the mutex is 0 then the process would block until the mutex is unblocked by another process (the one running its critical section) -Mutex_unlock:resets the mutex to 1 at the end of the critical section, and possibly wakes up a hung process. 2019-2020 Process management 28
  • 29.
    Semaphores: risk oferror - The use of semaphores is relatively complex and the slightest error can have fatal consequences on the overall functioning of the processes. - Example: Producer/consumer -What if, by mistake, the order of the two down statements in the producer was reversed? voidproducer() { int item; while (TRUE) { item = produce_item(); down(&mutex); down(&empty); insert_item(item); up(&mutex); up(&full); } voidconsumer() { int item; while (TRUE) { down(&full); down(&mutex); item = remove_item(); up(&mutex); up(&empty); consume_item(item); } } } 2019-2020 Process management 29
  • 30.
    Semaphores: risk oferror -Example: Producer/consumer (continued) -If mutex = 1 and empty = 0 - the producer will be able to perform the down(mutex) operation, which means that the mutex goes to 0. On the other hand, it will block after the down(empty) operation. -The consumer will be stuck on down(mutex) because the producer has not yet done the up(mutex) operation -The consumer can never increment the value of empty and the producer can never increment the value of mutex --deadlock --use of high-level synchronization primitives provided by a programming language: theMonitors 2019-2020 Process management 30
  • 31.
    Semaphores: risk oferror -Provide users with a way to define and specify part of the code that requires mutual exclusion without worrying about "complex" semaphore management -Minimize the risk of error when managing semaphores --use of high-level synchronization primitives provided by a programming language programming: Monitors 2019-2020 Process management 31
  • 32.
    The instructors -A monitoris a special module grouping functions, variables and data structures -Processes can call monitor functions, but they cannot directly access its internal structures -At a given time, only one process can be active in the monitor -The programmer does not have to worry about how to ensure mutual exclusion -Normally the compiler takes care of it. 2019-2020 Process management 32