Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-15-2009, 08:40 PM   #1
LQ Newbie
Registered: Sep 2008
Posts: 21

Rep: Reputation: 15
Need some help with this program.

Respected Sir,

Please see the program below:

int i;
for(i=0; i<3; ++i)
if (fork() == 0)

The above program executes fork 3 times which will create 8 processes. I want to modify this program in such a way that it should create 7 processes. Is it possible?

Please help me out..........

Old 02-15-2009, 08:58 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Normally you should break the loop if fork returns 0 (or -1, for that matter,) unless you have some reason for having a tree of processes. I assume you get 8 processes because 2^3 is 8, but forking this way will always leave you with powers of two. So what I'm really saying is that you need something like if (fork() <= 0) break;, then have your condition stop at 7. If you have a reason not to do that, I'd really like to hear what it is.
Old 04-28-2009, 09:32 PM   #3
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
I hope it is late enough that the assignment was already collected, so if you learned anything about fork() you might appreciate:

if ( fork() | fork() ) fork();


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Total recovery: Which program? ghost4linux, YaST2? Best drive imaging program? lagu2653 Linux - Software 1 06-20-2005 02:44 PM
Key bindings program (custom pasting, program execution, etc.) jrdioko Linux - Software 2 02-05-2005 10:09 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 10:36 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-15-2003 12:18 AM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 09:19 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration