diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..a4c54f93d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "workbench.colorCustomizations": { + "activityBar.background": "#561905", + "titleBar.activeBackground": "#782207", + "titleBar.activeForeground": "#FFFBF9" + } +} \ No newline at end of file diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..54ddd2700 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -8,7 +8,25 @@ int main(void) { - // Your code here + // Your code here + int x = 800; + printf("Initial value %d\n", x); - return 0; + pid_t pid = fork(); + + if (pid < 0) + { + exit(1); + } + if (pid == 0) + { + printf("value of x child process, (x:%d) (pid:%d)\n", x, getpid()); + } + else + { + x = 100; + printf("value of x parent process %d\n", x); + } + + return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..c79504c46 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..6ccc84fb7 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,31 @@ int main(void) { - // Your code here - - return 0; + // Your code here + FILE *fp; + + fp = fopen("text.txt", "w+"); + printf("Before forking pid: %d\n", (int)getpid()); + + fprintf(fp, "%s %s %s %s %d\n", "File", "write", "before", "fork", (int)getpid()); + + pid_t pid = fork(); + + if (pid < 0) //fork failed + { + exit(1); + } + else if (pid == 0) //child process + { + printf("AFTER forking CHILD pid: %d\n", (int)getpid()); + fprintf(fp, "%s %s %d\n", "Child-File", "write", (int)getpid()); + } + else + { + printf("AFTER forking PARENT pid: %d\n", (int)getpid()); + fprintf(fp, "%s %s %d\n", "Parent-File", "write", (int)getpid()); + } + fclose(fp); + + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..a5345f70d 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,4 @@ +File write before fork 53417 +Parent-File write 53417 +File write before fork 53417 +Child-File write 53418 diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..fb836c912 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..87b48c18a 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,24 @@ int main(void) { - // Your code here + // Your code here + printf("Before forking pid: %d\n", (int)getpid()); - return 0; + pid_t pid = fork(); + + if (pid < 0) + { + exit(1); + } + else if (pid == 0) + { + printf("AFTER forking CHILD says Hello pid: %d\n", (int)getpid()); + } + else + { + waitpid(pid, NULL, 0); + printf("AFTER forking PARENT says Goodbye pid: %d\n", (int)getpid()); + } + + return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..3d6e04736 Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..1e97bbc10 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,33 @@ int main(void) { - // Your code here + // Your code here + printf("Before forking pid: %d\n", (int)getpid()); - return 0; + pid_t pid = fork(); + + if (pid < 0) + { + exit(1); + } + else if (pid == 0) + { + printf("AFTER forking CHILD pid: %d\n", (int)getpid()); + + char *myargs[2]; + + myargs[0] = "/bin/ls"; + myargs[1] = NULL; + + execvp(myargs[0], myargs); + + printf("this should not be seen"); + } + else + { + + int wc = waitpid(pid, NULL, 0); + printf("this should not be seen"); + } + return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 000000000..5ccff13de Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..6da3b4232 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,49 @@ #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 - - return 0; + char inbuf[MSGSIZE]; + int p[2]; + + if (pipe(p) < 0) + { + fprintf(stderr, "pipe failed\n"); + exit(1); + } + + printf("Before forking pid: %d\n", (int)getpid()); + printf("\np[0]: %d\n", p[0]); + printf("p[1]: %d\n", p[1]); + pid_t pid = fork(); + + if (pid < 0) + { + exit(1); + } + else if (pid == 0) + { + printf("AFTER forking CHILD pid: %d\n", (int)getpid()); + + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + else + { + waitpid(pid, NULL, 0); + + printf("AFTER forking PARENT pid: %d\n", (int)getpid()); + for (int i = 0; i < 3; i++) + { + read(p[0], inbuf, MSGSIZE); + printf("Parent writes: %s\n", inbuf); + } + } + + return 0; } diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..c013c1413 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -20,7 +20,36 @@ and `clock_gettime()` should work just fine. int main() { - // Your code here - - return 0; + // Your code here + struct timespec start, end; + uint64_t diff; + + uint64_t sum = 0; + + int numiter = number_iter; + float avg = 0; + int count = 10 + + FILE * + fp; + fp = fopen("writes.csv", "w+"); + while (count-- > 0) + { + while (--numiter > 0) + { + clock_gettime(CLOCK_MONOTONIC, &start); + printf(""); + clock_gettime(CLOCK_MONOTONIC, &end); + + diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + sum += diff; + } + avg = sum / number_iter; + fprintf(fp, "%f\n", avg); + printf("%d: %f ns\n", count, avg); + sum = 0; + numiter = number_iter; + } + + return 0; }