Forked Function not assigning pointer

Posted by Luke Mcneice on Stack Overflow See other posts from Stack Overflow or by Luke Mcneice
Published on 2010-05-05T15:46:25Z Indexed on 2010/05/05 16:18 UTC
Read the original article Hit count: 311

Filed under:
|
|

In the code below I have a function int GetTempString(char Query[]); calling it in main works fine.

However, when calling the function from a fork the fork hangs (stops running, no errors, no output) before this line: pch = strtok (Query," ,"); the printf shows that the pointer to pch is null. Again this only happens when the fork is executing it.

What am I doing doing wrong?

int main()
{
if((Timer =fork())==-1) printf("Timer Fork Failed");    
    else if(Timer==0)
    {
        while(1)
        {       
        sleep(2);       
        GetTempString("ch 1,2,3,4");        
        }
    }
    else
    {
         //CODE

         GetTempString("ch 1,2,3,4");
        }
}

int GetTempString(char Query[])
{
        char * pch;


    printf("DEBUG: '%s'-'%d'\n",Query,pch);

    pch = strtok (Query," ,");//* PROBLEM HERE*

        //while loop for strtok...

        return 1;

}

© Stack Overflow or respective owner

Related posts about c

    Related posts about fork