• C

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 )
 --->  wpid = pid;   //****** evaluates true here. *******//
    for( cnt = 1; cnt < 60; cnt++ )
      usleep( 100000 );
      retval = waitpid( -1, &status, WNOHANG );
      if( retval == wpid )
                   /* 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 */

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is it possible the pid is 0 and the execl is failing?
When you call fork(), it creates a duplicate of your process (the child), so you now have two identical processes with slightly different code paths.  When the child process returns from fork, the returned pid is 0 (that is how you know you are the child).  When the parent process returns from fort, the returned pid is the process id of the newly created child process.  I suspect you are debugging through the parent process as it attempts to fork and execute the child.  Since the debugger is debugging the parent, the code path will follow the pid != 0 path.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
      when u call fork(), it will create another say child process just same as parent process... but fork returns different values in parent and child process...]
in parent process it returns process ID of the newly created child and in child process it return 0. this is for normal execution....
if any error occure while creating child process it will return -1 in the parent process and no child process is created.....

if(pid>0) then whatever statements enclosed in this if block are executed only in parent process and statements in else block are executed in child process....
all other commands that are outside if else block are executed in both parent n child process...

so in ur program the execl statement will be executed in newly created child process and rest will be in parent process

hope this helps
ibanjaAuthor Commented:

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().

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.