I am trying to imitate the bash function of starting a process in the background if "&" is at the end of the command. I have the following function ... and I donβt think that it does what I want it to do
int execute(char* args[],int background,int *cstatus){ pid_t child; pid_t ch; if ((child = fork()) == 0){ execvp(args[0],args); fprintf(stderr, "RSI: %s: command not found\n",args[0]); exit(1); }else{ if (child== (pid_t)(-1)) { fprintf(stderr,"Fork failed\n"); exit(1); }else{ if (background==0){ ch = wait(cstatus); }else{ printf("%ld Started\n",(long)getpid()); }} } return 0; } int wait_and_poll(int *cstatus){ pid_t status; status = waitpid(-1,cstatus,WNOHANG); if (status>0){ fprintf(stdout,"%ld Terminated.\n",(long) status); } return 0; }
If I just ran "ls -l", it works as expected. but if I want to run ls in the background ... and the program continues to accept new commands, I call the function with the background flag set to 1, and I want it to execute this process in the background, say that it created this process ... and then offer to accept the next command.
user1411893
source share