A (dumb?) waitpid(2) question

From: usvyatsky, ilya
Date: Tue Dec 21 2004 - 19:28:18 EST


As dumb as it seems, I am seing a weird behavior on my RH3.0 box
(2.4.21-20.Elsmp kernel).

It looks like (contrary to the man page and POSIX .1) waitpid(2) does not
return upon a SIGALRM.

I am porting an old piece of Solaris userland code (stripped from all useful
functionality):

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>

void sig_handler(int signal)
{
printf("Alarm!!!\n");
}

int main(int argc, char *argv[])
{
int child_pid = -1;

child_pid = fork();
if (child_pid < 0) {
perror("cannot fork()");
exit(1);
}

if (child_pid) {
/* parent */
int status = 0, timeout = 1;

do {
printf("Parent: setting an alarm for %d seconds\n", timeout);

signal(SIGALRM, sig_handler);
alarm(timeout);

printf("Parent: waiting for a child %d\n", child_pid);
child_pid = waitpid(child_pid, &status, 0);

if (child_pid < 0) {
if (errno == ECHILD) {
perror("Parent: waitpid");
exit(1);
}
if (errno == EINTR) {
printf("Parent: wait was interrupted by a signal");
}
}
} while (child_pid < 0);

printf("Parent: child %d ", child_pid);

if (WIFEXITED(status)) {
printf("terminated normally with status %d\n",
WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
printf("was killed by signal %d\n", WTERMSIG(status));
}
}
else {
/* child */
int timeout = 6;

printf("Child: sleeping for %d seconds\n", timeout);
sleep(timeout);
printf("Child: exiting\n");
exit(0);
}
return 0;
}

Here is the original Solaris output:

Child: sleeping for 6 seconds
Parent: setting an alarm for 1 seconds
Parent: waiting for a child 7187
Alarm!!!
Parent: wait was interrupted by a signalParent: setting an alarm for 1
seconds
Parent: waiting for a child -1
Alarm!!!
Parent: wait was interrupted by a signalParent: setting an alarm for 1
seconds
Parent: waiting for a child -1
Alarm!!!
Parent: wait was interrupted by a signalParent: setting an alarm for 1
seconds
Parent: waiting for a child -1
Alarm!!!
Parent: wait was interrupted by a signalParent: setting an alarm for 1
seconds
Parent: waiting for a child -1
Alarm!!!
Parent: wait was interrupted by a signalParent: setting an alarm for 1
seconds
Parent: waiting for a child -1
Alarm!!!
Parent: wait was interrupted by a signalParent: setting an alarm for 1
seconds
Parent: waiting for a child -1
Child: exiting
Parent: child 7187 terminated normally with status 0

And here's what Linux outputs:

Child: sleeping for 6 seconds
Parent: setting an alarm for 1 seconds
Parent: waiting for a child 24282
Alarm!!!
Child: exiting
Parent: child 24282 terminated normally with status 0

Is it a bug or a feature?
Any suggestions would be greatly appreciated...


Ilya Usvyatsky

EMC²
where information lives

Phone: 508-249-1299


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/