Title: Signals in LINUX Harish' M 7th December 2006
1Signals in LINUXHarish. M7th December 2006
2Agenda
- Brief introduction
- Signals available in linux
- Different ways of sending signals
- Different ways of catching and handling signals.
- Kernel code
- Quick summary
- References
3Introduction
- What Are Signals?
- Signals are notifications sent to a process
- o C, Z, Alarm, . . .
- Each signal may have a signal handler
- o When a signal is sent to a process,
- the OS stops the process
immediately - o Handler executes and finishes
- o Resume the process
- Signals are not interrupts
- o Interrupts are sent to OS by HW
- o Signals are sent to processes by OS
- Each UNIX signal has an integer number
- and a symbolic name
- o Defined in ltsignal.hgt
4Interprocess Communication Mechanisms
- Signals
- // discuss later
- Message Queues
- Allows processes to write messages which are read
by other messages - ipc_perm data structure.
- Semaphores
- Is a location in the memory whose value can be
tested and set. - Depending on the value which is set, the process
either enters the memory location or sleeps. - Shared Memory
- Communicating through memory which appears in all
participating processes virtual memory. - Synchronize access to memory.
5Process
Receives a signal
Function
Ignore
Catch execute
Defaullt action
6Signals
Standard
Real-time
1-32
33-63
7 8(No Transcript)
9Catchable and Non-Catchable Signals
- Non-catch able signals
- o KILL
- Terminate the process immediately
- Catch able termination signal is SIGKILL
- o STOP
- Suspend the process immediately
- Catch able suspension signal is SIGSTOP
- Can resume the process with signal CONT
- Catch able signals
- o All other predefined signals
- o All user-defined signals
10Real-time signals
- 32 real time signals
- Notation
- 32 (SIGRTMIN)
- 63 (SIGRTMAX)
- SIGRTMINn
- Description
- No definite meaning
- Application defined purposes
- First three real-time signals
- Default action --- terminate
11Real-time signalscont
Sigqueue()
- Real time signals are delivered
in a guaranteed order - RT signals Vs Std signals pending for a process
- _POSIX_SIGQUEUE_MAX (32)
RT Signal
pid receiving process Sig - signal value int
or pointer
Can be queued
Instance 1
Instance 2
sent
Receiving process
Std Signal
handles
Cannot be queued
Instance 1
Instance 2
SA-SIGACTION
12Sync Vs Async
Sent to
Causes
Sync signal
Unrecoverable error
Termination
Un related
Sent to
Causes
Async signal
Executing process
Termination
13Where is information about a signal stored
Kernel
Proc Table
User area
Sig handling info
Proc Table
Sig handler
Rel information
ignored
handled
blocked
posted
Each type of signal
14Sending signals
- A user can send a signal mainly in three ways.
- Using Keyboard
- Using the shell
- Through a program
15Sending Signals from Keyboard Steps o
Pressing keys generates interrupts to OS o OS
interprets a key sequence and sends a signal to
the running process Examples o
Ctrl-C causes the OS to send an INT signal to the
running process. By default, this causes the
process to immediately terminate. o Ctrl-Z
causes the OS to send a TSTP signal to the
running process. By default, this causes the
process to suspend execution.
16Sending Signals From The Shell
kill -ltsignalgt ltPIDgt o If no signal name or
number is specified, the default is to send an
SIGTERM signal to the process, o Signal SIGKILL
or 9 is special it cannot be caught o Example
send the INT signal to process with PID
1234 kill -INT 1234 The same affect as
pressing Ctrl-C if process 1234 is
running. fg o The command is foreground o
On UNIX shells, this command will send a CONT
signal o Resume execution of the process (that
was suspended with Ctrl-Z or a command
bg) o See man pages for fg and
17Effect of signals on process groups
18(No Transcript)
19Sending Signals with kill Program
kill program sends arbitrary signal to a
process or process group Just a front-end for
the kill system call Examples ?? kill 9
24818 Send SIGKILL to process 24818 ??
kill 9 24817 Send SIGKILL to every
process in process group 24817.
gt ./kill_example gt Child1 pid24818
pgrp24817 Child2 pid24819 pgrp24817 gt ps PID
TTY TIME CMD 24788 pts/2 000000 tcsh 24818
pts/2 000002 forks 24819 pts/2 000002
forks 24820 pts/2 000000 ps gt kill -9
-24817 gt ps PID TTY TIME CMD 24788 pts/2
000000 tcsh 24823 pts/2 000000 ps gt
20Installing A Signal Handler
Predefined signal handlers o SIG_DFL default
handler o SIG_IGN Ignore the signal To
install a handler, use include
ltsignal.hgt typedef void (sighandler_t)(int) si
ghandler_t signal(int sig, sighandler_t
handler) o Handler handler will be invoked,
when signal sig occurs o Return the old handler
on success SIG_ERR on error o On most non-Linux
UNIX systems, after the handler executes, the
OS resets the handler to SIG_DFL
Example include ltsignal.hgt ... if
(signal(SIGINT, SIG_IGN) SIG_ERR) exit(1)
21Signal system call
Signal
Arg 1
Arg 2
Signum
Pointer
User defined
Pre defined
SIG_IGN
SIG_DFL
22Implementing signal system call
- include ltsignal.hgt
- void my_handler (int sig) / function prototype
/ - int main ( void )
- / Part I Catch SIGINT /
-
- signal (SIGINT, my_handler)
- printf ("Catching SIGINT\n")
- sleep(3)
- printf (" No SIGINT within 3 seconds\n")
- / Part II Ignore SIGINT /
-
- signal (SIGINT, SIG_IGN)
- printf ("Ignoring SIGINT\n")
- sleep(3)
- printf ("No SIGINT within 3 seconds\n")
23Cont
- / Part III Default action for SIGINT /
-
- signal (SIGINT, SIG_DFL)
- printf ("Default action for SIGINT\n")
- sleep(3)
- printf ("No SIGINT within 3 seconds\n")
- return 0
-
- / User-defined signal handler function /
- void my_handler (int sig)
- printf ("I got SIGINT, number d\n", sig)
- exit(0)
-
24(No Transcript)
25sigaction
- include ltsignal.hgt
- Acts as an interface for reliable signals.
- Allows the calling process to specify the action
associated with a specific signal - Structure
- struct sigaction
-
- sighandler_t sa_handler
- unsigned long sa_flags
- sigrestore_t sa_restorer
- sigset_t sa_mask
-
- sa_handler pointer to a signal catching
function - sa_flags special flags to effect the behaviour
of the signal.
26Sigactioncont
- Sa_flags
- SA_NOCLDSTOP
- Do not generate SIGCHLD when children stop or
stopped children continue. - SA_ONSTACK
- The signal shall be delivered to the calling
process on that stack. - SA_RESETHAND
- The signal shall be reset to SIG_DFL or SIG_INFO
- SA_RESTART
- SA_NODEFER
27Implementing sigaction()
include ltsignal.hgt include ltstdlib.hgt include
ltstdio.hgt include ltunistd.hgt / Simple signal
handler / void handler(int sig) if (sig
SIGINT) printf("got SIGINT\n")
Else printf("got SIGTSTP\n") int
main(void) struct sigaction sa struct
sigaction oldint
28Sigaction().(Cont)
- / Initialize the sa structure /
- sa.sa_handler handler
- sigemptyset(sa.sa_mask)
- sa.sa_flags 0
- / Set up the signal handlers/
- sigaction(SIGINT, sa, oldint)
- sigaction(SIGTSTP, sa, NULL)
- / Sleep for five seconds, or until a signal
wakes us up/ - sleep(5)
- / Restore the behavior of SIGINT just to show
it can be done. / - sigaction(SIGINT, oldint, NULL)
- return 0
-
29(No Transcript)
30(No Transcript)
31(No Transcript)
32(No Transcript)
33Summary
- Over view of signals
- Different methods used
- Different ways to handle signals
- Sending and receiving signals
- Different programming techniques
34References
- http//linuxboxadmin.com/articles/bashdaemon.php
- http//www.linuxjournal.com/article/6483
- http//www.linuxjournal.com/article/3985
- http//cs-pub.bu.edu/fac/richwest/cs591_w1/notes/w
k3_pt2.PDF - http//www.linuxforums.org/forum/linux-programming
-scripting/46017-threads-signals.html - http//www.die.net/doc/linux/man/man1/strace.1.htm
l - http//www-128.ibm.com/developerworks/linux/librar
y/l-sigdebug.html?cadgr-lnxw06SignalDebug - http//www.linux-tutorial.info/modules.php?nameTu
torialpageid289
35Questions ?