Skip to content
Advertisement

Grouping child processes with setpgid()

I just don’t get the whole thing.

My process tree:

JavaScript

I want to make a process group (3, 4, 5), and send this group a signal from, say, 2.

I tried it this way:

JavaScript

Where should I place my setpgid() block? I tried placing it in 3, 0 and every other process, but setpgid()s return either “No such process” or “Operation not permitted”.

pids are stored in files, so I retrieve them just before calling setpgid()

Advertisement

Answer

A process can set the process group ID of only itself or any of its children. Furthermore, it can’t change the process group ID of one of its children after that child has called one of the exec functions. –APUE

In my opinion,

1.a grandparent can’t use setgpid() with its gradechild, you can check this easily.That’s to say, the code in pid 0 below won’t work:

JavaScript

2.you can only use setgpid() to change one’s and itselves chilld pgid,you can’t write down setpgid(pid5, pid3) in pid 3, because pid 3 and pid 5 aren’t parent and child.

So, you’d better use setgpid(someone’s pid, pgid) in itself.

But how can one process know other processes’ pid? A method is shared memory.

Here is one rough but a litte complex implement I just wrote, which don’t consider process synchronization.It works as you expected.

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement