Thread: Why isn't this printing all 1024 processes?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Why isn't this printing all 1024 processes?

    Code:
    #include "mpos-app.h"
    
    volatile int counter;
    
    void run_child(void);
    
    void
    start(void)
    {
    pid_t p;
    int status;
    
    counter = 0;
    
    while (counter < 1025) {
    int n_started = 0;
    
    // Start as many processes as possible, until it fails to start
    // a process or it has started 1025 processes total.
    while (counter + n_started < 1025) {
    p = sys_fork();
    if (p == 0)
    run_child();
    else if (p > 0)
    n_started++;
    else
    break;
    }
    
    if (n_started == 0)
    break;
    
    // Retrieve old processes' exit status with sys_wait(),
    // to make room for new processes.
    for (p = 2; p < NPROCS; p++)
    (void) sys_wait(p);
    }
    
    sys_exit(0);
    }
    
    void
    run_child(void)
    {
    int input_counter = counter;
    
    counter++;
    
    console_printf("Process %d lives, counter %d!\n",
    sys_getpid(), input_counter);
    sys_exit(input_counter);
    }

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    while (counter + n_started < 1025)
    Last edited by strictlyC; 04-18-2009 at 07:48 PM.

  3. #3

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    LOL, Two forums and he gets almost the same responce... I did write why it was doing it but it's so simple I edited in my first post. I'm still not going to say why cause it's so simple being pointed to the while loop should be enough focus on the problem.

    But to answer the question of the original poster as to why its not doing what it suppose to. Cause it's not logical right to produce the results you desire.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by strictlyC View Post
    LOL, Two forums and he gets almost the same responce... I did write why it was doing it but it's so simple I edited in my first post. I'm still not going to say why cause it's so simple being pointed to the while loop should be enough focus on the problem.
    Well, as a matter of fact you're wrong.

    Addition (+) is higher precedence than comparison (<), so "while (counter + n_started < 1025)" and "while ((counter + n_started) < 1025)" are equivalent.

    Your suggestion was stylistic/readability concern, not one that affects the behaviour of the code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  3. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM