Reputation: 31
I am coding in c and wanted to know via coding what's the best way to know if a signal (for example SIGUSR1) terminated a process. Is there a way to make it with a function and flag it so other processes may know it?
More info: The process is a program I did in C. Later when the process ends (with the signal or not) I wanted another program I have to know it. They are connected via fifos.
Upvotes: 0
Views: 1139
Reputation: 2785
In the parent process, you can use the wait()
system call's WIFSIGNALED(status)
macro to check if the child process was terminated by a signal.
Also you can get the signal number using WTERMSIG(status)
macro.
Here's a code to demonstrate the idea.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
pid_t n = fork();
if(n==0)
{
execvp(/*Some program here for child*/);
}
//parent does something
//Parent waits for child to exit.
int status;
pid_t childpid = wait(&status);
if(WIFSIGNALED(status))
printf("Parent: My child was exited by the signal number %d\n",WTERMSIG(status));
else
printf("Parent: My child exited normally\n"):
return 0;
}
You can read more about it in the manual: man 2 wait
Upvotes: 1