
[ Example of inter task communication ]
A method of sending or receiving data between tasks by means of ring
buffer ( or circulating buffer) is considered in the following
example.
Assume that the TASK-A writes a data into the buffer and the TASK-B
reads the data from the buffer. Writing a data into the buffer where a
data is remaining unread, or reading the buffer when no data is
written, is not allowed.
Above operation is controlled by using two semaphores.
+-------+
+------------> │ SEM C │ <--------------+
│ +-------+ │
│ +-------+ │
│ +------> │ SEM D │ <-------+ │
│ │ +-------+ │ │
Wait Signal Wait Signal
│ │ buffer(shared memory) │ │
+--------+ +--------+ Read +--------+
│ TASK A │ │ │ =====> │ TASK B │
+--------+ │--------│ +--------+
│ │ │
│ Write │--------│
+=====> │ │
+--------+
TASK A TASK B SEM C SEM D
---------------+---------------+---------------+--------
3 0
Wait D -1
Wait C (wait) 2
Write data
Signal D 0
Read data
Wait C (Ready) 1
Write data
Signal D 1
Wait C 0
Write data
Signal D 2
Wait C -1
Signal C 0
[semaphore counter value and what it represents]
initial value of SEM C = number of entry in the buffer
positive value of SEM C = number of available entry
positive value of SEM D = number of data written
negative value of SEM C = waiting for data to be read
negative value of SEM D = waiting for data to be written