linux-signal

Introduction

Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control.

Every signal has a default action associated with it. The default action for a signal is the action that a script or program performs when it receives a signal.
Some of the possible default actions are

  • Terminate the process.
  • Ignore the signal.
  • Dump core. This creates a file called core containing the memory image of the process when it received the signal.
  • Stop the process.(can run later on)
  • Continue a stopped process

sending signal to process
There are several methods of delivering signals to a program or script.

  • One of the most common is for a user to type CONTROL-xxx while a script is executing.
  • The other common method for delivering signals is to use the kill command, the syntax of which is as follows
    • $kill -sig pid

signal table(x86)

Default Action for signal!!!

  • Term Default action is to terminate the process.
  • Ign Default action is to ignore the signal.
  • Core Default action is to terminate the process and dump core
  • Stop Default action is to stop the process.
  • Cont Default action is to continue the process if it is currently stopped.
NO Short Keyboard Action Comment
2 SIGINT ctrl + c Term The process was “interrupted”.
3 SIGQUIT ctrl + \ Core Quit from keyboard
5 SIGTRAP CORE Trace/breakpoint trap
18 SIGCON Cont Continue if stopped
19 SIGSTOP STOP Stop process
20 SIGTSTP ctrl + z STOP Essentially the same as SIGSTOP
ctrl +d it is not a signal, it’s EOF (End-Of-File). It closes the stdin pipe.

After ctrl + Z, current process is stopped, then you can call $fg to run it again

important signal

  • A SIGTRAP signal is a type of signal that is sent to a process when it encounters a trap instruction, this signal is used mainly from within debuggers and program tracers

SIGSTOP vs SIGTSTP

SIGSTOP and SIGTSTP are both signals used in Unix-like operating systems to suspend processes.

  • Nature and Origin
    • SIGSTOP is a signal that can only be sent programmatically (e.g., using kill -STOP pid)
    • SIGTSTP can be generated both programmatically and by user input, typically by pressing Ctrl+Z in a terminal.
  • Handling by Processes
    • SIGSTOP cannot be caught, blocked, or ignored by the receiving process.
    • SIGTSTP can be caught, ignored, or handled differently by the process.
  • Default behavior
    • Both signals typically suspend the process until a SIGCONT signal is received to resume execution

Ref