I implemented a simple c shell to take in commands like sleep 3 &. I also implemented it to “listen” for sigchild signals once the job complete.
But how do I get the job id and command to be printed out like the ubuntu shell once it is completed?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would advise against catching
SIGCHLDsignals.A neater way to do that is to call
waitpidwith theWNOHANGoption. If it returns0, you know that the job with that particular pid is still running, otherwise that process has terminated and you fetch its exit code from thestatusparameter, and print the message accordingly.Moreover, bash doesn’t print the job completion status at the time the job completes, but rather at the time when the next command is issued, so this is a perfect fit for
waitpid.A small disadvantage of that approach is that the job process will stay as a zombie in the period between its termination and the time you call
waitpid, but that probably shouldn’t matter for a shell.