Creating more processes with fork()


 
Thread Tools Search this Thread
Top Forums Programming Creating more processes with fork()
# 1  
Old 06-18-2012
Creating more processes with fork()

Hello people

I need help

How to make ONE process to create MORE (not one) processes with fork(). I tried several codes but do not work.

Thanks
# 2  
Old 06-18-2012
Why not post the code you have as I cant understand what you are trying to get at here...
# 3  
Old 06-18-2012
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main () {
pid_t childpid= 0;
int i, n=4;

for (i= 1; i< n; i++)
if (childpid= fork()) break;
wait();
printf("i:%d process ID:%ld parent ID:%ld child ID:%ld\n", i, (long)getpid(),(long)getppid(), (long)childpid); return 0;
}


output:
i:4 process ID:2129 parent ID:2128 child ID:0
i:3 process ID:2128 parent ID:2127 child ID:2129
i:2 process ID:2127 parent ID:2123 child ID:2128
i:1 process ID:2123 parent ID:2122 child ID:2127


as you can see the parent is not the same
# 4  
Old 06-18-2012
Write out the if condition such that the parent and child processes are handled by separate sections of code depending on the return code of the fork call...then you will all the children belonging to the same parent...
# 5  
Old 06-18-2012
can you write the code please?
thanks
# 6  
Old 06-18-2012
It's just simple logic, and being asked to do all your work for you rubs me the wrong way. Is this homework?

The fork() call returns zero in the child, and nonzero in the parent.

The child must actually exit after it's done or it will get into parts of the loop it shouldn't.
# 7  
Old 06-18-2012
but how to do that? can you post code?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making processes using fork

Can anyone help me with this? Create a parent process that gets from the command line n arguments arg1, arg2, ... , argn. The parent will create n/3 son processes, each of them will create a file with the name argi by concatenate the files argi+1 and argi+2. How can i concatenate those... (1 Reply)
Discussion started by: bunicu01
1 Replies

2. Programming

Issue when fork()ing processes

Hi guys! I'll simplify my problem. I have the following code: #include <fcntl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <signal.h> #include <fcntl.h> #include <unistd.h> #include <sys/wait.h> #define max 25 #define buffdim 50 void p1(); void p2();... (2 Replies)
Discussion started by: pfpietro
2 Replies

3. Shell Programming and Scripting

fork processes

Hi, How to count how many processes opened by fork function in perl. Thanks (10 Replies)
Discussion started by: Anjan1
10 Replies

4. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

5. UNIX for Dummies Questions & Answers

Conserving processes: execve() vs fork()

Disclaimer: This is just a rainy day experiment. There is no expected "goal" other than to understand UNIX better. After reading about fork and exec, my understanding is that forking, as the UNIX shell does by design, consequentially may sacrafice some speed versus an approach that runs in... (1 Reply)
Discussion started by: uiop44
1 Replies

6. SCO

-sh: fork failed - too many processes in sco unix 5.0.5

Dear experts, I have done a re-installation of sco unix openserver 5.0.5 and managed to create users. The problem am facing is that of one user logging in more than 5 times. How can i overcome this problem. the system give the error below. -sh: fork failed - too many processes in sco unix... (5 Replies)
Discussion started by: njoroge
5 Replies

7. Programming

Creating Multiple Processes

I am having problems creating multiple forks. I want create a certain number of forks, each call a program and each wait for a different value. How is this accomplished my loop is not doing the trick. for (i = 0; i < 5; i++) { if (fork() < 0) { //print error } ... (3 Replies)
Discussion started by: Vikings1201
3 Replies

8. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

9. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

10. Shell Programming and Scripting

Shell script creating too many processes.

I have a shell script that I am running every 60 seconds, but it is creating this process to the point that it is causing the server to perfrom poorly. Below is my script, what can I change to prevent this? while true do java -classpath .....( all my classes here) >/dev/null 2>&1 ... (3 Replies)
Discussion started by: Miller_K
3 Replies
Login or Register to Ask a Question