0

I searched a lot but didn't find a solution. So it can be silly question.

The format of waitpid is

pid_t waitpid (pid_t pid, int *status, int options)

The pid parameter specifies exactly which process or processes to wait for. Its values fall into four camps:

< -1
Wait for any child process whose process group ID is equal to the absolute value of this value.

-1
Wait for any child process. This is the same behavior as wait( ).

0
Wait for any child process that belongs to the same process group as the calling process. 

> 0
Wait for any child process whose pid is exactly the value provided.

Now the question is what if parent and child have different group id and group id of child is 1. How to use waitpid for this specific child? Because we can't use -1 it will tell to wait for any child.

  • Mmm, not interested in an answer? I explained you how to do what you like using waitid(). – schily Jul 4 '18 at 14:41
0

You can only wait for children from your process.

If the child changes it's process group id, the new process group id can be used as a negative number with waitpid().

BTW: the function waitpid() is deprecated since 1989. The modern function is: waitid() and it supports what you like:

waitid(idtype, id, infop, opts) 
    idtype_t        idtype; 
    id_t            id; 
    siginfo_t       *infop;         /* Must be != NULL */ 
    int             opts; 

If you like to wait for a process group, use:

waitid(P_PGID, pgid, infop, opts);

So if you really have a process under process group ID 1, call:

waitid(P_PGID, 1, infop, opts);

But since init already uses this process group id, you would need to be the init process in order to able to have children under pgid 1.

This however will not work, if you are on a platform that does not implement waitid() as a syscall but as an emulation on top of the outdated waitpid().

The advantages of waitid() are:

  • allows to cleanly specify what to wait for (e.g. P_PID P_PGID P_ALL)

  • returns all 32 bits from the exit(2) parameter in the child back to the parent process.

  • allows to wait with the flag: WNOWAIT that does not reap the child and keeps it for later in the process table.

BTW: The siginfo_t pointer in waitid() is identical to the second parameter of the signal handler function for SIGCHLD.

  • The main doubt was how to pass child group id as -1 because by this the sense will change. – Prvt_Yadv Jul 4 '18 at 15:25
  • Well, I explained how to wait for process group 1 without waiting for all processes. Wasn't this what you were asking for? – schily Jul 4 '18 at 15:27
0

You'll have some trouble getting into a situation where the process group ID of a child process is 1.

Process groups are mostly for shells running foreground and background processes (pipelines). Usually, when a new process group is started, it gets the process group id from the first process's process id. You won't get a child with PID 1, so you won't get a child with PGID 1, either.

The setpgid() call allows to move process from one process group to another, but they still need to be part of the same (login) session, so you can't move a process to PGID 1 that way either.

If you have a shell running as PID 1, then it could have PGID 1, too. But as long as the shell changes the PGID of its children, they'll have a different PGID.

  • The question was how to wait for process group 1 and you did not answer that question. – schily Jul 4 '18 at 15:40
  • @schily, strictly speaking, the question was "How to use waitpid for this specific child?". I didn't answer that, since with very high likelihood the situation won't come up. The answer to that would have been to pass the child's PID to waitpid(). Neither did I see you answer that question, btw. Your answer uses waitid(), not waitpid(), which, as you say, is different. – ilkkachu Jul 4 '18 at 16:50
  • So you did not read my answer. waitpid() is outdated since 29 years and it's successor waitid() can do it. So I did answer the question on how to wait for pgrp 1. The questioner did not mention that he is working in a computer museum with machines from the 1980s where the OS has never been updated. – schily Jul 4 '18 at 17:57
  • @schily, I read your answer. Regardless of if the function they asked for is old or has a better replacement, that's the function they asked about. They didn't ask about waiting for the child using any system call. You didn't give an answer using that particular function, and for a reason. I, also, didn't give an answer using that particular function, but for another reason. But if you start nitpicking about what the question actually asks, please at least nitpick correctly. – ilkkachu Jul 4 '18 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.