Programmatically check if a process is being run in the background

前端 未结 4 876
粉色の甜心
粉色の甜心 2021-01-19 16:19

2 questions:

1) Is there any Linux/Posix API to know if a process has been invoked as a background process?

linux> myprogram &
4条回答
  •  悲哀的现实
    2021-01-19 17:00

    You might want to read the tty(4) man page. From what I understand, a process in the background does not have any /dev/tty (so open-ing that file would fail).

    The TIOCNOTTY ioctl should detach the calling process from its controlling terminal.

    You might be interested by the isatty(3) function (e.g. to detect that your stdin or stdout is or not a terminal).

提交回复
热议问题