LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Respected Sir,

Please see the program below:

main()
{
int i;
printf....
for(i=0; i<3; ++i)
if (fork() == 0)
printf.....
}

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..........

Thanks.
 
Old 02-15-2009, 07:58 PM   #2
ta0kira
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.
ta0kira
 
Old 04-28-2009, 08:32 PM   #3
johnsfine
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();
 
  


Reply


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 01:44 PM
Key bindings program (custom pasting, program execution, etc.) jrdioko Linux - Software 2 02-05-2005 09:09 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-14-2003 11:18 PM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

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