Fork + Waitpid

Discussion in 'C Programming' started by lasek, May 13, 2005.

  1. lasek

    lasek Guest

    Hi all,
    i have made a simple script that use fork, waitpid and sigaction for
    SIGCHLD.

    #include<sys/types.h>
    #include<sys/wait.h>
    #include<stdio.h>
    #include<errno.h>
    #include<unistd.h>
    #include<signal.h>

    int pid;

    void chld_signal(int sig)
    {
    int status,child_val;

    printf("\n SIGHCHILD trapped \n");

    if(waitpid(pid,&status,0)<0)
    {
    fprintf(stderr, "waitpid failed\n");
    return;
    }

    if(WIFEXITED(status))
    {
    child_val=WEXITSTATUS(status);
    printf("child's pid %d exited normally with status
    %d\n",pid,child_val);
    }
    }

    int main(int argc, char *argv[])
    {
    char *prog_argv[4];

    int iCount;

    struct sigaction chld_action;

    /* Initialize the signal structure mask. */

    chld_action.sa_handler=chld_signal;
    sigemptyset(&chld_action.sa_mask);
    chld_action.sa_flags=0;

    if(sigaction(SIGCHLD,&chld_action,NULL)<0)
    {
    fprintf(stderr,"\nSigaction failed\n");
    return 1;
    }
    else
    printf("\nSigaction Success\n");


    /*
    ** Build argument list
    */

    prog_argv[0] = "/usr/bin/ls";
    prog_argv[1] = "-lrt";
    prog_argv[2] = "/usr1/sdc/corsis/stuff";
    prog_argv[3] = NULL;

    /*
    ** Create a process for the cmd */

    if((pid=fork()) < 0)
    {
    fprintf(stderr,"Fork failed");
    exit(errno);
    }

    if(!pid)
    {
    /* Child */

    printf("\nExecuting command [%s]\n",prog_argv[0]);

    printf("\nParent Pid %d\n\n",getppid());

    execvp(prog_argv[0], prog_argv);
    }
    if(pid)
    {
    /*
    * Parent
    */

    printf("\nWe're in the parent; let's wait for the child pid [%d] to
    finish \n",pid);

    sleep(1);
    }

    return 0;
    }

    Now the problem, if i delete the 'sleep(1)' i can't see anything about all
    the printf into chld_signal function.
    If i put the 'sleep(1)' i can see all the printf, the code,status of
    waitpid.

    WHY ???

    Thanks and have a nice week.
    lasek, May 13, 2005
    #1
    1. Advertising

  2. lasek

    Artie Gold Guest

    [OT - redirect]Re: Fork + Waitpid

    lasek wrote:
    > Hi all,
    > i have made a simple script that use fork, waitpid and sigaction for
    > SIGCHLD.
    >

    [code snipped]

    Sorry, as your question has nothing to do with standard C, we can't help
    you here. Please try news:comp.unix.programmer, where your question
    would be *on* topic!

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, May 13, 2005
    #2
    1. Advertising

  3. In article <>,
    lasek <> wrote:
    >i have made a simple script that use fork, waitpid and sigaction for
    >SIGCHLD.


    That's not a "script", that's a program.

    fork() and waitpid() are not specified by the C standards. Nor are
    several of the include files you list.

    >Now the problem, if i delete the 'sleep(1)' i can't see anything about all
    >the printf into chld_signal function.
    >If i put the 'sleep(1)' i can see all the printf, the code,status of
    >waitpid.


    >WHY ???


    Try flushing your buffers. But moreso, try reading the
    documentation about which functions may legally be called from
    a signal handling routine. If you are looking at POSIX.1-1990,
    it's in section 3.3.1.3 "Signal Actions".
    --
    'The short version of what Walter said is "You have asked a question
    which has no useful answer, please reconsider the nature of the
    problem you wish to solve".' -- Tony Mantler
    Walter Roberson, May 13, 2005
    #3
  4. lasek wrote:
    > Hi all,
    > i have made a simple script that use fork, waitpid and sigaction for
    > SIGCHLD.


    You need to post this to a newsgroup for your implementation. None of
    'fork', 'waitpid', 'sigaction', or 'SIGCHLD' have any meaning in C
    itself. These are features of your implementation. Similarly, the headers
    > #include<sys/types.h>
    > #include<sys/wait.h>
    > #include<unistd.h>

    are not standard C headers, but are implementation-specific.

    Your other 3 headers *are* standard ones:
    > #include<stdio.h>
    > #include<errno.h>
    > #include<signal.h>


    [...]

    > Now the problem, if i delete the 'sleep(1)' i can't see anything about all
    > the printf into chld_signal function.

    sleep() is not a standard C function. An implementation-specific
    newsgroup or mailing list is what you want.
    Martin Ambuhl, May 13, 2005
    #4
  5. lasek

    SM Ryan Guest

    # void chld_signal(int sig)
    # {
    # int status,child_val;
    #
    # printf("\n SIGHCHILD trapped \n");
    #
    # if(waitpid(pid,&status,0)<0)
    # {
    # fprintf(stderr, "waitpid failed\n");
    # return;
    # }
    #
    # if(WIFEXITED(status))
    # {
    # child_val=WEXITSTATUS(status);
    # printf("child's pid %d exited normally with status
    # %d\n",pid,child_val);
    # }
    # }

    You should avoid calling non-reentrant functions from a signal handler; stdio
    is generally nonreentrant.


    # Now the problem, if i delete the 'sleep(1)' i can't see anything about all
    # the printf into chld_signal function.

    When the program exits, you should expect all of its signal/interrupt handlers
    to be terminated at the same time unless you've arranged something with the
    operating system to allow persistent handlers. If you don't sleep, the next
    statement exits the program, probably before the child exits.

    (On unix, you synchronise a parent with a child exit with the wait* family
    functions (see also `man waitpid`). The SIGCHLD handler doesn't do the
    synchronise; it merely lets you know a child is awaiting synchronisation.)

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Raining down sulphur is like an endurance trial, man. Genocide is the
    most exhausting activity one can engage in. Next to soccer.
    SM Ryan, May 14, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spawnl and waitpid

    , Feb 27, 2007, in forum: Python
    Replies:
    13
    Views:
    764
  2. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    560
    Eric Snow
    Jan 8, 2009
  3. Mike

    'waitpid' query

    Mike, Jan 28, 2009, in forum: C Programming
    Replies:
    10
    Views:
    570
    Kenny McCormack
    Jan 29, 2009
  4. Fan
    Replies:
    1
    Views:
    353
    Christopher Head
    Jul 16, 2011
  5. Thomas Hafner

    chaining processes, Process.waitpid

    Thomas Hafner, Apr 14, 2007, in forum: Ruby
    Replies:
    0
    Views:
    103
    Thomas Hafner
    Apr 14, 2007
Loading...

Share This Page