What is a signal in Unix?

2019-03-24 19:45发布

问题:

This comment confuses me: "kill -l generally lists all signals". I thought that a signal means a quantized amount of energy.

[Added] Please, clarify the (computational) signal in Unix and the physical signal. Are they totally different concepts?

[Added] Are there major differences between paradigms? Is the meaning the same in languages such as C, Python and Haskell? The signal seems to be a general term.

回答1:

I cannot believe that people are not comparing things such as hardware and software or stressing OS at some points.

Comparison between a signal and an interrupt:

The difference is that while interrupts are sent to the operating system by the hardware, signals are sent to the process by the operating system, or by other processes. Note that signals have nothing to do with software interrupts, which are still sent by the hardware (the CPU itself, in this case). (source)

Definitions

  1. process = a program in execution, according to the book below

Further reading

  1. compare the signal to Interrupts and Exceptions

  2. Tanenbaum's book Modern Operating Systems



回答2:

The manual refers to a very basic mechanism that allow processes or the operation system to notify other processes by sending a signal. The operation system can use it to notify programs about abortions of them (signal SIGABRT) or about a segmentation fault (often caused by accessing a null-pointer, SIGSEGV), to name two of them.

Some unix servers use signals so the administrator can use kill to send them a signal, causing them to re-read their configuration file, without requiring them to restart.

There are default actions taken for some signals and other signals are just ignored. For example on receive of a SIGSEGV, the program terminates, while receiving a SIGCHLD, meaning a child-process died, will by default result in nothing special.

There is a ANSI C standard function that installs a signal handler, which is a function that can execute some code when receiving a signal, called signal (read in man signal). In different unix's, that function behave different, so its usage is discouraged. Its manpage refers to the sigaction function (read man sigaction), which behaves consistent, and is also more powerful.



回答3:

A physical signal and a Unix signal are indeed different concepts. When a Unix signal is sent from one process to another, there is no specific corresponding physical signal. Unix signals are merely an abstraction so programmers can talk about processes communicating with one another.

Unix signals could have been called messages, events, notifications, or even a made-up term like "frobs". The designers just chose the name "signal", and it stuck.



回答4:

A signal is a message, either to the target process, or to the OS about the target process. It is part of the unix API (and is defined in various POSIX standards).

Read man kill, man signal, and man sigaction.

Other SO questions that might be helpful:

  • What is the difference between sigaction and signal?


回答5:

Some from my notes :

Allows asynchronous communication

  • Between processes belonging to the same user
  • From the system to any process
  • From the system manager to any process
  • All associated information is in the signal itself
  • Many different signals

SIGINT

  • From the system to all processes associated to a terminal
  • Trigger: ^C pressed
  • Usual way to stop a running process

SIGFPE

  • From the kernel to a single process
  • Trigger: error in floating point operation

SIGKILL

  • To a single process
  • Stops the execution of the destination process

SIGALRM

  • From the kernel to a single process
  • Trigger: timer expiration

SIGTERM

  • To a single process
  • Recommends the process to terminate gracefully

SIGUSR1, SIGUSR2

  • From any process to any other
  • Without a predefined semantic
  • Freely usable by programmers

Sending a signal to another process

  • int kill(pid, signal_ID)

The programmer can decide what to do when a signal is received

  • Use the default behavior
  • Ignore it
  • Execute a user function

Detecting an interrupted write

if (write(fd, buff, SIZE)<0) {
  switch (errno) {
   case EINTR:
    warning(“Interrupted write\n”);
    break;
  }
}…


回答6:

A signal is a message which can be sent to a running process.

For example, to tell the Internet Daemon (inetd) to re-read its configuration file, it should be sent a SIGHUP signal.

For example, if the current process ID (PID) of inetd is 1234, you would type: kill -SIGHUP 1234



回答7:

A signal is "an event, message, or data structure transmitted between computational processes" (from Wikipedia).



回答8:

In this case signal means 'message'. So it's sending a message to a process which can tell the process to do various things.



回答9:

A unix signal is a kind of message that can be sent to and from unix processes. They can do things like tell a process to quit (SIGKILL) or that a process had an invalid memory reference (SIGSEGV) or that the process was killed by the user hitting control-c (SIGINT).

from a *nix command line type in:

man signal

that will should you all the signals available.



回答10:

Signal is basically an interrupt that tells the process that a particular event has happened.

Signal generally send by the kernel, meanwhile a process can also send the signal to other process (depends on permission ans all ) by using kill and killall command and a process can send signal to itself by using raise.

Major use of signal:

  1. To handle the interrupt.

  2. Process synchronization.



回答11:

  1. Signal is an interrupt that used to intimate a process that a particular event has happened.

  2. Signal can be send by kernel to running process or one process to another process.

  3. In bash kill and killall command used to send the signal.