I have a running service which runs in background.
It execute shell commands by function
system(cmd)
I need to report fail when command execution takes more than 60 seconds.
Parent doesn't need to wait for 60 seconds of time if the cmd execution completed already.
runCommand()
{
pid_t pid;
pid = fork();
if( pid == 0 )
{
system(cmd); exit(0);
}
else if ( pid > 0)
{
sleep(60);
childTerminated = waitpid( pid, 0, WNOHANG);
if( childTerminated == 0 )
{
kill(pid, SIGKILL);
timeout = true;
}
else
timeout = false;
}
}