Simple Processes Program

This is a discussion on Simple Processes Program within the Linux Programming forums, part of the Platform Specific Boards category; Hello, I try to make a program which gets the number of desired childs from user, then creates them sequentially ...

  1. #1
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42

    Simple Processes Program

    Hello,
    I try to make a program which gets the number of desired childs from user, then creates them sequentially and destroys them in reverse order.
    I just don't understand why it doesn't work

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main(int argc, char** argv) {
            int pid, status,n_sons,current_son=0;
            printf("Enter number of sons:");
            scanf("%d",&n_sons);
    
            do {
                    pid=fork();
                    if(pid !=0 ) {
                            printf("Created Son, with PID:%d and PPID:%d\n",pid,getppid());
                            wait(&status);
                            printf("Son with PID:%d, terminated with status:%d\n",pid,status);
                            break;
                    }
                    else {
                           current_son++;
                    }
            }while(current_son < n_sons);
    
            return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So when does the "son" exit?

    I think you are expecting your loop to get updated from the son code, which isn't what happens, so you get an infinite loop of creating new processes (until there is no more space in the process table, and the OS returns an error, which is != 0, so it then "waits" for a process that doesn't exist. And the main process will not end until ALL of the children have died, and they are all busy trying to create new branches of themselves.

    --
    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.

  3. #3
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    as long as the number of sons is less than maximum, every son becomes a father. the son whose index is greater than maximum won't be a father, he'll finish the loop, and then all fathers.
    so the loop isn't infinite, but it doesn't work in the correct order....

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so your loop is really quite weird (because it exits in the middle using a break).

    And every child process will try to create n_sons, and every child of those will try to create n_sons dhildren, and so on and so on. Say n_sons is 2, that means 2 children, each creating 2 children -> now we have 6 processes. The last 4 creates another 8 children, which becomes 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768. It never ends (until you have run out of number of processes the machine can produce - but chances are that when that kills one of the children, there is still another that creates a new child, that attempts to create another 2, etc, etc). This is generally a good way to completely prevent anyone else from creating any process on the machine.

    --
    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.

  5. #5
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    why?
    lets say n_sons is 3, hence
    father -> creates first son -> father waits for first son to terminate
    first son -> creates second son -> first son waits for second son to terminate
    second son -> creates third son -> second son waits for third son to terminate
    third son -> creates fourth son -> fourth son ends loop and terminate itself
    third son terminates
    second son terminates
    first son terminates
    father terminates
    ...
    well that's how it suppose to work, but it actually works out of order, here's the output for n_sons=5:
    Code:
    Created Son, with PID:18656 and PPID:18577
    Created Son, with PID:18659 and PPID:18657
    Created Son, with PID:18660 and PPID:18658
    Son with PID:18660, terminated with status:0
    Son with PID:18659, terminated with status:0
    Created Son, with PID:18658 and PPID:18656
    Son with PID:18658, terminated with status:0
    Created Son, with PID:18657 and PPID:18655
    Son with PID:18657, terminated with status:0
    Son with PID:18656, terminated with status:0

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so there is no guarantee of which will run first, the child or parent process. I still don't understand your logic and how you avoid doing infinite processes, but be that as it may (I haven't actually looked CAREFULLY at your code).

    --
    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.

  7. #7
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    that's right, there's no guarantee who will run first the father or son but in the output you can see that some sons are created after some sons terminated and that shouldn't happen cause every son should wait till his son is terminated, and the termination process should begin only when the "extra" son is created !?
    that's what i don't understand !? or maybe it's some delay of output messages !?
    Last edited by khdani; 11-18-2008 at 10:37 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, 18660 is terminated before 18658 is terminated. That seems right to me. All other ones also seem OK. But maybe I'm missing something.

    --
    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.

  9. #9
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    yes it terminates them in correct order (somehow) but 18658 and 18657 are created after 18660 created and terminated, and 18657 is created just after 18658. so from the output messages it's seen that it creates them in some unsync order but terminates (somehow luckily) in order.
    i want them to be created in order, and just after the last one was created to terminate in reverse order.

  10. #10
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    ok i fixed it

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main(int argc, char** argv) {
            int pid, status,n_sons,current_son=0;
            printf("Enter number of sons:");
            scanf("&#37;d",&n_sons);
    
            do {
                    pid=fork();
                    if(pid !=0 ) {
                            wait(&status);
                            printf("Terminated Son #%d\n",current_son);
                            break;
                    }
                    else {
                           printf("Created Son #%d\n",current_son);
                           current_son++;
                    }
            }while(current_son < n_sons);
    
            return 0;
    }
    and the ouput:
    Code:
    Created Son #0
    Created Son #1
    Created Son #2
    Created Son #3
    Created Son #4
    Created Son #5
    Terminated Son #5
    Terminated Son #4
    Terminated Son #3
    Terminated Son #2
    Terminated Son #1
    Terminated Son #0
    i just don't really understand why it didn't work before !?
    Last edited by khdani; 11-18-2008 at 11:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 10:01 PM

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