• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

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
0
ibanja
Asked:
ibanja
2 Solutions
 
ozoCommented:
Is it possible the pid is 0 and the execl is failing?
0
 
brettmjohnsonCommented:
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.
0
 
amol_chaudhariCommented:
hi,
      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
0
 
ibanjaAuthor Commented:
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now