KEMBAR78
OS Lab 12 | PDF | Operating System | System Software
0% found this document useful (0 votes)
9 views13 pages

OS Lab 12

The document provides an overview of signal handling in Linux, explaining that signals are events generated by the system to notify processes of important events. It describes various types of signals, their numeric values, and how processes can handle them by either ignoring them, accepting default actions, or executing custom code. Additionally, it covers the use of the 'kill' command to send signals and the 'trap' command to manage signal responses in shell scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

OS Lab 12

The document provides an overview of signal handling in Linux, explaining that signals are events generated by the system to notify processes of important events. It describes various types of signals, their numeric values, and how processes can handle them by either ignoring them, accepting default actions, or executing custom code. Additionally, it covers the use of the 'kill' command to send signals and the 'trap' command to manage signal responses in shell scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Operating System (Lab) Lab – 12

Fall 2022 Signal Handling

Linux signals

A signal is an event generated by the UNIX and Linux systems in


response to some condition. Upon receipt of a signal, a process may
take action.
A signal is just like an interrupt; when it is generated at the user level,
a call is made to the kernel of the OS, which then acts accordingly.

Signals are software interrupts.

A robust program need to handle signals. This is because signals are a


way to deliver asynchronous events to the application.

A user hitting ctrl+c, a process sending a signal to kill another


process etc are all such cases where a process needs to do signal han-
dling.

In Linux, every signal has a name that begins with characters SIG. For
example:

 A SIGINT signal that is generated when a user presses ctrl+c.


This is the way to terminate programs from terminal.
 A SIGALRM is generated when the timer set by alarm function
goes off.
 A SIGABRT signal is generated when a process calls the abort
function.
etc

Signals, to be short, are various notifications sent to a process in or-


der to notify it of various "important" events. By their nature, they in-
terrupt whatever the process is doing at this minute, and force it to
handle them immediately. Each signal has an integer number that
represents it (1, 2 and so on), as well as a symbolic name that is usu-
ally defined in the file /usr/include/signal.h or one of the files included
by it directly or indirectly (HUP, INT and so on. Use the command 'kill
-l' to see a list of signals supported by your system).

Each signal may have a signal handler, which is a function that gets
called when the process receives that signal. The function is called in
"asynchronous mode", meaning that no where in your program you
have code that calls this function directly. Instead, when the signal is
sent to the process, the operating system stops the execution of the
process, and "forces" it to call the signal handler function. When that

Page 1 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

signal handler function returns, the process continues execution from


wherever it happened to be before the signal was received, as if this
interruption never occurred.

Introduction to Signals

You already know that you can stop the execution of most running
programs by interrupting them from the keyboard, usually with Ctrl-
C.

Ctrl C

For interrupt we press Ctrl+C.


New file

Page 2 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

For one second delay

Write Sleep 1 for one second delay and exceute it and see its ouput.
You can interrupt this execution by pressing Ctrl+C.

Page 3 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

What actually happens is that the part of the Linux kernel software
that controls the keyboard, sends a message to the running process
through CPU. This message is called a signal. Infact there are about
30 different signal types that can be sent to a Linux process, each of
which is generated by some specific hardware or software condition.
In addition to the normal mechanisms for generating and sending
signals, the kill command can also be used to send any signal to a
process at will.
Each of the signal is given a small integer value which is used as
command line switch to kill, along with process ID of the process to
which the signal should be sent. The general format of the kill
command is:
Kill [-sig] pid
Where sig is the signal to send and pid is the process ID to which it
should be sent. The square brackets just indicate that the
specification of the pid is optional – the brackets themselves should
not be type. If a signal number is not specified to the kill command
then the terminate signal is used by default.

Some of the more common signals and their numeric values are:
Signal Value Notes
SIGHUP 1 Used to kill your processes when you logout
SIGINT 2 Generated from keyboard (Ctrl-C)
SIGQUIT 3 Generated from keyboard (Ctrl-\)
SIGILL 4 Illegal Instruction

Page 4 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

SIGFPE 8 Floating point exception


SIGKILL 9 Sure process termination – cannot be ignored
SIGALRM 14 Generated at the end of an alarm() system call
SIGTERM 15 Default signal sent by kill command

You can fill singnal list by typing command kill -l

When a process receives a signal, it can normally do one of three


different things;
1. Ignore the signal;
2. Accept the signal’s default action; defined in OS.
3. Execute some code to deal with the signal.

Kill Command

For Kill command, execute this program

Open new command prompt to write kill command.


But to execute kill command, you need process id.
So, write this command:
echo “process id $$”

Page 5 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

Exceute this

For terminate command

Page 6 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

Trap a Signal:

From inside a shell script it is possible to trap a signal and then to get
a sequence of commands executed when that signal is received. This
is done with the trap command. There are three basic forms for the
trap command, one for each of the tree different signal response
types:
Trap "commands" signal_list
Trap will execute the commands in the quotes when any of the signals
corresponding to the numbers in the signal_list is received by the
script.

To restore signals to their default action, use the trap command in the
form:
Trap signal_list

Without any commands specified.


The third form, which allows signals to be ignored, has the format:

Page 7 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

Trap " " signal_list


Which just specifies a null commands string.

Signal Handling in C:

The signal() function:


C++ signal-handling library provides function signal to trap
unexpected events. Following is the syntax of the signal() function:
void (*signal (int sig, void (*func)(int)))(int);
Keeping it simple, this function receives two arguments: first
argument as an integer which represents signal number and second
argument as a pointer to the signal-handling function.

Let us write a simple C++ program where we will catch SIGINT


signal using signal() function. Whatever signal you want to catch in
your program, you must register that signal using signal function and
associate it with a signal handler.
Program using signals:

Page 8 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

Exercise 2:
Type the following and execute it.

Press <ctrl-c> while the program is running and see what happens.

Let’s discuss the program. The first question that comes to mind is…
why is there a call to the signal function inside the signal handler (this
is what we will be calling the catcher function)? When the signal is
caught once, the function which handles the signal is reset, and needs
to be set again. If we don’t do this, the default action will occur. Try
commenting out the function and see what happens. The working of
the program is simple. It will run normally and print working after
every second. When the signal is caught, the normal flow of the
program is interrupted. The catcher function is executed and then the
normal flow of the program resumes.

You will notice that commenting the signal line made no difference.
Does that make everything I just mentioned wrong? No. The original
UNIX signal () would reset the handler and System V (and the old
Linux kernel and libc4,5) do the same. On the other hand, BSD does
not reset the handler. But remember, that if you’re working on UNIX
and Linux (before version 6.0, not sure about 6.1), the function has to
be set again. All our code will assume that this is how the signal
function works.

Example

Page 9 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

For Delay

Page 10 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

Signal handler Function

Page 11 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

Page 12 of 13
Operating System (Lab) Lab – 12
Fall 2022 Signal Handling

Page 13 of 13

You might also like