question on exit status - Minix

This is a discussion on question on exit status - Minix ; hi ive written my own shell as outlined in the exercises at the end of chapter 1 in the minix book but ive got 1 question: on replacing the core image of the child process with the new program code, ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: question on exit status

  1. question on exit status

    hi

    ive written my own shell as outlined in the exercises at the end of
    chapter 1 in the minix book but ive got 1 question:

    on replacing the core image of the child process with the new program
    code, something strange happend: if the requested executable file is
    not found, i simply have my child process exit abnormally like this:

    if (fork() == 0)
    {
    execve(args[0], args, NULL);
    _exit(-1);
    }
    else
    {
    id = wait(&status);
    printf("Exit status of process %i is %i\n", id, status);
    }

    It all works fine IF THE COMMAND IS FOUND. If it is not found, I get:

    Exit status of process 865 is -256.

    Exit status should be -1. Looks like some kind of a type mismatch
    between the exit status as returned and as read by wait() but I don't
    see what type I'd have to read here...

    Thanx,

    Martin


  2. Re: question on exit status


    sancho1980 wrote:
    > hi
    >
    > ive written my own shell as outlined in the exercises at the end of
    > chapter 1 in the minix book but ive got 1 question:
    >
    > on replacing the core image of the child process with the new program
    > code, something strange happend: if the requested executable file is
    > not found, i simply have my child process exit abnormally like this:
    >
    > if (fork() == 0)
    > {
    > execve(args[0], args, NULL);
    > _exit(-1);
    > }
    > else
    > {
    > id = wait(&status);
    > printf("Exit status of process %i is %i\n", id, status);
    > }
    >
    > It all works fine IF THE COMMAND IS FOUND. If it is not found, I get:
    >
    > Exit status of process 865 is -256.
    >
    > Exit status should be -1. Looks like some kind of a type mismatch
    > between the exit status as returned and as read by wait() but I don't
    > see what type I'd have to read here...


    Yup. That's intentional, and documented (somewhat) in the wait(2)
    manual pages.

    Exit values in Unix environments (that is, the values returned from
    exit(2) ) range from 0 to 255. There are no negative exit values.

    The wait(2) syscall returns two pieces of information in the status
    field:
    1) the numeric value of the signal that caused the child's termination,
    and
    2) the low order 8 bits of the return code.
    Two macros are usually provided to split the int status out into these
    two fields:
    WEXITSTATUS for the return code / exit status, and WTERMSIG for the
    termination signal. Other macros are provided to establish whether or
    not these two components are actually set.

    Your code presumes that the value returned by exit(2) is preserved in
    whole when passed to wait(). However, this is not the case, and the
    results you are getting probably can be interpreted as normal
    termination, returncode 255

    HTH
    --
    Lew Pitcher


  3. Re: question on exit status


    Lew Pitcher wrote:
    > sancho1980 wrote:
    > > hi
    > >
    > > ive written my own shell as outlined in the exercises at the end of
    > > chapter 1 in the minix book but ive got 1 question:
    > >
    > > on replacing the core image of the child process with the new program
    > > code, something strange happend: if the requested executable file is
    > > not found, i simply have my child process exit abnormally like this:
    > >
    > > if (fork() == 0)
    > > {
    > > execve(args[0], args, NULL);
    > > _exit(-1);
    > > }
    > > else
    > > {
    > > id = wait(&status);
    > > printf("Exit status of process %i is %i\n", id, status);
    > > }
    > >
    > > It all works fine IF THE COMMAND IS FOUND. If it is not found, I get:
    > >
    > > Exit status of process 865 is -256.
    > >
    > > Exit status should be -1. Looks like some kind of a type mismatch
    > > between the exit status as returned and as read by wait() but I don't
    > > see what type I'd have to read here...

    >
    > Yup. That's intentional, and documented (somewhat) in the wait(2)
    > manual pages.
    >
    > Exit values in Unix environments (that is, the values returned from
    > exit(2) ) range from 0 to 255. There are no negative exit values.
    >
    > The wait(2) syscall returns two pieces of information in the status
    > field:
    > 1) the numeric value of the signal that caused the child's termination,
    > and
    > 2) the low order 8 bits of the return code.
    > Two macros are usually provided to split the int status out into these
    > two fields:
    > WEXITSTATUS for the return code / exit status, and WTERMSIG for the
    > termination signal. Other macros are provided to establish whether or
    > not these two components are actually set.
    >
    > Your code presumes that the value returned by exit(2) is preserved in
    > whole when passed to wait(). However, this is not the case, and the
    > results you are getting probably can be interpreted as normal
    > termination, returncode 255


    Oops. That interpretation would be for the success side of your check.
    For the failture side, -256 would be a WEXITSTATUS of 0, and a WTERMSIG
    of 255


    --
    Lew


+ Reply to Thread