0
#include <unistd.h>
#include <stdio.h>
int child;
pid_t pid;
        int main(void)
    {
        int i ;
        for(i=0;i<3;i++)
        {
            pid = fork();

            child = getpid();

            if( pid == 0) // child
                printf("Child process %d\n", child);
        }

        return 0;
    }

This is my code.Each time the for loop is executing it's creating with fork an child process and a parent process.I don't know what process is created(child or parent) but i ask what is the child process and print the id number of it.And after executing the code it's displaying like

Child process:2001
Child process:2002
Child process:2003
Child process:2004
Child process:2005
Child process:2006
  • 5
    Read *carefully* the documentation of [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html) and [Advanced Linux Programming](http://advancedlinuxprogramming.com/) – Basile Starynkevitch Oct 25 '14 at 14:34
  • Each child is also in the breeding game as well – Ed Heal Oct 25 '14 at 14:36
  • @Deduplicator That's not a duplicate, this question isn't because of buffer. – Yu Hao Oct 25 '14 at 14:38
  • i read it many times. I don't get it , fork just duplicates a process form a parent into a child and it makes one process, and if i do like 3 times like in for i making this like 3 childs and 3 parents.It dosen't make sense to have 6-7 childs and 5 parents for looping the fork about 3 times. –  Oct 25 '14 at 14:38
  • @YuHao: Just saw my mistake. Though I'm sure there is a dupe somewhere. – Deduplicator Oct 25 '14 at 14:39
  • these answers dosen't help me with anything –  Oct 25 '14 at 14:41
  • 1
    @Hoenir they are not answers, they are comments. – Philipp Oct 25 '14 at 14:43
  • @Philipp i don't care –  Oct 25 '14 at 14:44

4 Answers4

3

A picture is worth a thousand words:

enter image description here

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
2

No Doubt the for loop is iterating over 3 times, But this is what happens

        fork1
        /    \
   fork2      fork3
   /  \        /   \
fork4 fork5  fork6 fork7

Hence you will get 7 values printed

Output on my screen

Child process 4910
Child process 4911
Child process 4912
Child process 4913
Child process 4915
Child process 4914
Child process 4916

You will get 2^n-1 processor get created

In your case 2^3-1 = 8 - 1 = 7.

Manjunath N
  • 1,365
  • 11
  • 22
  • thanks ! just the best answer know i know how to think to create just 3 childs not 7 –  Oct 25 '14 at 14:50
  • 7 additional processes, there will be 8 processes running in total – 4pie0 Oct 25 '14 at 14:51
  • hello @AB_ If you observe the program, the pid is compared against the value 0, which means child process. hence 7 process. no doubt there will be 8 processes running, i totally agree. – Manjunath N Oct 25 '14 at 14:56
0

After forking, both the child and the parent will live, and both will execute the next loop, and then both will fork, etc...

It's not clear what you want to do, but you can get the desired output by exiting in the child after printing.

Check a similar question: Confused with output of fork system call

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

There will be 7 calls to fork(). And there will be 2^3 = 8 processes in total after this. Like this: (numbers are process id)

// P - parent after fork                            
// c - child after fork              18099
//                               P,18099 c18101
//            P,18099 c18102                             P18101 c18103
//P,18099 c18104        P18102 c18110          P18101 c18105      P18103 c18112

I have tested this in the past using this code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>  
static int *glob_var;
int main(int argc, char** argv) {

    glob_var = mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE, 
                    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
    *glob_var = 0;
    /* fork is called 15 times
     * will create 16 processes
     * parent, 18099 forks 4 times,
     * it's 1st child 18101 forks 3 times
     * it's 2nd child 18102 forks 2 times(1st child of 18102, the 18110 forks 1 time)
     * it's 3rd child 18104 forks 1 time
     * 1st child of 18101, the 18103 forks 2 times
     * 2nd child of 18101, the 18105 forks 1 time
     * 1st child of 18103, the 18112 forks 1 time
     */
    int pid; int i = 0;                                       
    for( i = 0; i < 4; ++i)
        pid = fork();
    sleep(1); printf("fork, pid:%d\n", pid);
    (*glob_var)++;
    fflush( stdout); sleep(1);
    /* I am child, print */
    if( pid==0) printf( "glob_var:%d\n", *glob_var);
    fflush( stdout); sleep(10);
    munmap(glob_var, sizeof *glob_var);
    return (EXIT_SUCCESS);
}
4pie0
  • 29,204
  • 9
  • 82
  • 118