19

Is it possible to change the parent process of a process? If yes, how?

For example,

  • how does screen manage to attach a screen session and the processes running inside it to different shell processes? Is there change of parent process?

  • I seem to heard of other ways of change of shell process in which a program is running, but I don't remember. Is there also change of parent process of the program?

  • I thought disown on a process changes the parent process of the process, simply because the name disown implies that. But I found it is not true.

  • Emacs client can attach to emacs server on a different terminal tab. Is there change of parent process?

4
  • 1
    disown just removes a given child from a shell's internal list of child processes. The child's PPID remains that of the shell. The shell has forgotten that it ever started that child, but the kernel remembers. Commented Apr 2, 2015 at 22:55
  • Does the process remember its parent?
    – Tim
    Commented Apr 2, 2015 at 23:21
  • If it wants to know, it calls getppid(2), a system call, and system calls are handled by the kernel. A program could be confused by issuing that call, saving the value, and then using that value after its parentage has changed. There is a chance of a race condition here. Commented Apr 2, 2015 at 23:52
  • Sounds like an interesting new kernel feature. Commented Jul 8, 2017 at 0:57

1 Answer 1

18

The parent process id (ppid) of a process cannot be changed outside of the kernel; there is no setppid system call. The kernel will only change the ppid to (pid) 1 after the processes parent has terminated - if the process did not respond to a signal that the parent was terminated. For this to happen, the process needs to have ignored various signals (SIGHUP, SIGTERM, etc.) beforehand.

screen(1) has a very elegant means of handling detaching and reattaching. When you first start screen, you are actually starting a user interface (ui), which by default will create a daemon (the session manager). This daemon has no terminal associated with it, a new process group (setpgrp(2)), a new session id (setsid(2)). The daemon, running as SCREEN, will then create subprocesses connected to pseudo-terminals (pty), then multiplexes the data from the ptys and the ui (screen). The subprocesses think they are talking with a real terminal.

If the ui screen terminates, the daemon SCREEN will still be running, buffering data, handling signals, waiting for a new ui, etc. because it is a different process group and in its own session. When you reattach with a new ui screen, then the daemon will continue to multiplex as it was doing before. The daemon will run continue running until all subprocesses terminate, is killed, a fatal bug is encountered or the host reboots.

9
  • Thanks. I added "Emacs client can attach to emacs server on a different terminal tab. Is there change of parent process?"
    – Tim
    Commented Apr 2, 2015 at 0:51
  • 1
    Every process has only one parent, until either the parent dies or it dies. If it dies, the point is moot. If the parent dies, then the PPID becomes 1, the init process. This is the only time the parent process would change - when the parent process terminates. Connecting via interprocess communication (pipes, sockets, etc.) has no affect on the PPID.
    – Arcege
    Commented Apr 2, 2015 at 1:37
  • How does Emacs attach a client to a server in different terminal tabs?
    – Tim
    Commented Apr 2, 2015 at 2:02
  • The server would listen on a socket (usually a UNIX domain socket file) waiting for connections. The client(s) would open a connection on that socket. Tabs are irrelevant to the communication between the client and the server, it could different tabs, different teminal emulates (xterm vs rxvt vs terminal), or could be xemacs. Each client knows where to connect, so it could be from anywhere.
    – Arcege
    Commented Apr 2, 2015 at 2:37
  • 1
    The parent process ID does not always become 1. This received wisdom absolutism has been wrong for over 3 years, now.
    – JdeBP
    Commented Apr 2, 2015 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .