First of all, i apologise for my english... I am from Slovenia.

So, lets get to the point. I am writting a research projet at collage (Computer Science) and i am writting about clone. I done it all, but stuck on the last exercise. I must write a program, that creates n processes ( n is input number when call program in bash)... Then every new created process starts his own processes again. First new created process creates one new child, second created process creates two new childs and so on ...

My program looks somethink like this. In function main, i have loop for and inside of it i am creating n-number of clones. Then, in the function that is called when child is created (lets say it function A), i get clone's PID and PPID and write it on the screen. And here starts problem. In that function, i must somehow again create new processes with another clone, that calls another funtion ( B ), where i write PID and PPID on the screen, but everythink i am trying, only one child of a child is created. the loop in funtion A is running propretly, but it creates only one clone. I tried __WCLONE but seems to not helping.

Thanks for any answers

Here is my code:

`

#include <stdlib.h>
#include <sys/shm.h>
#include <errno.h>
#include <linux/sched.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int i;
int ppid, pid;
int **child_stack;
int counter;
int num_of_process;



void process1() {
    pid=getpid();
    ppid=getppid();
    printf("PID(child of a child) =  %d \nPPID(child of a child) = %d \n",pid, ppid);
    _exit(0);
}

void process(int j) {
    pid=getpid();
    ppid=getppid();
    printf("PID =  %d \nPPID = %d \n",pid, ppid);
    for(counter=0; counter<j;counter++) {
        clone(process1, child_stack, CLONE_VFORK, NULL);
    }
    _exit(0);           
}           

int main(int argc, char** argv)
{

    if (argc < 2) {
        printf("To few parameters\n");
        exit(0);
    }
    child_stack = (void **) malloc(16384) + 16384 / sizeof (*child_stack);
    num_of_process=atoi(argv[1]);
        for(i=1; i<=num_of_process; i++)
        {
            clone(process, child_stack, CLONE_VFORK, i);

        }

    exit(0);

}

`

I ran your code and I get exactly what is expected. That is, I called $ ./clone_test 2 and I got this output:

PID =  27719 
PPID = 27718 
PID(child of a child) =  27720 
PPID(child of a child) = 27719 
PID =  27721 
PPID = 27718 
PID(child of a child) =  27722 
PPID(child of a child) = 27721 
PID(child of a child) =  27723 
PPID(child of a child) = 27721

Which means that the original process (PID 27718) spawns the child processes 27719 and 27721. And the child process 27719 spawns one child process 27720. And the second child process 27721 spawns two children, 27722 and 27723.

If I understood right, this is exactly the behavior you want, right? Can you clarify what the problem is?

You are right, that is what i want, but it seems that this code didnt work on my computer. Some guy from other forum suggested me that may be problem in stack, that i should declare and reserve a new stack pointer for processes that are created in function process and use it when a child is creating his own children. I did that and it worked.

Thanks for answer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.