Reputation: 45
I'm implementing a small shell, I have to run commands either in the background or foreground, I have to keep a list of all commands running in the background - the list is called JobList. once a command that's run in the background is done I have to remove it from the list, in order to do that I have a function called : RemoveFinishedJobs() where I use waitpid() to check if one of the commands that ran in the background is finished. However, for some reason waitpid() always returns that all the commands are finished even though it's not possible. for example when I enter the following commands:
there is a command that's special for the shell I'm implementing :
jobs - this is a command that's supposed to print all the commands that are running in the background that still haven't finished
sleep 1000&
sleep 5000&
jobs
I enter the commands without waiting so there's no way that sleep 5000& is finished by the time I'm done typing jobs!
here's the code I use to check if a command has finished :
for (auto it = this->job_list.begin(); it != this->job_list.end(); ++it)
{
int finished_pid = waitpid( (*it).pid , NULL , WNOHANG );
if ( finished_pid == (*it).pid )
{
this->job_list.erase(it);
--it;
}
}
I can't figure out what I'm doing wrong here, I'd appreciate some help :D
Upvotes: 0
Views: 61