problem with mutltiple fork()


 
Thread Tools Search this Thread
Top Forums Programming problem with mutltiple fork()
# 1  
Old 03-07-2012
problem with mutltiple fork()

Hi, can someone please help me with creating mutltiple fork.. I was expecting something like this:

I am a child: 1 PID: 1215
I am a child: 2 PID: 1216
I am a child: 3 PID: 1217
I am a child: 4 PID: 1218

I am a child: 5 PID: 1219
I am a child: 6 PID: 1215
I am a child: 7 PID: 1216
I am a child: 8 PID: 1217
I am a child: 9 PID: 1218

I am a child: 10 PID: 1219
I am a child: 11 PID: 1215
I am a child: 12 PID: 1216
I am a child: 13 PID: 1217
I am a child: 14 PID: 1218

I am a child: 15 PID: 1219
I am a child: 16 PID: 1215
I am a child: 17 PID: 1216
I am a child: 18 PID: 1217
I am a child: 19 PID: 1218

I am a child: 20 PID: 1219

but i get this on output:

I am a child: 1 PID: 1950
I am a child: 2 PID: 1951
I am a child: 3 PID: 1952
I am a child: 4 PID: 1953
I am a child: 5 PID: 1954
I am a child: 6 PID: 1955
I am a child: 7 PID: 1956
I am a child: 8 PID: 1957
I am a child: 9 PID: 1958
I am a child: 10 PID: 1959
I am a child: 11 PID: 1960
I am a child: 12 PID: 1961
I am a child: 13 PID: 1962
I am a child: 14 PID: 1963
I am a child: 15 PID: 1964
ladislav@ubuntu:~$ I am a child: 16 PID: 1965
I am a child: 17 PID: 1966
I am a child: 20 PID: 1969
I am a child: 18 PID: 1967
I am a child: 19 PID: 1968


Here is my source code. Thank
you for your help in advance.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void Fork_Children (int N_Children)
{
    int i;
    int pid;
    for (i = 1; i <= N_Children; i++) 
    {
        pid = fork();
       
        if (pid == 0) 
        {
            printf("I am a child: %d PID: %d\n",i, getpid());
            sleep(5);            
            return;
        }
    }
}

int main (int argc, char *argv[]) 
{
    Fork_Children(atoi(argv[1]));
    return 0;
}

# 2  
Old 03-07-2012
Your main() program doesn't wait for its children, so finishes first, and you get a shell prompt before the children even finish sleeping.

To wait for child processes, do wait().

Code:
for(n=1; n<atoi(argv[1]); n++)
{
  int s;
  wait(&2);
}

# 3  
Old 03-07-2012
you men something like this:
because it didn't work.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main (int argc, char *argv[])
{
    int i;
    int pid;
    for (i = 1; i <= atoi(argv[1]); i++) 
    {
        pid = fork();
        wait(&2); 
        if (pid == 0) 
        {
            printf("I am a child: %d PID: %d\n",i, getpid());
                   
            return 0;
        }
    }
}

# 4  
Old 03-07-2012
I meant the code I actually gave you, right at the end of main(), after your function but before the return 0. I don't know where you got wait(&2). It takes a pointer to a variable.
# 5  
Old 03-07-2012
I have solved it with wait(NULL). But now I have no idea how to put pipes into it. I mean the first child generate for example random number and send it via pipe to another children and to the last child. I was thinking about to use 2d array pipe (int pipe[Number_of_children][2]), but I don't know how to put it into the for loop..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main (int argc, char *argv[])
{
int i;
int pid;
for (i = 1; i <= atoi(argv[1]); i++) 
{
pid = fork();
wait(NULL);
if (pid == 0) 
{	

printf("I am a child: %d PID: %d\n",i, getpid());

return 0;
}
}
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Very basic problem with fork() using c

Hi guys, I have the following code: int main(int argc, char *argv) { int pid1,pid2,i=0; pid1=fork(); i+=2; if(!pid1) i++; if(i%3) pid2=fork(); if (pid2==0) { printf("sea \n "); i-=1; } if(i>=2)... (4 Replies)
Discussion started by: pfpietro
4 Replies

2. UNIX for Dummies Questions & Answers

Problem with fork() while reading files

Good evening everyone. I have my finals and I'm facing a problem: I have a for cycle that is supposed to fork 2 children but somehow it forks only the first one. What am I doing wrong ? #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>... (1 Reply)
Discussion started by: pfpietro
1 Replies

3. Programming

Problem with fork() and execlp process

Hello everyone, this is my first post. I have a task to use a fork to create multiple processes and then use execlp to run another program to add 2 numbers. The problem I am having is we are supposed to use the exit() call in the execlp to return the small integer. This is a bad way to... (3 Replies)
Discussion started by: Johnathan_1017
3 Replies

4. Programming

help in C of fork() problem

i am a beginner of C, and i tired to fork multiple child and all of them belongs to same parents and each of child responsible for printing individual data. but i don't have any idea how to do...... Can any body help? thanks a lot really. (7 Replies)
Discussion started by: wendy1089
7 Replies

5. Programming

Fork and then exec problem with signals

Hi All, In my program i am handling SIGHUP signal. In the handler i fork and then exec on child process same binary file which is running. Parent process will die after 10 mins. Now my child process which was exec with same binary file is not receiving SIGHUP signal. Below is the progran code:... (6 Replies)
Discussion started by: sushil_shalin
6 Replies

6. UNIX for Dummies Questions & Answers

simple fork() problem

I have this little program ... int main(void){ printf("Before"); fork(); printf("After"); } output is this..... BeforeAfterBeforeAfter Shouldnt it be.....BeforeAfterAfter After parent is forked child receives the copy of program and continues from next statement... (3 Replies)
Discussion started by: joker40
3 Replies

7. Programming

problem implementing fork

Hi, I was honing my linux programming skill when this nuisance started bugging me. I wanted to create an empty file creator program. While creating a large file it must print # for progress bar. But the output shows it happening reverse way. ie. first it copies file and shows the progress... (7 Replies)
Discussion started by: dheerajsuthar
7 Replies

8. UNIX for Advanced & Expert Users

Problem due to Fork Error

Hi, I have developed a datastage job...which has many process running in parallel..but because of Fork Error my job is not working:( Can any body help me out to solve this Fork error problem.:rolleyes: My Os is SUNOS. IS there any setting in Unix through admin where in if i set some paramter... (8 Replies)
Discussion started by: Amey Joshi
8 Replies

9. Programming

fork() problem

i'm just trying to make 2 process read from the same 1 line a time. For some reason only the child reads. #include<stdio.h> #include <sys/types.h> void getlinefromfilep(void); void getlinefromfilec(void); int see=0; FILE * fileptr1; //need globe variable to tell pro3 to stop main()... (3 Replies)
Discussion started by: ddx08
3 Replies

10. Programming

fork problem

Hi, Consider the following piece of code: int main(void) { int i; pid_t pidp; for (i=0;i<4;i++) { switch (pidp=fork()) { case -1: fprintf(stdout, "Error during fork.\n"); exit (1); case 0: fprintf(stdout, "From child: I am... (4 Replies)
Discussion started by: qntmteleporter
4 Replies
Login or Register to Ask a Question