incrementing number

This is a discussion on incrementing number within the C Programming forums, part of the General Programming Boards category; Hi, im forking 3 times in a loop like this but the variable "count" does not increment, it stays on ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    20

    incrementing number

    Hi, im forking 3 times in a loop like this but the variable "count" does not increment, it stays on '1' and therefore this is an infinite loop, and this simple thing dont make sense to me, some extra eyes to help me plz?

    I have checked so that the pointer adress is the correct one every loop.

    Code:
    void increase(int* x)
    {
     *x += 1;
    }
    
    
    main()
    {
     int pid, i, number = 0;
    
    
     for(i = 0 ; 0 < 3 ; i++)
     {
      pid = fork();
    
    
      if(pid == -1)
      {
       exit(0);
      }
      else if(pid == 0)
      {
       increment(&number);
    
    
       sleep(3);
       exit(0);
      }
      else
      {
       waitpid(-1, NULL, 0);
    
    
      if(number >= 3)
      {
       exit(0);
      }
     }
    }
     sleep(5);
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Well, 0 will always be less than 3 in your for loop.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Code:
      pid = fork();
     
     
      if(pid == -1)
      {
       exit(0);
      }
      else if(pid == 0)
      {
       increment(&number);
    Each new process gets a new copy of number to play with.
    So as far as the parent is concerned, the number never really changes.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    20
    Thank you very much! I get it now, I made everything much more simple by just using the for loop and checking value of i because it increments

    Is it normal that the child's group PID is the same as the parent PID?

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Read this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    20
    Quote Originally Posted by std10093 View Post
    Read this.
    I think you missed that I ask about GROUP PID :P thanks anyway bro, that was usefull information even if I didnt need it atm

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    No idea if this is what you want.

    process group ID

    Process Group Functions - The GNU C Library

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Gatsu View Post
    Is it normal that the child's group PID is the same as the parent PID?
    From The Linux kernel: Processes, 10.2 Process Groups:
    Every process is member of a unique process group, identified by its process group ID. (When the process is created, it becomes a member of the process group of its parent.)

    Bye, Andreas

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Gatsu View Post
    I think you missed that I ask about GROUP PID :P thanks anyway bro, that was usefull information even if I didnt need it atm
    Yeah I missed that. Sorry
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incrementing Twice?
    By Kemaratu in forum C Programming
    Replies: 3
    Last Post: 09-28-2009, 12:13 AM
  2. Replies: 7
    Last Post: 03-27-2008, 10:15 AM
  3. Incrementing a number each time a function is called.
    By Coder87C in forum C++ Programming
    Replies: 17
    Last Post: 01-31-2005, 07:04 PM
  4. Incrementing by 2 not 1
    By Eckey in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2004, 04:16 PM
  5. Incrementing Build Number & Visual C++
    By kuphryn in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2001, 12:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21