You are on page 1of 26

Lab 4 - Signals CIE 302 - Operating systems

PROCESS

A
C B paint
Media Browser
player
Signals
What is a signal?
 It is a software notification sent to a process.
 It is a short message sent to a process or a group of processes, containing the
number identifying the signal.
 Each signal notifies the process that an important event has occurred.
 Signals interrupt the process & force it to handle the received signals.
 Each signal has an integer number, as well as a mnemonic name.
Signal is identified by:
 Identifier
Name
Signal handler
Signals
What is a signal?
Signal handler: is a function that is called when the process receives a
signal.
All signals have a default signal handler.
If no handlers are assigned to a signal, the default handlers are executed.
 SIGTERM: default handler is exit ( ) system call.
SIGABRT: default handler is abort ( ) system call.
Signals User Process

signals systems calls

What is a signal? Operating System


 Signals cause the process to stop whatever it is doing at the moment, and force
the process to handle them immediately.
 Signals are generated by the OS and received and handled by a process.
How does a signal operate?:
1. A signal is sent to a process.
2. OS stops the execution of the process & forces the process to call the signal
handler function.
3. When the signal handler returns, the process continues its execution as if the
interruption never occurred.
Signals
What is a signal?
 kill –l : this command shows the list of supported signals on the system.
Signals
What is a signal?
# Signal name Comment
1 SIGHUP Hangup detected on controlling terminal or death of controlling process

2 SIGINT Interrupt from keyboard


6 SIGABRT the abort() function, This is an emergency stop.
9 SIGKILL The process was explicitly killed, no cleanup is done.
10 SIGUSR1 Left for the programmers to do whatever they want.
12 SIGUSR2
14 SIGALRM A process can request a “wake up call” from the OS by calling the alarm() function.

15 SIGTERM The process was explicitly killed, cleanup is done to the process
17 SIGCHLD Sent by a child to its parent on exit
Signals
Try signal01.c:
 This program is supposed to display a series of numbers.
 Ctrl + C kill -2 : this command will cause the program to stop running.
 An interrupt / abort signal was sent to the process.
 Ctrl + Z kill -20 kill -SIGTSTP : this command will spend the process to the
background, it is an interactive command.
Signals
Try signal01.c:
ps -u: this command provides a snapshot of the
running processes, that are associated with the bash.
 fg or % : this command will resume the working
of the process.
 kill -SIGCONT kill -18: this command resume
the execution of a process in the background.
 kill -SIGSTOP kill -19: this command stops a
process for later resumption.
 kill -SIGINT kill -2 : this command stops a
process permanently.
Signals
Sources of generating a signal:
 Hardware:
 A process attempts to access addresses outside its own address space.
 Divide by zero.

 Kernel / OS:
 Notifying the process that an I/O device for which it has been waiting is available.

 Processes:
A child process notifying its parent process that it has terminated.

 User:
Pressing keyboard sequences that generate a quit, interrupt or stop signal.
Signals
Signal actions:
• This works for most signals, but there are two signals
Ignore the signal
that can never be ignored: SIGKILL and SIGSTOP.

• The runtime environment sets up a set of default signal


Use the default handlers for each process.
action For example, the default signal handler for the TERM
signal calls the exit() system call.

• We specify to the operating system a user-supplied


Catch the signal function (user own handler) that should be called on
delivery of the signal.
Signals
Methods of generating a signal:
Using keyboard
• Ctrl-C sends SIGINT to the running process.
• Ctrl-Z sends SIGTSTP.
Using the command line
• The kill command can be used to send any signal to process.
• Example: kill -9 <PID>
Using system calls
• The kill system call can also be used to send signals.
• Example: kill(my_pid, SIGSTOP); //send to process with id = my_pid
• Example: raise(SIGSTOP); //send to itself
Signals
System calls:
 Signal handler
void handler(int signum); // it consists of:
When a signal handler executes, the parameter  Return type: void
passed to it is the number of the signal.  Handler name: catch_int
 Input: number of signal
 Signal installer
 if handler is SIG_IGN, then signals of
signal(SIGINT, handler); type signum are ignored
 if handler is SIG_DFL, then revert to
 Try signal02.c default action
 otherwise handler is user-defined
function call the signal handler.
 This installs or registers the signal
handler.
Signals
Signal handling:
 Raising a signal: it is the act
of generating a signal.
Delivering a signal: the
handler is being executed.
Pending signal: it is the state
in which the signal resides after
it is raised & before being
executed.
Signals
OS structure for signals:
 For each signal, the OS maintain 2 integers with bits
corresponding to the signal numbers.
 These 2 integers represents the pending and blocked
signals.
Each time the OS selects a process to be processed,
the pending & blocked integers are checked.
 OS executes a certain routine in the process code to
handle the pending signals.
Blocked signals can be set and cleared by using the
sigprocmask function.
Signals
Try signal03.c:
Wait(): it blocks a parent process until
its child process exits or a SIGCHLD
signal is received.
SIGSHILD:
Kernel sends this signal to the process
when the child terminates.
The default action : ignore.
Signals
Sending signals:
 A process also can explicitly send signals to itself or to another process.
 raise() and kill() function can be used for sending signals.
int raise(int signum);
Commands OS to send a signal of type signum to the current process.
Returns 0 to indicate success, non-0 to indicate failure.
int ret = raise(SIGINT); /* Process commits suicide.
*/
Signals
Sending signals:
int kill(pid_t iPid, int iSig);
Sends a iSig signal to the process whose id is iPid
Equivalent to raise(iSig) when iPid is the id of current process
pid_t iPid = getpid(); /* Process gets its id.*/
kill(iPid, SIGINT); /* Process sends itself a SIGINT
signal (commits suicide) */
If pid is greater than zero, the signal is sent to the process whose process ID is
equal to pid. If pid is 0, the signal is sent to all processes, except system
processes.
kill() returns 0 for a successful call, -1 otherwise and sets errno accordingly.
Signals
Sending signals:
There are four different conditions for the pid argument to kill():
pid > 0. The signal is sent to the process whose process ID is pid.
pid == 0 The signal is sent to all processes whose process group ID equals the
process group ID of the sender and for which the sender has permission to send the
signal.
pid < 0. The signal is sent to all processes whose process group ID equals the
absolute value of pid and for which the sender has permission to send the signal.
pid == −1. The signal is sent to all processes on the system for which the sender
has permission to send the signal.
Signals
User defined signals:
 SIGUSR 1 & SIGUSR 2: they are 2 signals that are available for users and do
not have a preassigned use.
 They are used as a mechanisms so that processes can pass data among each
other (Interprocess communication “IPC”).
Try signal04.c
Signals
Signals in Child Processes:
 After forking, the child process inherits a copy of the parent’s signal
dispositions and a copy of the parent’s signal mask.
 The Handlers defined in the parent process before the fork (before creating the
child) is inherited.
Signal handlers are inherited but not shared.
Try signal05.c
Signals
Process groups:
 Each process belongs to exactly one process group
Signals
Process groups:
 killall -g: this command kills the process group to which the process belongs.
The kill signal is only sent once per group, even if multiple processes belonging
to the same process group were found.
int killpg(pid_t pgrp, int sig); /* send the signal specified
by sig to the process group
specified by pgrp.*/
Try signal06.c
Signals
Alarm ( ):
 The alarm function sets a timer that will expire at a specified time in the future.
When the timer expires, the SIGALRM signal is generated.
 If we ignore or don’t catch this signal, its default action is to terminate the
process.
The alarm() function will return a non zero value, if another alarm has been
previously set and the value is the number of seconds remaining for the previous
scheduled alarm due to delivered.
Otherwise alarm() will return zero.
Signals
Alarm ( ):
 Generating SIGALRM using signal() function cannot be stacked.
Only one SIGALRM generation can be scheduled.
Successive calls of signal() function reset the alarm clock of the calling process.
Try signal07.c
Signals
Signal

raise, kill  Ignore


functions  Catch
 Default

You might also like