chap 7lesson08emsysnewmultipleandcountingsemaphoreresourcekey
TRANSCRIPT
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
1/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1
Inter-Process Communication andSynchronization of Processes, Threads
and Tasks:
Lesson-8: Use of Multiple Semaphores
and counting Semaphore forSynchronizing the Tasks
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
2/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2
Use of Multiple SemaphoresUse of Multiple Semaphores
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
3/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3
Use of Multiple Semaphores forUse of Multiple Semaphores for
Synchronizing the TasksSynchronizing the Tasks
Example of the use of two semaphores
for synchronizing the tasks I, J and Mand the tasks J and L, respectively
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
4/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4
Use of two semaphores for synchronizing
tasks I, J, K, L and M
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
5/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
5
OSSemPost ( )an OS IPC function for
posting a semaphore and assume
OSSemPend ( )
another OS IPC function
for waiting for the semaphore.
Let sTaskis the mutex semaphore pendingand posted at each task to let another run.
Let sTask1 initially is 1 and sTask2, sTask3
and sTask4 initially are 0s
OS Functions forOS Functions for Semaphore
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
6/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
6
Consider Codes such that firstly taskIwill
run, thenJ, then K, thenL, thenIwhen at aninitial instance sTask1 = 1 and sTask2 =
sTask3 = sTask4 = 0
CodesCodes
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
7/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
7
Running of Tasks A, B, C, and DRunning of Tasks A, B, C, and DSynchronized through IPCs s0, s1, s2 and s3Synchronized through IPCs s0, s1, s2 and s3
Task A Task B
Release
s1
Take s1
Task CTask C
Releases2
Take s2
Releasess3
Task DTask D
Take s3
Releases0
Take s0
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
8/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
8
Task A sends an IPC s1, B is waiting for s1,Task A sends an IPC s1, B is waiting for s1,
when s1 releases, B takes s1 and runs.when s1 releases, B takes s1 and runs.
Similarly, C runs on taking s2, D runs onSimilarly, C runs on taking s2, D runs on
taking s3, again A runs on taking s0.taking s3, again A runs on taking s0.
Running of the codes of tasks A to DRunning of the codes of tasks A to D
synchronizes using the IPCssynchronizes using the IPCs
Running of Tasks A, B, C, and DRunning of Tasks A, B, C, and D
Synchronized through IPCs s0, s1, s2Synchronized through IPCs s0, s1, s2
and s3and s3
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
9/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
9
Codes for taskCodes for taskIIwait for runningwait for running
static void Task_ I (void *taskPointer) {
.
while (1) {
OSSemPend (sTask1) /* Post the
semaphore sTask1. Means that OSfunction decrements sTask1 in
corresponding event control block.sTask1 becomes 0 and following code
run*/
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
10/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
10
Codes for taskCodes for taskIIrun and release semaphore forrun and release semaphore for
JJ
/* Codes for Task_I */
.
OSSemPost (sTask2) /* Post the
semaphore sTask2. This means that OSfunction increments sTask2 in
corresponding event control block.
sTask2 becomes 1 */
};
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
11/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
11
Codes for taskCodes for taskJJwait for semaphore fromwait for semaphore fromII
static void Task_ J (void *taskPointer) {
.
while (1) {OSSemPend (sTask2) /* Wait sTask2. Means
wait till sTask2 is posted and becomes 1.
When sTask2 becomes 1 and the OSfunction decrements sTask2 in
corresponding event control block, sTask2
becomes 0. Task then runs further the
following code*/
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
12/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
12
Codes for task J run and release semaphoreCodes for task J run and release semaphore
for Kfor K /* Code for Task J */
.
.
OSSemPost (sTask3) /* Post the semaphore
sTask3. Means that OS function incrementssTask3 in corresponding event control
block. sTask3 becomes 1. */
};
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
13/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
13
Codes for task K wait for semaphore from JCodes for task K wait for semaphore from J
static void Task_ K (void *taskPointer) {
.
while (1) {OSSemPend (sTask3) /* Wait for the
semaphore sTask3. Means that wait till
sTask3 is posted and becomes 1. WhensTask3 becomes 1 and the OSSemPend
decrements sTask3 in corresponding event
control block. sTask3 becomes 0. Task then
runs further the following code*/
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
14/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
14
Codes for task K run and release semaphoreCodes for task K run and release semaphore
for Lfor L
/* Code for Task K */
.
.
OSSemPost (sTask4) /* Post the semaphore
sTask4. This means that OS function
increments sTask4 in corresponding event
control block. sTask4 becomes 1. */
};
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
15/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
15
Codes for task L wait for semaphore from KCodes for task L wait for semaphore from K
static void Task_ L (void *taskPointer) {
.
while (1) {
OSSemPend (sTask4) /* Wait for the semaphore
sTask4. This means that task waits till sTask4 is
posted and becomes 1. When sTask4 becomes 1
and the OS function is to decrements sTask3 in
corresponding event control block. sTask4
becomes 0. Task then runs further the followingcode*/
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
16/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
16
Codes for task L run and release semaphoreCodes for task L run and release semaphore
for Ifor I
/* Code for Task L */
. .
OSSemPost (sTask1) /* Post the semaphore
sTask1. This means that OS function
increments sTask1 in corresponding event
control block. sTask1 becomes 1. */ };
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
17/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
17
Number of tasks waiting for sameNumber of tasks waiting for same
semaphoresemaphore
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
18/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
18
Number of tasks waiting for SameNumber of tasks waiting for Same
SemaphoreSemaphore
OS Provides the answer
In certain OS, a semaphore is given tothe task of highest priority among the
waiting tasks. In certain OS, a semaphore is given to
the longest waiting task (FIFO mode).
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
19/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
19
Number of tasks waiting for SameNumber of tasks waiting for Same
SemaphoreSemaphore In certain OS, a semaphore is given as per
selected option and the option is provided tochoose among priority and FIFO.
The task having priority, if started takes a
semaphore first in case the priority option isselected. The task pending since longer
period takes a semaphore first in case the
FIFO option is selected.
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
20/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
20
Counting SemaphoreCounting Semaphore
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
21/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
21
OS counting semaphore functionsOS counting semaphore functions Counting semaphore scntis an unsigned 8
or 16 or 32 bit-integer. A value ofscntcontrols the blocking or
running of the codes of a task.
scntdecrements each time it is taken.
scntincrements when released by a task.
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
22/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
22
CountingCounting--semaphoresemaphore scntat an instance reflects the initialized
value minus the number of times it is taken
plus the number of times released.
scntcan be considered as the number of
tokens present and the waiting task will dothe action if at least one token is present.
The use ofscntis such that one of the task
thus waits to execute the codes or waits fora resource till at least one token is found
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
23/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
23
Assume that a task can send on a network the
stacks into 8 buffers. Each time the task runs it takes the semaphore and
sends the stack in one of the buffers, which is next
to the earlier one. Assume that a counting semaphore scntis
initialized = 8. After sending the data into the
stack, the task takes the scntand scntdecrements.When a task tries to take the scntwhen it is 0, then
the task blocks and cannot send into the buffer
Counting Semaphore applicationCounting Semaphore application
exampleexample
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
24/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
24
ConsiderChocolate delivery task.
It cannot deliver more than the total number
of chocolates, total loaded into the machine.
Assume that a semCnt is initialized equal to
total. Each time, the new chocolates loaded in the
machine, semCnt increments by the number
of new chocolates.
ACVM ExampleACVM Example
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
25/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
25
static void Task_Deliver (void *taskPointer) {.
while (1) { /* Start an infinite while-loop. */
/* Wait for an event indicated by an IPC from TaskRead-Amount */
.
If (Chocolate_delivered) OSSemPend (semCnt) /* Ifchocolate delivered is true, if semCnt is not 1 or >1 (which means is 0 or less) else decrement thesemCnt and continue remaining operations */
.
};
Chocolate delivery taskChocolate delivery taskcodecode
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
26/30
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
27/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
27
We learnt Multiple semaphores can be used inMultiple semaphores can be used in
multitasking systemmultitasking system
Different set of semaphores can shareDifferent set of semaphores can share
among different set of tasks.among different set of tasks.
Semaphore provides a mechanism toSemaphore provides a mechanism tosynchronize the running of taskssynchronize the running of tasks
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
28/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
28
We learnt Counting semaphore provides a way ofCounting semaphore provides a way of
taking it and releasing it number of timestaking it and releasing it number of times
When taken by a waiting task sectionhen taken by a waiting task section
when it is 1 or > 1, it decrementsit decrements, the
semaphore becomes available It increments when posts (sent or released)increments when posts (sent or released)
a task .a task .
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
29/30
-
8/3/2019 Chap 7Lesson08EmsysNewmultipleandCountingsemaphoreresourcekey
30/30
2008 Chapter-7 L8: "Embedded Systems - Architecture, Programmingand Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
30
End of Lesson 8 of Chapter 7End of Lesson 8 of Chapter 7