try to the following code maybe help for you
but you need to modify this code for your requirement
and this code need to debug,it just a sample thinking of create process with recursion
layer is the count of child process
Code:
void process_tree(int layer)
{
if(layer<=0) return;
switch(fork()){
case 0:
process_tree(layer-1);
break;
case -1:
fprintf(stderr,"fork failed\n");
break;
default:
break;
}
}