Determine, if a process is running in the foreground

Sometimes it is necessary to determine, whether a process can read from the keyboard. For background processes, which are started like $ procName &, os.isatty(1) gives the wrong answer. This is how it can be done:

flagForeGround = (os.getpgrp() == os.tcgetpgrp(sys.stdout.fileno()))