Thread: Forking

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Forking

    When you fork and modify a variable, is it modified in the parent process too?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    IIRC no. When the child process is created it gets it's own memory area.

    Test program:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main (void)
    {
      int fork_return;
      int x = 5;
    
      x++;
    
      fork_return = fork();
    
      if ( fork_return == 0)
      {
        sleep(10);
        printf("In parent: ");
        x+=10;
      }
      else
      {
        x*= 10;
        printf("In child: ");
      }
    
      printf(" %d\n", x);
    
      return 0;
    }
    Result:
    In child: 60
    In parent: 16

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Well, in that case, is there any way to start a new thread sharing the memory with its parent?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Nevermind. Just started reading up on pthreads.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yea I find multi threading more useful the multi process.
    I personally use the pth library from GNU.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Quote Originally Posted by Thantos
    IIRC no. When the child process is created it gets it's own memory area.

    Test program:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main (void)
    {
      int fork_return;
      int x = 5;
    
      x++;
    
      fork_return = fork();
    
      if ( fork_return == 0)
      {
        sleep(10);
        printf("In parent: ");
        x+=10;
      }
      else
      {
        x*= 10;
        printf("In child: ");
      }
    
      printf(" %d\n", x);
    
      return 0;
    }
    Result:
    In child: 60
    In parent: 16

    Shouldn't be like this:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main (void)
    {
      int fork_return;
      int x = 5;
    
      x++;
    
      fork_return = fork();
    
      if ( fork_return == 0)
      {
        sleep(10);
        printf("In child: ");
        x+=10;
      }
      else
      {
        x*= 10;
        printf("In parent: ");
      }
    
      printf(" %d\n", x);
    
      return 0;
    }
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Meh small mistake. Basic idea lives on.

  9. #9
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Just a simple question:

    Consider this code:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main(void) {
        int fork_return;
        fork_return = fork();
    
        if( fork_return == 0 ) {
            sleep(5);
            printf("Child only ");
        }
        else {
            printf("Parent only! ");
        }
    
        printf("I am touchabled by parent and child");
    
        return 0;
    }
    Then this:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main(void) {
        int fork_return;
        fork_return = fork();
    
        if( fork_return == 0 ) {
            sleep(5);
            printf("Child only ");
            return 0;
        }
        else {
            printf("Parent only! ");
        }
    
        printf("Now I am  touchabled by parent only");
    
        return 0;
    }
    Ok, I understand this two code. But look at this one:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main(void) {
        int fork_return;
        fork_return = fork();
    
        if( fork_return == 0 ) {
            sleep(5);
    
    
            //Why does this not show up in console
            printf("Child only ");
            //End of Why
    
    
            char *arg[] = {
                  "ls",
                  NULL
            };
            execvp("ls",arg);
        }
        else {
            printf("Parent only! ");
        }
    
        printf("Now I am touchabled by parent only");
    
        return 0;
    }
    Why?????

    Just as reference, I use Slackware 9.1 and GNU C++ compiler version 3.3.3.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Why?????
    1) Try printing a newline with that as well
    2) Try calling fflush(stdout) before the exec call

    Output is buffered, so my guess is the buffers get trashed along with everything else owned by the program, when the exec happens.
    Incomplete (ie unflushed) output is lost.
    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.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Also
    Code:
           char *arg[] = {
                  "ls",
                  NULL
            };
    needs to go before the sleep(). You can only declare variables at the beginning of a block in C (which is what I'm guessing you are using since you are using printf() and linux)

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>You can only declare variables at the beginning of a block in C
    It's a C99 extension that allows this, maybe his compiler has it too. Or maybe it's being compiled as c++
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Most likely the later then the former...

  14. #14
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Quote Originally Posted by Salem
    > Why?????
    1) Try printing a newline with that as well
    2) Try calling fflush(stdout) before the exec call

    Output is buffered, so my guess is the buffers get trashed along with everything else owned by the program, when the exec happens.
    Incomplete (ie unflushed) output is lost.
    That works. Thank you.....
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forking
    By sean in forum Linux Programming
    Replies: 5
    Last Post: 02-24-2009, 02:45 PM
  2. Forking Issue...
    By cookie in forum Linux Programming
    Replies: 1
    Last Post: 07-10-2008, 04:46 PM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. have I got forking problems, or what?
    By reptonite in forum C Programming
    Replies: 8
    Last Post: 02-23-2006, 06:11 PM
  5. Forking advice
    By zee in forum C Programming
    Replies: 4
    Last Post: 06-18-2004, 02:35 PM