13

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?

  • 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. – Warren Young Apr 2 '15 at 22:55
  • Does the process remember its parent? – Tim Apr 2 '15 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. – Warren Young Apr 2 '15 at 23:52
  • Sounds like an interesting new kernel feature. – ChuckCottrill Jul 8 '17 at 0:57
12

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.

  • Thanks. I added "Emacs client can attach to emacs server on a different terminal tab. Is there change of parent process?" – Tim Apr 2 '15 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 Apr 2 '15 at 1:37
  • How does Emacs attach a client to a server in different terminal tabs? – Tim Apr 2 '15 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 Apr 2 '15 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 Apr 2 '15 at 15:47
-1

I understand. You need to change the kernel to write some module to do so! I think it will be usefull in some cases. For example you do some long hard work they take many resources for an hours... And when system not respond(as usual in this case) you do some unpredictable actions(because of you need to do and you don't sure you click a mouse in correct place so system not respond for a long time) and kill accidentally parent process. System usually kills all children! But if child process is root and parent only ordinal user and action do make also ordinal user this process not killed in any case! And his parent will be init with PID 1. And after system at last respond you want to restore hierarchy. But you can't!!! Standard you start upgrade system as root from terminal as ordinary user with su. Why? So to get all mistakes and warnings on console. Especially the utility to upgrade is GUI. They get this info to nothing... I remember in windows OS this can be done. There are special WinAPI functions. Why in Linux this can't be done? It's don't clear... Its simple thing!!!

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.