fork() question - why is pid always > 0

Posted on 2006-04-20
Last Modified: 2010-04-15
I have downloaded a Google scrapper from The source code is at

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 */

Question by:ibanja
    LVL 84

    Expert Comment

    Is it possible the pid is 0 and the execl is failing?
    LVL 23

    Accepted Solution

    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.
    LVL 2

    Assisted Solution

          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

    Author Comment


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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now