The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
fork() help alexicopax High Level Programming 3 03-08-2007 12:08 AM
shm sem fork etc... Please help Dana73 High Level Programming 1 02-28-2006 04:51 AM
Fork or what? crippe High Level Programming 0 03-08-2005 01:21 AM
fork() MKSRaja High Level Programming 2 02-07-2005 07:55 AM
Fork Deepali UNIX for Dummies Questions & Answers 5 08-26-2001 05:14 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-01-2005
Registered User
 

Join Date: Aug 2005
Posts: 36
Arrow Fork ()

hi all
About this code

for (i = 1; i < n; i++)
if ((childpid = fork()) <= 0)
break;


I really can't understand the output .

and the way fork () return the value .
how about the process Id ,the child process Id and the parent ID
in this case

so please answer me soon
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-02-2005
andryk's Avatar
Registered User
 

Join Date: Sep 2003
Posts: 448
Quote:
... and the way fork () return the value .
fork() is a bit special, you call it once but return twice
That is it returns once in the child with return code of 0, and return once in parent with retcode of the child pid, if all went right and if my memories didnt fail agen
Reply With Quote
  #3 (permalink)  
Old 11-03-2005
Registered User
 

Join Date: Oct 2005
Posts: 12
Quote:
Originally Posted by iwbasts
hi all
About this code

for (i = 1; i < n; i++)
if ((childpid = fork()) <= 0)
break;
I think there will be n process at the end. If u skip break statement then u will get 2^n process ( 2 to the power of n).

Regards,
Reply With Quote
  #4 (permalink)  
Old 11-03-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,454
Quote:
Originally Posted by parasa
I think there will be n process at the end. If u skip break statement then u will get 2^n process ( 2 to the power of n).

Regards,

If it all if there is any anything to be done by the child

there would be only 2^(n-1)-2 child processes
and 1 parent process
as for validates between and 1 and < n
Reply With Quote
  #5 (permalink)  
Old 11-08-2005
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
People try to be a little too clever with fork sometimes... whenever I use it I try to write my code like this:

Code:
pid_t pid=fork();
if(pid > 0)
{
  int status;
  /* This process has created an independent child process */

  do_other_stuff();

  /* Wait for it to finish so it doesn't become a zombie */
  wait(&status);
}
if(pid == 0)
{
  /* This process IS the child process! */
  do_stuff();

  /* Exit the program when we're done doing stuff */
  exit(0);
}
else
{
  /* No child process was created because of some sort of error */
}
Reply With Quote
  #6 (permalink)  
Old 11-09-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,454
Quote:
Originally Posted by Corona688
People try to be a little too clever with fork sometimes... whenever I use it I try to write my code like this:

Code:
pid_t pid=fork();
if(pid > 0)
{
  int status;
  /* This process has created an independent child process */

  do_other_stuff();

  /* Wait for it to finish so it doesn't become a zombie */
  wait(&status);
}
if(pid == 0)
{
  /* This process IS the child process! */
  do_stuff();

  /* Exit the program when we're done doing stuff */
  exit(0);
}
else
{
  /* No child process was created because of some sort of error */
}
this could be a better way,

pid_t pid;
if( (pid=fork()) < 0 )
{
/* Error Stuff */
}
else if(pid == 0 )
{
/* child stuff */
}
else
{
/* parent stuff */
}


the above will not mandate two if-condition checks
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0