diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100644 index 000000000..7664791f0 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..a51303c9a 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,25 @@ int main(void) { // Your code here + int x = 100; + int child = fork(); + if (child < 0) + { + fprintf(stderr, "Nope fork failed.\n"); + exit(1); + } + else if (child == 0) + { + printf("child start: %i \n", x); + x = 50; + printf("child end: %i \n", x); + } + else + { + printf("parent start: %i \n", x); + x = 60; + printf("parent end: %i \n", x); + } return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100644 index 000000000..7884e3752 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..62f008f81 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -1,14 +1,42 @@ -// 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 #include #include +#include int main(void) { - // Your code here - + // Your code here + FILE *txt = fopen("./text.txt", "w"); + int child = fork(); + + if (child < 0) + { + fprintf(stderr, "Nope. fork failed"); + exit(1); + } + else if (child == 0) + { + char *str1 = "The truth is out there\n"; + fwrite(str1, sizeof(char), strlen(str1), txt); + // txt = fopen("text.txt", "a"); + // fprintf(txt, "The truth is out there\n" ); + // fclose(txt); + printf("child\n"); + } + else + { + // txt = fopen("text.txt", "a"); + // fprintf(txt, "Shut up Mulder\n"); + // fclose(txt); + char *str2 = "Shut up Mulder\n"; + fwrite(str2, sizeof(char), strlen(str2), txt); + printf("parent\n"); + } + fclose(txt); + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..3277e1244 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,2 @@ +Shut up Mulder +The truth is out there diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100644 index 000000000..3c0f867af Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..9d8c75f33 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,21 @@ int main(void) { // Your code here + int child = fork(); + if (child < 0) + { + printf("Nope. Fork failed\n"); + exit(1); + } + else if (child == 0) + { + printf("Knock knock\n"); + } + else + { + waitpid(child, NULL, 0); + printf(" Who's there?\n"); + } return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100644 index 000000000..f1ee99595 Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..0c0f898fb 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,23 @@ int main(void) { - // Your code here - + // Your code here + int rc = fork(); + if (rc < 0) + { + printf("Nope. Fork failed\n"); + exit(1); + } + else if (rc == 0) + { + printf("hey that worked! (pid: %d)\n", getpid()); + int run_it = execl("/bin/ls", "ls", "-1", NULL); + printf("run_it %d \n", run_it); + } + else + { + waitpid(rc, NULL, 0);; + printf("parent\n"); + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100644 index 000000000..b1371c4e8 Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..4ecf2b955 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 @@ -17,6 +17,41 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here - + char buf[MSGSIZE]; + int p[2]; + if (pipe(p) < 0) + { + fprintf(stderr, "Nope. Pipe failed\n"); + exit(1); + } + int rc = fork(); + + if (rc < 0) + { + printf("Nope. Fork failed\n"); + exit(2); + } + else if (rc == 0) + { + printf("child writing\n"); + + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + + printf("child done\n"); + } + else + { + waitpid(rc, NULL, 0);; + close(p[1]); + printf("parent\n"); + + while (read(p[0], buf, MSGSIZE) > 0) + { + printf("%s\n", buf); + } + printf("parent done\n"); + } return 0; } diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100644 index 000000000..b6a5aa2ce Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..1ec1df66b 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -1,14 +1,14 @@ /* Write a program that will find the average time it takes for your computer -to make a system call an empty write to stdout. Your program should use the -`clock_gettime` procedure to time how long a single system call takes. It'll -do this for one million iterations, and then find the average of all of those +to make a system call an empty write to stdout. Your program should use the +`clock_gettime` procedure to time how long a single system call takes. It'll +do this for one million iterations, and then find the average of all of those iterations in nanoseconds. For some helpful documentation and examples of using time-related system calls, visit this site: https://www.cs.rutgers.edu/~pxk/416/notes/c-tutorials/gettime.html -While the linked site does say that `clock_gettime()` does not work on OSX, this -turns out to only be the case for OSX versions < 10.12. Anything later than that -and `clock_gettime()` should work just fine. +While the linked site does say that `clock_gettime()` does not work on OSX, this +turns out to only be the case for OSX versions < 10.12. Anything later than that +and `clock_gettime()` should work just fine. */ #include @@ -21,6 +21,24 @@ and `clock_gettime()` should work just fine. int main() { // Your code here - + + long sum = 0; + long time; + double avg; + struct timespec start, end; + + for (int i = 0; i < number_iter; i++) + { + 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); + sum += time; + } + + avg = sum / (float) number_iter; + printf("Average is %f ns \n", avg); + return 0; }