From f85e7e6f9d3afafc68eb9a803943f893eefc9acc Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Wed, 13 Feb 2019 14:35:05 -0600 Subject: [PATCH 01/10] ex1.c complete --- ex1/ex1.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..5ff601acd 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -8,7 +8,42 @@ int main(void) { - // Your code here + printf("BEFORE FORK (pid: %d) \n", (int)getpid()); + int x = 10; + int *xp = &x; + + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + printf("Greetings from child (pid: %d)\n", f); + printf("Childs x: %i\n", x); + printf("Childs pointer to x: %p\n", xp); + printf("Childs pointer to x value: %i\n", *xp); + x = x + 5; + printf("%s\n", "Child adding 5 to x"); + printf("Childs x: %i\n", x); + printf("Childs pointer to x: %p\n", xp); + printf("Childs pointer to x value: %i\n", *xp); + } + else + { + // we are in the parent branch + printf("Greetings from parent (pid: %d)\n", f); + printf("Parents x: %i\n", x); + printf("Parents pointer to x: %p\n", xp); + printf("Parents pointer to x value: %i\n", *xp); + x = x + 5; + printf("%s\n", "Parent adding 5 to x"); + printf("Parents x: %i\n", x); + printf("Parents pointer to x: %p\n", xp); + printf("Parents pointer to x value: %i\n", *xp); + } return 0; } From bbb889373801a806a132ab97af353c87fe8fc376 Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Wed, 13 Feb 2019 14:43:57 -0600 Subject: [PATCH 02/10] more pointer experiments in ex1.c --- ex1/ex1.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ex1/ex1.c b/ex1/ex1.c index 5ff601acd..27fd48ee1 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -30,6 +30,9 @@ int main(void) printf("Childs x: %i\n", x); printf("Childs pointer to x: %p\n", xp); printf("Childs pointer to x value: %i\n", *xp); + int *npx = &x; + printf("Childs NEW pointer to x: %p\n", npx); + printf("%s\n", "---END CHILD---\n\n"); } else { @@ -43,6 +46,9 @@ int main(void) printf("Parents x: %i\n", x); printf("Parents pointer to x: %p\n", xp); printf("Parents pointer to x value: %i\n", *xp); + int *npx = &x; + printf("Childs NEW pointer to x: %p\n", npx); + printf("%s\n", "---END PARENT---\n\n"); } return 0; From a7783f7826a81ef7ea717347c867082fdce45d23 Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Wed, 13 Feb 2019 15:17:08 -0600 Subject: [PATCH 03/10] ex2.c complete --- ex2/ex2.c | 29 +++++++++++++++++++++++++---- ex2/text.txt | 2 ++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..2d7dfe6d4 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -1,5 +1,5 @@ -// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory -// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor +// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory +// 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? #include @@ -8,7 +8,28 @@ int main(void) { - // Your code here - + FILE *fp; + fp = fopen("text.txt", "r+"); + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + fprintf(fp, "Greetings from child: %d\n", (int)getpid()); + } + else + { + // we are in the parent branch + fprintf(fp, "Greetings from parent: %d\n", (int)getpid()); + } + fclose(fp); + return 0; } + +// Answer yes they were both able to write +// The parent wrote first the the child diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..690ec1d0f 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,2 @@ +Greetings from parent: 10998 +Greetings from child: 10999 From 6d6fe031cb2a31d28c4b359ccf4c51fca6a3786f Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Wed, 13 Feb 2019 15:33:15 -0600 Subject: [PATCH 04/10] ex3.c complete --- .gitignore | 2 ++ ex3/ex3.c | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e43b0f988..2fea5da15 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .DS_Store +examples +a.out \ No newline at end of file diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..fdef59496 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,23 @@ int main(void) { - // Your code here + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + printf("%s\n", "hello"); + } + else + { + // we are in the parent branch + wait(NULL); + printf("%s\n", "goodbye"); + } return 0; } From 4d7716e090a33596652d104b4463698cf391e134 Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Wed, 13 Feb 2019 15:56:02 -0600 Subject: [PATCH 05/10] ex4.c complete --- ex4/ex4.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..60cdf8300 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -1,6 +1,6 @@ // Write a program that calls `fork()` and then calls some form of `exec()` -// to run the program `/bin/ls`. Try a few variants of `exec()`, such as -// `execl()`, `execle()`, `execv()`, and others. Why do you think there +// to run the program `/bin/ls`. Try a few variants of `exec()`, such as +// `execl()`, `execle()`, `execv()`, and others. Why do you think there // are so many variants of the same basic call? #include @@ -10,7 +10,25 @@ int main(void) { - // Your code here + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + char *args[] = {"ls", "-a", NULL}; + // execvp(args[0], args); + execvp(args[0], args); + } + else + { + // we are in the parent branch + int wc = waitpid(f, NULL, 0); + printf("%s\n", "Child finished, exiting program"); + } return 0; } From a447184ce921f9d30603dee7ba018291573f210e Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Wed, 13 Feb 2019 16:59:55 -0600 Subject: [PATCH 06/10] Stuck on ex5.c, compiles fine, hangs indefinately --- ex5/ex5.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..9aa3d0787 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -1,7 +1,7 @@ // Write a program that forks a child and creates a shared pipe -// between the parent and child processes. Have the child write -// the three messages to the parent and have the parent print out -// the messages. +// between the parent and child processes. Have the child write +// the three messages to the parent and have the parent print out +// the messages. #include #include @@ -10,13 +10,42 @@ #define MSGSIZE 16 -char* msg1 = "hello world #1"; -char* msg2 = "hello world #2"; -char* msg3 = "hello world #3"; +char *msg1 = "hello world #1"; +char *msg2 = "hello world #2"; +char *msg3 = "hello world #3"; int main(void) { // Your code here - + int f = fork(); + char inbuf[MSGSIZE]; + int p[2]; + if (pipe(p) < 0) + { + fprintf(stderr, "pipe failde\n"); + exit(1); + } + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + else + { + // we are in the parent branch + for (int i = 0; i < 3; i++) + { + wait(NULL); + read(p[0], inbuf, MSGSIZE); + printf("% s\n", inbuf); + } + } return 0; } From 686aa6c90c1b25746f32ee5a54939fa055ad2468 Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Thu, 14 Feb 2019 15:13:29 -0500 Subject: [PATCH 07/10] Added close statements to parent and child branches --- ex5/ex5.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ex5/ex5.c b/ex5/ex5.c index 9aa3d0787..920b74e08 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -33,19 +33,23 @@ int main(void) else if (f == 0) { // we are in the child branch + close(p[0]); // close the read branch write(p[1], msg1, MSGSIZE); write(p[1], msg2, MSGSIZE); write(p[1], msg3, MSGSIZE); + close(p[1]); // close the write branch } else { // we are in the parent branch + close(p[1]); // close the write branch + wait(NULL); for (int i = 0; i < 3; i++) { - wait(NULL); read(p[0], inbuf, MSGSIZE); - printf("% s\n", inbuf); + printf("%s\n", inbuf); } + close(p[0]); // close the read branch } return 0; } From 8768a27035f3076af2ae8450b4e0c30607a3e3ea Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Thu, 14 Feb 2019 15:50:04 -0500 Subject: [PATCH 08/10] moved fork() below creating pipe, ex5 complete --- ex5/ex5.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ex5/ex5.c b/ex5/ex5.c index 920b74e08..ff4bb5e78 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -17,7 +17,6 @@ char *msg3 = "hello world #3"; int main(void) { // Your code here - int f = fork(); char inbuf[MSGSIZE]; int p[2]; if (pipe(p) < 0) @@ -25,6 +24,7 @@ int main(void) fprintf(stderr, "pipe failde\n"); exit(1); } + int f = fork(); if (f < 0) { printf("%s\n", "Fork faliled"); From d74362f79a95d38c501494f65ac9923925cd6a90 Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Thu, 14 Feb 2019 17:00:06 -0500 Subject: [PATCH 09/10] ex6.c complete --- ex6/ex6.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..75face290 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -12,15 +12,26 @@ and `clock_gettime()` should work just fine. */ #include -#include +#include +#include #include #define number_iter 1000000 #define BILLION 1000000000L +struct timespec start, end; + int main() { - // Your code here - + clock_gettime(CLOCK_MONOTONIC, &start); + for (int i = 0; i < number_iter; i++) + { + printf(""); + } + clock_gettime(CLOCK_MONOTONIC, &end); + double diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + double individual = diff / number_iter; + printf("Average time in nanoseconds: %lf\n", individual); + return 0; } From e6cee20b0d1b4b819fd2576e34177ac31a052d28 Mon Sep 17 00:00:00 2001 From: Kevin Brack Date: Thu, 14 Feb 2019 17:08:52 -0500 Subject: [PATCH 10/10] Changed function to call write to stdout per help channel comments --- ex6/ex6.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ex6/ex6.c b/ex6/ex6.c index 75face290..c3243ebb2 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -15,6 +15,7 @@ and `clock_gettime()` should work just fine. #include #include #include +#include #define number_iter 1000000 #define BILLION 1000000000L @@ -26,7 +27,7 @@ int main() clock_gettime(CLOCK_MONOTONIC, &start); for (int i = 0; i < number_iter; i++) { - printf(""); + write(fileno(stdout), NULL, 0); } clock_gettime(CLOCK_MONOTONIC, &end); double diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;