Unix - race condition example
Script started on Wed Apr 30 08:43:38 1997
sh-2.00$ cat race.c
/* race.c - show race condition. */
#include
static void charatatime(char *);
int main(void)
{
pid_t pid;
if( (pid = fork()) < 0)
{
perror("Fork error");
exit(1);
}
else if( pid == 0)
{
charatatime("Output from child\n");
}
else
{
charatatime("Output from parent\n");
}
exit(0);
}
static void
charatatime(char * str)
{
char * ptr;
int c;
/* Ensure that characters sent to stdout are output as soon
as possible - make stdout unbuffered. */
setbuf(stdout,NULL);
for(ptr = str; c = *ptr++; )
putc(c, stdout);
}
sh-2.00$ race
OOutputput from parent
sh-2.00$ ut from child
race
Output from parent
Output from child
sh-2.00$ race
OOutput from child
utput from parent
sh-2.00$ race
Output from parent
sh-2.00$ Output from child
race
Output from child
Output from parent
sh-2.00$ race
Output from parent
sh-2.00$ Output from child
race
Output from parent
sh-2.00$ Output from child
race
Output from child
Output from parent
sh-2.00$ race
OOutput from child
utput from parent
sh-2.00$ exit
exit
script done on Wed Apr 30 08:44:15 1997