Ktustudents - In: Ec 308 Embedded Systems Module-5
Ktustudents - In: Ec 308 Embedded Systems Module-5
Ktustudents - In: Ec 308 Embedded Systems Module-5
MODULE-5
SYLLABUS
KTUStudents.in
Inter Process Communication and Synchronization -Process, tasks and threads –Shared
data– Inter process communication - Signals – Semaphore – Message Queues – Mailboxes
– Pipes –Sockets – Remote Procedure Calls (RPCs).
Process Features
KTUStudents.in
Example - Mobile Phone Device embedded software
➢ Software highly complex.
➢ Number of functions, ISRs, processes threads, multiple physical and virtual
device drivers, and several program objects that must be concurrently
processed on a single processor.
➢ Voice encoding and convoluting process─ the device captures the spoken words
through a speaker and generates the digital signals after analog to digital
conversion, the digits are encoded and convoluted using a CODEC,
➢ Modulating process,
➢ Display process,
➢ GUIs (graphic user interfaces), and
➢ Key input process ─ for provisioning of the user interrupts
➢ Process ID,
➢ Process priority,
➢ Parent process (if any),
➢ Child process (if any), and
➢ Address to the next process PCB which will run,
➢ Allocated program memory address blocks in physical memory and in secondary
(virtual) memory for the process-codes,
➢ Allocated process-specific data address blocks
➢ Allocated process-heap (data generated during the program run) addresses,
➢ Allocated process-stack addresses for the functions called during
running of the process,
➢ Allocated addresses of CPU register-save area as a process context represents by
CPU registers, which include the program counter and stack pointer
KTUStudents.in
➢ Allocated addresses of CPU register-save area as a process context [Register-
contents
(define process context) include the program counter and stack pointer contents]
➢ process-state signal mask [when mask is set to 0 (active) the process is
inhibited from running and when reset to 1, the process is allowed to run],
➢ Signals (messages) dispatch table [process IPC functions],
➢ OS allocated resources‘ descriptors (for example, file descriptors for open
files, device descriptors for open (accessible) devices, device-buffer
addresses and status, socket- descriptor for open socket), and
➢ Security restrictions and permissions.
Thread Features
➢ Process… heavyweight
o Process considered as a heavyweight process and a kernel-level controlled
entity.
o Process thus can have codes in secondary memory from which the pages
KTUStudents.in
can be swapped into the physical primary memory during running of the
process. [Heavy weight means its running may depend on system
resources]
o May have process structure with the virtual memory map, file
descriptors, user–ID, etc.
o Can have multiple threads, which share the process structure thread
o A process or sub-process within a process that has its own program
counter, its own stack pointer and stack, its own priority parameter for its
scheduling by a thread scheduler
o Its‘ variables that load into the processor registers on context switching.
o Has own signal mask at the kernel. Thread‘s signal mask
o When unmasked lets the thread activate and run.
o When masked, the thread is put into a queue of pending threads.
Multiprocessing OS
Thread’s stack
➢ When a function in a thread in OS is called, the calling function state is
placed on the stack top.
➢ When there is return the calling function takes the state information from
the stack top
➢ A data structure having the information using which the OS controls the
thread state.
➢ Stores in protected memory area of the kernel.
KTUStudents.in
➢ Consists of the information about the thread state
5.3 Task
Task Features
➢ An application program can also be said to be a program consisting of the tasks
and task behaviors in various states that are controlled by OS.
➢ A task is like a process or thread in an OS.
➢ Task─ term used for the process in the RTOSes for the embedded systems.
▪ For example, VxWorks and μCOS-II are the RTOSes,
which use the term task.
➢ A task consists of executable program (codes), state of which is controlled by OS,
the state during running of a task represented by information of process status
(running, blocked, or finished),process-structure—its data, objects and resources,
and task control block (PCB).
➢ Runs when it is scheduled to run by the OS (kernel), which gives the control of the
CPU on a task request (system call) or a message.
➢ Runs by executing the instructions and the continuous changes of its state
takes place as the program counter (PC) changes.
➢ Task is that executing unit of computation, which is controlled by some process
at the OS scheduling mechanism, which lets it execute on the CPU and by some
process at OS for a resource-management mechanism that lets it use the system
memory and other system-resources such as network, file, display or printer.
➢ A task is an independent process.
➢ No task can call another task. [It is unlike a C (or C++) function, which can
For more study materials: WWW.KTUSTUDENTS.IN
call another function.]
➢ The task─ can send signal (s) or message(s) that can let another task run.
➢ The OS can only block a running task and let another task gain access of CPU
to run the servicing codes
KTUStudents.in
Task States
Running state
• Executing the codes and getting the system resources at this instance. It
will run till it needs some IPC (input) or wait for an event or till it gets pre-
empted by another higher priority task than this one.
•
Blocked (waiting) state
• Execution of task codes suspends after saving the needed parameters into
its Context. It needs some IPC (input) or it needs to wait for an event or wait
for higher priority task to block to enable running after blocking.
➢ Inter process communication (IPC) means that a process (scheduler or task or ISR)
generates some information by setting or resetting a Token or value, or generates an
output so that it lets another process take note or to signal to OS for starting a process
or use it under the control of an OS.
5.5 Signals
For more study materials: WWW.KTUSTUDENTS.IN
Features:
KTUStudents.in
2. Connect ()
➢ This function is used to connect an interrupt vector to a signal number,
with signaled handler function and signal handler arguments. The interrupt
vector provides the program counter value for the signal handler function
address.
3. signal ( )
➢ It is the function signal ( ) to send a signal identified by a number to a
signal handler task.
4. Mask ()
➢ This function is used to mask the signal
5. Unmask ()
➢ This function is used to unmask the signal
6. Ignore ()
➢ This function is used to ignore the signal
Application Example:
➢ Signal is handled only by a very high priority process (service routine). That may
disrupt the usual schedule and usual priority inheritance mechanism.
➢ Signal may cause reentrancy problem [process not retuning to state identical to
the one before signal handler process executed].
5.6 Semaphore
➢ A semaphore is special kind of shared program variable.
➢ A semaphore is a kernel object that one or more tasks can acquire or release
for the purpose of synchronization or mutual exclusion.
➢ Mutual exclusion is a provision by which only one task at a time can access a
shared resource (port, memory block, etc.)
➢ The value of a semaphore is a non-negative integer.
KTUStudents.in
➢ If the integer data is only allowed to take the values 0 and 1 then the semaphore is
referred to as a binary semaphore.
➢ Binary semaphore allows only one process at a time to access the shared
resource.
➢ If the integer data is allowed to take any non-negative value then the semaphore is
referred to as a General Semaphore or Counting Semaphore.
➢ Counting Semaphore allows N > 1 processes to access the resource
simultaneously. Instead of having states empty and full, it uses a counter S. The
counter is initialised to N, with S = 0 corresponding to the full state.
➢ OS provides the semaphore IPC functions for creating, releasing and taking of the
semaphore
Semaphore functions:
➢ Shared Resources like shared memory buffer are to be used only by one task
(process or thread) at an instance.
➢ OS Functions provide for the use of a semaphore resource key for running of the
codes in critical section.
➢ Let a task A, when getting access to a resource/critical section notifies to the OS to
KTUStudents.in
have taken the semaphore (take notice)That is, an OS function OSSemPend() runs
to notify. The OS returns the semaphore as taken (accepted) by decrementing the
semaphore from 1 to 0.
➢ Now the task A accesses the resource.
➢ The task A, after completing the access to a resource/critical section it notiufies to
the OS to have posted that semaphore (post notice). That is an OS function
OSSemPost() runs to notify. The OS returns the semaphore as released by
incrementing the semaphore from 0 to 1.
➢ Figure below shows the use of semaphore between A and B. It shows the five
sequential actions at five different times.
KTUStudents.in
➢ Mutex means mutually exclusive key.
➢ Mutex is a binary semaphore usable for protecting use of resource by other task
section at an instance
➢ Let the semaphore sm has an initial value = 1
➢ When the semaphore is taken by a task the semaphore sm decrements from 1 to 0
and the waiting task codes starts.
➢ Assume that the sm increments from 0 to 1 for signalling or notifying end of use
of the semaphore that section of codes in the task.
➢ When sm = 0 ─assumed that it has been taken (or accepted) and other task code
section has not taken it yet and using the resource
➢ When sm = 1─assumed that it has been released (or sent or posted) and other task
code section can now take the key and use the resource.
P and V semaphores
Features:
KTUStudents.in
fixed pre-defined size assigned by the scheduler.
5. When an RTOS call is to insert into the queue, the bytes are as per the pointed
number of bytes. For example, for an integer or float variable as a pointer, there
will be four bytes inserted per call. If the pointer is for an array of 8 integers, then
32 bytes will be inserted into the queue.
6. When a queue becomes full, there may be a need for error handling and user codes
for blocking the task(s). There may not be self-blocking.
➢ Figure (a ) shows the memory blocks at OS for inserting deleting and other
functions.
➢ Figure (b) shows the functions for the queue in the OS.
➢ Figure (c) shows a queue message block with the messages or message pointers.
➢ Two pointers *QHEAD and *QTAIL are for queue head and tail memory
locations
KTUStudents.in
OSQCreate
➢ This function is used to create a queue and initialize the queue.
OSQPost
➢ This function is used to post a message to the message block as per the queue tail
pointer, *QTAIL.
OSQPend
➢ This function is used to wait for a queue message at the queue and reads and
deletes that when received.
OSQAccept
➢ This function is used to delete the present queue head after checking its presence
KTUStudents.in
yes or no and after the deletion the queue head pointer increments.
OSQFlush
➢ This function is used to deletes messages from queue head to tail, after the flush
the queue head and tail points to QTop, pointer to start of the queue.
OSQQuery
➢ This function is used to query the queue message-block when read and but the
message is not deleted. The function returns pointer to the message queue
*QHEAD if there are the messages in the queue or else NULL. It return a pointer
to data structure of the queue data structure which has *QHEAD, number of
queued messages, size of the queue and. table of tasks waiting for the messages
from the queue.
OSQPostFront
➢ This function is used to send a message as per the queue front pointer, *QHEAD.
Use of this function is made in the following situations. A message is urgent or is
of higher priority than all the previously posted message into the queue.
5.8 Mailbox
Features
➢ Mailbox (for message) is an IPC through a message-block at an OS that can be
used only by a single destined task.
➢ The source (mail sender) is the task that sends the message pointer to a created
mailbox.
➢ OS provides for inserting and deleting message into the mailbox message-pointer.
➢ Each mailbox for a message need initialization (creation) before using the
functions in the scheduler for the message queue and message pointer pointing to
Null.
Types:
KTUStudents.in
➢ One type is only one message per mailbox is available.
➢ Another type is mailbox with provision for multiple messages or message pointers.
➢ Third one is OS can provide multiple mailbox messages with each message having
a priority parameter. The read (deletion) can then only be on priority basis in case
mailbox has multiple messages.
Mailbox Functions
5.9 Pipes
➢ One task using the function fwrite in a set of tasks can write to a pipe at the
back pointer address, *pBACK.
➢ One task using the function fread in a set of tasks can read (delete) from a pipe
at the front pointer address, *pFRONT.
➢ In a pipe there may be no fixed number of bytes per message but there is end
pointer.
➢ A pipe can therefore be limited and have a variable number of bytes per
message between the initial and final pointers.
➢ Pipe is unidirectional. One thread or task inserts into it and other one deletes
from it.
Pipe Functions
➢ Figure (a) shows the write and read the pipe using device drivers.
➢ Figure (b) shows the functions for the pipe in the OS.
➢ Figure (c) shows a pipe messages in the message buffer.
KTUStudents.in
KTUStudents.in
Figure (c) Pipe message block
➢ A pipe could be used for inserting the byte steam by a process and deleting the
bytes from the stream by another process.
➢ However, for example, we need that the card information to be transferred from a
process A as byte stream to the host machine process B and the B sends messages
as byte stream to the A
➢ There is need for bi-directional communication between A and B.
➢ We need that the A and B ID or address information when communicating must
also be specified either for the destination alone or for the source and destination
both. [The messages in a letter are sent along with address specification.]
➢ We need to use a protocol for communication
➢ A protocol, for example, provides along with the byte stream information of the
address or port of the destination or addresses or ports of source and destination
KTUStudents.in
both or the protocol may provide for the addresses and ports of source and
destination in case of the remote processes.
➢ For example, UDP (user datagram protocol) is used as connectionless protocol.
➢ UDP header contains source port (optional) and destination port numbers, length
of the datagram and checksum.
➢ Port means a process or task for specific application.
➢ Port number specifies the process.
➢ Datagram means a data, which is independent need not in sequence with the
previously sent data.
➢ Checksum is sum of the bytes to enable the checking of the erroneous data
transfer.
➢ TCP (transport control protocol) is used as connection oriented protocol.
Features:
➢ Socket Provides for a bi-directional pipe like device, which also use a protocol
between source and destination processes for transferring the bytes.
➢ Provides for establishing and closing a connection between source and destination
processes using a protocol for transferring the bytes.
➢ May provide for listening from multiple sources or multicasting to multiple
destinations.
➢ Two tasks at two distinct places or locally interconnect through the sockets.
➢ Multiple tasks at multiple distinct places interconnect through the sockets to a
socket at a server process.
➢ The client and server sockets can run on same CPU or at distant CPUs on the
Internet.
Socket Functions
KTUStudents.in
➢ Figure (a) shows the write by a server task and read by a client task using
device drivers.
➢ Figure (b) shows the functions for the socket for both server and client in the
OS.
➢ Figure (c) shows byte stream at socket device.
KTUStudents.in
Figure (c) Socket message stream
➢ A method used for connecting two remotely placed functions by first using a
protocol for connecting the processes. It is used in the cases of distributed tasks.
➢ The RTOS can provide for the use of RPCs. These permits distributed
environment for the embedded systems.
➢ The OS IPC function allows a function or method to run at another address space
of shared network or other remote computer.
➢ The client makes the call to the function that is local or remote and the server
response is either remote or local in the call.
➢ Both systems work in the peer-to-peer communication mode. Each system in peer-
to-peer mode can make an RPC.
➢ An RPC permits remote invocation of the processes in the distributed systems.
➢ The RPC provides the inter task communication when a task is at system1 and
another task is at system 2.
➢ Figure below shows the initialised virtual sockets between the client set of tasks
and a server set of tasks at RTOS.
KTUStudents.in