2

I am forking a child process to run a command using execve. I am installing and defining 3 signal handlers: SIGCHLD,SIGINT and SIGSTP as follows:

void sigchld(int sig)
{
while((pid=waitpid(-1,&stat,WNOTRACE|WNOHANG))>0)
{
  if(WIFEXITED(stat))
    //normal exit: Delete child from job list
  if(WIFSIGNALED(stat))
    //interrupted by signal: delete job from job list
  if(WIFSTOPPED(stat))
   //Stopped: put child in background
 }
}

//SIGINT Handler:
kill(-pid,sig)

//SIGSTOPPED Handler:
kill(-pid,sig)

Now, when I run a process and put it in background(ctrl z) in a loop(100 times), in most cases I get error: waitpid error: Interrupted system call

Why am I getting this?

Maxsteel
  • 1,922
  • 4
  • 30
  • 55
  • Please show the complete code. The code as shown does not issue "*`waitpid error: Interrupted system call`*" – alk Nov 02 '15 at 12:20
  • `waitpid()` with getting `WNOHANG` passed is not expected to fail with `EINTR`. – alk Nov 02 '15 at 12:22

2 Answers2

2

Interrupted system call :

If a process caught a signal while the process was block in a "slow" system call, then the system call was interrupted.

The problem is the waitpid function was interrupted due to the receipt of a signal sent by the calling process.

The waitpid is a interrupted system call. So, the problem with interrupted system call is that we now have to handle the error return explicitly.

You have to handle the error in the waitpid using errno.

The interrupted system call means, for example, read statement is executed, the read will read the input from stdin, in between reading there is a signal is occurred, now the system have to handle that signal, now the read will be finished, and set the errno to EINTR.

1

I had this problem as well. When I registered my handler, I used a sigaction handler, which looks a bit different than the case here. In my case, I didn't include the SA_RESTART flag. Including this handler means that system calls will automatically restart themselves if interrupted.

dylan-slack
  • 102
  • 5