Process Management – Changing Process Group of a Running Process

processprocess-groupssession

Is there a way to change PID, PPID, SID of a running process? It would make sense for the answer to be no, but I'd like to make sure.

Best Answer

A process can set its own PGID and SID with the system calls setpgid setsid. The target group/session can't be chosen arbitrarily: setpgid can only move to another process group in the same session, or create a new process group whose PGID is equal to the PID; setsid can only move the process to its own session, making the SID equal to the PID.

These calls are reserved to the process itself: a process cannot change another process's PGID or SID, with one exception: a process can change its children's PGID if they're still running the original process image (i.e. they haven't called execve to run a different program).

Some systems may allow other behaviors but I don't think any modern Unix system deviates fundamentally.

It is possible to indirectly change a process's PGID or SID by using a debugger to make the process call the setpgid or setsid system call (via ptrace). Since this requires ptrace permission, it must be done from another process running as root or as the same user, and there must not be any restriction on debugging (many modern Linux system require the debugger to be an ancestor of the debuggee).

A process's PID never changes. A process's PPID can only change once, and only for one reason: when the parent dies, the PPID changes from the parent's PID to 1 (the process is adopted by init).

Note that in some systems, a process can have different PID values (and consequently also PPID/PGID/SID since they all start as the PID of some process) depending on how you look at it. For example, with Linux namespaces, each process has a potentially different PID in each namespace where it's visible.