Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficient way to read exit codes (popen, system, exec...?) 1

Status
Not open for further replies.

LenRI

Programmer
Jul 15, 2003
13
0
0
US
I'm calling a little C program (let's say PorgA) I wrote from a C++ application (call this one ProgB) which I am in the process of writing. I know I can talk between them by other means (signals, pipes, sockets, etc...), but I am simply trying to read the exit code of ProgA after calling it from ProgB. What would be in your opinion the best way to accomplish this. Mind you I not only need to get the exit code, but also the PID of ProgA whenever I launch it.

Thanks for your help.

Len
 
Which OS/Compiler do you have?

Because something like
Code:
int status = system("myprogA");
Will work in Linux, but won't work for some windows compilers.

Since you mention PID, I'll guess Linux or Unix.
The way to get the PID and the exit status in these operating systems is something like this
Code:
pid_t pid = fork();
if ( pid == 0 ) {
  // running in child process, replace it with progA
  char *argv[] = { "./progA", NULL };
  execv( argv[0], argv );
} else {
  // running in parent, wait for progA to complete
  // and return its exit status.
  int status;
  waitpid( pid, &status, 0 );
  printf("Child %d exited with status %d\n", pid, status );
}


--
 
Thank you very much. This is exactly what I was trying to accomplish. By the way, it is on Linux.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top