40 lines
1.0 KiB
C
40 lines
1.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
#include <signal.h>
|
|
|
|
int main(void)
|
|
{
|
|
int status;
|
|
pid_t fork_pid;
|
|
|
|
if ( (fork_pid = fork()) == 0 )
|
|
{
|
|
printf("* I am the son. About to terminate by signal... *\n");
|
|
// Send SIGKILL to this process (self-termination with a signal)
|
|
raise(SIGKILL); // Alternatively, try SIGTERM, SIGSEGV, etc.
|
|
}
|
|
else if (fork_pid == -1)
|
|
{
|
|
printf("fork() failed!\n");
|
|
exit(2);
|
|
}
|
|
|
|
wait(&status); // wait for termination of child process
|
|
|
|
// status: full status returned by wait()
|
|
// (status >> 8) & 0xff: higher byte (exit code in normal exit)
|
|
// status & 0xff: lower byte (termination signal or additional info)
|
|
printf("wait status: 0x%x | Exit code: 0x%x | Signal: 0x%x |\n",
|
|
status, (status >> 8) & 0xff, status & 0x7f);
|
|
|
|
// Check if child terminated by signal
|
|
if (WIFSIGNALED(status))
|
|
{
|
|
printf("Child was terminated by signal %d\n", WTERMSIG(status));
|
|
}
|
|
|
|
return 0;
|
|
}
|