0% found this document useful (0 votes)
11 views24 pages

Slides 6 - Signals

The document provides an overview of signals in UNIX systems, detailing their purpose for inter-process communication and how they notify processes of system events. It includes a list of common signals, methods for sending signals via keystrokes, commands, and function calls, as well as information on signal handling, blocking, alarms, and interval timers. Additionally, it discusses the installation of signal handlers and provides examples of using various signal-related functions.

Uploaded by

osama.dafai18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views24 pages

Slides 6 - Signals

The document provides an overview of signals in UNIX systems, detailing their purpose for inter-process communication and how they notify processes of system events. It includes a list of common signals, methods for sending signals via keystrokes, commands, and function calls, as well as information on signal handling, blocking, alarms, and interval timers. Additionally, it discusses the installation of signal handlers and provides examples of using various signal-related functions.

Uploaded by

osama.dafai18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

SIGNALS

CONTENTS
• Introduction
• List of Signals
• Sending Signals via Keystrokes and via Commands
• Sending Signals via Function Call
• Signal Delivery and Handling
• Installing a Signal Handler
• Blocking Signals
• Alarms
• Interval Timers 2
Introduction
• Introduced in UNIX systems to simplify inter-process
communication.
• Used by the kernel to notify processes of system events.
• A signal is a short message sent to a process, or group of
processes, containing a number identifying the signal.
• A process can send a signal and interrupt another process.
• The process receiving the signal can provide a handler (function)
for handling this signal.
• Signals can be blocked, i.e., prevented from being received.
3
• A signal is a notification of an event
• Event gains attention of the OS
• OS stops the application process immediately, sending it a signal
• Kernel forces target process to “handle” signal.
• Signal handler executes to completion
• Application process resumes where it left off

4
List of Signals
Signal Value Action Comment
• SIGHUP 1 Term Hangup detected on controlling terminal or death of controlling process
• SIGINT 2 Term Interrupt from keyboard
• SIGQUIT 3 Core Quit from keyboard
• SIGILL 4 Core Illegal Instruction
• SIGABRT 6 Core Abort signal from abort(3)
• SIGFPE 8 Core Floating point exception
• SIGKILL 9 Term Kill signal
• SIGSEGV 11 Core Invalid memory reference
• SIGPIPE 13 Term Broken pipe: write to pipe with no readers

Term=terminate, Core=terminate and dump, Ignore, Stop

5
List of Signals (continued)
Signal Value Action Comment
• 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,26 Stop tty input for background process
• SIGTTOU 22,27 Stop tty output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

6
Sending Signals via Keystrokes and via Commands
• Type Ctrl-c
• Keyboard sends hardware interrupt
• Hardware interrupt is handled by OS
• OS sends a 2/SIGINT signal
• Type Ctrl-z
• Keyboard sends hardware interrupt
• Hardware interrupt is handled by OS
• OS sends a 20/SIGTSTP signal
• Issue a “kill –sig pid” command
• OS sends a sig signal to the process whose id is pid
• Example: kill -SIGINT 1234 /*
• Issue a “fg” or “bg” command to a job
• OS sends a 18/SIGCONT signal
7
Sending Signals via Function Call

kill()
int kill(pid_t iPid, int iSig);

• Sends a iSig signal to the process whose id is iPid

Example
pid_t iPid = getpid(); /* Process gets its id.*/
kill(iPid, SIGINT); /* Process sends itself a SIGINT signal */

8
Signal Handling
• Each signal type has a default handler
• Most default handlers exit the process
• A program can install its own handler for signals of any type
• Exceptions: A program cannot install its own handler for signals of type:
• 9/SIGKILL
• Default handler exits the process
• Catchable termination signal is 15/SIGTERM
• 19/SIGSTOP
• Default handler suspends the process
• Can resume the process with signal 18/SIGCONT
• Catchable suspension signal is 20/SIGTSTP
9
Signal Delivery and Handling

• When do you interrupt?


• mid instruction?
• mid system call?

10
Signal-Related Data Structures

• sigset_t stores array of signals sent to a process.


• The process descriptor has several fields for tracking sent, blocked and pending
signals.
struct sigaction {
void (*sa_handler)();/* handler address */
sigset_t sa_mask; /* blocked signal list */
int sa_flags; /* options e.g., SA_RESTART */
}

11
Installing a Signal Handler
• sigaction()
int sigaction(int iSig, const struct sigaction *psAction,
struct sigaction *psOldAction);
• iSig: The type of signal to be affected
• psAction: Pointer to a structure containing instructions on how to handle signals of
type iSig, including signal handler name and which signal types should be blocked
• psOldAction: (Irrelevant for our purposes)
• Installs an appropriate handler
• Automatically blocks signals of type iSig
• Returns 0 if successful
12
Example

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

static void myHandler(int iSig) {


printf("In myHandler with argument %d\n", iSig);
}

13
/* Using the POSIX function sigaction() */ /* Using the old C90 signal() function.
Its behavior varies between
int main(void) { Linux versions.
It is advised to avoid its use. */
struct sigaction sAction;
sAction.sa_flags = 0;
sAction.sa_handler = myHandler; int main(void) {
sigemptyset(&sAction.sa_mask);
sigaction(SIGINT, &sAction, NULL); signal(SIGINT, myHandler);

printf("Entering an infinite loop\n"); printf("Entering an infinite loop\n");


for (;;); for (;;);
return 0; return 0;
} }

14
Blocking Signals
Each process has a signal mask in the kernel
• OS uses the mask to decide which signals to deliver
• User program can modify mask with sigprocmask()
sigprocmask()
int sigprocmask(int iHow, const sigset_t *psSet, sigset_t *psOldSet);
• psSet: Pointer to a signal set
• psOldSet: (Irrelevant for our purposes)
• iHow: How to modify the signal mask
• SIG_BLOCK: Add psSet to the current mask
• SIG_UNBLOCK: Remove psSet from the current mask
• SIG_SETMASK: Install psSet as the signal mask

Functions for constructing signal sets


• sigemptyset(), sigaddset(), …
15
Blocking Signals Example
sigset_t sSet;
int main(void) {
sigemptyset(&sSet);
sigaddset(&sSet, SIGINT);
sigprocmask(SIG_BLOCK, &sSet, NULL);

}

16
Alarms
alarm()
unsigned int alarm(unsigned int tsec);
• Sends signal 14/SIGALRM after tsec seconds
• Cancels pending alarm if tsec is 0
• Uses real time
• Time spent executing other processes counts
• Time spent waiting for user input counts
• Return value is meaningless
Used to implement time-outs 17
Alarms Example 1
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

static void myHandler(int iSig) {


printf("In myHandler with argument %d\n", iSig);

/* Set another alarm. */


alarm(2);
}

18

int main(void)
{
sigset_t sSet;

/* Make sure that SIGALRM is not blocked. */


sigemptyset(&sSet);
sigaddset(&sSet, SIGALRM);
sigprocmask(SIG_UNBLOCK, &sSet, NULL);

signal(SIGALRM, myHandler);
alarm(2); /* Set an alarm. */
printf("Entering an infinite loop\n");
for (;;) ;
return 0;
} 19
Alarms Example 2
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

static void myHandler(int iSig) {


printf(“\nSorry. You took too long.\n");
exit(EXIT_FAILURE);
}

20

int main(void){
int i;
sigset_t sSet;

/* Make sure that SIGALRM is not blocked. */


sigemptyset(&sSet);
sigaddset(&sSet, SIGALRM);
sigprocmask(SIG_UNBLOCK, &sSet, NULL);
signal(SIGALRM, myHandler);
printf(“Enter a number: “);
alarm(5); /* Set a timeout of 5 seconds. */
scanf(“%d”, &i);
printf(“You entered the number %d.\n, i);
return 0;
} 21
Interval Timers
setitimer()
int setitimer(int iWhich, const struct itimerval *psValue,
struct itimerval *psOldValue);
• Sends 27/SIGPROF signal continually
• Timing is specified by psValue
• psOldValue is irrelevant for our purposes
• Uses virtual time (CPU time)
• Time spent executing other processes does not count
• Time spent waiting for user input does not count
• Returns 0 iff successful

Used by execution profilers


22
Interval Timer Example

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>

static void myHandler(int iSig) {


printf("In myHandler with argument %d\n", iSig);
}

23

int main(void)
{
struct itimerval sTimer;
signal(SIGPROF, myHandler);

/* Send first signal in 1 second, 0 microseconds. */


sTimer.it_value.tv_sec = 1;
sTimer.it_value.tv_usec = 0;

/* Send subsequent signals in 1 second, 0 microseconds intervals. */


sTimer.it_interval.tv_sec = 1;
sTimer.it_interval.tv_usec = 0;

setitimer(ITIMER_PROF, &sTimer, NULL);

printf("Entering an infinite loop\n");


for (;;) ;
return 0;
} 24

You might also like