Go Back   TestMagic Forums > Test preparation > GRE Subject Tests > GRE Computer Science
Register Forum Rules FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 2009 October 11th, 02:05 PM   #1 (permalink)
I JUST got here.
 
Join Date: Aug 2009
Posts: 15
Quratulain2009 just joined TestMagic.
about fork method

function M1()
{
for(i=1;i<5;i++)
fork();
}

how many child in memory?

A) 1
B) 4
C) 0

or any other.
Quratulain2009 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2009 October 11th, 07:48 PM   #2 (permalink)
Mountain Goat
 
Join Date: Oct 2009
Posts: 3
zorthian just joined TestMagic.
Quote:
Originally Posted by Quratulain2009 View Post
function M1()
{
for(i=1;i<5;i++)
fork();
}

how many child in memory?

A) 1
B) 4
C) 0

or any other.
The key thing here is that each child process inherits the current value of i from its parent.
Here's the spawn hierarchy:
P (original parent process)
4 ( child threads)
(3 2 1 0) --> (no. of threads forked by each of the threads in the previous level)
( (2 1 0) ( 1 0) (0) )
(( (1 0) (0) ) ( (0) ) )
((( (0) )))

Adding, we get 15 child processes. The way to read the above is each non-zero entry 'i' in one level expands to i entries in the next level.

- Zorth.
zorthian is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2009 November 2nd, 01:29 PM   #3 (permalink)
Eager!
 
Join Date: May 2008
Posts: 39
Enigma211 just joined TestMagic.
Is the analysis accurate here ?

When first time fork is called with i=1 the child process will inherit the same value for i and then fork itself again. Doesn't this look like endless forks ?
Enigma211 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2009 November 2nd, 08:52 PM   #4 (permalink)
I JUST got here.
 
Join Date: Oct 2009
Posts: 8
blah321 just joined TestMagic.
No endless forks, because the child does not start running from the beginning - it starts from just after the 'fork();' statement (just like the parent, which also continues executing any statement after the 'fork();')
Child is created with i=1. There is no statement after fork();, so the child continues the loop (i++), and creates a grandchild with i=2...
blah321 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

What you can do
You cannot post new threads
You cannot post replies
You cannot post attachments
You cannot edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:31 PM.

Contact TestMagic   TestMagic Forums      Archive   Privacy Statement

TestMagic Locations   Legal   Privacy


SEO by vBSEO 3.2.0
Copyright © 2009 TestMagic
Ad Management by RedTyger

Scroll Up