Is there a way to change the process group of a running process?

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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply