Hello Friendz,

Suppose I have a program like

// A.cpp
int main()
{
     if(some_condition)
          return 1;
     
     return 0;
}

Now from another program I want to run the above program, like

//B.cpp
int main()
{
     ...
     ...
     system("A");
     ...
     ...
     return 0;
}

Here how can I catch the return value of program A from the program B.


Amit

Dani AI

Generated

Short answer for Solaris: either decode the wait-status returned by system() with the macros in <sys/wait.h>, or run the child with fork() + one of the exec*() calls and obtain its status with waitpid(). The reason you saw values like 256 instead of 1 is that the value returned by system() is the wait(2) status word (exit code in the high byte), not the raw exit code. Use WIFEXITED(status) / WEXITSTATUS(status) to extract the real exit code.

Example (POSIX, Solaris) using fork/exec and waitpid:

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

int main(void)
{
    pid_t pid = fork();
    if (pid < 0) { perror("fork"); return 1; }
    if (pid == 0) {
        char *argv[] = { "./A", NULL };
        execv(argv[0], argv);
        perror("execv");    /* exec failed */
        _exit(127);
    }

    int status;
    if (waitpid(pid, &status, 0) == -1) { perror("waitpid"); return 1; }

    if (WIFEXITED(status))
        printf("A exited with %d\n", WEXITSTATUS(status));
    else if (WIFSIGNALED(status))
        printf("A killed by signal %d\n", WTERMSIG(status));
    else
        printf("A ended with status 0x%x\n", status);

    return 0;
}

If you prefer system(), decode its return the same way:

int st = system("./A");
if (st != -1 && WIFEXITED(st)) exit_code = WEXITSTATUS(st);

Notes and troubleshooting: and were right that system() often surrounds your program with a shell (so you must decode the shell/child wait-status). ’s mention of wait4() is another variant (it returns the same status word). If spawn* man pages are missing on your box, either check man posix_spawn or stick with fork+exec/waitpid. Always check return values and use _exit() in the child after exec failure to avoid stdio buffering issues.

Recommended Answers

All 9 Replies

Member Avatar for Member #248612

What OS? If Windows try CreateProcess() followed by GetExitCodeProcess():

STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi;
    BOOL bRes = CreateProcess(
        NULL, const_cast<LPSTR>(commandLine), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    if (bRes) 
    {
        WaitForSingleObject(pi.hProcess, INFINITE);
        DWORD dwExitCode;
        GetExitCodeProcess(pi.hProcess, &dwExitCode);
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    }

What OS? If Windows try CreateProcess() followed by GetExitCodeProcess():

I am using Solaris.

check your man pages for spawn family of functions.

check your man pages for spawn family of functions.

Thanx Dragon.
But there is no manual entry for spawn family of functions.

-bash-3.00$ man spawnl
No manual entry for spawnl.
-bash-3.00$

Can u pls suggest some other way to do it.

Amit

and see wait4(). I haven't used these in over 20 years, so you will have to test them out to see how they work.

> system("A");
You may be in luck. Some implementations do the right thing with result = system("A"); The POSIX way would be to use fork(), execl() and wait(), something like

if ( fork() == 0 ) {
  execl()  // run A
} else {
  wait() // wait for A, and get the exit status
}

You'll have to read the manual pages for those functions for actual usage.

> system("A");
You may be in luck. Some implementations do the right thing with result = system("A");

Hi Salem,
In my system it is giving 256 times of the actual result.
I am using Studio 11 in Solaris 10 environment.
Can u pls explain it.

Thanx,
Amit

You mean the manual page for system() DOESN'T explain it?

This is one of those perennial questions...

system() returns the exit code of the command shell (probably bash --or if your system is really old, sh [check your /usr/bin/sh to see if it is a link to bash or not if you want to know]).
Only if your libc was made in an abnormal way will system() execute the program directly.

I've answered this before with a little module. (Alas, it is on another forum.)
Duoas's ExecProcess class
Post size is limited there, so keep scrolling for both files.

If you are new to C++ in general the following link may also be good reading.
Call external program in C++ (Linux) by Hannahlv

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.