LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to make ppid of a process as init without killing its parent (https://www.linuxquestions.org/questions/programming-9/how-to-make-ppid-of-a-process-as-init-without-killing-its-parent-267301/)

shantha 12-17-2004 01:42 AM

how to make ppid of a process as init without killing its parent
 
Hi all!!

Can i make parent of a process as init without killing that process's parent?

Thanks in advance.
Shantha

itsme86 12-17-2004 03:53 AM

You're going to have to give a little more detail. Also, what language is your program written in?

shantha 12-17-2004 03:58 AM

Hi
i want to make the parent of a process as init without killing the parent process which has created the process.
I m using C on linux platform.

itsme86 12-17-2004 04:23 AM

Maybe man 3 daemon is what you're looking for. I've never tried it myself, but good luck.

Hko 12-17-2004 04:56 AM

The daemon() function will do it, sure. But it may be a bit too much, as the process will become a real daemon. That is, it looses any terminal connection so it will not be able to print any output never more. Also it looses its process group (whatever that means remains still unclear to me).

You can just kill the parent, and init will adopt your process. If you don't want to have the parent killed, because it is your shell you don't want to exit, you can fork() your program, and just exit the parent. Like this:

Code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    if (fork()) return 0;  /* exit parent inmediately */

    /* Give the parent a second to die. */
    sleep(1);

    /* Report our new parent. Will be init (pid #1) */
    printf("\n My parent pid# is now: %d\n\n", getppid());

    /*
    * Delay below enables you to check things with something
    * like: pstree -A | less -S
    */
    sleep(30);
    return 0;
}


Hko 12-17-2004 05:00 AM

You can also do this to shell scripts.
For example, when I want to be root on my computer, I start an xterm with a red color. But I also don't want the root-xterm to exit when I exit the shell it was started from (its parent process). So I had a shell-script:
Code:

#!/bin/bash
exec xterm -bg IndianRed -fg White -title "root shell" -e su &

...but this was before I knew about "nohup".

shantha 12-17-2004 06:08 AM

Hi Hko
in your code the parent of the process is killed. Is there any way to make the parent of the forked process as init. I think u have undertood my question. I dont want to kill any process. Is there any way of doing it in linux using C.

Hko 12-17-2004 06:39 AM

Quote:

Originally posted by shantha
in your code the parent of the process is killed.
No. The parent is not killed, it exits itself by returning 0 from main(). When the program starts, it fork()s a child. This child process is running the same program. After starting a child process, the parent exits, and the child continues to run. But because the parent has exited the child does not have a parent anymore, so init "adopts" it and becomes the parent.

Quote:

Is there any way to make the parent of the forked process as init.
Yes, by making sure the parent exits...

Quote:

I think u have undertood my question.
I'm not so sure about that anymore. If you really tried and looked at the code I posted, and it's not what you were asking for, please clarify your question.

Quote:

I dont want to kill any process. Is there any way of doing it in linux using C.
Yes. The code I posted does this. Somehow you need to leave the child without a parent. If you dont want to kill it, you'll have to make a parent that exits itself. I would not know of any other way, except for calling daemon(), like itsme86 mentioned. But that does the same thing (I'm sure about that), even twice, and also some other things like loosing the connection to the terminal.

What happens in the code I posted is:
  • Before the program ("MYProgram") does anything, the situation is like this:
    Code:

    init
     \_ bash
        \_ MyProgram

    init is the parent of the shell (bash).
    the shell is the parent of MyProgram.
  • Start another process running the same program: fork()ing. After fork() the situation is something like this:
    Code:

    init
     \_ bash
        \_ MyProgram
              \_ MyProgram

  • Then fork() returns the child pid in the parent process (i.e. non-zero), so the parent (also running MyProgram) exits in the if-statement by returning from main()
    Code:

    init
     \_ bash
        <no-process-here-anymore>
              \_ MyProgram

  • The system (the kernel or init, I'm not sure) notes that the single process that runs MyProgram does not have a parent, so it's decided that init becomes its parent.
    Code:

    init
     \_ bash
     \_ MyProgram

It takes a short time for the parent process to actually disappear, therefor MyProgram wait a second before reporting (printf-statement) who its parent is.

shantha 12-17-2004 07:08 AM

Hi Hko

Thanks for your replies.
What i want is after the fork both child and parent should be running(not even exit) but only the parent of the child should be init.

Hko 12-17-2004 07:26 AM

AFAIK this is the only way.
If you want to keep two processes running, just fork another that does not exit...

shantha 12-17-2004 07:36 AM

Thank for all the replies sent.

Bye,
Shantha


All times are GMT -5. The time now is 02:12 AM.