Post #4080669
2026-07-07 15:37 UTC
It makes a difference if you compile this with -std=c17 or -std=gnu17. I don’t know why yet. If you compile with -std=c17 and press ctrl+c, waitpid will return -1, if you compile with -std=gnu17 and press ctrl+c, waitpid will return pid. Tested with recent GCC and Clang on x86_64-unknown-linux-gnu.
#include <signal.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
static void signal_handler(int sig) {
printf("caught signal %d\n", sig);
}
int main() {
signal(SIGINT, signal_handler);
pid_t pid = fork();
if (pid < 0) {
return -1;
} else if (pid == 0) {
execlp("cat", "cat", NULL);
return -1;
}
printf("pid == %d. press ctrl+c ...\n", pid);
int status;
pid_t ret = waitpid(pid, &status, 0);
if (ret < 0) {
perror("waitpid");
return -1;
}
printf("waited; ret == %d, status == %d\n", ret, status);
}
Replies (0)
No replies.