I am writing a C program that uses fork(), execvp() for child process. I want to stop, continue and kill a child process, how can I handle it ? I don't know much about signals.
Asked
Active
Viewed 9,793 times
1 Answers
0
Call the signal function within a process to setup a signal handler, a function that will be called when that process receives the specific signal you specify.
Here's an example signal handler function for the SIGUSR1 signal:
static void usr1_signal_handler(int signo)
{
printf("Received SIGUSR1!\n");
finish_up = true;
}
and an example of how to set it up:
if (signal(SIGUSR1, usr1_signal_handler) == SIG_ERR) {
printf("An error occurred while setting a signal handler.\n");
}
You can signal a process using the kill function:
kill(pid, SIGUSR1);
In this case, pid could be the process id you receive when you call fork.

Gavin Higham
- 89
- 1
- 4
-
1You can't safely call `printf()` in a signal handler. https://port70.net/~nsz/c/c11/n1570.html#note188 – Andrew Henle Nov 17 '21 at 14:44