我如何知道 C 程序的可执行文件是在前台还是后台运行?
在前台运行
$./a.out
在我的 C 程序中,我想知道我的可执行文件是否像这样
$./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 &
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您是前台作业,
或者
STDIN_FILENO
或STDERR_FILENO
或您附加到控制终端的任何文件描述符。 (如果您不确定,open("/dev/tty")
总是会为您提供控制终端的文件描述符(如果存在)。)这就是 openssh 确实如此,并且比如果您只想快速检查,请处理
SIGTTIN/SIGTTOU
。另一方面,您可能
都处于后台或前台
在任何时间点 。你不能期望你检查一次之后它仍然是正确的(或错误的)。
If you are the foreground job,
or
STDIN_FILENO
orSTDERR_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
or foregrounded
at any point in time. You cannot expect that you can check this once and it will still be true (or false) later.
来自 Bash 参考手册:作业控制基础知识 :
因此,解决方案是为
SIGTTIN
安装信号处理程序,然后尝试从stdin
读取(关闭缓冲,否则会阻塞)。如果返回“0 bytes read”,则表明您正在前台运行。[编辑] 请注意,进程的状态可以更改。您可以使用 shell 的作业控制命令(Ctrl-Z、
bg
、fg
和jobs
)来执行此操作。From the Bash Reference Manual: Job Control Basics:
So the solution is to install a signal handler for
SIGTTIN
and then try to read fromstdin
(turn buffering off or it will block). If you get "0 bytes read" back, then you're running in the foreground.[EDIT] Note that the status of a process can change. You can use the job control commands of the shell (Ctrl-Z,
bg
,fg
andjobs
) to do this.据我所知,这是不可能的,通常也没有必要。
请解释您为什么要这样做。为什么。
To my knowledge this is not possible and usually not necessary either.
Please explain why you want to do this.
[无效]
IIRC,getppid()(在 *nix 系统上)将为您提供父 ID。如果它是 0,则“控制台”是您的父级,因此您在后台运行。
[/invalid]
[编辑]
请注意,这仅在 *nix 系统上有效(并且只有在没有人删除 /dev/tty 的情况下——无论出于何种原因)
[/编辑]
[invalid]
IIRC, getppid() (on *nix systems) will give you the parent id. if it is 0, the 'console' is your parent and so you are running in the background.
[/invalid]
[edit]
note that this is only valid on *nix systems (and then only if nobody has deleted /dev/tty -- for whatever reason)
[/edit]
您可能有多个进程
在后台运行:
使用:
fg %2
将vim进程发送回前台。要将最后一个进程发送回前台,只需使用:
fg
,不带任何内容参数。
要暂停在后台运行的进程,请使用:
-19
信号是 SIGSTOP(由 Ctrl - Z 发送的信号)。您始终可以通过键入来查看列表
kill -l
在后台/前台之间移动作业:
如果您已经键入命令并忘记使用
&
,您可以通过键入^Z (CTRL-Z)
将前台作业置于后台,以暂停该作业,然后输入bg
,将其置于后台:您可以使用
jobs
命令列出当前shell的作业。请记住,“退出 shell”也会影响作业:
向作业和进程发送信号
您可以使用 %(JOBID) 而不是进程号 (PID) 向使用作业号从当前 shell 启动的作业发送信号(包括终止信号):
发送信号对于不是从当前 shell 启动的进程或作业,您首先需要使用 ps 查找其进程号(PID)。
您可以参考这个链接:
进程和作业
Linux 中的一般作业控制命令是:
这几乎就是全部了。请注意命令中作业编号前面的 % - 这就是告诉kill您正在谈论作业而不是进程的原因。
There may be a possibility that you have more than one process
running in the background:
use:
fg %2
to send the vim process back to foreground.To send the last process back to foreground simply use:
fg
with noarguments.
To suspend the process running in the background, use:
The
-19
signal is SIGSTOP (the signal sent by Ctrl - Z) .you can always see the list by typing
kill -l
Moving jobs between background / foreground:
If you have already typed a command and forgot to use the
&
, you can put a foreground job into the background by typing^Z (CTRL-Z)
to suspend the job, followed bybg
, to put it into the background:You can list the jobs of the current shell using the
jobs
command.Just remember that "exiting shell" affects jobs as well:
Sending signals to jobs and processes
You can send signals, including termination signals, to jobs that are started from the current shell using job numbers using %(JOBID) instead of process numbers(PID):
To send signals to processes or jobs that are not started from the current shell, you first need to use
ps
to find their process numbers(PID).You can refer to this link:
processes and jobs
The general job control commands in Linux are:
That's pretty much all of them. Note the % infront of the job number in the commands - this is what tells kill you're talking about jobs and not processes.