From c70c50df1fb1638e2fe5ec24bc97f6b37d225bc2 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 13 Feb 2019 18:24:57 -0500 Subject: [PATCH 1/6] initial commit, finished ex1 --- ex1/ex1.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..0f57e9fac 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,23 @@ int main(void) { // Your code here + int x = 100; + int rc = fork(); + if (rc < 0) + { + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (rc == 0) + { + // x = 20; + printf("Child x:%d \n", x); + } + else + { + x = 200; + printf("Parent x:%d \n", x); + } return 0; } From 5d1cb9427f6bda28de5d3fa3b62c6e686b601d79 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 13 Feb 2019 18:28:15 -0500 Subject: [PATCH 2/6] ex2 --- ex2/ex2.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..d8d54b66c 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,6 +9,38 @@ int main(void) { // Your code here - + FILE *file; + // int rc = fork(); Having fork here will overwrite th file + file = fopen("text.txt", "w"); + if (file == NULL) + { + printf("Could not open text.txt"); + exit(1); + } + int fileNum; + fileNum = fileno(file); + printf("Pointer: %d\n", fileNum); + + int rc = fork(); + if (rc < 0) + { + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (rc == 0) + { + char *txt = "Child."; + fprintf(file, "Child: %s", txt); + + printf("Child descriptor: %d\n", fileNum); + } + else + { + char *txt = "Parent."; + fprintf(file, "Child %s", txt); + printf("Parent descriptor: %d\n", fileNum); + } + + fclose(file); return 0; } From 3c48132715c590fcdc2b4093a3b5c049785045b3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 13 Feb 2019 18:30:29 -0500 Subject: [PATCH 3/6] ex3 --- ex3/ex3.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..3efafd177 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,20 @@ int main(void) { // Your code here - + int child_process = fork(); + if (child_process < 0) + { + fprintf(stderr, "Fork failed\n"); + exit(1); + } + else if (child_process == 0) + { + printf("hello.\n"); + } + else + { + int wc = waitpid(child_process, NULL, 0); + printf("goodbye.\n"); + } return 0; } From 5809f7ae2ffce8334db05f51a422a20bafc7505b Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Feb 2019 18:27:04 -0500 Subject: [PATCH 4/6] ex4 --- ex4/ex4.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..5c0aaf85a 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -11,6 +11,26 @@ int main(void) { // Your code here + int child = fork(); + + if (child < 0) + { + printf("\nFork failed\n"); + } + else if (child == 0) + { + char *args[2]; + args[0] = "/bin/ls"; // list files and dirs + args[1] = NULL; + + execvp(args[0], args); + printf("\nNot found\n"); + } + else + { + int wc = waitpid(child, NULL, 0); + printf("\nParent\n"); + } return 0; } From 7c930e32bc0b3d9110bc8d4436dd1f39670c0301 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Feb 2019 18:28:50 -0500 Subject: [PATCH 5/6] ex5 --- ex4/ex4.c | 2 +- ex5/ex5.c | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ex4/ex4.c b/ex4/ex4.c index 5c0aaf85a..d2f220b7c 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -20,7 +20,7 @@ int main(void) else if (child == 0) { char *args[2]; - args[0] = "/bin/ls"; // list files and dirs + args[0] = "/bin/ls"; args[1] = NULL; execvp(args[0], args); diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..c6cdb7bef 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -17,6 +17,23 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here - + char s[MSGSIZE]; + int p[2]; + + if (pipe(p) < 0) + { + fprintf(stderr, "\npipe error\n"); + exit(1); + } + + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + + for (int i = 0; i < 3; i++) + { + read(p[0], s, MSGSIZE); + printf("%s\n", s); + } return 0; } From 438bac353f68cd3cbda075a9ed88d71fd973366b Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Feb 2019 18:29:43 -0500 Subject: [PATCH 6/6] ex6 --- ex6/ex6.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..91a5ff6ba 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -21,6 +21,18 @@ and `clock_gettime()` should work just fine. int main() { // Your code here - + int time = 0; + for (int i = 0; i < number_iter; i++) + { + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + write(fileno(stdout), NULL, 0); + clock_gettime(CLOCK_MONOTONIC, &end); + + time += BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + } + time /= number_iter; + + printf("\nAverage time: %d nanoseconds\n", time); return 0; }