Rewrited Homework of Lecture 3

フォークのお話が間違っていたみたいでして.

I did my homework of lecture 4 ,but
http://d.hatena.ne.jp/tito_3G/20070430/1177948303
It's not a thing that teacher requires.

So I'll reupdate of that. Here we go!!

Let's make child processes untill thousand generation.

before I wrote how many children same process has, but It's not true. The correct question is "Force child process to fork and make next child process and next, and time it.

I see! I wrote the program. see.

#include <stdio.h>
#include <time.h>

void fork_function( int num, clock_t based_time){


        if( num > 1000 ){
                printf( " ( '3') < the end !\n");
                exit(0);
        }

        int pid = fork();
        if( pid > 0 ){
                printf( "%d clock: I'm %d th generation!! \n", clock() - based_time, num );
                fork_function( num + 1 , based_time);
        }
        else if( pid < 0 ){
                printf( "fork error\n" );
                exit(0);
        }
        wait( pid );
}

int main(){
        fork_function( 0, clock());
}

and parts of execution result ...

0 cpu clock's: I'm 0 th generation!!
0 cpu clock's: I'm 1 th generation!!
1 cpu clock's: I'm 2 th generation!!
1 cpu clock's: I'm 3 th generation!!
1 cpu clock's: I'm 4 th generation!!
1 cpu clock's: I'm 5 th generation!!
1 cpu clock's: I'm 6 th generation!!
1 cpu clock's: I'm 7 th generation!!
1 cpu clock's: I'm 8 th generation!!
1 cpu clock's: I'm 9 th generation!!
2 cpu clock's: I'm 10 th generation!!
2 cpu clock's: I'm 11 th generation!!
2 cpu clock's: I'm 12 th generation!!
2 cpu clock's: I'm 13 th generation!!
2 cpu clock's: I'm 14 th generation!!
3 cpu clock's: I'm 15 th generation!!
3 cpu clock's: I'm 16 th generation!!
3 cpu clock's: I'm 17 th generation!!
3 cpu clock's: I'm 18 th generation!!
3 cpu clock's: I'm 19 th generation!!
4 cpu clock's: I'm 20 th generation!!
4 cpu clock's: I'm 21 th generation!!
4 cpu clock's: I'm 22 th generation!!
4 cpu clock's: I'm 23 th generation!!
4 cpu clock's: I'm 24 th generation!!
5 cpu clock's: I'm 25 th generation!!
5 cpu clock's: I'm 26 th generation!!
5 cpu clock's: I'm 27 th generation!!
5 cpu clock's: I'm 28 th generation!!
6 cpu clock's: I'm 29 th generation!!
6 cpu clock's: I'm 30 th generation!!
6 cpu clock's: I'm 31 th generation!!
6 cpu clock's: I'm 32 th generation!!
6 cpu clock's: I'm 33 th generation!!
7 cpu clock's: I'm 34 th generation!!
7 cpu clock's: I'm 35 th generation!!
7 cpu clock's: I'm 36 th generation!!
7 cpu clock's: I'm 37 th generation!!
8 cpu clock's: I'm 38 th generation!!
8 cpu clock's: I'm 39 th generation!!
8 cpu clock's: I'm 40 th generation!!
8 cpu clock's: I'm 41 th generation!!
8 cpu clock's: I'm 42 th generation!!
9 cpu clock's: I'm 43 th generation!!
9 cpu clock's: I'm 44 th generation!!
9 cpu clock's: I'm 45 th generation!!
9 cpu clock's: I'm 46 th generation!!
9 cpu clock's: I'm 47 th generation!!
9 cpu clock's: I'm 48 th generation!!
10 cpu clock's: I'm 49 th generation!!
10 cpu clock's: I'm 50 th generation!!
10 cpu clock's: I'm 51 th generation!!
10 cpu clock's: I'm 52 th generation!!
...
118 clock: I'm 628 th generation!!
118 clock: I'm 629 th generation!!
118 clock: I'm 630 th generation!!
118 clock: I'm 631 th generation!!
118 clock: I'm 632 th generation!!
118 clock: I'm 633 th generation!!
118 clock: I'm 634 th generation!!
118 clock: I'm 635 th generation!!
119 clock: I'm 636 th generation!!
119 clock: I'm 637 th generation!!
119 clock: I'm 638 th generation!!
119 clock: I'm 639 th generation!!
119 clock: I'm 640 th generation!!
119 clock: I'm 641 th generation!!
119 clock: I'm 642 th generation!!
119 clock: I'm 643 th generation!!
119 clock: I'm 644 th generation!!
119 clock: I'm 645 th generation!!
119 clock: I'm 646 th generation!!
119 clock: I'm 647 th generation!!
...
144 clock: I'm 969 th generation!!
144 clock: I'm 970 th generation!!
144 clock: I'm 971 th generation!!
144 clock: I'm 972 th generation!!
144 clock: I'm 973 th generation!!
144 clock: I'm 974 th generation!!
144 clock: I'm 975 th generation!!
144 clock: I'm 976 th generation!!
144 clock: I'm 977 th generation!!
144 clock: I'm 978 th generation!!
144 clock: I'm 979 th generation!!
144 clock: I'm 980 th generation!!
144 clock: I'm 981 th generation!!
144 clock: I'm 982 th generation!!
145 clock: I'm 983 th generation!!
145 clock: I'm 984 th generation!!
145 clock: I'm 985 th generation!!
145 clock: I'm 986 th generation!!
145 clock: I'm 987 th generation!!
145 clock: I'm 988 th generation!!
145 clock: I'm 989 th generation!!
145 clock: I'm 990 th generation!!
145 clock: I'm 991 th generation!!
145 clock: I'm 992 th generation!!
145 clock: I'm 993 th generation!!
145 clock: I'm 994 th generation!!
145 clock: I'm 995 th generation!!
145 clock: I'm 996 th generation!!
146 clock: I'm 997 th generation!!
146 clock: I'm 998 th generation!!
146 clock: I'm 999 th generation!!
146 clock: I'm 1000 th generation!!
 ( '3') < the end !

I can see my pc can make about 6.85 generations per clock. I executed this some time, I found each time is difference. The range is 125 - 220 clocks. This example may be at good conditions.