Thread: incrementing number

  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
    39,660
    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.

  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
    4,183
    No idea if this is what you want.

    process group ID

    Process Group Functions - The GNU C Library

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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-27-2009, 11:13 PM
  2. Replies: 7
    Last Post: 03-27-2008, 09: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, 03:16 PM
  5. Incrementing Build Number & Visual C++
    By kuphryn in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2001, 12:25 PM