The man page for wait()
tells you what the return values mean.
If wait() or waitpid() returns due to the delivery of a signal to the calling process, -1 shall be returned and errno set to [EINTR]....Otherwise, (pid_t)-1 shall be returned, and errno set to indicate the error.
To find out what the errno
is, you can use perror()
and strerror()
#include <errno.h>
#include <string.h>
// ...
perror("wait error: ");
// or
printf("errno(%d): %s\n", errno, strerror(errno));
From the wait()
man page the errors could be:
The wait()
function shall fail if:
ECHILD
The calling process has no existing unwaited-for child processes.
EINTR
The function was interrupted by a signal. The value of the location pointed to by stat_loc is undefined.
So once you print the errno
value you should have a good idea what went wrong. I don't see anything in your code specifically showing what caused it. A few things you might want to do as good practice, compile with -Wall
and make sure you're addressing all warnings, and also be sure to initialize all variables.