Some signals have a fixed meaning (e.g., terminating a process)
Others are user-defined
Signals can be sent to a process by the OS or another process
Sent by the OS: The kernel (the core of the OS) sends signals to processes when certain system-level events happen
An illegal instruction (like dividing by zero)
A memory access violation (SIGSEGV)
A child process terminating, which sends a SIGCHLD signal to its parent process
Sent by another process: Processes can send signals to each other (provided they have the necessary permissions)
Using the kill command in a Linux/Unix terminal is a common example: kill 12345 sends a SIGTERM signal to the process with ID 12345
Sent by the user: You can directly trigger signals from your keyboard
The most common example is pressing Ctrl+C, which causes the terminal to send a SIGINT signal to the currently running foreground process
Signal handler
When a process receives a signal, it must have a way to handle it
For every signal, a process has a defined action
Every signal has a default behavior defined by the OS
For most signals that indicate an error or a termination request (like SIGINT, SIGTERM, SIGSEGV), the default action is to terminate the process
For other signals, the default might be to be ignored
Ignore the signal: The process can be configured to simply discard the signal and continue its work as if nothing happened
Catch the signal with a custom handler: A programmer can write a specific function - a custom signal handler that will be executed whenever a certain signal is received. This allows a process to override the default behavior
Example: Imagine a text editor. If you press Ctrl+C, you don't want it to just quit and lose all your unsaved work. A well-written editor will catch the SIGINT signal. Instead of terminating (the default action), its custom handler will execute code that prompts the user: "Do you want to save your changes before exiting?"
Sockets enable communication between processes on the same or different machines
TCP/UDP sockets for communication across machines
To do this, a socket needs a unique address, which is a combination of an IP address (which computer to find) and a port number (which application on that computer to talk to)
Unix sockets for communication on the local machine
Communication via sockets
Processes open sockets and connect them
Both the "server" process (the one waiting for a connection) and the "client" process (the one initiating the connection) must first ask the OS to create a socket endpoint for them
The client process uses its socket to "call" the server's address. The OS makes the connection, and once the server "accepts" the call, a two-way communication channel is established
Messages written into one socket can be read from another
The OS transfers data through the socket buffer
When a process writes data to a socket, it doesn't instantly appear in the other process. Instead, the data is placed in a socket buffer, which is a temporary storage area in the OS's memory
The OS reads the data from the sender's buffer, handles the complex task of transmitting it (e.g., breaking it into TCP packets), and places it in the receiver's socket buffer
When the receiving process decides to read from the socket, it is actually pulling data out of this buffer