Chap 7Lesson09EmsysNewPVSem
-
Upload
ankit-sanghvi -
Category
Documents
-
view
220 -
download
0
Transcript of Chap 7Lesson09EmsysNewPVSem
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
1/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1
Inter-Process Communication and
Synchronization of Processes, Threadsand Tasks:
Lesson-9: P and V SEMAPHORES
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
2/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2
P and V SEMAPHORES
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
3/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3
P and V semaphoresP and V semaphores
An efficient synchronisation mechanism
POSIX 1003.1.b, an IEEE standard. POSIXfor portable OS interfaces in Unix.
P and V semaphores
represents the byintegers in place of binary or unsigned
integers
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
4/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4
P and V semaphoreP and V semaphore VariableVariabless The semaphore, apart from initialization, is
accessed only through two standard atomicoperationsP and V
P (for wait operation) derived from a
Dutch word Proberen, which means 'totest'.
V (for signal passing operation) derived
from the word 'Verhogen' which means 'to
increment'.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
5/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
5
P semaphore function signals that the task
requires a resource and if not availablewaits for it.
V semaphore function signals which the
task passes to the OS that the resource is
now free for the other users.
P and V Functions forP and V Functions for Semaphore
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
6/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
6
1. /* Decrease the semaphore variable*/
sem_1 = sem_1 -1;
2. /* If sem_1 is less than 0, send a message toOS by calling a function waitCallToOS.Control of the process transfers to OS,
because less than 0 means that some otherprocess has already executed P function onsem_1. Whenever there is return for the OS,it will be to step 1. */
if (sem_1 < 0){waitCallToOS (sem_1);}
P FunctionP Function
P (&Sem1)P (&Sem1)
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
7/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
7
3. /* Increase the semaphore variable*/
sem_2 = sem_2 + 1;
4. /* If sem_2 is less or equal to 0, send amessage to OS by calling a functionsignalCallToOS. Control of the processtransfers to OS, because < or = 0 means thatsome other process is already executed Pfunction on sem_2. Whenever there isreturn for the OS, it will be to step 3. */
if (sem_2 < = 0){signalCallToOS (sem_2);}
V FunctionV Function
V (&Sem2)V (&Sem2)
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
8/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
8
P and V SEMAPHORE FUNCTIONS
WITH SIGNALING ORNOTIFICATION PROPERTY
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
9/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
9
P and V SEMAPHORE FUNCTIONS WITH
SIGNAL OR NOTIFICATION PROPERTYProcess 1 (Task 1):
while (true) {
/* Codes */
.
V (&sem_s);/* Continue Process 1 if sem_s is not equal to
0 or not less than 0. It means that no process
is executing at present. */.
};
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
10/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
10
P and V SEMAPHORE FUNCTIONS WITH
SIGNAL OR NOTIFICATION PROPERTYProcess 2 (Task 2):
while (true) {
/* Codes */
.
P (&sem_s);/* The following codes will execute only
when sem_s is not less than 0. */
.
};
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
11/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
11
P and V SEMAPHORE FUNCTIONSWITH MUTEX PROPERTY
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
12/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
12
Use of P and V semaphores at the tasks I and J andUse of P and V semaphores at the tasks I and J and
actions by scheduleractions by scheduler
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
13/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
13
Program counter assignments (context
Switch) to the process or function when using
P and V semaphores
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
14/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
14
P and V Semaphore functions with mutex
property
Wait for starting Critical SectionProcess 1 (Task 1):
while (true) {
/* Codes before a critical region*/
.
/* Enter Process 1 Critical region codes*/P (&sem_m);
/* The following codes will execute only
when sem_m is not less than 0. */
.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
15/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
15
P and V Semaphore functions with mutex
property
running end exiting Critical SectionProcess 1 (Task 1):
.
/* Exit Process 1 critical region codes */
V (&sem_m);
/* Continue Process 1 if sem_m is not equal to0 or not less than 0. It means that no processis executing at present. */
.};
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
16/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
16
P and V Semaphore functions with mutex
propertyWait for starting other Critical
Section
Process 2 (Task 2):
while (true) {/* Codes before a critical region*/
.
/* Enter Process 2 Critical region codes*/
P (&sem_m);
/* The following codes will execute onlywhen sem_m is not less than 0. */
.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
17/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
17
P and V Semaphore functions with mutex
property
running end exiting the CriticalSection
Process 2 (Task 2):
./* Exit Process 2 critical region codes */
V (&sem_m);
/* Continue Process 2 if sem_m is not equal to0 or not less than 0. It means that no processis executing at present. */
.
};
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
18/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
18
P and V SEMAPHORE FUNCTIONS
WITH COUNTING SEMAPHOREPROPERTY
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
19/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
19
P and V Semaphore functions with Count
property
producer Wait for empty place if = 0Process c (Task c):
while (true) {/* sem_c1 represent number of
empty places*/
/* Codes before a producer region*/
.
./* Exit Process 3 region codes */
P (&sem_c1);
/* Continue Process c if sem_c1 is not equalto 0 or not less than 0. */
.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
20/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
20
P and V SEMAPHORE FUNCTIONS
FOR PRODUCER-CONSUMER
PROBLEM (BOUNDED BUFFER
PROBLEM)
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
21/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
21
ProducerProducer--Consumer and Bounded BufferConsumer and Bounded Buffer
ProblemsProblemsConsider three examples:
(i) a task transmits bytes to an I/O streamfor filling the available places at the stream;
(ii) a process 'writes'an I/O stream to a
printer-buffer; and
(iii) a task of producing chocolates is being
performed.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
22/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
22
ProducerProducer--Consumer and Bounded BufferConsumer and Bounded Buffer
ProblemsProblems Example (i) another task reads the I/O-
stream bytes from the filled places andcreates empty places.
Example (ii), from the print buffer an I/Ostream prints after a buffer-read and after
printing, more empty places are created.
Example (iii), a consumer is consuming the
chocolates produced and more empty places(to stock the produced chocolates) arecreated.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
23/32
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
24/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
24
Bounded Buffer ProblemBounded Buffer Problem
Example (ii) The task cannot write from
the memory to print buffer if there are noempty places at the print-buffer.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
25/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
25
ProducerProducer
--Consumer ProblemConsumer Problem
Example (iii) The producer cannot
produce chocolates if there are no emptyplaces at the consumer end.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
26/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
26
P and V Semaphore functions with producer-
consumer problem solutionProcess 3 (Task 3):
while (true) {
/* Codes before a producer region. sem_c2number of empty places created by process4 */
.
/* Enter Process 3 Producing region codes*/
P (&sem_c2);/* The following codes will execute only
when sem_c2 is not less than 0. */
.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
27/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
27
P and V Semaphore functions with producer-
consumer problem solutionProcess 3 (Task 3):
.
/* Exit Process 3 region codes */
V (&sem_c1);
/* Continue Process 3 if sem_c1 is not equalto 0 or not less than 0. */
.
};
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
28/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
28
P and V Semaphore functions with producer-
consumer problem solutionProcess 4 (Task 4):
while (true) {/* Codes before a producer
region. sem_c1 number of filled placescreated by process 3 */
.
/* Enter Process 4 Consuming region codes*/
P (&sem_c1);
/* The following codes will execute onlywhen sem_c1 is not less than 0. */
.
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
29/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
29
P and V Semaphore functions with producer-
consumer problem solutionProcess 4 (Task 4):.
/* Exit Process 4 region codes */V (&sem_c2);
/* Continue Process 4 if sem_m is not equal to0 or not less than 0. It means that no processis executing at present. */
.};
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
30/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
30
SummarySummary
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
31/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
31
We learnt P and V semaphore functions are POSIXP and V semaphore functions are POSIX
100.3b IEEE accepted standard for the100.3b IEEE accepted standard for the
semaphoresemaphore IPCsIPCs..
These can be used as event signaling, asThese can be used as event signaling, as
mutex, as counting semaphore and asmutex, as counting semaphore and assemaphores for the bounded buffer problemsemaphores for the bounded buffer problem
solution (producersolution (producer
consumer problem)consumer problem)
-
8/3/2019 Chap 7Lesson09EmsysNewPVSem
32/32
2008 Chapter-7 L9: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
32
End of Lesson 9 of Chapter 7End of Lesson 9 of Chapter 7