I am trying to implement the fg
command in my mini shell.
The problem is as follows:
- A process(gedit) is started in foreground.
- I stop with ctrl+z and check exit my wait loop by checking the return value of
WIFSTOPPED(status)
:
while(1){ pid_t pid_check = waitpid(pid,&signal,WNOHANG|WUNTRACED); if(pid_check == pid){ if(WIFSTOPPED(signal)){ tempid = pid_check; break; } else if(WIFEXITED(signal)) break; else if(WIFSIGNALED(signal)) break; } … }
I want this to resume when fg command is given:
kill(tempid,SIGCONT); while(1){ pid_t pid_check = waitpid(pid,&signal,WNOHANG|WUNTRACED); if(pid_check == pid){ if(WIFSTOPPED(signal)){ tempid = pid_check; printf("Here"); break; } else if(WIFEXITED(signal)) break; else if(WIFSIGNALED(signal)) break; } }
But WIFSTOPPED keeps returning non zero so waitpid is breaking and process goes in background. I tried to search online but couldn’t find anything useful.
Advertisement
Answer
One thing I notice is that you’re sending the continuation signal to tempid
, then monitoring pid
.
Check to make sure that these are set to the same PID, although it would probably be safer and more readable to just use:
kill(pid,SIGCONT);
before entering the monitoring loop.
You might also want to consider renaming your signal
variable to avoid possible clashes with the signal()
function.