Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

INTER-PROCESS

COMMUNICATION AND
SYNCHRONISATION:
Lesson-12: Signal Function

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 1
Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Signal

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 2
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal
 One way for messaging is to use an OS
function signal ( ).
 Provided in Unix, Linux and several
RTOSes.
 Unix and Linux OSes use signals profusely
and have thirty-one different types of
signals for the various events.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 3
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal
 A signal is the software equivalent of
the flag at a register that sets on a
hardware interrupt. Unless masked by
a signal mask, the signal allows the
execution of the signal handling
function and allows the handler to run
just as a hardware interrupt allows the
execution of an ISR

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 4
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal…

 Signal is an IPC used for signaling


from a process A to OS to enable
start of another process B
 Signal is a one or two byte IPC
from a process to the OS.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 5
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal …
 Signal provides the shortest communication.
 The signal ( ) sends a one-bit output for a
process, which unmasks a signal mask of a
process or task (called signal handler)
 The handler has coding similar to ones in an
ISR runs in a way similar to a highest
priority ISR.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 6
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal …
 An ISR runs on an hardware interrupt
provided that interrupt is no masked. The
signal handler also runs on signal provided
that signal is no masked

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 7
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal …
 Signal ( ) forces a signaled process or task
called signal handler to run.
 When there is return from the signaled or
forced task or process, the process, which
sent the signal, runs the codes as happens on
a return from the ISR.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 8
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal ...

 OS connects a signal to a process or


ISR j (called signal handler function),
and resets the signal mask of j.
 Then the j runs after all higher than j
priority processes (or ISRs) finish.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 9
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal ...

 An OS provision for signal as an


IPC function means a provision for
interrupt-message from a process
or task to another process or task

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 10
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal ( ) IPC function
1. SigHandler ( ) to create a signal handler
corresponding to a signal identified by the
signal number and define a pointer to signal
context. . The signal context saves the
registers on signal.
2. 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.
Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,
2008 11
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal ( ) IPC functions

3. A function signal ( ) to send a signal


identified by a number to a signal handler
task
4. Mask the signal
5. Unmask the signal
6. Ignore the signal

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 12
Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Comparison between signal and
semaphore

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 13
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal and Semaphore
 Some OSes provide the signal and
semaphore both IPC functions
 Every OS provides semaphore IPC
functions.
 When the IPC functions for signal are not
provided by an OS, then the OS employs
semaphore for the same purpose.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 14
Raj Kamal, Publs.: McGraw-Hill, Inc.
Task A sending signal sB to initiate Task B (signal
handler) to run

OS Task B
signal
Task A mask B start B

Send sB

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 15
Raj Kamal, Publs.: McGraw-Hill, Inc.
Task A sending semaphore as event flag semi to initiate a
task section waiting to take semi before it could run

OS Task B
signal
Task A mask B Wait semi = 0
semi
semi = 1 Take Start

Release semi
semi

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 16
Raj Kamal, Publs.: McGraw-Hill, Inc.
3. Example of using signal ( )

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 17
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal ( ) Example

 Handling of exception.
 An exception is a process that is
executed on a specific reported
run-time condition.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 18
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal ( ) Example
A signal reports an error (called
'Exception') during the running of a
task and then lets the scheduler initiate
an error-handling process or ISR, for
example, initiate error-login task.
- Handling of signal is similar to an ISR
handling function using an interrupt
vector.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 19
Raj Kamal, Publs.: McGraw-Hill, Inc.
4. Advantage of using a Signal

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 20
Raj Kamal, Publs.: McGraw-Hill, Inc.
Advantage of using a Signal

 Unlike semaphores, it takes the


shortest possible CPU time. The
signals are the flag or one or two byte
message used as the IPC functions for
synchronizing the concurrent
processing of the tasks.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 21
Raj Kamal, Publs.: McGraw-Hill, Inc.
Signal handler Tasks B, C, and D Synchronizing
their execution by signals s1, s2 and s3

Task A Task B Task C Task D


Start B Start C Start D

Send s2 Send s3
Send s1

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 22
Raj Kamal, Publs.: McGraw-Hill, Inc.
Task i sending signal s to initiate signal handler ISR j
Execute signal ( )

OS
ISR j
signal
Task i mask j start j

Send sj
Return

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 23
Raj Kamal, Publs.: McGraw-Hill, Inc.
Advantage of using a Signal

1. A signal is the software equivalent of the


flag at a register that sets on a hardware
interrupt.
2. It is sent on some exception or on some
condition, which can set during running of
a process or task or thread.
3. Sending a signal is software equivalent of
throwing exception in C/C++ or Java
program

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 24
Raj Kamal, Publs.: McGraw-Hill, Inc.
Advantage of using a Signal

4. Unless process is masked by a


signal mask, the signal allows
the execution of the signal
handling process, just as a
hardware interrupt allows the
execution of an ISR

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 25
Raj Kamal, Publs.: McGraw-Hill, Inc.
Advantage of using a Signal

5. A signal is identical to setting a flag


that is shared and used by another
interrupt servicing process.
6. A signal raised by one process forces
another process to interrupt and to
catch that signal provided the signal
is not masked at that process.

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 26
Raj Kamal, Publs.: McGraw-Hill, Inc.
5. Drawbacks of using a Signal

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 27
Raj Kamal, Publs.: McGraw-Hill, Inc.
Drawbacks of using a Signal
1. Signal is handled only by a very high
priority process (service routine).
That may disrupt the usual schedule
and usual priority inheritance
mechanism.
2. Signal may cause reentrancy
problem [process not retuning to state
identical to the one before signal
handler process executed].
Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,
2008 28
Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 29
Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
 (i) The simplest IPC for messaging and
synchronizing processes is signal. A
signal provides the shortest message.
Signals are used for initiating another
ISR or task and error handling process
called signal handler.
(ii) A signal is equivalent throwing
exception in Java/C/C++ or setting of
an interrupt flag

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 30
Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson-12 on Signal

Chapter-8 L12: "Embedded Systems - Architecture, Programming and Design" ,


2008 31
Raj Kamal, Publs.: McGraw-Hill, Inc.

You might also like