Linux Device Drivers

Linux Device Drivers


Linux
Modules
Character drivers
IO & Memory
Linux Kernel
Process Management
Process Address space

Linux Scheduler
Memory Management
Interrupts
Signals
System Calls
Kernel Synchronization
Linux Inter Process Communications




Serial Ports
Parallel Ports
Introduction to Hardware
Linux Timers
DMA in Linux
Linux Threads
Linux Thread Synchronization

Linux Multi Threading
Debugging in Linux
GDB GNU Debugger
KDB Kernel Debugger
KGDB Kernel GNU Debugger
Example Ethernet Driver




Signals

Signals

A signal is a notification to a process that an event has occurred.

Signals usually occur asynchronously. Signals can be sent:

     by one process to another process (or to itself)

     by the kernel to a process.

 

Signals are defined in header file <signals.h>.

You can list a system's set of signals using the kill command (kill -l):

1) SIGHUP2) SIGINT3) SIGQUIT4) SIGILL

5) SIGTRAP 6) SIGIOT7) SIGBUS8) SIGFPE

9) SIGKILL 10) SIGUSR111)SIGSEGV 12) SIGUSR2

13) SIGPIPE 14) SIGALRM15)SIGTERM 17) SIGCHLD

18) SIGCONT 19) SIGSTOP20)SIGTSTP 21) SIGTTIN

22) SIGTTOU 23) SIGURG24)SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF28)SIGWINCH 29)SIGIO

30) SIGPWR

 

Some important signals are:

SIGALRM: Alarm clock

SIGKILL: Kill

SIGUSR: User defined signal

SIGCLD: Death of a child process

SIGILL: Illegal Instruction

SIGINT: Interrupt character

 

There are five conditions that generate signals.

 

1.     The kill system call allows a process to send a signal to another process or to itself

int kill (intpid, intsig);

2.     The kill command is also used to send signals.

3.     Certain terminal characters generate signals. For example, CTRL-C generates a SIGINT signal.

4.     Certain hardware conditions generate signal. For example, floating point arithmetic errors generate a SIGFPE error.

5.     Certain software conditions that are noticed by the kernel cause signals to be generated.

 

A process can do followings with a signal:

 

1.     A process can provide a function that is called whenever a specific type of a signal occurs. This function, called a signal handler, can do whatever the process wants to handle the condition.

2.     A process can choose to ignore a signal. All signals, other than SIGKILL can be ignored.

3.     A process can allow the default to happen. Normally a process is terminated on receipt of a signal, with certain signals generating a core image of the process in its current working directory.

 

A process specifies how it wants a signal handled by calling the signal system call

 

#include <signal.h>

int(*signal(intsig, void (*func) (int))) (int);

 

To have the function myintrcalled when the SIGINT signal is generated,

signal(SIGINT, myintr);

 

POSIX Signals

The Standard C signal() function has several problems:

 

     There is no way to determine the current action for a signal. This means that a called function cannot use the signal() function without disturbing the caller. There is no way to save and restore signal state.

 

     When a signal occurs, there is no way to block other signals to keep the signal handler from being interrupted.

 

     There is no way for an implementation to cleanly extend the signal Mechanism

 

The POSIX-defined signal functions correct these problems.

 

The main function for manipulating signals is sigaction().

It is defined as:

int sigaction(intsig, const structsigaction*act,

struct sigaction* oact);

 

The sigaction structure is defined in the header <signal.h> to include the following members:

 

void(*)() sa_handler SIG_DFL for the default action.

or

SIG_IGN to ignore this signal

or

pointer to the signal-catching function.

 

sigset_tsa_mask Additional signals to be blocked during the execution of the signal-catching function.

intsa_flags This member is used only for the SIGCHLD signal. If the value SA_NOCLDSTOP is used, then SIGCHLD will not be generated when children stop.

 

Signal Sets

 

The type sigset_t holds some sets of signals. On some systems, it may be a simple int with one bit per signal. On other systems, it may be a complex structure with version numbers, lists of signals, or other extensions.

Because an application program does not know the format of a signal set, several functions are provided to operate on signal sets.

 

The sigemptyset() Function

int sigemptyset(sigset_t*set);

Is used to initialize the signal set pointed to by set. All signals are excluded from the set

 

The sigfillset() Function

This is the same as sigemptyset(), except all signals are included.

 

The sigaddset() Function

int sigaddset(sigset_t*set, const intsigno);

adds the signal specified by signo to the set pointed to by set.

This function will return zero if signo is valid. It will return 1 and set errno to EINVAL if the signal number is invalid.

 

The sigdelset() Function

This function is the same as sigaddset() except that the signal is removed from the set.

 

The sigprocmask() Function

Blocked signals are signals that are temporarily prevented from delivery. It can be useful to inhibit signals during execution of a critical section of code. The list of blocked signals can also be changed using the sigprocmask() function