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