Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 56677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:34:19+00:00 2026-05-10T17:34:19+00:00

If yes, on which operating system, shell or whatever? Consider the following Java program

  • 0

If yes, on which operating system, shell or whatever?

Consider the following Java program (I’m using Java just as an example; any language would be good for this question, which is more about operation systems):

public class ExitCode {     public static void main(String args[]) {         System.exit(Integer.parseInt(args[0]));     } } 

Running it on Linux and bash, it returns always values less equal 255, e.g. (echo $? prints the exit code of the previous executed command)

> java ExitCode 2; echo $? 2  > java ExitCode 128; echo $? 128  > java ExitCode 255; echo $? 255  > java ExitCode 256; echo $? 0  > java ExitCode 65536; echo $? 0 

EDITED: most of the answers below fully explain what happens on UNIX variants. I’m still wondering about other OSes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:34:20+00:00Added an answer on May 10, 2026 at 5:34 pm

    Using wait() or waitpid()

    It is not possible on Unix and derivatives using POSIX functions like wait() and waitpid(). The exit status information returned consists of two 8-bit fields, one containing the exit status, and the other containing information about the cause of death (0 implying orderly exit under program control, other values indicating that a signal killed it, and indicating whether a core was dumped).

    Using sigaction() with SA_SIGINFO

    If you work hard, and read the POSIX specification of sigaction() and <signal.h> and Signal Actions, you will find that you can get hold of the 32-bit value passed to exit() by a child process. However, it is not completely straight-forward.

    #include <errno.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <time.h> #include <unistd.h>  static siginfo_t sig_info = { 0 }; static volatile sig_atomic_t sig_num = 0; static void *sig_ctxt = 0;  static void catcher(int signum, siginfo_t *info, void *vp) {     sig_num = signum;     sig_info = *info;     sig_ctxt = vp; }  static void set_handler(int signum) {     struct sigaction sa;     sa.sa_flags = SA_SIGINFO;     sa.sa_sigaction = catcher;     sigemptyset(&sa.sa_mask);      if (sigaction(signum, &sa, 0) != 0)     {         int errnum = errno;         fprintf(stderr, 'Failed to set signal handler (%d: %s)\n', errnum, strerror(errnum));         exit(1);     } }  static void prt_interrupt(FILE *fp) {     if (sig_num != 0)     {         fprintf(fp, 'Signal %d from PID %d (status 0x%.8X; UID %d)\n',                 sig_info.si_signo, (int)sig_info.si_pid, sig_info.si_status,                 (int)sig_info.si_uid);         sig_num = 0;     } }  static void five_kids(void) {     const int base = 0xCC00FF40;     for (int i = 0; i < 5; i++)     {         pid_t pid = fork();         if (pid < 0)             break;         else if (pid == 0)         {             printf('PID %d - exiting with status %d (0x%.8X)\n',                    (int)getpid(), base + i, base + i);             exit(base + i);         }         else         {             int status = 0;             pid_t corpse = wait(&status);             if (corpse != -1)                 printf('Child: %d; Corpse: %d; Status = 0x%.4X - waited\n', pid, corpse, (status & 0xFFFF));             struct timespec nap = { .tv_sec = 0, .tv_nsec = 1000000 }; // 1 millisecond             nanosleep(&nap, 0);             prt_interrupt(stdout);             fflush(0);         }     } }  int main(void) {     set_handler(SIGCHLD);     five_kids(); } 

    When run (program sigexit73 compiled from sigexit73.c), this produces output like:

    $ sigexit73 PID 26599 - exiting with status -872349888 (0xCC00FF40) Signal 20 from PID 26599 (status 0xCC00FF40; UID 501) Child: 26600; Corpse: 26599; Status = 0x4000 - waited PID 26600 - exiting with status -872349887 (0xCC00FF41) Signal 20 from PID 26600 (status 0xCC00FF41; UID 501) Child: 26601; Corpse: 26600; Status = 0x4100 - waited PID 26601 - exiting with status -872349886 (0xCC00FF42) Signal 20 from PID 26601 (status 0xCC00FF42; UID 501) Child: 26602; Corpse: 26601; Status = 0x4200 - waited PID 26602 - exiting with status -872349885 (0xCC00FF43) Signal 20 from PID 26602 (status 0xCC00FF43; UID 501) Child: 26603; Corpse: 26602; Status = 0x4300 - waited PID 26603 - exiting with status -872349884 (0xCC00FF44) Signal 20 from PID 26603 (status 0xCC00FF44; UID 501) $ 

    With the one millisecond call to nanosleep() removed, the output is apt to look like:

    $ sigexit73 sigexit23 PID 26621 - exiting with status -872349888 (0xCC00FF40) Signal 20 from PID 26621 (status 0xCC00FF40; UID 501) Child: 26622; Corpse: 26621; Status = 0x4000 - waited PID 26622 - exiting with status -872349887 (0xCC00FF41) PID 26623 - exiting with status -872349886 (0xCC00FF42) Signal 20 from PID 26622 (status 0xCC00FF41; UID 501) Child: 26624; Corpse: 26623; Status = 0x4200 - waited Signal 20 from PID 26623 (status 0xCC00FF42; UID 501) Child: 26625; Corpse: 26622; Status = 0x4100 - waited PID 26624 - exiting with status -872349885 (0xCC00FF43) PID 26625 - exiting with status -872349884 (0xCC00FF44) $ 

    Note that there are only three lines starting Signal here, and also only three lines ending waited; some of the signals and exit statuses are lost. This is likely to be because of timing issues between the SIGCHLD signals being set to the parent process.

    However, the key point is that 4 bytes of data can be transmitted in the exit() status when the code uses sigaction(), SIGCHLD, SA_SIGINFO to track the status.

    Just for the record, the testing was performed on a MacBook Pro running macOS Mojave 10.14.6, using GCC 9.2.0 and XCode 11.3.1. The code is also available in my SOQ (Stack Overflow Questions) repository on GitHub as file sigexit73.c in the src/so-1843-7779 sub-directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The easiest way is to just learn how to do… May 11, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer I think you're stuck with what C# gives you, and… May 11, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer This will generate the result you want dict((myTuple[1], index) for… May 11, 2026 at 8:03 pm

Related Questions

I'm working on a site which is at the core/ master of a number
I have a public property set in my form of type ListE<T> where: public
Suppose I have the following two strings containing regular expressions. How do I coalesce
I want to know if i can create a custom google maps application,on which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.