Hi!
I've been tring to do backgrounding by having the first child process fork and then immediately exit, with the grandchild executing the requested program.
But I don't know why I keep getting two copies of output
my code for implementing background:
Code:
if(cmd->background == 1){
pid_t childPid;
if((childPid == fork()) < 0){ /* error */
perror("BAD FORK");
}else if(childPid == 0){ /* child process */
exit(0);
}
else if((childPid = fork()) == 0){ /*grandchild process */
execvp(cmd->argvs[0][0], cmd->argvs[0]);
}else{ /* parent process */
waitpid(childPid, NULL, WNOHANG);
}
Then I ran the command like this "seq 1 5", the result I get was:
Code:
shell: seq 1 5 &
shell: shell: 1
2
3
4
5
1
2
3
4
5
Can anyone tell me what's wrong with my code?