UNIX 中进程组 ID 和作业 ID 的区别 [英] Difference between process group id and job id in UNIX

查看:68
本文介绍了UNIX 中进程组 ID 和作业 ID 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我进程组 ID 和作业 ID 之间的区别.jobid 是 shell 程序的内置程序还是与内核有关?它们各自的用途是什么?当一个进程在后台运行时,是只设置了 jobid 还是设置了 pgid?setpgid()函数有什么用?

Please tell me the difference between a process group ID and a jobid. Is jobid a builtin of a shell program or is it related to the kernel? What are the uses of each of them? When a process is run in background, is only jobid set or is the pgid set as well? What are the uses of the setpgid() function?

当一个进程在后台运行时,内核是否也参与其中,还是shell负责处理后台或前台?

When a process is run in background, is the kernel also involved or does the shell take care of which is background or foreground?

推荐答案

好问题.作业 id 主要只是一个 shell 构造.内核以参与作业控制的信号形式提供支持,以及内核确切知道将作业控制信号发送到哪些进程的方式.

Good questions. The job id is mostly just a shell construct. There is support in the kernel in the form of the signals that are involved in job control, and the way in which the kernel knows exactly which processes to send the job control signals to.

严格来说,您的第一个问题的答案是作业 id 纯粹是一个 shell 创建.它的存在是因为一个管道(或者很少有另一个 shell 分组构造)可能由多个进程组成,这些进程应该作为一个单元进行控制.

Strictly speaking, the answer to your first question is that the job id is purely a shell creation. It exists because a pipeline (or, rarely, another shell grouped construct) may consist of multiple processes that should be controlled as a unit.

为了回答您的最后一个问题,shell 首先执行 fork(2) 然后执行 execve(2) 来启动所有进程.与 & 的唯一区别是 shell 不执行 wait(2)(或相关变体),因此程序可以在后台"继续.在 Unix 中,前台和后台实际上几乎没有区别.

To answer your last question, the shell starts all processes by first doing a fork(2) and then doing an execve(2). The only difference with & is that the shell does not do a wait(2) (or a related variant) and so the program can continue "in the background". There is actually little distinction in Unix between foreground and background.

进程组是由 shell 定义的关联,以便内核知道处理一组各种后台"进程的单个前台"进程.这很重要,以便后台进程在决定突然从终端读取时生成信号.(这样的终端可能连接到标准输入.)这将导致作业"生成一个信号,shell 将提示用户做某事.

The process group is an association defined by shells so that the kernel knows about a single "foreground" process that handles a set of various "background" processes. This is mainly important so that the background processes will generate a signal should they decide to suddenly read from a terminal. (Such terminal probably being connected to standard input.) This will cause the "job" to generate a signal and the shell will prompt the user to do something.

尝试 (sleep 5; read x)& 并在 6 秒后键入 return 或其他内容,以便 shell 唤醒.那时你会看到类似...

Try (sleep 5; read x)& and after 6 seconds type a return or something so that the shell wakes up. That's when you see something like...

[1]+ 停止(睡眠 5;读取 x)

[1]+ Stopped ( sleep 5; read x )

...然后你输入 fg 把它拉到前台.

...and you then type fg to pull it into the foreground.

最初,Unix 有管道,它有 &,但是没有办法在前台和后台之间移动命令或管道,也没有办法帮助突然决定读取的后台进程标准输入.

Originally, Unix had pipelines, and it had &, but there was no way to move a command or pipeline between foreground and background and no way to help a background process that suddenly decided to read standard input.

Bill Joy 和其他人在 BSD 和 csh(1) 的早期版本中添加了作业控制和内核支持.这些是由商业 Unix 逐行挑选的,并为类似工作的 Linux 内核克隆.

Job control and the kernel support for it were added by Bill Joy and others in early versions of BSD and csh(1). These were picked up line-for-line by commercial Unix and in cloned for the work-alike Linux kernel.

关于进程组和ps(1)的问题...为了支持 shell 中的作业控制,内核进程状态包括进程组 ID 和会话 ID.进程组作业是一回事,但作业号只是shell组成的句柄.如果会话 ID 与 pid 相同,则进程是会话领导者,如果 pgid 与 pid 相同,则进程是进程组领导者.我相信 ps(1) 打印的 + 正在发生一些更微妙的事情.每个终端都知道它的前台进程组是什么,所以我相信一个进程会得到一个 + if pid == pgid &&(pgid 是其控制终端的前台 pg).

Regarding the questions about process groups and ps(1)... In order to support job control in shells, the kernel process state includes a process group ID and a session ID. A process group and a job are the same thing, but a job number is just a handle the shell makes up. A process is a session leader if the session ID is the same as the pid, and a process is a process group leader if the pgid is the same as the pid. I believe something a bit more subtle is happening with the + that ps(1) prints. Each terminal knows what its foreground process group is, so I believe a process gets a + if pid == pgid && (pgid is the foreground pg for its controlling terminal).

总而言之,内核保存了几个状态项:pid、pgid、sid,一个进程可能有一个控制终端,一个终端可能有一个前台 pgid.这些凭据主要用于支持作业控制,但也用于在用户注销时撤销对终端的访问权限.

In summary, the kernel keeps several items of state: pid, pgid, sid, and a process may have a controlling terminal and a terminal may have a foreground pgid. These credentials are mostly intended to support job control but are also used to revoke access to a terminal when a user logs out.

这篇关于UNIX 中进程组 ID 和作业 ID 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆