mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-22 08:53:29 +00:00
Support parent death signal & Refactor do_exit
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
e8c8c027c5
commit
57fc6a5402
@ -23,6 +23,7 @@ itimer/timer_create
|
||||
mmap/map_shared_anon
|
||||
pthread/pthread_test
|
||||
pty/open_pty
|
||||
signal_c/parent_death_signal
|
||||
signal_c/signal_test
|
||||
"
|
||||
|
||||
|
49
regression/apps/signal_c/parent_death_signal.c
Normal file
49
regression/apps/signal_c/parent_death_signal.c
Normal file
@ -0,0 +1,49 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <sys/prctl.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void signal_handler(int signum)
|
||||
{
|
||||
if (signum == SIGTERM) {
|
||||
printf("child process reveives SIGTERM\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == -1) {
|
||||
perror("fork");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (pid > 0) {
|
||||
printf("Parent PID: %d\n", getpid());
|
||||
// Ensure parent won't exit before child process runs
|
||||
sleep(1);
|
||||
} else {
|
||||
printf("CHild PID: %d\n", getpid());
|
||||
|
||||
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = signal_handler;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
sigaction(SIGTERM, &sa, NULL);
|
||||
|
||||
// Child waits for signal from parent death
|
||||
while (1) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user