about "Create Process", how to get the return status?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
about "Create Process", how to get the return status?
with "system(COMMAND)", we can get the return status of COMMAND.
Then, what about fork, execvp, can they get the return status?
///////////////////////////////////////////////////////////////////////////
the follow is some code, but it doesn't get the return value:
///////////////////////////////////////////////////////////////////////////
int spawn (char* program, char** arg_list)
{
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0)
/* This is the parent process. */
return child_pid;
else {
/* Now execute PROGRAM, searching for it in the path. */
execvp (program, arg_list);
/* The execvp function returns only if an error occurs. */
fprintf (stderr, "an error occurred in execvp\n");
abort ();
}
}
//Unix version
//returns 1 on success, 0 on failure
//
int fork_exec_wait(char *cmd, //duplicate this in argv[0]
char *argv[], //last argv[] must be NULL
const char *type,//"", "p" = use path env. variable
int *result, //can be NULL
int *fd_to_close_in_child, //[in], array of fd
int fd_to_close_in_child_count) //number of valid fd
{
pid_t pid;
int j, status;
assert(cmd);
assert(argv);
assert(type);
assert(!fd_to_close_in_child_count || fd_to_close_in_child);
if (!cmd || !argv || !type) return 0;
if ((pid=fork())<0) return 0;
if (!pid) {
//child continues
for (j=0; j<fd_to_close_in_child_count; j++)
if (fd_to_close_in_child[j]) close(fd_to_close_in_child[j]);
if (strstr(type,"p")) execvp(cmd, argv);
else execv(cmd, argv);
_exit(127); //execv() failed
}
//parent continues
while (waitpid(pid,&status,0)<0) if (EINTR!=errno) return 0;
if (result) {
if (WIFEXITED(status)) *result=WEXITSTATUS(status);
else *result=255;
}
return 1;
} //fork_exec_wait()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.