Types of Signals in Linux

Signals

1. Types of Signals

1.1 Special Keys in Terminal

  • ctl + c generates SIGINT
  • ctl + z generates SIGTSTP
  • ctl + \ generates SIGQUIT
  • kill -9 pid generates SIGKILL

1.2 Hardware Exceptions

  • Illegal Instruction
  • Privilege Instruction
  • Division by Zero
  • Segmentation Fault
  • Bus Error

1.3 Software Exceptions

  • Illegal Memory Access
  • Out of Bounds Access
  • Stack Overflow

1.4 Operating System Exceptions

  • Illegal System Call
  • Invalid System Command
  • Insufficient System Resources

1.5 User-Defined Signals

  • kill -sig pid
  • kill -SIGINT pid
  • kill -15 pid
  • kill -SIGTERM pid
  • kill -1 pid

Signal Functions

kill(pid_t pid,int sig)

raise(int sig) function

abort(void) function

Signal Generation

  • Hardware Interrupt
  • System Call
  • Software Interrupt

Signal Capture and Handling

  • signal function
#include <signal.h>
void (*signal(int signum, void (*func)(int)))(int);
  • sigaction function
#include <signal.h>
int sigaction(int signum, const struct sigaction *act, struct sigaction *oact);
struct sigaction {
    void (*sa_handler)(int);
    sigset_t sa_mask;
    int sa_flags;
    void (*sa_sigaction)(int, siginfo_t *, void *);
};

Signal Set Handling Functions

sigset_t is a signal set, signal set operation functions

  • sigemptyset initializes the signal set to empty
  • sigfillset initializes the signal set to all signals
  • sigaddset adds a signal to the signal set
  • sigdelset removes a signal from the signal set
  • sigismember checks if a signal is in the signal set
  • sigprocmask blocks/unblocks the signal set
  • sigpending checks the currently blocked signals
  • sigsuspend blocks the signal set until a signal in the set is caught
  • sigwait waits for a signal in the signal set
  • sigwaitinfo waits for a signal in the signal set and retrieves detailed information about the signal
  • sigtimedwait waits for a signal in the signal set and retrieves detailed information about the signal, while allowing a timeout
  • sigaction captures signals
  • siginterrupt sets whether the signal interrupts system calls

PCB saves: Pending Signal Set

Blocked Signal Set

Delivered State: Signal generated and responded to; Pending State: Signal generated but not responded to

#include <signal.h>
#include <stdio.h>

void printsigset(const  __sigset_t *set)
{
    int i;
    // Iterate through signal set
    for (i = 1; i < NSIG; ++i) {
        // Check if signal set contains signal i
        if (sigismember(set, i) == 1)
            // If it contains, print signal number
            printf("%d ", i);
        else
            // If not, print 0
            printf("0  ");
    }
}

int main(int argc, char const *argv[])
{
    // Define signal sets s and p
    __sigset_t s,  p;

    // Clear signal set s
    __sigemptyset(&amp;s);

    // Add SIGINT signal to signal set s
    __sigaddset(&amp;s, SIGINT);

    // Block signals in signal set s
    sigprocmask(SIG_BLOCK, &amp;s, NULL);

    // Enter infinite loop
    while (1)
    {
        // Get currently blocked signals set p
        sigpending(&amp;p);

        // Print signal set p
        printsigset(&amp;p);

        // Sleep for 1 second
        sleep(1);
    }
    
    return 0;
}

Blocking and Unblocking

  • sigprocmask
  • sigpending
  • sigsuspend

2. Process Behavior in Handling Signals

SIG_IGN ignores the signal

SIG_DFL default handling action

a signal handling function captures the signal

Default Signal Handling for 5 Cases

Term terminates the process

Ign ignores the signal

Core terminates the process and dumps memory

Stop pauses the process

Cont continues execution of the paused process

Types of Signals in Linux

Leave a Comment