0

For my school assignment, my professor wants me to store the state of background processes that i've made with my program and stored in a process table with each process entry being either "in progress" or "completed". To check whether a process has been completed(terminated) my professor needs me to use waitpid() to do this.

I don't know how this can be done because if i loop through all the processes i've created in the background(a bunch of sleep()'s) and i check each process by checking if waitpid() with each process's id as an argument to waitpid() and it return something other than -1(not an error), then each process will jump to the foreground when i really just want to know if that process is terminated or not.

Professor's original question:

BACKJOBS: A command that prints a list of background jobs and their current status. This feature is similar to the jobs command when using tcsh. The current status (i.e., in progress or completed) of each job can be determined by using the waitpid system call (see man waitpid for more information). The calls that report an error will be those that have already completed, so their status will be “completed”. The calls that do not result in an error will have a status of “in progress”.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
shawn a
  • 799
  • 3
  • 13
  • 21
  • 1
    Calling `waitpid()` will not cause a process to move to the foreground. If you want help with your code, you need to post it. – Barmar Oct 15 '14 at 06:45
  • 1
    Are you using the `WNOHANG` option so that `waitpid()` will return immediately, rather than waiting for the process to exit? – Barmar Oct 15 '14 at 06:46
  • Will using waitpid(pid, status, WNOHANG) return immediately with a return value and not bring the process to the foreground(if it is not terminated)? If so, can I simply loop through each process with it's respective pid using waitpid(pid, status, WNOHANG) whenever i wish to get a list of the status of every process? Because that would solve my program completely. – shawn a Oct 15 '14 at 07:44

0 Answers0