LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2004, 01:42 AM   #1
shantha
LQ Newbie
 
Registered: May 2004
Location: Bangalore
Posts: 7

Rep: Reputation: 0
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
 
Old 12-17-2004, 03:53 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're going to have to give a little more detail. Also, what language is your program written in?
 
Old 12-17-2004, 03:58 AM   #3
shantha
LQ Newbie
 
Registered: May 2004
Location: Bangalore
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-17-2004, 04:23 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Maybe man 3 daemon is what you're looking for. I've never tried it myself, but good luck.
 
Old 12-17-2004, 04:56 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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;
}
 
Old 12-17-2004, 05:00 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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".
 
Old 12-17-2004, 06:08 AM   #7
shantha
LQ Newbie
 
Registered: May 2004
Location: Bangalore
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-17-2004, 06:39 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.

Last edited by Hko; 12-17-2004 at 07:00 AM.
 
Old 12-17-2004, 07:08 AM   #9
shantha
LQ Newbie
 
Registered: May 2004
Location: Bangalore
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-17-2004, 07:26 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
AFAIK this is the only way.
If you want to keep two processes running, just fork another that does not exit...
 
Old 12-17-2004, 07:36 AM   #11
shantha
LQ Newbie
 
Registered: May 2004
Location: Bangalore
Posts: 7

Original Poster
Rep: Reputation: 0
Thank for all the replies sent.

Bye,
Shantha
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting parent process ID in sh pradeepp Programming 2 09-22-2005 06:13 AM
Regarding Parent & Chaild Process toraghun Linux - General 1 03-23-2005 08:53 AM
New process w/o current parent? novaprime Programming 2 01-04-2005 09:06 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration