Thread: get the status of the child process

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    13

    get the status of the child process

    i want to get the status of child process (when execvp is failed) in the father process so im useing the next code: (im programming on linux)
    Code:
    pid=fork();
    if(pid==0)
    {
    if(execvp(com[0],com)==-1)
    exit(-1);
    }
    else
    {
    waitpid(pid,&status,0);
    }
    but the value of status is stay a junk number.
    can someone help me in this problem ?
    thanks !

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you set status to 0 before calling waitpid() does it still contain 0 afterwards?
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by mystic-d View Post
    Code:
    if(execvp(com[0],com)==-1)
    exit(-1);
    wait();
    your child is getting exit as soon as it spawns a new process using execvp( )..
    and parent gets the same with signal, the process spawned by execvp( ) is still running..
    and is in zombie state..

    try the 'wait();' and see it must be stopped till the execvp process returns..
    C's Motto: who cares what it means? I just compile it!!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gibsosmat View Post
    your child is getting exit as soon as it spawns a new process using execvp( )..
    and parent gets the same with signal, the process spawned by execvp( ) is still running..
    and is in zombie state..

    try the 'wait();' and see it must be stopped till the execvp process returns..
    What? You can't do wait() after exit() - because you never get there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by matsp View Post
    What? You can't do wait() after exit() - because you never get there.

    --
    Mats
    its the else case not inside the if( )..
    so that the child will wait for execvp( )
    I think his problem being invalid pid provided to waitpid( )..
    if we put wait( ); there it will wait till the execvp is done well..

    if you just want to see what the status of the execvp( ) from parent process I think this is not what he should do..
    C's Motto: who cares what it means? I just compile it!!

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    13
    i was set status to be zero before the "waitpid" and afterwards its have the value: "65280".

    (but i want to get "-1"..)

  7. #7
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Code:
    #include <unistd.h>
    #include <signal.h>
    ...
    pid=fork();
    if(pid==0)
    {
         if(execvp(com[0],com)==-1)
            {
                printf("execvp failed\n");
                pause();
               exit(-1);
            }
    }
    else
    {
        waitpid(pid,&status,0);
        printf("Status: &#37;d\n",status);
        kill(pid, SIGINIT);
    }
    try this..
    Last edited by gibsosmat; 11-16-2007 at 11:10 AM.
    C's Motto: who cares what it means? I just compile it!!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you look at the manual page, there are a bunch of macros which extract the true exit status of the child process from some other information which is all encoded into the same integer.

    > afterwards its have the value: "65280".
    Which is FF00 in hex, so your -1 is in there somewhere - see the macros!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    13
    i tryed all the macros and cant get it work...
    any help will be aperciated...
    thanks !

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Tried how?
    Show us what you did.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Child process status
    By paraglidersd in forum C Programming
    Replies: 8
    Last Post: 07-24-2008, 10:51 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM