Thread: An Autonomous Child Process

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    An Autonomous Child Process

    Hello!
    First, allow me to apologize in advance, in case this question has already appeared.
    My problem consists of the following:
    I am trying to create an application that has to launch another executable without depending on it's actions. That is, that from the moment it's called, it has absolutely no bearing whatsoever on the main file.
    I've tried "forking" it, "exec"s in all their variants, but the results are disappointing...
    Here's a more 'graphic' description of what I am hoping to get:
    Code:
    // This is the main program...
    exec//or whatever it should be, is summoned
    //Running continues(of the "main"), without holding or pausing,  with the other Executable working in the background...
    Thank you for your attention,
    Thanks,
    Daniel

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    what part of the "fork/exec" part is it that you are not happy with. Perhaps your forked process needs to "untie" itself from the main process?

    Edit: Look at the setsid() and setpgrp() system calls.

    --
    Mats
    Last edited by matsp; 11-29-2008 at 12:05 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Call fork()
    Then in the child, call exec()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Unfortunately I didn't quite follow you there...

    Hi,
    Above all, let me thank you for the rapid responses.
    I regret to say however, that I wasn't able to successfully implement what you had suggested.
    I mean, I've already attempted to "fork" then "exec", but in vain.
    I was hoping that, should this not burden you too heavily, to place a snippet as an example, supposing that the two executables are "a.o", and "b.o"...
    Thank you,
    Thanks again,
    Daniel
    Last edited by danielakkerman; 11-29-2008 at 01:22 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post an example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    30
    Do something like this
    Code:
    void functionToDoWhatever(void);
    
    main(){
    
    int pid;
    
    printf("this is my main process");
    
    printf("I will now make a child process that is totally independent from the main(parent) process");
    
    pid = fork();
    
    if (pid == 0)
    functionToDoWhatever();
    
    else
    printf("fork failed. Child process could not be created");
    
    
    
    }
    
    
    void functionToDoWhatever(){
    
    print("im the functionToDoWhatever, and I run inside a child process that is independent from the main(parent) process");
    
    //put whatever code you want in here to do whatever you want.
    
    }

    pid holds the return value of fork which is an int. If it is zero then a child proces was created successfully, otherwise there was an error. If pid is zero then it was successful and the function functionTODoWhatever() will run in your child while your main function still runs.

    If im wrong someone correct/modify what im saying. I think im pretty close to how a fork() works.
    Last edited by jakemott; 11-29-2008 at 04:29 PM. Reason: forgot to put pid = fork(). I had fork() by iteself the return value wasnt being assigned to anything

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So the posted code does not actually assign pid with the result of fork().

    And fork returns TWO values (well, one value to the parent process, and another to the child process). The parent process receives the child process id, the child process receives a zero value, and if it's LESS THAN ZERO, it is an error. So you probably need an if-else if-else to determine if it is the child or parent and if the parent got a success or fail (I don't believe there is an option of a child being produced if there is a failure).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. inter process communcation, parent - child
    By tallan in forum C Programming
    Replies: 5
    Last Post: 02-28-2009, 04:04 AM
  3. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  4. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM