problems with fork() inheritating

Hi,
thanks for any ideas...
in main() i wrote this code:

...

for(i=0;i<10;i++){
pid=fork();
switch(pid){
case -1 printf("error");
case 0:child();
default:waitpid(pid,NULL,0);
}
}

...

void child(){
SOMETHING;
}

If SOMETHING is not while(1){} cykle, then all children are made(but ends one after another). If it is while(1){} it blocks everything.

My QUESTION is: What should I do for creating all children and make them try to go to shared memory?(ALL forever...one after another randomly)

I can use the POSIX semaphores...
If not necessary I wouldn't want to use the SYS5 sem., mess. queues or pipes...

switch(pid){
case -1: printf("error"); break;
case 0:child(); break;
default:waitpid(pid,NULL,0);
}

Correct me if I'm wrong, but shouldn't there be a 'break' statement in the switch? In your case, even when the pid is 0, the default case will always be run, in which case it will enter waitpid, which will wait for the child to end. And since the child is running a long (infinite) while loop, it never ends and your code is always waiting.

Thanks,
Well, I tried to test it, but it didn't do anything(despite of the fact that you were right), but i tried to put the default section in comment like this:
default:;//... and I almost couldn't believe, all the children were born and STARTED randomly switch...

I probably don't understad to fork enough, cause I thought, that the default section was caghting the zombie processes(cause the child did'nt do anything and ended as soon they were born...). So it's probably completly diffrent...

If ANYBODY knows why it didn't work before, please ASNWER

It was caused by the thing that the child was living(I thought it wasn't) and I was waiting while it ends, so it never could have 10 paralel processes...

Could you please give a snippet of the code in the function child and the statements after the default section ?

Basically what is that you are trying to achieve !

Is there any program termination statements with the function child ?

Am sure, its not going to hurt or cost you some more time, if sem are replaced by ' semaphores ' and mess replaced by ' messages ' :slight_smile: