473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

fork() in a for loop

I'm writing a program to stress test the scheduler on a unix distribution. I'm creating a user specified number of processes using fork() and each process should work out prime numbers up to 1000. I'm using a for loop to create the specified number of processes but my program creates too many. I know I need to make sure only the parent forks but i'm just not sure how. Any guidance you could give me would be a massive help. Thanks

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

int prime_calc(int prime_range) {
int prime = 1;
int i;
int p;
for(i = 0; i <= prime_range; i++) {
for(p = 2; p <= prime_range; p++) {
if( i != p && i % p == 0) {
prime = 0;
break;
}
}
if(prime == 1) {
printf("Prime: %d\n", i);
}
prime = 1;
}
return 0;
}

int main() {
int prime_range = 1000;
int i;
int pid;
int pid1;
for(i = 0; i < 3; i++) {
pid1 = fork();
}
if(pid1==-1) {
perror("bad fork");
exit(0);
}
if(pid1==0) {
pid = getpid();
printf("The process id is: %d\n", pid);
prime_calc(prime_range);
}
else {
wait(0);
}
return 0;
}
Nov 21 '11 #1
2 9455
donbock
2,426 Expert 2GB
You want a parent process that forks off some number of child processes. Each child process works out the prime numbers up to 1000. You don't say, but I propose that the parent process has nothing else to do after it gets all of the children going.

This is going to sound like heresy, but I think this is a job for the ignoble goto.

Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<nchildren; i++) {
  2.    pid = fork();
  3.    if (pid == 0) goto child_only;
  4.    ...
  5.    }
  6. ...
  7. return;
  8.  
  9. child_only:
  10. ...
On another topic, are you certain this will produce the expected load on the operating system? What if the earliest [oldest] children finish their prime number task before the parent finishes forking off their younger siblings?
Nov 22 '11 #2
johny10151981
1,059 1GB
I didn't do much programming using linux and fork,

But I guess you should not use fork in loop its point less,

You can call fork once and then call your prim number counting functions in thread
Nov 22 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
3
by: PC | last post by:
Is it possible to create pipe()s (stdin/stdout) for a child process and forking a shell with execl() and controlling the shell's stdin/stdout from the parent with select()? Heres a little snippet...
6
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
27
by: steve | last post by:
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();...
11
by: ramu | last post by:
Hi All, We know that a c function never returns more than one value. Then how come the fork() function returns two values? How it is implemented? Regards
5
by: mutley | last post by:
for(i=1;i<CONFIG_POLLER_FORKS;i++) { if((pid = fork()) == 0) { server_num=i; break; } else { pids=pid;
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
4
by: fussbol12 | last post by:
I cannot find what is giving me the segfault in my code...I used printf statements to check for errors....anyone see the problem?? #include <stdio.h> #include <sys/ptrace.h> #include...
4
johny10151981
by: johny10151981 | last post by:
/*name of the built program is dmns*/ int main() { int i, j; i=fork(); if(i<0) return 1; printf("created pid=%d",i); while(1) {i=i;} //unlimited loop
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.