fork inside an for

M

mutley

for(i=1;i<CONFIG_POLLER_FORKS;i++)
{
if((pid = fork()) == 0)
{
server_num=i;
break;
}
else
{
pids[i-1]=pid;
}
}

in this code:
the fork occurs just inside this little cut of code, I don't understand
if,
- in first fork(),
is create a child and it entries in if,
server_num receive a number 1
in break; i don't understand if, both child and parent break, or
just parent, and this child, continue or
execute de for() white parent?



excuse-me my inglish, I'm brazilian
 
K

Kenny McCormack

for(i=1;i<CONFIG_POLLER_FORKS;i++)
{
if((pid = fork()) == 0)

Allow me to be the first (of many) to say:

Not portable. Can't discuss it here. Blah, blah, blah.
 
R

Richard

mutley said:
for(i=1;i<CONFIG_POLLER_FORKS;i++)
{
if((pid = fork()) == 0)
{
server_num=i;
break;
}
else
{
pids[i-1]=pid;
}
}

in this code:
the fork occurs just inside this little cut of code, I don't understand
if,
- in first fork(),
is create a child and it entries in if,
server_num receive a number 1
in break; i don't understand if, both child and parent break, or
just parent, and this child, continue or
execute de for() white parent?


Its offtopic here, but ...

fork creates a NEW process. The break in the child code breaks to outside
the for loop : in a different address space. So effectively fork returns
twice - once in the child process and once in the parent process (the
else statement). The break cant break the parent because in the parent
fork return 0 - so the else statement is reached.

There are lots of tutorials available : use google.

http://yolinux.com/TUTORIALS/ForkExecProcesses.html
 
K

Keith Thompson

Richard said:
mutley said:
for(i=1;i<CONFIG_POLLER_FORKS;i++)
{
if((pid = fork()) == 0)
{
server_num=i;
break;
}
else
{
pids[i-1]=pid;
}
}
[snip]

Its offtopic here, but ...
[snip]

.... it's on topic on comp.unix.programmer.

(Please don't try to answer off-topic questions, especially when
there's a group where it's topical, full of experts who probably know
more about this than either of us.)
 
K

Keith Thompson

Frederick Gotham said:
Troll Alert: Kenny McCormack

The only way to deal with trolls is to limit your reaction to reminding
others not to respond to trolls.

Information on trolls: http://members.aol.com/intwg/trolls.htm

Frederick, at this point I think almost everyone is aware that KM is a
troll, and most of us have been doing a good job of ignoring him. By
posting this followup every time he posts, you're giving him more
attention.

My suggestion: Ignore him completely. If anyone else posts a
followup, post a gentle reminder not to feed the troll (unless someone
else has already done so).

You might also consider killfiling him, but that's entirely up to you.

Your efforts are appreciated, but I don't believe they're necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top