Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在用 C 编写一个 shell。我正在尝试为每个作业设置一个新的进程组。每个管道的第一个进程成为作业进程组的领导者,其他进程加入领导者的进程组。
因此,在这种情况下,cat somefile | wc流程cat将成为流程领导者。
cat somefile | wc
cat
如何使用 实现这种行为setpgid?如果我在每个进程上循环,我是否需要存储pid第一个 fork 的值并将其用作后续循环迭代的组 ID 以在子类中设置适当的进程组 ID?
setpgid
pid
如果我是你,我会使用父母收到的 fork() pid 并执行 setpgid(child_pid,your_gid);。getpgid(0) 可用于返回您的组 ID。使用这两条信息来执行 setpgid()。