Skip to content

Instantly share code, notes, and snippets.

@rkoch
Last active December 24, 2015 21:39
Show Gist options
  • Save rkoch/6867081 to your computer and use it in GitHub Desktop.
Save rkoch/6867081 to your computer and use it in GitHub Desktop.
exec() Problem in Eclipse debugging mode
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int pArgc, const char* pArgv[]) {
pid_t pid = fork(); // vfork() would also be an option
if (pid < 0) {
std::cerr << "Error creating child process" << std::endl;
exit(EXIT_FAILURE);
} else if (pid == 0) {
execl("/usr/bin/wc", "/usr/bin/wc", "-l", "/some/none/existent/file.txt", (char*) 0);
// _exit(1); // Alternative possibility of exiting the process, which does not work in eclipse either
} else {
int status;
waitpid(pid, &status, 0); // waitpid() fixes the problem (see revision 1)
if (WIFEXITED(status)) {
int retcode = WEXITSTATUS(status);
if (retcode != 1) {
std::cerr << "retcode was not 1 (would indicate failure of word count)" << std::endl;
exit(EXIT_FAILURE);
}
}
}
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment