3. 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.
5. 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.
6.
7. 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();
}
8. 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);
}
11. 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
12. void INThandler(int sig)
{
char c;
signal(SIGINT, SIG_IGN);
signal(SIGALRM, SIG_IGN);
printf(“INT handler: i = %d and j = %dn”, 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
}
13. void ALARMhandler(int sig)
{
signal(SIGINT, SIG_IGN);
signal(SIGALRM, SIG_IGN);
printf(“ALARM handler: alarm signal receivedn”);
printf(“ALARM handler: i = %d and j = %dn”, i, j);
alarm(SECOND); //set Alarm clock
signal(SIGINT, INThandler);
signal(SIGALRM, ALARMhandler);
}
14. 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.nn”);
}
15. 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 = %ldn”, i, fact);
}
}
16. 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);
17. 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.
18. Process Groups
• What happens when you Control-C a program
that created several children?
• Typically the program and its children terminate
• Why the children?
19. 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.
20. • 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.