The SIGIO signal informs a process as soon as from a socket (or generally from a file descriptor) data can be read.
In order for a process to be able to react to the SIGIO signal, you must take the following precautions in the program code:
Define a signal handling function using the sigaction() function (see the manual "C Library Functions for POSIX Applications").
Set either the process number or the process group number to allow your process number or process group number to be informed of pending inputs. Set the process number or process group number with the fcntl() function. The default process group of a socket is group 0.
Allow asynchronous notification of waiting input/output requests with another fcntl() call.
The following program code illustrates how a process is prepared for receiving SIGIO signals. When data can be read or written on the socket s, the process is informed asynchronously by a call of the user-defined SIGIO signal handler function io_handler().
#define _XOPEN_SOURCE_EXTENDED #define ERROR_EXIT(M) perror(M); exit(1) #include <signal.h> #include <fcntl.h> #include <sys/file.h> #include <unistd.h> #include <stdio.h> ... void io_handler(int sig, siginfo_t *si, void *ucp); int s; struct sigaction act; ... if (sigaction(SIGIO, NULL, &act) != 0) { ERROR_EXIT("sigaction/GET"); } act.sa_sigaction = io_handler; if (sigaction(SIGIO, &act, NULL) != 0) { ERROR_EXIT("sigaction/SET"); } if (fcntl(s, F_SETOWN, getpid()) < 0) { ERROR_EXIT("fcntl/F_SETOWN"); } if (fcntl(s, F_SETFL, FASYNC) < 0) { ERROR_EXIT("fcntl/F_SETFL=FASYNC"); }