fork() problem

Posted on 2007-07-30
Last Modified: 2013-12-27

I have a C program runnig on Unix Solaris, doing a fork.
The problem is that fork() returns with a positive number but errno is set to 10 (ECHILD).
Then waitpid returns with the same error since, obviously, the child process was never really created.
Why doesn't fork() return with -1?
In all code examples I have seen, none handled such a case.

Question by:nivo_Z
    LVL 7

    Assisted Solution

    First, what is the data type of the variable to which you assign the pid returned by fork()? If that is unsigned (watch out for implicit casts as well), then -1 will look like a (large) positive number.

    If that is not an issue, then: is the fork() happening in a multi-threaded environment?

    - Ravs

    Author Comment

    The type is pid_t and most of the time it is working ok, the problem happens only once in a while.
    LVL 22

    Assisted Solution

    What makes you think that the fork did not really create a child process? If the return code for
    fork is positive, you should not look at errno. The errno variable is not cleared at each call,
    so it's value is whatever the last error return in your program was unless -1 is returned. Many things that you do not know about inside of library calls and such can set errno, so you should never look at it unless an error was returned. Further evidence of this is the fact that ECHILD
    is not an expected error from the fork call.

    You do not say what the positive return value from fork is. Does it look like a reasonable pid
    number? Is it fairly close to the pid of the parent? Have you tried using the truss command to
    see if a new child process is created?

    If you are using Solaris 10, you could use dtrace to try to see what is going on.

    If the child process changes it's process group and then exits, you might be having a
    race condition where the child exits before you call waitpid. If the child does
    not change it's process group, then even if it did exit fast, the return status should
    be available and the waitpid would not fail.

    You might posting the code that you use to do the fork. Someone here might see something
    in it.

    Author Comment


    I know that the child process was not created because after the fork, before the switch on the pid, I print a message to the log. This message should appear twice, once printed by the parent and once by the child. Most of the time I do get these 2 messages, but once in a while I get only the parent message.
    Also, I set errno to 0 right before the fork.
    Here is the relevant code:

       errno = 0;
       pid = fork ();
       printolog("%s: %s - fork return: %d. errno: %d", func_nm, (pid) ? "parent" : "child", pid, errno));

        signal(SIGCHLD, SIG_DFL);

       switch (pid) {
         case -1:
             /* code */
         case 0:   /* child */
         /* code */
         default: /* parent */
              r = waitpid (pid, &status, 0);
            /* some more code */
             }  /* end switch(pid) */

    LVL 12

    Accepted Solution

    Hi nivo Z,

    I might be wrong, but I'm wondering if the parent and the child is printing a message to same log file (which would make the log file a shared resource). If so, there might be a conflict sometimes between the parent and child process.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now