简体   繁体   English

Shell中管道的作业控制

[英]Job Control with Piping in Shell

I am writing a shell in C. I am trying to set up a new process group for each job. 我正在用C编写Shell。我正在尝试为每个作业设置一个新的流程组。 The first process of each pipeline becomes the leader of the job's process group, the other processes join the leader's process group. 每个管道的第一个流程成为工作流程组的领导者,其他流程加入领导者流程组。

So in this case, cat somefile | wc 因此,在这种情况下, cat somefile | wc cat somefile | wc , the cat process will be the process leader. cat somefile | wccat进程将成为流程负责人。

How can I achieve this behaviour using setpgid ? 如何使用setpgid实现此行为? If I am looping over each process, do I need to store the value of the pid of the first fork and use that as the group ID for subsequent iterations of the loop to set the appropriate process group ID in the child class? 如果我遍历每个进程,是否需要存储第一个fork的pid的值并将其用作组ID,以便在后续循环迭代中在子类中设置适当的进程组ID?

If I were you, I'd take the fork() pid that the parent recieves and do setpgid(child_pid,your_gid);. 如果我是你,我将使用父级收到的fork()pid并执行setpgid(child_pid,your_gid);。 getpgid(0) can be used to return your group id. getpgid(0)可用于返回您的组ID。 Use those two pieces of information to do setpgid(). 使用这两条信息来执行setpgid()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM