Transcript Signals

Alarms & Signals
1
B. RAMAMURTHY
CH. 8
Amrita-UB-MSES-cse524-2016-10
3/29/2016
Inter-process communication
2
 You created new processes using fork() system call
 You delegated work to the newly created process





using exec() system call
How can communicate between processes?
For sending messages and data? pipe() system call
For signaling /interrupting each other? kill() system
call
We will kill() signals and also system alarm and
signals
Chapter 8
Amrita-UB-MSES-cse524-2016-10
3/29/2016
Multitasking
3
 Multitasking relies on system functions for realizing
interprocess communication and synchronization.
 Ch.8 discusses semaphores (we discussed it last
class), locks (we will not), pipes (last presentation),
signals (this presentation) and sockets (we will not).
 The data sharing among tasks and issues with it are
discussed with Mars Pathfinder as example at the
beginning of ch.8 (Read it).
Amrita-UB-MSES-cse524-2016-10
3/29/2016
Intercept Signals
4
 These are a type of software interrupt that tasks or
devices may fire at other tasks.
 kill(pid,signal#) is the command for initiating an
interrupt!
 Two parameters : destination process pid and
signal# for the action to be taken
 Very efficient.
Amrita-UB-MSES-cse524-2016-10
3/29/2016
Examples
Pag
e5
 Consider g++ myProg.c
 You want to kill this process after you started the compilation..hit
cntrl-C
 Consider execution of a program called “badprog”
>badprog
It core dumps .. What happened? The error in the program results in a
signal to kernel to stop and dump the offending code
 Consider “kill –p <pid>”
 Kill issues a termination signal to the process identified by the pid
3/29/2016
Termination of a process
Pag
e6
• Normal completion, time limit exceeded, memory
•
•
•
•
•
unavailable
Bounds violation, protection error, arithmetic error,
invalid instruction
IO failure, Operator intervention, parent termination,
parent request, killed by another process
A number of other conditions are possible.
Segmentation fault : usually happens when you try
write/read into/from a non-existent
array/structure/object component. Or access a pointer to a
dynamic data before creating it. (new etc.)
Bus error: Related to function call and return. You have
messed up the stack where the return address or
parameters are stored.
3/29/2016
Process Termination
 Process executes last statement and asks the operating system
to delete it (exit)
 Output data from child to parent (via wait)
 Process’ resources are deallocated by operating system
 Parent may terminate execution of children processes (abort)
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 If parent is exiting
 Some operating system do not allow child to continue if
its parent terminates
 All children terminated - cascading termination
Page
7
3/29/2016
Signals
Pag
e8
 Signals provide a simple method for transmitting software







interrupts to UNIX process
Signals cannot carry information directly, which limits their
usefulness as an general inter-process communication
mechanism
However each type of signal is given a mnemonic name; Ex:
SIGINT
See signal.h for others
SIGHUP, SIGINT, SIGILL, SIGTRAP, SIGFPE, SIGKILL
SIGALRM (sent by kernel to a process after an alarm timer
has expired)
SIGTERM
signal (signal id, function) simply arms the signal
3/29/2016
Signal
Value
Action Comment
------------------------------------------------------------------------SIGHUP
1
Term Hangup detected on controlling terminal
or death of controlling process
SIGINT
2
Term Interrupt from keyboard
SIGQUI
3
Core Quit from keyboard
SIGILL
4
Core Illegal Instruction
SIGABR
6
Core Abort signal from abort(3)
SIGFP
8
Core Floating point exception
SIGKILL 9
Term Kill signal
SIGSEG
11
Core Invalid memory reference
SIGPIPE 13
Term Broken pipe: write to pipe with no readers
SIGALRM 14
Term Timer signal from alarm(2)
SIGTERM 15
Term Termination signal
SIGUSR1
30,10,16 Term User-defined signal 1
SIGUSR2
31,12,17 Term User-defined signal 2
SIGCHLD
20,17,18 Ign
Child stopped or terminated
SIGCONT
19,18,25 Cont Continue if stopped
SIGSTOP
17,19,23 Stop Stop process
SIGTSTP
18,20,24 Stop Stop typed at tty
SIGTTIN
21,21,26 Stop tty input for background process
SIGTTOU
22,22,27 Stop tty output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
Page
9
3/29/2016
Realtime signals
Pag
e 10
 Linux supports real-time signals as originally defined
in the POSIX.1b real-time extensions (and now
included in POSIX.1-2001). Linux supports 32 realtime signals, numbered from 32 (SIGRTMIN) to 63
(SIGRT- MAX)
 Main difference is that these are queued and not lost.
 Realtime signals are delivered in guaranteed order.
3/29/2016
Intercept Signals
Pag
e 11
Tas
k1
Tas
k2
Two essential parameters are destination process identifier
and the signal code number: kill (pid, signal)
Signals are a useful way of handling intermittent data arrivals o
conditions.
3/29/2016
Handling Signals
Pag
e 12
 Look at the examples:
 Catching SIGALRM
 Ignoring SIGALRM
 sigtest.c
 sigHandler.c
 pingpong.c
 See /usr/include/sys/iso/signal_iso.h for signal
numbers
3/29/2016
Signals and Alarms
Pag
e 13
#include <signal.h>
unsigned int alarm( unsigned int seconds );
alarm(a); will start a timer for a secsonds and will interrupt the
calling process after a secs.
time(&t); will get you current time in the variable t declared as
time_t t
ctime(&t); will convert time to ascii format
Alarm has a sigaction function that is set for configuring the alarm
handler etc.
sigaction(SIGALRM, &act, &oldact) ; the third paramter is for old
action configuration
3/29/2016
Sample programs
Pag
e 14
 Starting new tasks in linux: page 165
 Programs in pages: 174-180 on signals and alarms
 See demos directory for the code
 See page 175 for the second program
 See page 178 … for the third program
3/29/2016
Pingpong
Pag
e 15
Pare
nt
PSIG 43
Chil
d
CSIG 42
3/29/2016
Observe in pingpong.c
Pag
e 16
 pause(): indefinite
 sleep(): sleep is random/finite time
 While loop
 Signal handlers
 Re-arming of the signals
3/29/2016
Volatile
Pag
e 17
 A variable should be declared volatile whenever its
value could change unexpectedly. In practice, only
three types of variables could change:



Memory-mapped peripheral registers
Global variables modified by an interrupt service routine
Global variables within a multi-threaded application
 Registers in devices are abstracted for programmatic
access as “volatile” type
3/29/2016
Summary
Pag
e 18
 We studied signals and alarms and their
specification and example programs
3/29/2016
Lets Review
19
 Ch.4 81 -87 : Details on cyclic executives
 Ch.5 FSM as design tool : p.96 FSM for a toaster
 Ch.7: runtime support environment: p.153 different
implementation environments
 7.15 starting new tasks using fork
 Ch.8 Task communications and signals… starts off
with Mars Pathfinder example
Amrita-UB-MSES-cse524-2016-10
3/29/2016