KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to mimic the bash feature of running process in the background if "&" is found at the end of the command. I have the following function...and I don't think it's doing what I want it to do int execute(char* args[],int background,int *cstatus){ pid_t child; pid_t ch; /*Pid of child returned by wait*/ if ((child = fork()) == 0){ /*Child Process*/ execvp(args[0],args); fprintf(stderr, "RSI: %s: command not found\n",args[0]); /*If execvp failes*/ exit(1); }else{ /*Parent process*/ if (child== (pid_t)(-1)) { fprintf(stderr,"Fork failed\n"); exit(1); }else{ if (background==0){ /*If not running in background..wait for process to finish*/ ch = wait(cstatus); }else{ printf("%ld Started\n",(long)getpid()); /* printf("Parent: Child %ld exited with status = %ld\n", (long) ch, (long)cstatus); */ }} } 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 run "ls -l " it works as expected..but if I want to run ls in the background..and have the program keep accepting new commands I call the function with background flag set as 1 and I want it to run the process in the background, tell me that it has created the process..and then prompt to accept next command.
Tags (comma-separated)
Save Edits
Cancel