C Language Interview: Signal Handling and Inter-Process Communication
In modern operating systems, inter-process communication (IPC) and signal handling are two very important concepts. In C language interviews, examiners often focus on these two topics as they involve crucial aspects of system programming. This article will detail the basic concepts of signal handling and inter-process communication, illustrated with code examples.
Signal Handling
What is a Signal?
In operating systems, a signal is a mechanism for asynchronous event notification. When a certain condition occurs, the kernel can send a specific notification to one or more processes. These conditions may include user requests, error reports, or other events.
Common Types of Signals
Here are some commonly used signals:
<span>SIGINT</span>
: Terminal interrupt (Ctrl+C)<span>SIGTERM</span>
: Request to terminate the program<span>SIGKILL</span>
: Force program termination (cannot be caught)<span>SIGUSR1/SIGUSR2</span>
: User-defined signals
Signal Handling Function
To handle specific signals, we can set up a signal handling function. Below is a simple example that prints a message and continues running when it receives a SIGINT signal.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void signal_handler(int signum) {
printf("Received signal %d\n", signum);
}
int main() {
// Set the handler for SIGINT
signal(SIGINT, signal_handler);
printf("Process ID: %d\n", getpid());
while (1) {
printf("Running...\n");
sleep(1); // Pause for 1 second to reduce CPU usage
}
return 0;
}
Code Explanation
- Include necessary header files.
- Define a function named
<span>signal_handler</span>
that will execute when the specified signal is received. - In the
<span>main</span>
function, use the<span>signal</span>
function to connect the SIGINT signal to our handler function. - The program enters an infinite loop, printing “Running…” once per second. During this process, if you press Ctrl+C, the console will output the received SIGINT signal number and continue executing.
Inter-Process Communication (IPC)
Inter-process communication allows different processes to exchange data. Here we will explain several common methods, including pipes, message queues, and shared memory. First, we will demonstrate the simplest IPC method using pipes.
Pipes
Pipes are used to facilitate data flow between participants, a typical scenario being the transfer of information between parent and child processes. Below is an example of creating an unnamed pipe for IPC:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
return 1;
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return 1;
}
if (pid == 0) {
close(pipefd[0]); // Child process closes read end
const char* msg = "Hello from child";
write(pipefd[1], msg, sizeof(msg));
close(pipefd[1]);
} else {
close(pipefd[1]); // Parent process closes write end
char buffer[128];
read(pipefd[0], buffer, sizeof(buffer));
printf("Received message: %s\n", buffer);
close(pipefd[0]);
}
return 0;
}
Code Explanation
- First, include necessary header files.
- Use
<span>pipe()</span>
to create an unnamed pipe; if it fails, output an error message and return. - Use
<span>fork()</span>
to create a child process, at this stage both child and parent will coexist in the same space but with different PIDs: - Child process closes the read end of the pipe, allowing it to only write, then sends a message to the parent process;
- Parent process closes the write end accordingly, thus only responsible for reading information from the child process and printing it out.
Conclusion
This article introduced basic concepts in C language, such as signals and how to set up your own signal handlers, as well as a simplified IPC method using unnamed pipes. This knowledge is not only crucial for interviews but also provides a foundation for deeper understanding of complex projects in the future. Therefore, continuously practicing these techniques in the relevant field will effectively enhance your programming skills and workplace competitiveness.