c - Execute background process and check status from parent -



c - Execute background process and check status from parent -

i'm trying to:

execute background process fork() phone call , execvp() within children. if phone call execvp() successful, adding pid of kid process list. if phone call execvp() returned -1 (error), not add together pid list.

my problem is: if execvp() returns -1 (for instance, if executable isn't found) can't "communicate" parent proccess since i'm within kid process in fork(). , if utilize waitpid() wait child, i'm not running background process (since lose concurrency).

i tried using wnohang , checking *status (wexitstatus(status) == 1) doesn't work (since parent process executes "instantly" , doesn't know if kid has exited yet, or that's think)

my current code:

void background(char *vector[]) { pid_t childp; int status; childp=fork(); if (childp==-1){ perror("error using fork \n"); return; } if (childp==0) { //child process if (execvp(vector[0],vector)==-1) { perror("exec"); exit(1); } } waitpid(-1, &status, wnohang); if(wifexited(status)) { //child exited if (wexitstatus(status) == 1) { //child exited error return; } } if (insertprocess(childp,vector)==-1) perror("full list"); else updatelist(); }

the solution i've found @ moment inserting sleep(1) phone call before waitpid (source: http://stackoverflow.com/a/26617152/1339354 ), looks more hack done. other ideas?

if using waitpid() wnohang, sure especify kid process. if don't, waitpid() returns status of lastly kid dead.

i'm not sure if trying save on list process success. if trying it, why not instert , delete process when die? command signal sigchld. in that, utilize waitpid process died , delete in list. deactivate signal sigchld when create process, insert process on list , active 1 time again signal.

c background exec fork pid

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -