fork()

S

steve

I was given the following code, and asked what the possible outputs
could be. We're learning about processes and forking.


int value;
int main(){
int pid, number = 1;
value = 2;
pid = fork();
value = value + 2;
number = number + 2;
if (pid) {
printf(" 0 %d %d \n", value, number);
waitpid(pid, NULL, 0);
}
else{
printf(" 1 %d %d \n", value, number);
}
return 0;



}


I've come up with 3 possibilities so far; I don't know if any are
right:

1) the fork() fails; the parent's pid is assigned a negative integer,
so the if() condition passes and the output is 0 4 3


2) the fork() succeeds, the child* (pid = 0 from successful fork())
prints 1 4 3 and then returns; the parent (pid = child's process id #)
starts up prints 0 4 3 and returns


3) the fork() succeeds, but the child's quantum expires^ before it
reaches the printf(), the parent (pid = child's process id #) starts,
prints 0 4 3 and then waits for the child; the child (pid = 0 from
successful fork()) starts back up and prints 1 4 3 and returns; the
parent returns


* I guess the child takes precedence over the parent (system
dependent?) because every time I've run the program the output has been

1 4 3 followed by 0 4 3.


^ I guess the CPU's quantum length would have to be incredibly small
for case 3 to happen, but it's possible, right?


I'm new to this material, so any comments would be truly appreciated.
 
F

Flash Gordon

I was given the following code, and asked what the possible outputs
could be. We're learning about processes and forking.

<snip incomplete off topic code>

Reposting a mere 4 of 5 hours after your original post is extremely
impolite. This is not a chat room and there are participants across the
globe. You should wait at least a day preferably two before assuming
that people have not seen your article.
 
K

Kenny McCormack

I was given the following code, and asked what the possible outputs
could be. We're learning about processes and forking.


int value;
int main(){
int pid, number = 1;
value = 2;
pid = fork();
value = value + 2;
number = number + 2;

Allow me to be the first to say this - and I say it from the deepness of my
heart, with all the kindness and love one has come to associate with the
helpful posts you get in this newsgroup:

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

Malcolm

Kenny McCormack said:
Allow me to be the first to say this - and I say it from the deepness of
my
heart, with all the kindness and love one has come to associate with the
helpful posts you get in this newsgroup:

Not portable. Can't discuss it here. Blah, blah, blah.
I don't have a clue about fork(). I do all my parallel jobs under MPI.

If I'm chatting away about MPI, and someone else is chatting about fork(),
we can't talk to each other. Then the volume of posts increases, because the
scope of the group is so much larger. We've got threads on Windows, threads
on Open GL, threads on the differences between various flavours of Unix,
threads on gcc options and arcana. Soon the newsgroup has fundamentally
changed, and probably not for the better.
 
K

Kenny McCormack

I don't have a clue about fork(). I do all my parallel jobs under MPI.

If I'm chatting away about MPI, and someone else is chatting about fork(),
we can't talk to each other. Then the volume of posts increases, because
the scope of the group is so much larger. We've got threads on Windows,
threads on Open GL, threads on the differences between various flavours of
Unix, threads on gcc options and arcana. Soon the newsgroup has
fundamentally changed, and probably not for the better.

Yes, indeed. All of those things are also:

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

My point exactly.
 
C

CBFalconer

Malcolm said:
I don't have a clue about fork(). I do all my parallel jobs under MPI.
... snip ...

+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
==============================================================

Without your quoting him, most of us would be blissfully unaware
that he exists.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

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
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top