Home > Mobile >  In C Programming, how can you fork() a N number of function call to run in a child process?
In C Programming, how can you fork() a N number of function call to run in a child process?

Time:09-29

I was wondering how you can fork() N number of function calls in C in which a function gets its in own child process and the parent process will Wait() for each child process to complete. I was thinking all functions to be running concurrently with each other, aka function1 runs around the same time as function2. Then the overall program would complete (exit). My vision, is that you could think af Main() as like a parent process (I'm aware that fork duplicates all of the code in a project file) and then inside Main(), you can call a function outside to run a specific algorithm, but in its own process. Here is what I am thinking in code below:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int function1() {
    //Runs a specific algorithm in its own process
}

int function2() {
    //Runs a specific algorithm in its own process
}

int function3() {
    //Runs a specific algorithm in its own process
}

int function4() {
    //Runs a specific algorithm in its own process
}

int main() {
    //Main (thought as parent) calls function1... function4
    //All functions are running at the same time or concurrently
    //Each function gets their own child process
    
    //each process runs and Main (or parent) waits for each function as a child process to complete 
    //Then main(parent process) cleanly terminates
   return 1;
}

I am just trying to get my feet wet in Multi-threaded/Multi-Process programming, so I fully expect write/printf statements to be interleaved with eachother when you spawn more than one process threads. And I am NOT dealing with shared memory within' the different functions.

Meaning:

Prints from: function 1: Prints something 1
Prints from: function 4: Prints something 4
Prints from: function 2: Prints something 2
Prints from: function 3: Prints something 3
Prints from: function 1: Prints something 1
Prints from: function 1: Prints something 1
Prints from: function 2: Prints something 2  

Please let me know if I to clarify anything further?

CodePudding user response:

Inside the main function you would initiate a for loop to create all child processes.

pid_t childPid, pid;
int status = 0;

for (i = 0; i < N; i  ) {
    if ((childPid = fork()) == 0) {
        //execute function x for each one
        exit(0);
    }
}

Then to wait for all children

while ((pid = wait(NULL)) > 0); //Wait for all child to complete

If you want to be more precise you can store the pids and call waitpid() with the appropriate ids.

CodePudding user response:

I'm not a c programmer per-se, but here's a go:

inside main, you call fork(), and if the return value is 0, you're the child, and if it's non-zero, you're the parent, and the return value is the PID of the child you just created. In the child you'd call the function you want then return, and in the parent, you'd collect the PIDs of the children, and call wait(), or better yet waitpid() for each child.

  • Related