How to restart a child process from parent process in c - c

How to restart the child process from parent process when child process terminates. In one application I have created a child process which has an infinite while loop. The application runs in a router. When some event happens my child process terminates. But after rebooting the router everything works fine. Is it possible to restart my child process from parent process(parent process always works).
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
while(1)
{
printf("Child process\n");
sleep(1);
}
}
else
{
while(1)
{
printf("parrent process\n");
}
}
return 0;
}

Assuming you only start one process and it's another executable:
pid_t pid = 0;
void sigchld(int unused)
{
if (pid) {
int status = 0;
waitpid(pid, &status, WNOHANG);
if (WIFSTOPPED(status) || WIFCONTINUED(status)) return;
if (!WIFSIGNALED(status)) return; // It exited rather than terminated
pid = 0;
}
if ((pid = vfork() == 0) {
/* fork() isn't really signal safe anymore but vfork() still is! */
execle(...);
_exit(0);
}
}
int main()
{
pid = 0;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sigchld;
sa.sa_flags = SA_NODEFER;
sigaction(SIGCHLD, &sa, NULL);
kill(SIGCHLD, getpid());
while(1)
{
printf("parrent process\n");
}
}
Trivially adaptable to more than one, but if it's not another executable you have your work cut out from you. Attempting to fork() and run more code inside the signal handler is fraught with pearl. fork() itself is only safe if you haven't installed any pthread_atfork() handlers (which don't run in vfork()). You cannot call malloc() in a signal handler, and stdio is unreasonably tricky and best to assume it just doesn't work.

Related

Unable to handle signals on Linux from inside of forked process

I am trying to execute a fork function and use a signal handler in the child process, however when I run the program and try to send the signal the parent process gets the signal instead of the child process. The code:
pid_t id = fork();
if(id == 0)
{
struct sigaction ss;
ss.sa_handler = &sigstop;
sigaction(SIGTSTP, &ss, NULL);
int status;
status = execvp(arguments[0], arguments);
if(status == -1)
perror("MSH_ERROR (unknown command");
exit(EXIT_SUCCESS);
}
else
{
wait(NULL);
}

How to determine a child process is a a background process or foreground process by looking at code?

we know that if a parents process wait for the termnination of its child processes first, then the child processes are called foreground process, for example:
int main() {
int status;
...
if ((pid = fork()) == 0){
...// Child runs
exit(0);
}
waitpid(pid, &status, 0); //parent wait for its child to reap it
}
since the parent process uses waitpid to wait and reap its child, so the child process is foreground process.
But what if I use signal handler to reap child processes as:
void sigchld_handler(int s){
int olderrno = errno;
pid = waitpid(-1, NULL, 0);
errno = olderrno;
}
volatile sig_atomic_t pid;
int main() {
sigset_t mask, prev;
signal(SIGCHLD, sigchld_handler);
sigemptyset(&mask);
while(1) {
sigprocmask(SIG_BLOCK, &mask, &prev); // Block SIGCHLD
if ((pid = Fork()) == 0){
...// Child runs
exit(0);
}
// Wait for SIGCHLD to be received
pid = 0;
while (!pid)
sigsuspend(&prev);
...//Do some work after receiving SIGCHLD
}
exit(0);
}
then can I say the child process in latter is a foreground process, because the parent wait for it using while (!pid) sigsuspend(&prev);?
But my understanding is that a child can be a foreground process only if the parents explicitly the child's process id in waitpid like waitpid(pid, &status, 0);, bu in the latter example, the parents doesn't specify child process's id to reap (use -1 in waitpid)?

Setting parent to child process group with setpgid fails

I'm trying to change the pgrp of the processes to that of the child's so i can setsid on the parent process. The only thing is I keep getting an EPERM error code. Both processes have the same session group, according to htop.
I'm basing this off of this blog post, so I can change which terminal output gets directed to.
void sig_exit(int signum)
{
_Exit(0);
}
pid_t change_process_group()
{
pid_t child_pid;
if ((child_pid = fork()) < 0)
{
perror("fork failed while attaching to term");
exit(1);
}
if (child_pid == 0)
{
pid_t parent = getppid();
setpgid(0, getpid());
signal(SIGUSR1, sig_exit); // wait till parent tells child to exit
//sleep(5);
//kill(parent, SIGUSR2);
pause();
printf("Shouldn't reach this\n");
}
//sleep(5);
//signal(SIGUSR2, sig_wait);
//pause();
int parent_pid = getpid();
int code = setpgid(parent_pid, child_pid); // need child process group
printf("%s\n", strerror(errno));
setsid();
return child_pid;
}
main()
{
pid_t child = change_process_group();
kill(child, SIGUSR1);
}
The commented out lines were from me thinking the processes might not be executing in the correct order, but those don't appear to fix the problem.
How may I correctly use setpgid to change the pgrp of the parent process to the child's?
This is a race condition and it works if you uncomment the sleep(5) line in the parent. When you call setpgid(parent_pid, child_pid), the child_pid process group must exist. It isn't enough that there exists a process with the PID child_pid: setpgid needs an existing process group unless the process is putting itself into its own group. If setpgid(parent_pid, child_pid) in the parent runs after setpgid(0, getpid()) in the child, it works.
Sleeping is both inefficient and fragile, so instead of that the parent should wait for a notification from the child. Signals are fragile because there aren't many different signals and they could come from anywhere. A good way to communicate between related processes is a pipe. Since all you need here is a one-time notification, you can set up a pipe and read from it in the parent (with the write end closed in the parent). The parent will wait until the child writes to the pipe or closes it. In the child, just close the write end of the pipe when you've finished the preparations. The parent's read call (or select if you need to do other things at the same time) will return.
Proof-of-concept code:
pid_t change_process_group()
{
pid_t child_pid;
int child_ready_pipe[2];
if (pipe(child_ready_pipe) < 0)
{
perror("pipe");
exit(1);
}
if ((child_pid = fork()) < 0)
{
perror("fork failed while attaching to term");
exit(1);
}
if (child_pid == 0)
{
close(child_ready_pipe[0]);
sleep(1); // mimic slow start of the child
if (setpgid(0, 0))
perror("child setpgid to create group");
close(child_ready_pipe[1]);
signal(SIGUSR1, sig_exit); // wait till parent tells child to exit
pause();
printf("Shouldn't reach this\n");
}
close(child_ready_pipe[1]);
int parent_pid = getpid();
char ignored;
read(child_ready_pipe[0], &ignored, 1);
close(child_ready_pipe[0]);
if (setpgid(parent_pid, child_pid) < 0) // need child process group
perror("parent setpgid");
if (setsid() < 0)
perror("parent setsid");
return child_pid;
}

Inform parent process about child get signal after signal handler in child process is served

Hi currently I am collecting backtrace of child process in signal handler of child process . Then planning to send collected backtrace to parent process using message queue .
My problem is when child process get any signal. child signal handler runs but informs parent process that child exited normally instead of child get signal.
below is my code
void childProcess()
{
int h =0 ;
for(h=0;h<10;h++)
{
printf("child for loop running %d\n",h);
//sleep(1);
int q = 1/0; // generate floating point exception
}
exit(0);
}
void signalhandler(int signum, siginfo_t *si, void *arg)
{
printf("signal received %s\n",strsignal(signum));
printf("%d\n",signum);
void *array[100];
int size = 100;
int addrLen = backtrace(&array,size);
char ** sym = backtrace_symbols(&array,addrLen);
int j = 0;
printf("Test crashed due to %s\n",strsignal(signum));
for(j=0;j<addrLen;j++)
{
printf("%u : %s\n",array[j],sym[j]);
}
raise(signum);
exit(signum);
}
void registerSignals()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = signalhandler;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGFPE, &sa, NULL);
}
int main()
{
//while(1)
{
pid_t pid;
pid = fork();
if(pid == 0)
{
// child
printf("child process id is %d\n",getpid());
registerSignals();
childProcess();
}
else
{
printf("parent process id is %d\n",getpid());
// parent
int iStatus;
pid_t childPID = waitpid(pid,&iStatus,0);
printf("iStatus is %d\n",WIFEXITED(iStatus));
if(childPID == -1)
{
printf("wait pid failed\n");
}
else if(WIFEXITED(iStatus)==1)
{
printf("child exited normally!\n");
}
else if (WIFSIGNALED(iStatus)==1)
{
printf("child process terminated abnormally !!\n");
int iSignalnumber = 0;
// to fetch the signal number
iSignalnumber = WTERMSIG(iStatus);
printf("child process terminated due to %s\n",strsignal(iSignalnumber));
// to check core file is generated or not
if(WCOREDUMP(iStatus)==1)
{
printf("core file is generated \n");
}
else
{
printf("core file is not generated \n");
}
}
int h ;
for(h = 0; h<10;h++)
{
printf("parent executing : %d\n",h);
}
}
printf("while loop executing with pid : %d \n", getpid());
sleep(1);
}
}
My requirement is after signal handler is served in child process the
parent should print "child process terminated abnormally !!" but I am
getting "child exited normally!" message
From wait()'s Linux docs:
WIFEXITED(wstatus)
returns true if the child terminated normally, that is, by
calling exit(3) or _exit(2), or by returning from main().
The child signal handler ends the process using exit(), so everything works a specified.
Remove the call to exit() from the signal handler to get the expected result.
The call to raise() inside the signal handler most likely leads to recursive calls, so remove is as well.

Killing a child process from a signal handler

I'm writing a simple shell, and I have to fork a child process an external program using execv. I have to send the signal TSTP(Cntl+Z) to the signal handler, and then kill the currently running child process. My problem is I can't find a way to pass the Child pid into the signal handler. If i do a getpid() in the handler, it just returns the parent pid. I also tried setting the child pid as getpid() inside the child process, and having that variable as a global variable, but that also didn't work. Here is some of the code I have so far.
void handler(int);
//in main
if (!built_in_cmd(myArgc,myArgs)) {
pid_t pid;
char *x = myArgs[0];
if((pid=fork())<0)
printf("Parent: fork() process failed");
else {
if (pid == 0) {
y=getpid();
printf("Parent: My child has been spawned. %d %d\n",y,getppid());
execv(x, myArgs);
exit(0);
}
else {
signal(SIGTSTP,handler);
wait(0);
printf("Parent: My child has terminated.\n");
}
}
}
return;
//outside main
void handler(int signo){
kill(idk,SIGKILL);
}
Signals are asynchronous in nature, there's no way to pass any extra state to them except through global variables. Assuming that you only ever have one thread waiting for a child, it's safe to use a global, but otherwise there's no multithread-safe way of doing so:
// At global scope
pid_t child_pid = (pid_t)-1;
...
void myfunc()
{
pid_t pid;
if((pid = fork()) < 0)
...
else if(pid == 0)
...
else
{
child_pid = pid;
...
}
}

Resources