21
UNIX SIGNAL PROGRAMMMING

Unix signals

Embed Size (px)

Citation preview

Page 1: Unix signals

UNIXSIGNAL

PROGRAMMMING

Page 2: Unix signals

I’ll skip the intro…..

Page 3: Unix signals

Some Functions In signal.h

• signal()Syntax: int (*signal(int sig, void (*func)()))()

• sighold()Syntax: int sighold(int sig)

• sigrelse() Syntax: “”• sigignore() Syntax: “”• sigpause() Syntax: “”

Note: All the functions except signal() deals with the signal mask of a process.

Page 4: Unix signals

signal(), Definition:

#include <signal.h>

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);

Page 5: Unix signals

Description

The signal() system call installs a new signal handler for the signal with number signum. The signal handler is set to sighandler which may be a user specified function, or either SIG_IGN or SIG_DFL.

Page 6: Unix signals
Page 7: Unix signals

Example 1:#include <stdio.h>#include <signal.h>

void INThandler(int);

void main(void){if (signal(SIGINT, SIG_IGN) != SIG_IGN)signal(SIGINT, INThandler);while (1)pause();}

Page 8: Unix signals

void INThandler(int sig){

char c;signal(sig, SIG_IGN);printf(“Did you hit Ctrl-C?\nDo you really want to quit

[y/n]?”);c = getchar();if (c == ‘y’ || c = ‘Y’)

exit(0);else

signal(SIGINT, INThandler);}

Page 9: Unix signals

How to handle mutiple Signals?

signal(SIGINT, INThandler);signal(SIGQUIT, QUIThandler);

void INThandler(int sig){

// SIGINT handler code}

void QUIThandler(int sig){

// SIGQUIT handler code}

Page 10: Unix signals

ORsignal(SIGINT, SIGhandler);signal(SIGQUIT, SIGhandler);

void SIGhandler(int sig){

switch (sig) {case SIGINT: // code for SIGINTcase SIGQUIT: // code for SIGQUITdefault: // other signal types

}}

Page 11: Unix signals

Example 2:#include <stdio.h>#include <stdlib.h>#include <signal.h>

#define MAX_i 10000#define MAX_j 20000#define MAX_SECOND (2)

void INThandler(int);void ALARMhandler(int);int SECOND, i, j

Page 12: Unix signals

void INThandler(int sig){

char c;signal(SIGINT, SIG_IGN);signal(SIGALRM, SIG_IGN);printf(“INT handler: i = %d and j = %d\n”, i, j);printf(“INT handler: want to quit [y/n]?”);c = tolower(getchar());if (c == ‘y’) {printf(“INT handler: done”); exit(0);}signal(SIGINT, INThandler);signal(SIGALRM, ALARMhandler);alarm(SECOND); //Unix System Call

}

Page 13: Unix signals

void ALARMhandler(int sig){

signal(SIGINT, SIG_IGN);signal(SIGALRM, SIG_IGN);printf(“ALARM handler: alarm signal

received\n”);printf(“ALARM handler: i = %d and j = %d\

n”, i, j);alarm(SECOND); //set Alarm clocksignal(SIGINT, INThandler);signal(SIGALRM, ALARMhandler);

}

Page 14: Unix signals

void main(int argc, char *argv[]){

long sum;SECOND = abs(atoi(argv[1]));signal(SIGINT, INThandler);signal(SIGALRM, ALARMhandler);alarm(SECOND);for (i = 1; i <= MAX_i, i_++) {sum = 0;for (j = 1; j <= MAX_j; j++)sum += j;}printf(“Computation is done.\n\n”);

}

Page 15: Unix signals

How to raise a signal from a process?

void main(void){

long fact;signal(SIGUSR1, SIGhandler);for (prev_fact=i=1; ; i++, prev_fact = fact) {fact = prev_fact * i;if (fact < 0)raise(SIGUSR1); //……use raise()else if (i % 3 == 0)printf(“ %ld = %ld\n”, i, fact);}

}

Page 16: Unix signals

To send a signal to a Process

Use Unix system call kill() to send a signal to another process:

int kill(pid_t pid, int sig);

Page 17: Unix signals

The Unix kill Command

• The kill command can also be used to send a signal to a process:

kill –l /* list all signals */kill –XXX pid1 pid …… pid

• In the above XXX is the signal name without the initial letters SIG.

• kill –KILL 1357 2468 kills process 1357 and 2468.

• kill –INT 6421 sends a SIGINT to process 6421.• A kill without a signal name is equivalent to

SIGTERM.• -9 is equal to –SIGKILL.

Page 18: Unix signals

Process Groups

• What happens when you Control-C a program that created several children?

• Typically the program and its children terminate

• Why the children?

Page 19: Unix signals

In addition to having unique ID, process also belongs to a process group.

• Several processes can be members of the same process group.

• When a process forks, the child inherits its process group from its parent.

• A process may change its process group to a new value by using setpgid ().

• When a process execs, its process group remains the same.

Page 20: Unix signals

• System Call: pid_t setpgid (pid_t pid, pid_t pgrpId)

setpgid () sets the process group ID of the process with PID pid to pgrpId.

• System Call: pid_t getpgid (pid_t pid)

getpgid () returns the process group ID of the process with PID pid.

Page 21: Unix signals

THANK YOU