Topic: userhook1 problem

When I run this command from the console prompt it works just fine, but when I run it from userhook1
it causes a segmentation fault.  I am trying to get two programs to talk to each other one in background and one
in forground. 

Command works from console:

/mnt/storage/part1 & /mnt/storage/part2

Here is userhook1:

/mnt/storage/part1 & /mnt/storage/part2

Here is part1.c:
#include <stdio.h>
#include <time.h>
#include <string.h>
int main()
{
    time_t CurTime;
    time_t OldTime;
    FILE * talk;
    for(;;)
    {
        CurTime = time(&CurTime);
        if ((CurTime != OldTime) && (0 == (CurTime % 5)))
        {
            talk = fopen("/tmp/talk","wc");
            fprintf(talk,"The current time is %ld\n",CurTime);
            OldTime = CurTime;
            fclose(talk);
        }
    }
    return 0;
}

Here is part2.c:

#include <stdio.h>
#include <time.h>
#include <string.h>
int main()
{
    time_t CurTime;
    time_t OldTime;
    FILE * listen;
    char line [100]={'\0'};
    for(;;)
    {
        CurTime = time(&CurTime);
        if ((CurTime != OldTime) && (3 == (CurTime % 5)))
        {
            listen = fopen("/tmp/talk","rc");
            fgets(line,sizeof(line),listen);
            fclose(listen);
            printf("Line was %s",line);
            OldTime = CurTime;
        }
    }
    return 0;
}

Since it always works from the command line and always crashes from userhook1 how do I track down this issue?
HELP!

Re: userhook1 problem

Because you are running part1 and part2 as different processes there is no guarantee as to the order in which they are actually completing their tasks. In particular, I noticed that you are not doing any error checking. In part2, you should check that you have a valid file handle after the fopen before you try to use it. When you run the tasks from userhook1 the operating system is probably (and arbitrarily) trying to execute part2 before part1 and so when part2 tries to open the file it fails and gets a null pointer resulting in a segmentation fault.

You could have something like:

listen = NULL; while (listen == NULL) {sleep(1); listen = fopen("/tmp/talk","rc");}

to get around this problem but in practice it would be better to use a pipe instead of a file for the processes to communicate with each other.