Possible race in fork()

Richard B. Johnson (root@chaos.analogic.com)
Mon, 25 Jan 1999 10:34:58 -0500 (EST)


Hi!
On a system at home I had a consitant problem trying to set the
terminal speed after a fork(). It was traced to the fact that
a termios structure, set by the parent, was being corrupted during
the fork().

I have written a test program that shows this problem without using
any termios stuff, just malloc()ed memory. The problem does not
occur with a machine here at work (same CPUs, same CPU board), but
128Mb of memory. Both are dual pentium 400MHz SMP machines.

Both machines are running 2.1.131.
My machine at home, showing the problem, only has 32 Mb of memory
so the problem may be anywhere in the VM handling.

When the problem occurs, both the child and parent's memory contain
random data, not related to either the zeros or the incrementing
values the test program writes.

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>

#define LEN 0x1000001

void dummy(int unused)
{

}

main()
{
unsigned char *p1;
unsigned char *p2;
unsigned char *pr;
unsigned int i;

signal(SIGUSR1, dummy);

p1 = (unsigned char *) malloc(LEN);
p2 = (unsigned char *) malloc(LEN);
pr = p1;
for(i=0; i< LEN; i++)
*pr++ = (unsigned char) i;
pr = p2;
for(i=0; i< LEN; i++)
*pr++ = (unsigned char) i;
switch(fork())
{
case 0:
memset(p1, 0x00, LEN);
pr = p2;
for(i=0; i< LEN; i++)
{
if(*pr != (unsigned char) i)
{
fprintf(stderr, "Child buffer corrupted during fork()");
fprintf(stderr, "Offset = %d, was %02X, should be %02X\n",
i, *pr, (i & 0xff));
}
pr++;
}
usleep(100000);
kill(getppid(), SIGUSR1);
exit(0);
break;
case -1:
exit(1);
break;
default:
printf("Waiting for kid's signal...");
fflush(stdout);
pause();
puts("Ok!");
pr = p1;
for(i=0; i< LEN; i++)
{
if(*pr != (unsigned char) i)
{
fprintf(stderr, "Parent buffer corrupted during fork()");
fprintf(stderr, "Offset = %d, was %02X, should be %02X\n",
i, *pr, (i & 0xff));
}
pr++;
}
wait(&i);
break;
}
return 0;
}

Cheers,
Dick Johnson
***** FILE SYSTEM WAS MODIFIED *****
Penguin : Linux version 2.1.131 on an i686 machine (400.59 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.
Wisdom : It's not a Y2K problem. It's a Y2Day problem.

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