RTOS Semaphores
void led2_thread(void const
*argument) { int main() {
while (true) { Thread
Hthread1(led1_thread);
[Link]();
Thread
led2 = !led2; Hthread2(led2_thread);
Thread::wait(1000);
[Link](); while (true)
} {
} }
void led1_thread(void const }
*argument) {
while (true) {
[Link]();
led1 = !led1;
Thread::wait(1000);
[Link]();
}
Multiple semaphore
Multiple semaphores can be used in
multitasking system
Different set of semaphores can
share among different set of tasks
Semaphore provides a mechanism to
synchronize the running of tasks
Example for multiple
semaphore
Task A sends an IPC s1, B is waiting for s1
when s1 releases, B takes s1 and runs.
Similarly, C runs on taking s2
D runs on taking s3, again A runs on
taking s0.
Running of the codes of tasks A to D
synchronizes using the IPCs
Use of Multiple
Semaphores
Example of the use of two semaphores for
synchronizing the tasks I, J and M and the tasks J
and L, respectively
Number of tasks waiting for Same
Semaphore
OS Provides the answer
In certain OS, a semaphore is given
to the task of highest priority among
the waiting tasks.
In certain OS, a semaphore is given
to the longest waiting task (FIFO
mode).
In certain OS, a semaphore is given
as per selected option and the option
is provided to choose among priority
and FIFO.
The task having priority, if started
takes a semaphore first in case the
priority option is selected. The task
pending since longer period takes a
semaphore first in case the FIFO
option is selected.
Counting Semaphore
Counting semaphore scnt is an
unsigned 8 or 16 or 32 bit-integer.
A value of scnt controls the blocking
or running of the codes of a task.
scnt decrements each time it is
taken.
scnt increments when released by a
task
P and V SEMAPHORES
An efficient synchronization mechanism
POSIX Portable OS interfaces in unIX
P and V semaphores represents the by
integers in place of binary or unsigned
integers
The semaphore is accessed only through
two standard atomic operations P and V
P (for wait operation) derived from a Dutch
word Proberen, which means 'to test'.
V (for signal passing operation) derived
from the word 'Verhogen' which means 'to
increment'.
P and V Functions for
Semaphore
P semaphore function signals that
the task requires a resource and if
not available waits 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 SEMAPHORE FUNCTIONS
WITH MUTEX PROPERTY
Program counter assignments (context
Switch) to the process or function
when using P and V semaphores