The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: problems with fork() inheritating
imparas
Newbie
Rank: 1



UID 185
Digest Posts 0
Credits 0
Posts 37
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:23 AM  Profile | P.M. 
problems with fork() inheritating



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...


Top
d16man
Newbie
Rank: 1



UID 104
Digest Posts 0
Credits 0
Posts 52
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:23 AM  Profile | P.M. 
code:

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.
Top
ch1one
Newbie
Rank: 1



UID 85
Digest Posts 0
Credits 0
Posts 58
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:24 AM  Profile | P.M. 
thanks



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
Top
carljohn
Newbie
Rank: 1



UID 83
Digest Posts 0
Credits 0
Posts 28
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:24 AM  Profile | P.M. 
the answer



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...
Top
ajscottsr
Newbie
Rank: 1



UID 44
Digest Posts 0
Credits 0
Posts 51
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:24 AM  Profile | P.M. 
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 ?


quote:
if not necessary i wouldn't want to use the sys5 sem., mess. queues or pipes..



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

 

All times are GMT, the time now is Mar 22, 2010 08:43 PM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.005442 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP