Sigaction
   HOME

TheInfoList



OR:

In computing, sigaction is a function API defined by
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
to give the programmer access to what should be a program's behavior when receiving specific OS
signals In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The ''IEEE Transactions on Signal Processing'' ...
.


General

In
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating systems, one means of
inter-process communication In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. Typically, applications can use IPC, categori ...
is through signals. When an executing unit (
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management *Business process, activities that produce a specific se ...
or thread) receives a signal from the OS, it should react in some way defined by the datasheet and the conventional meaning of this signal (i.e. by dumping its data, stopping execution, synchronizing something...). The sigaction() system call is used to declare the behavior of the program should it receive one particular non-system-reserved signal. This is done by giving along with the system call a structure containing, among others, a function pointer to the signal handling routine. Some predefined signals (such as SIGKILL) have locked behavior that is handled by the system and cannot be overridden by such system calls.


sigaction structure

The POSIX standard requires that the sigaction structure be defined as below in the header file and it should contain at least the following fields: struct sigaction ; Implementations are free to define additional, possibly non-portable fields. The sa_handler member specifies the address of a function to be called when the process receives the signal. The signal number is passed as an integer argument to this function. The sa_mask member specifies additional signals to be blocked during the execution of signal handler. sa_mask must be initialized with sigemptyset(3). The sa_flags member specifies some additional flags. sa_sigaction is an alternate signal handler with different set of parameters. Only one signal handler must be specified, either sa_handler or sa_sigaction. If it is desired to use sa_sigaction instead of sa_handler, SA_SIGINFO flag must be set.


Replacement for signal()

The sigaction() function provides an interface for reliable signals in replacement of the unreliable signal() function. Signal handlers installed by the signal() interface will be uninstalled immediately prior to execution of the handler. Permanent handlers must therefore be reinstalled by a call to signal() during the handler's execution, causing unreliability in the event a signal of the same type is received during the handler's execution but before the reinstall. Handlers installed by the sigaction() interface can be installed permanently and a custom set of signals can be blocked during the execution of the handler. These signals will be unblocked immediately following the normal termination of the handler (but not in the event of an abnormal termination such as a C++ exception throw.)


Use in C++

In C++, the try catch programming structure may be (depending on host platforms) based on signalling. To catch signals translated into C++ exceptions, special compiler switches may be necessary on some platforms such as -fnon-call-exceptions for GCC and the Intel C Compiler.Option -fnon-call-exceptions
/ref>


Example

#include #include #include #include #include #include #define NUMCHLDS 10 void sigchld_handler(int, siginfo_t*, void*); sig_atomic_t nexitedchlds = 0; int main(int argc, char *argv[]) void sigchld_handler(int signo, siginfo_t *sinfo, void *context)


References


External links

* {{man, sh, sigaction, SUS, examine and change a signal action C POSIX library Unix signals