Invalid commands for child process in background in C -
i have following code in c:
if ((childpid = fork()) == 0) { if (execvp(argv[0], argv) < 0) { //execute failed exit(1); } } else if (childpid < 0) { //fork failed } else { //if execvp failed don't here //else }
what want is:
i enter command.
if not executable should not wait next entered command.
if executable should things in parent process.
if enter e.g. sleep 1m
should execute in child process, things in parent process , should still able execute more jobs (this works fine). when execute abcdef
(invalid command) stuff in parent process anyway.
can tell me how code should like?
i tried following:
void signalhandler(int signal) { if (signal==sigchld) { printf("child ended\n"); wait(null); } } //in main signal(sigchld,signalhandler); //... if ((childpid = fork()) == 0) { if (execvp(t_argv[0], t_argv) < 0) { kill(getppid(),sigchld); } }
is correct? way error afterwards (when it's finished).
waitpid(childpid, &status, wnohang)
tells me finished error (-1).
one possible solution use pair of anonymous pipes, child process writes in write-end of pipe status needs pass on parent. in parent check read-end of pipe, if don't receive before child-process exits okay , child process executed program.
if parent receive before child process exits, means exec
call failed.
Comments
Post a Comment