我怎么知道如果一个C程序的可执行文件在前台或后台运行吗? [英] How do I know if an C program's executable is run in foreground or background?
问题描述
在我的C程序我想,如果我的可执行文件在这样前台运行要知道
$。/ a.out的
或像这样
$ / a.out的&安培;
如果你是前台作业,
的getpgrp()== tcgetpgrp(STDOUT_FILENO)
或 STDIN_FILENO
或 STDERR_FILENO
或任何文件描述符你被附加到控制终端。 (如果你不知道,打开(为/ dev / tty的)
总是会得到你一个文件描述符到你的控制终端,如果存在的话)。
这是 OpenSSH的做什么,比处理 SIGTTIN / SIGTTOU
如果你只是想快速检查。
在另一方面,你可能已经后台运行
$ ./a.out
^ Z
[1] +停止./a.out
$ BG
[1] + ./a.out&
或前景化
$ FG
./a.out
在任何时间点。你不能指望你以后可以一次检查这一点,它仍然是真实的(或假的)。
In my C program I want to know if my executable is run in foreground like this
$./a.out
or like this
$./a.out &
If you are the foreground job,
getpgrp() == tcgetpgrp(STDOUT_FILENO)
or STDIN_FILENO
or STDERR_FILENO
or whichever file descriptor you're attached to your controlling terminal by. (If you're not sure, open("/dev/tty")
will always get you a file descriptor to your controlling terminal, if one exists.)
This is what openssh does, and is a bit easier than handling SIGTTIN/SIGTTOU
if you just want a quick check.
On the other hand, you may have been backgrounded
$ ./a.out ^Z [1]+ Stopped ./a.out $ bg [1]+ ./a.out &
or foregrounded
$ fg ./a.out
at any point in time. You cannot expect that you can check this once and it will still be true (or false) later.
这篇关于我怎么知道如果一个C程序的可执行文件在前台或后台运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!