Link to home
Start Free TrialLog in
Avatar of ibanja
ibanja

asked on

fork() question - why is pid always > 0

I have downloaded a Google scrapper from scroogle.org. The source code is at  http://www.searchlores.org/nbbw.c

The problem I am having is that the pid does not == 0 so the execl function does not execute the snarf program.  I am new to fork() and I am not sure why it might not be returning 0 here as the child process.

Any insights appreciated.  Below is part of the forkit() function that is giving me problems with an arrow at the appropriate spot (--->):

 strcat( resource, "&btnG=Google+Search" );

    /* snarf is used to search Google. Depending on your cgi-bin permissions
       situation, you may need an extra copy of snarf in the user's directory
       who owns the cgi-bin. Otherwise, some cgi-bin wrappers won't see the
       correct permissions on snarf and Apache will most likely abort with
       a "Premature end of script headers" error. Google should receive a
       reasonable user-agent from snarf or whatever is used, or the results
       may return as charset ISO 8859-1 instead of UTF-8 */

   pid = fork();

   if( pid == 0 )
         execl( "/usr/bin/snarf", "/usr/bin/snarf", "-nqm", resource, wdump, NULL );
   else if( pid == -1 )
       return;
   else
 --->  wpid = pid;   //****** evaluates true here. *******//
    for( cnt = 1; cnt < 60; cnt++ )
   {
      usleep( 100000 );
      retval = waitpid( -1, &status, WNOHANG );
      if( retval == wpid )
         return;
   }
                   /* after 6 seconds, we still have no sign of returning */
   kill( wpid, SIGKILL );
   sleep( 1 );
   remove( wdump );     /* remove if it's there; no harm done if it isn't */

}
/* _________________________________________________________ END FORKIT */

Thanks,
ibanja
Avatar of ozo
ozo
Flag of United States of America image

Is it possible the pid is 0 and the execl is failing?
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ibanja
ibanja

ASKER

Thanks,  

That makes it clear. Ironically I awoke at 2:00am this morning with this realization - that the child process may still be running the execl() function and that I am debugging in the parent process. I appreciate the input. I am finally understanding the nature of fork().

ibanja