2

I have learn linux programming for a while, I know signal & system call are totally different concept.

To my understanding, system call is the interface that kernel provide to outside, and signal is a mechanism to let process receive info from kernel or other process. They are kind reverse process to me.

Am I understanding correctly?

My question is:

Do there have deep or simpler relationship between this 2 concept?

This might seems a silly question, but sometimes I mix them, I need to think carefully to distinct between them. I am trying to get an simple opinion to help me understand relationship & distinction between them.

Eric
  • 22,183
  • 20
  • 145
  • 196

2 Answers2

5

System calls are the rendezvous point between user space and kernel space. It's how normal, user-level code traps into kernel space when something a little more complex needs to be made - reading from a device, writing to a device, changing a hardware configuration, sending network packets, you name it.

So basically, user code interacts with the kernel via syscalls; invoking a syscall is a request to the kernel for a service. While doing so, an interrupt is generated that "wakes up" the kernel. This is called trapping into kernel space.

Signals, on the other hand, are an independent and different communication mechanism. Signals are used by the kernel to asynchronously notify user processes of various events (in some cases, I/O available, or an invalid memory access attempt, or an illegal instruction, etc.), but they are also used between processes: if you have the correct permissions, you can send a signal from a user-space process to another user-space process.

You can set up a custom handler for user-reserved signals such asSIGUSR1 and SIGUSR2 and do whatever pleases you with these. You can use signals to write a basic parent/child synchronization mechanism with the help of sigsuspend(2) and sigaction(2) (and a flag). You can kill an unresponsive process with SIGKILL (although it's advisable that first you try SIGTERM to give it a chance to terminate gracefully).

So, you see, the possibilities are endless. Syscalls are a request to the kernel for a service, adhere to a strictly defined API, and allow you to enter and leave kernel mode for administrative operations. Signals are more like a generic process communication mechanism that also happens to be used by the kernel to notify user processes, but there are other uses.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • 1
    To be precise, the software generated interrupt is a "trap" and is not handled the same as a hardware "interrupt". See: https://stackoverflow.com/questions/3149175/what-is-the-difference-between-trap-and-interrupt – Anthony O Oct 05 '19 at 03:48
1

The common communication channel between user space program and kernel is given by the system calls. But there is a different channel, that of the signals, used both between user processes and from kernel to user process.

you can read more about signals in https://www.win.tue.nl/~aeb/linux/lk/lk-5.html

Faezeh
  • 161
  • 1
  • 5