Does exit() imply signal when using diff utility? - Unix

This is a discussion on Does exit() imply signal when using diff utility? - Unix ; The following is taken from a much larger program. sub diff_files($$) { my ($file1, $file2) = @_; my @cmd = ("diff", "-Nu2", "$file1", "$file2"); print "$progname: executing \"" . join(" ", @cmd) . "\"\n" if ($verbose > 1); system (@cmd); ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Does exit() imply signal when using diff utility?

  1. Does exit() imply signal when using diff utility?

    The following is taken from a much larger program.

    sub diff_files($$) {
    my ($file1, $file2) = @_;

    my @cmd = ("diff", "-Nu2", "$file1", "$file2");
    print "$progname: executing \"" . join(" ", @cmd) . "\"\n" if
    ($verbose > 1);

    system (@cmd);
    my $exit_value = $? >> 8;
    my $signal_num = $? & 127;
    my $dumped_core = $? & 128;

    error ("$cmd[0]: core dumped!") if ($dumped_core);
    error ("$cmd[0]: signal $signal_num!") if ($signal_num);
    return $exit_value;
    }

    And when I man diff:
    An exit status of 0 means no differences were found, 1 means some
    dif-
    ferences were found, and 2 means trouble.


    Does this mean if a program exits (vs return), it will generate a
    signal?! I'm sort of lost on this one because this is the only place
    in like maybe 25,000 lines of code, he does this kind of funky move.

    Chad


  2. Re: Does exit() imply signal when using diff utility?

    K-mart Cashier wrote:
    > The following is taken from a much larger program.
    >
    > sub diff_files($$) {
    > my ($file1, $file2) = @_;
    >
    > my @cmd = ("diff", "-Nu2", "$file1", "$file2");
    > print "$progname: executing \"" . join(" ", @cmd) . "\"\n" if
    > ($verbose > 1);
    >
    > system (@cmd);
    > my $exit_value = $? >> 8;
    > my $signal_num = $? & 127;
    > my $dumped_core = $? & 128;
    >
    > error ("$cmd[0]: core dumped!") if ($dumped_core);
    > error ("$cmd[0]: signal $signal_num!") if ($signal_num);
    > return $exit_value;
    > }
    >
    > And when I man diff:
    > An exit status of 0 means no differences were found, 1 means some
    > dif-
    > ferences were found, and 2 means trouble.
    >
    >
    > Does this mean if a program exits (vs return), it will generate a
    > signal?!


    No, it means the value that you pass to exit() is not precisely
    the same as the value that you get from system().

    From "man system":

    The system() function causes string to be given to the shell
    as input . . . then
    returns the exit status of the shell in the format specified
    by waitpid(2).

    Aha. Now, from "man waitpid":

    If waitpid() returns because the status of a child process
    is available, then that status may be evaluated with the
    macros defined by wstat(3XFN)

    And from "man wstat":

    WEXITSTATUS(stat)
    If the value of WIFEXITED(stat) is non-zero, this
    macro evaluates to the exit code that the child pro-
    cess passed to _exit() (see exit(2)) or exit(3C), or
    the value that the child process returned from main.

    And from (which is mentioned by "man wstat"):

    #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))

    There are several other macros in as well that give
    you other information about the process.

    Basically, what's going on here is that the operating system is
    packing lots of information about what happened with the process
    into a single integer rather than using a struct. This is probably
    because in the old days, you couldn't assign structs in C compilers
    (or at least in some that I used). Thus C programmers had a habit
    of packing as much stuff into a single integer as possible.

    - Logan

  3. Re: Does exit() imply signal when using diff utility?

    On Mar 21, 8:58 pm, Logan Shaw wrote:
    > K-mart Cashier wrote:
    > > The following is taken from a much larger program.

    >
    > > sub diff_files($$) {
    > > my ($file1, $file2) = @_;

    >
    > > my @cmd = ("diff", "-Nu2", "$file1", "$file2");
    > > print "$progname: executing \"" . join(" ", @cmd) . "\"\n" if
    > > ($verbose > 1);

    >
    > > system (@cmd);
    > > my $exit_value = $? >> 8;
    > > my $signal_num = $? & 127;
    > > my $dumped_core = $? & 128;

    >
    > > error ("$cmd[0]: core dumped!") if ($dumped_core);
    > > error ("$cmd[0]: signal $signal_num!") if ($signal_num);
    > > return $exit_value;
    > > }

    >
    > > And when I man diff:
    > > An exit status of 0 means no differences were found, 1 means some
    > > dif-
    > > ferences were found, and 2 means trouble.

    >
    > > Does this mean if a program exits (vs return), it will generate a
    > > signal?!

    >
    > No, it means the value that you pass to exit() is not precisely
    > the same as the value that you get from system().
    >
    > From "man system":
    >
    > The system() function causes string to be given to the shell
    > as input . . . then
    > returns the exit status of the shell in the format specified
    > by waitpid(2).
    >
    > Aha. Now, from "man waitpid":
    >
    > If waitpid() returns because the status of a child process
    > is available, then that status may be evaluated with the
    > macros defined by wstat(3XFN)
    >
    > And from "man wstat":
    >
    > WEXITSTATUS(stat)
    > If the value of WIFEXITED(stat) is non-zero, this
    > macro evaluates to the exit code that the child pro-
    > cess passed to _exit() (see exit(2)) or exit(3C), or
    > the value that the child process returned from main.
    >
    > And from (which is mentioned by "man wstat"):
    >
    > #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
    >
    > There are several other macros in as well that give
    > you other information about the process.
    >
    > Basically, what's going on here is that the operating system is
    > packing lots of information about what happened with the process
    > into a single integer rather than using a struct. This is probably
    > because in the old days, you couldn't assign structs in C compilers
    > (or at least in some that I used). Thus C programmers had a habit
    > of packing as much stuff into a single integer as possible.
    >
    > - Logan


    Hypethetically speaking, on the following lines of code:

    system (@cmd);
    my $exit_value = $? >> 8;
    my $signal_num = $? & 127;
    my $dumped_core = $? & 128;

    Could he gotten the same values if he would have used fork()/exec()?

    Chad


  4. Re: Does exit() imply signal when using diff utility?

    On Mar 21, 8:58 pm, Logan Shaw wrote:
    > K-mart Cashier wrote:
    > > The following is taken from a much larger program.

    >
    > > sub diff_files($$) {
    > > my ($file1, $file2) = @_;

    >
    > > my @cmd = ("diff", "-Nu2", "$file1", "$file2");
    > > print "$progname: executing \"" . join(" ", @cmd) . "\"\n" if
    > > ($verbose > 1);

    >
    > > system (@cmd);
    > > my $exit_value = $? >> 8;
    > > my $signal_num = $? & 127;
    > > my $dumped_core = $? & 128;

    >
    > > error ("$cmd[0]: core dumped!") if ($dumped_core);
    > > error ("$cmd[0]: signal $signal_num!") if ($signal_num);
    > > return $exit_value;
    > > }

    >
    > > And when I man diff:
    > > An exit status of 0 means no differences were found, 1 means some
    > > dif-
    > > ferences were found, and 2 means trouble.

    >
    > > Does this mean if a program exits (vs return), it will generate a
    > > signal?!

    >
    > No, it means the value that you pass to exit() is not precisely
    > the same as the value that you get from system().
    >
    > From "man system":
    >
    > The system() function causes string to be given to the shell
    > as input . . . then
    > returns the exit status of the shell in the format specified
    > by waitpid(2).
    >
    > Aha. Now, from "man waitpid":
    >
    > If waitpid() returns because the status of a child process
    > is available, then that status may be evaluated with the
    > macros defined by wstat(3XFN)
    >
    > And from "man wstat":
    >
    > WEXITSTATUS(stat)
    > If the value of WIFEXITED(stat) is non-zero, this
    > macro evaluates to the exit code that the child pro-
    > cess passed to _exit() (see exit(2)) or exit(3C), or
    > the value that the child process returned from main.
    >
    > And from (which is mentioned by "man wstat"):
    >
    > #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
    >
    > There are several other macros in as well that give
    > you other information about the process.
    >
    > Basically, what's going on here is that the operating system is
    > packing lots of information about what happened with the process
    > into a single integer rather than using a struct. This is probably
    > because in the old days, you couldn't assign structs in C compilers
    > (or at least in some that I used). Thus C programmers had a habit
    > of packing as much stuff into a single integer as possible.
    >
    > - Logan


    Hypethetically speaking, on the following lines of code:

    system (@cmd);
    my $exit_value = $? >> 8;
    my $signal_num = $? & 127;
    my $dumped_core = $? & 128;

    Could he gotten the same values if he would have used fork()/exec()
    instead of using system()?

    Chad


  5. Re: Does exit() imply signal when using diff utility?

    In article <1174537699.717264.41760@d57g2000hsg.googlegroups.c om>,
    "K-mart Cashier" wrote:

    > Hypethetically speaking, on the following lines of code:
    >
    > system (@cmd);
    > my $exit_value = $? >> 8;
    > my $signal_num = $? & 127;
    > my $dumped_core = $? & 128;
    >
    > Could he gotten the same values if he would have used fork()/exec()
    > instead of using system()?


    If you mean fork()/exec()/waitpid(), then yes, since that's basically
    what system() does.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

+ Reply to Thread