Skip to content

Commit

Permalink
for sub
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat authored Apr 14, 2020
1 parent ed10783 commit e68ac7d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions exercises/ex09/fork.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* Example code for Exercises in C.
Copyright 2016 Allen B. Downey
License: MIT License https://opensource.org/licenses/MIT
*/

#include <stdio.h>
Expand All @@ -19,6 +17,9 @@ License: MIT License https://opensource.org/licenses/MIT
// error information
extern int errno;

// this is a global int value that should be shared by all the
// child processes
int global_int;

// get_seconds returns the number of seconds since the
// beginning of the day, with microsecond precision
Expand All @@ -34,6 +35,23 @@ void child_code(int i)
{
sleep(i);
printf("Hello from child %d.\n", i);
// The address of the global int should be the same for all the children
printf("This is the address of global_int : %p\n", &global_int);

int* local;

if (i == 0) {
local = malloc(sizeof(int) * 5);
for (int j = 0; j < 5; j++) {
local[j] = j + 1;
}
sleep(5);
free(local);
} else {
// We should be able to access the array that was allocated
// by the first child
printf("READ : %p\n", &local[i]);
}
}

// main takes two parameters: argc is the number of command-line
Expand Down

0 comments on commit e68ac7d

Please sign in to comment.