Thread: Need help with fork

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Need help with fork

    Hi.

    I'm new to C and now I need to make a small program that creates processes using fork but I'm having some trouble with it.

    What I need to do is to create a certain number of processes, for example 8, and inside each of these processes I need to create some others, lets say 10 for example. In each of the created processe, I need to execute a bash command.

    What is the best solution to achieve this? I've made the following test, but it doesn't create processes from the main and instead it cascades the process creation.

    Code:
    for (int x=0; x < 8; x++) {     
            pid = fork();
            if (pid == 0) { // Child
                pid = fork();
                if (pid == 0) { // Child
                    execlp("ls","ls" ,NULL);
                    exit(0);
                } 
            } else {
                execlp("ps","ps",NULL);
            }            
    }
      
    for (int x=0; x < 8; x++) {
            pidw = wait(&status);
    }
    This creates some processes, but the exec to "ps" gives me errors! Also, I want to issue the "ps" at the end so that it can show the 8 created processes.

    Thanks,
    Mike

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    but the exec to "ps" gives me errors!
    And what would those errors be?
    Also, I want to issue the "ps" at the end so that it can show the 8 created processes.
    Then why do you have it in the loop? And be aware that when you use execlp(), it will replace the currently running process, so if you use execlp() in your parent process, your program will stop running (assuming execlp() was successful).

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Quote Originally Posted by cas View Post
    And what would those errors be?
    The error is: Signal 17 (CHLD) caught by ps (procps version 3.2.8).

    Then why do you have it in the loop? And be aware that when you use execlp(), it will replace the currently running process, so if you use execlp() in your parent process, your program will stop running (assuming execlp() was successful).
    Have tried outside the loop, but without luck.

    I know that execlp like the other exec's, replace the current process, but for what I have to do, I'm forced to use them and have the main process wait for the others to finish and only then exit, but while the main processes are running, I have to issue the "ps" to present them.

    After this new explanation, where should I put the "ps" and what should be the best way to build the loop so that I have the main process creating 8 new processes and these 8 will create another 10 each.

    Thanks,
    Mike

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Loop 8 times. Each iteration, fork. If you're in the child, loop 10 times. Each iteration, execlp(). Make sure that every child either calls execlp() or exits; if execlp() fails, you'll want to exit, too.

    If you need to use execlp() to call ps, then you'll have to fork and call ps in the child. Just do this once, after your loops.

    You also should cast NULL to char* when passing it to execlp(); on some systems, passing a plain NULL won't work.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It doesn't help that you force your parent process to execlp("ps",...). That is effectively forcing your launching process to become an instance of ps. So, basically, your parent process will fork() a process and then stop (as it is now executing ps). The loop at the end (waiting for status) will never be executed. The process running ps (since it is the same one that spawned the children) will be the parent, which explains the CHLD signal.
    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.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you want the children to "create another 10 each" then you either have to do that before the exec or have the exec'ed program do it. What program are you exec'ing?

    And couldn't you use system() to run ps from the parent proc?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Quote Originally Posted by cas View Post
    Loop 8 times. Each iteration, fork. If you're in the child, loop 10 times. Each iteration, execlp(). Make sure that every child either calls execlp() or exits; if execlp() fails, you'll want to exit, too.

    If you need to use execlp() to call ps, then you'll have to fork and call ps in the child. Just do this once, after your loops.

    You also should cast NULL to char* when passing it to execlp(); on some systems, passing a plain NULL won't work.
    The problem is that the 8 processes that I need to create, must first execute a "exec" and after that they must create the 10 new processes where each of them will issue a "exec" and when these 10 processes finish, another "exec" must be issued and after all this finishes, the main process will continue to run.

    Also, the 8 processes created in first, must show in the "ps" with the same name of the original one and with the PPID of the original process.

    I've tested several ways, but without success. If someone could put a code snippet that will help me.

    Thanks,
    Mike

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are you saying that the exec done by the first 8 children all have to restart the original program and take a different path. It sounds like you need to pass an argument into the exec'ed programs.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The problem is that the 8 processes that I need to create, must first execute a "exec" and after that they must create the 10 new processes
    That's not possible unless you fork (again) before the exec. But doing that is straight-forward.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Quote Originally Posted by oogabooga View Post
    Are you saying that the exec done by the first 8 children all have to restart the original program and take a different path. It sounds like you need to pass an argument into the exec'ed programs.
    The execution is as follows, process A creates the 8 processes and these 8 processes exec some program, but these 8 processes should wait for the exec to complete and after that, create the 10 other processes where each of them exec's some programs and when they finish, a new process is created that will exec again a program. Process A must wait for the 8 processes to finish and then it will continue to run.

    Hope that with this explanation someone could present a small code snippet that can lead me to the final solution.

    Thanks,
    Mike

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jmike72 View Post
    Hope that with this explanation someone could present a small code snippet that can lead me to the final solution.
    I would hope, with that explanation, that you could produce a small code snippet that would implement the final solution.
    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.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    implement the final solution
    Ominous.

    The key to the solution is for the child to fork again.
    Code:
    loop 8 times:
        fork
        if child:
            loop 10 times:
                fork
                if child:
                    exec
            wait for kids
            exec
    wait for kids
    whatever
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork()
    By Jimb0 in forum C Programming
    Replies: 8
    Last Post: 05-01-2011, 05:19 AM
  2. Fork()
    By Drainy in forum C Programming
    Replies: 1
    Last Post: 11-13-2009, 10:08 AM
  3. How to use fork()!
    By ioat in forum C Programming
    Replies: 10
    Last Post: 06-27-2009, 07:07 AM
  4. fork(maybe)
    By linuxdude in forum C Programming
    Replies: 8
    Last Post: 04-18-2004, 09:00 PM