Docsity

Signal Handling-Network Programming-Lecture Slides, Slides of Network Programming

Partial preview of the text

Download Signal Handling-Network Programming-Lecture Slides and more Slides Network Programming in PDF only on Docsity!

Network^ Programming(Lecture

Signal^ Handling • A signal is a notification^ to^ a^ process

that^ an event^ has^ occurred. • Signals^ are^ sometimes

called^ software^ interrupts.

-^ Signals^ usually^

occur^ asynchronously,

mean^ that^ a process^ doesn't^ know

ahead^ of^ time^ exactly

when a^ signal^ will^ occur. • Signals^ can^ be^ sent^ –^ By^ one^ process^ to

another^ process

-^ By^ the^ kernel^ to^ a

process

Disposition

-^ Every^ signal^ has

a^ disposition,^ which

is^ also called^ the^ action

associated^ with

the^ signal.

-^ We^ set^ the^ disposition

of^ a^ signal^ by^ calling the^ sigaction function.

Three^ choices

for^ disposition

-^ Provide^ a^ function

that^ is^ called^ whenever

a specific^ signal^ occurs. • Ignore^ a^ signal • Set^ the^ default^ disposition

for^ a^ signal

ignore^ a^ signal • Ignore a signal^ by^ setting^ its^

disposition^ to SIG_IGN.

default^ disposition • set the default^ disposition^ for

a^ signal^ by^ setting its^ disposition^ to^

SIG_DFL.

-^ The^ default^ is^ normally

to^ terminate^ a^ process

on receipt^ of^ a^ signal,

with^ certain^ signals

also generating^ a^ core

image^ of^ the^ process

in^ its current^ working^ directory. • There^ are^ a^ few^ signals

whose^ default^ disposition is^ to^ be^ ignored:^ SIGCHLD

and^ SIGURG^ (sent

on the^ arrival^ of^ out‐

of‐band^ data)

Common^ kill

signals

Signal^ name^ Signal value^ Effect SIGHUP^1

Hangup SIGINT^2

Interrupt^ from keyboard SIGKILL^9

Kill^ signal SIGTERM^15

Termination^ signal SIGSTOP^ 17,19,

Stop^ the^ process

SIGKILL^ and

SIGSTOP

-^ These^ signal^ cannot

be^ caught^ or^ ignored.

-^ When^ killing^ a^ process

or^ series^ of^ processes,

it^ is^ common sense^ to^ start^ trying^

with^ the^ least^ dangerous

signal, SIGTERM. • That^ way,^ programs^ that

care^ about^ an^ orderly

shutdown get^ the^ chance^ to^ follow

the^ procedures^ that

they^ have been^ designed^ to^ execute

when^ getting^ the^ SIGTERM signal,^ such^ as^ cleaning

up^ and^ closing^ open

files.

-^ If^ you^ send^ a^ SIGKILL

to^ a^ process,^ you^ remove

any^ chance for^ the^ process^ to^ do

a^ tidy^ cleanup^ and^ shutdown,

which might^ have^ unfortunate

consequences.

Simplify^ function

prototype^ using typedef

-^ The^ normal^ function

prototype^ for^ signal is^ complicated^ by the^ level^ of^ nested^ parentheses.^ –^ void^ (*signal^ (int signo,

void^ (*func)^ (int)))^ (int);

-^ To^ simplify^ this,^ we

define^ the^ Sigfunc type

in^ unp.h header as:^ –^ typedef void^ Sigfunc(int); • stating^ that^ signal^ handlers

are^ functions^ with^ an

integer argument^ and^ the^ function

returns^ nothing^ (void).

The function^ prototype^ then

becomes

-^ Sigfunc *signal^ (int signo,

Sigfunc *func);

-^ A^ pointer^ to^ a^ signal

handling^ function^ is the^ second argument^ to^ the^ function,

as^ well^ as^ the^ return

value^ from the^ function.

Set^ handler

-^ The^ sa_handler member

of^ the^ sigaction structure^ is^ set^ to

the^ func argument.

Set^ SA_RESTART

flag

-^ SA_RESTART^ is

an^ optional^ flag.

When^ the^ flag is^ set,^ a^ system^ call

interrupted^ by^ this

signal will^ be^ automatically

restarted^ by^ the

kernel.

-^ If^ the^ signal^ being

caught^ is^ not^ SIGALRM,

we specify^ the^ SA_RESTART

flag,^ if^ defined.

-^ Some^ older^ systems,

notably^ SunOS^ 4.x, automatically^ restart

an^ interrupted^ system call^ by^ default^ and

then^ define^ the complement^ of^

this^ flag^ as^ SA_INTERRUPT.

Call^ sigaction • We call sigaction and^ then^ return

the^ old action^ for^ the^ signal

as^ the^ return^ value

of^ the signal^ function.