simple program that I created to demonstrate the fork system call
and how memory gets copied on write from one process to another
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
int calc_pi() //I copied this function from the net somewhere
{
int r[2800 + 1];
int i, k;
int b, d;
int c = 0;
for (i = 0; i < 2800; i++) {
r[i] = 2000;
}
for (k = 2800; k > 0; k -= 14) {
d = 0;
i = k;
for (;;) {
d += r[i] * 10000;
b = 2 * i - 1;
r[i] = d % b;
d /= b;
i--;
if (i == 0) break;
d *= i;
}
//printf("%.4d", c + d / 10000);
c = d % 10000;
}
return 0;
}
int main(int argc, char ** argv)
{
int pid;
printf("I am the parent proccess \n");
int counter = 0;
while ( counter <= 2000 ) {
calc_pi();
counter += 1;
}
int * myparentmemory = malloc(10000000 * sizeof(int));
pid = fork();
sleep(10);
if (pid == 0){
sleep(1);
printf("I am the child proccess\n");
argv[0][1] = 'c';
argv[0][2] = 'h';
argv[0][3] = 'i';
argv[0][4] = 'l';
argv[0][5] = 'd';
int * mychildmemory = malloc(10000000 * sizeof(int));
memset (myparentmemory,'x',5000);
}
else {
int exitstatus = 0;
sleep(1);
printf("I am the parent of the child proccess ID %d\n", pid);
memset (myparentmemory,'x',50000);
printf("child exited with status of = %d\n",wait(&exitstatus));
}
sleep(1000);
return 0;
}