-
Notifications
You must be signed in to change notification settings - Fork 0
/
foo.c
34 lines (32 loc) · 772 Bytes
/
foo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
int main(int argc, char *argv[])
{
int k, n, id;
double x = 0, z;
if(argc < 2 )
n = 1; //default value
else
n = atoi ( argv[1] ); //from command line
if ( n < 0 || n > 20 )
n = 2;
x = 0;
id = 0;
for ( k = 0; k < n; k++ ) {
id = fork ();
if ( id < 0 ) {
printf(1, "%d failed in fork!\n", getpid());
} else if ( id > 0 ) { //parent
printf(1, "Parent %d creating child %d\n", getpid(), id );
wait ();
} else { // child
printf(1, "Child %d created\n",getpid());
for ( z = 0; z < 8000000.0; z += 0.01 )
x = x + 3.14 * 89.64; // useless calculations to consume CPU time
break;
}
}
exit();
}