diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..36287e344 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..e6423ff02 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -2,13 +2,28 @@ // (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process? // What happens to the variable when both the child and parent change the value of x? +// Answer: At the start the child process has the same value (100). +// When changed the variable has two values one on the main thread and one on the forked thread. + #include #include #include int main(void) { - // Your code here + + int x = 100; + + if (x == 100){ + int new_process = fork(); + if (new_process){ + printf("CP has same value as MP: %d\n", x); + printf("Child process will now change the val: "); + x = 10; + } + + } + printf("From main process the value is left the same: %d\n", x); return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..81ba892d2 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..88803d04b 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -2,13 +2,28 @@ // and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor // returned by `fopen()`? What happens when they are written to the file concurrently? +// Answer: Yes they can both access the file descriptor. The file is written to 3 times. + #include #include #include int main(void) { - // Your code here + FILE *fp; + + fp = fopen ("text.txt", "r+"); + + fprintf(fp, "%s %d\n", "We are in", getpid()); + + int new_process = fork(); + + if (new_process) + { + fprintf(fp, "%s %d\n", "We are in", getpid()); + } + + fclose(fp); return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..01cd6a13c 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,3 @@ +We are in 43508 +We are in 43508 +We are in 43508 diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..726d4ab9b Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..20e9653f7 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -6,10 +6,20 @@ #include #include #include +#include int main(void) { - // Your code here + int new_process = fork(); + // Return value of fork() + // On success, the PID of the child process is returned in the parent, and 0 is returned in the child. + + if (new_process){ + printf("hello from child\n"); + } else { + printf("goodbye from main\n"); + } + return 0; } diff --git a/ex3/tempCodeRunnerFile.c b/ex3/tempCodeRunnerFile.c new file mode 100644 index 000000000..0bb9446b9 --- /dev/null +++ b/ex3/tempCodeRunnerFile.c @@ -0,0 +1,5 @@ + + int new_process = fork(); + if (new_process){ + printf("%s from child\n", hello); + } \ No newline at end of file diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..866be13ee Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..91eed2c08 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,17 @@ int main(void) { - // Your code here + int new_process = fork(); + + // Return value of fork() + // On success, the PID of the child process is returned in the parent, and 0 is returned in the child. + + if (new_process){ + // printf("hello from child\n"); + execl("/bin/ls", "ls", NULL); + } else { + // printf("goodbye from main\n"); + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 000000000..e9be56eeb Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..89fe1d963 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -16,7 +16,34 @@ char* msg3 = "hello world #3"; int main(void) { - // Your code here + char buf[MSGSIZE]; + int p[2]; // a two-element array to hold the read and write file descriptors that are used by the pipe + + // establish our pipe, passing it the p array so that it gets populated by the read and write file descriptors + if (pipe(p) < 0) { + fprintf(stderr, "pipe failed\n"); + exit(1); + }else { + int new_process = fork(); + if (new_process){ + printf("hello from child\n"); + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + printf("goodbye from child\n"); + + } else { + printf("hello from parent\n"); + for (int i = 0; i < 3; i++) + { + // read 16 bytes of data from the read file descriptor + read(p[0], buf, MSGSIZE); + printf("\t%s\n", buf); + } + printf("goodbye from parent\n"); + } + } + return 0; } diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100755 index 000000000..d44621c60 Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..b1ebd3b59 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -20,7 +20,15 @@ and `clock_gettime()` should work just fine. int main() { - // Your code here - + struct timespec start, end; + /* now re-do this and measure CPU time */ + /* the time spent sleeping will not count (but there is a bit of overhead */ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); /* mark start time */ + printf( "" ); /* write to stdout */ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); /* mark the end time */ + + uint64_t diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + printf("elapsed process CPU time = %llu nanoseconds\n", (long long unsigned int) diff); + return 0; }