signals

Work in Progress

Summary

POSIX signals

SignalMeaningDefault Action
SIGINTinterrupt from keyboard (ctrl+c)terminate
SIGTERMtermination requestterminate
SIGKILLimmediate killterminate (cannot be caught)
SIGSTOPstop processstop (cannot be caught)
SIGCONTcontinue stopped processcontinue
SIGALRMtimer expiredterminate
SIGCHLDchild process state changeignore by default
SIGSEGVillegal memory access (seg fault)terminate, dump core

POSIX syscalls for signals

SyscallIncludeFunction
kill(pid, sig)<signal.h>sends a signal to a process or process group
signal(sig, handler)<signal.h>installs a simple signal handler for a signal
raise(sig)<signal.h>sends a signal to the calling/current process
pause()<unistd.h>suspends the process until a signal is received
alarm(time)<unistd.h>schedules a SIGALRM signal after a specified number of seconds

Concept

  • asynchronous notification regarding an event
  • a software interrupt sent to a process
  • handling:
    • default handler
    • user supplied handler
  • sent by:
    • another process
    • OS
    • the same process
c
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void myOwnHandler(int signo) {
	if (signo == SIGSEGV){
		printf("Memory access blows up!\n");
		exit(1);     
	}
}

int main() {
	int *ip = NULL;
	if (signal(SIGSEGV, myOwnHandler) == SIG_ERR) // replace the default handler
		printf("Failed to register handler\n");
	
	*ip = 123; // causes seg fault
	
	return 0;
}