diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100644 index 000000000..ce1e35438 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..74cbf04f9 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -2,13 +2,29 @@ // (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? +// In the program below, the parent process decrements and the child process increments. Because they are two separate processes running the +// code independently, x = 99 for the parent and x = 101 for the child. + #include #include #include int main(void) { - // Your code here + int x = 100; + int pid = fork(); + if (pid < 0) { + fprintf(stderr, "fork failed\n"); + exit(1); + } + + if (pid == 0) { + x++; + } + else { + x--; + } + printf("%d, %s, %d\n", pid, "hello", x); return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100644 index 000000000..47c46d195 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..7342383e4 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -2,12 +2,31 @@ // 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? +// Both the child and the parent can access the file descriptor, if both processes attempt to write to the file then +// both attempts will be saved. + #include #include #include int main(void) { + FILE *fp; + + fp = fopen("text.txt", "r+"); + + int pid = fork(); + + if (pid == 0) { + fprintf(fp, "%s %s %s %s", "The", "child", "wrote", "this."); + + fclose(fp); + } + else { + fprintf(fp, "%s %s %s %s", "The", "parent", "wrote", "this."); + + fclose(fp); + } // Your code here return 0; diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..fabff7c7b 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1 @@ +The parent wrote this.The child wrote this. \ No newline at end of file diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100644 index 000000000..f6cacda46 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..d0516f13f 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,14 @@ int main(void) { - // Your code here + int pid = fork(); + waitpid(pid, NULL, 0); + if (pid == 0) { + printf("%s\n", "hello"); + } + else { + printf("%s\n", "goodbye"); + } return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100644 index 000000000..39f796d5d Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..df3e19995 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,9 @@ int main(void) { - // Your code here + char *args[]={"/bin/ls", NULL}; + fork(); + execv(args[0], args); return 0; } diff --git a/ex5/a.out b/ex5/a.out new file mode 100644 index 000000000..dbe17a8da Binary files /dev/null and b/ex5/a.out differ diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100644 index 000000000..54faf1d99 Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..620c3a239 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -16,7 +16,30 @@ char* msg3 = "hello world #3"; int main(void) { - // Your code here - + char inbuf[MSGSIZE]; + int p[2]; + if (pipe(p) < 0) { + fprintf(stderr, "pipe failed\n"); + exit(1); + } + int pid = fork(); + + if (pid < 0) { + fprintf(stderr, "fork failed\n"); + exit(1); + } + + if (pid == 0) { + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + else { + for (int i = 0; i < 3; i++) { + // read 16 bytes of data from the read file descriptor + read(p[0], inbuf, MSGSIZE); + printf("%s\n", inbuf); + } + } return 0; } diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100644 index 000000000..924eef602 Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..c434b4b53 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -12,6 +12,7 @@ and `clock_gettime()` should work just fine. */ #include +#include #include #include @@ -20,7 +21,19 @@ and `clock_gettime()` should work just fine. int main() { - // Your code here + uint64_t diff; + struct timespec start, end; + int i; + + clock_gettime(CLOCK_MONOTONIC, &start); + for (int i = 0; i < number_iter; i++) + { + write(fileno(stdout), NULL, 0); + } + clock_gettime(CLOCK_MONOTONIC, &end); + + diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + printf("elapsed time = %llu nanoseconds\n", (long long unsigned int) diff); return 0; } diff --git a/stretch/src/balance.txt b/stretch/src/balance.txt new file mode 100644 index 000000000..301160a93 --- /dev/null +++ b/stretch/src/balance.txt @@ -0,0 +1 @@ +8 \ No newline at end of file diff --git a/stretch/src/bankers b/stretch/src/bankers new file mode 100644 index 000000000..2d0f3f724 Binary files /dev/null and b/stretch/src/bankers differ diff --git a/stretch/src/bankers.c b/stretch/src/bankers.c index b44aed25c..83bc112ec 100644 --- a/stretch/src/bankers.c +++ b/stretch/src/bankers.c @@ -86,12 +86,7 @@ void read_balance(int fd, int *balance) */ int get_random_amount(void) { - // vvvvvvvvvvvvvvvvvv - // !!!! IMPLEMENT ME: - - // Return a random number between 0 and 999 inclusive using rand() - - // ^^^^^^^^^^^^^^^^^^ + return rand() % 1000; } /** @@ -116,16 +111,27 @@ int main(int argc, char **argv) // message to stderr, and exit with status 1: // // "usage: bankers numprocesses\n" + if (argc < 2) + { + fprintf(stderr, "usage: bankers numprocessess\n"); + exit(1); + } // Store the number of processes in this variable: // How many processes to fork at once - int num_processes = IMPLEMENT ME + int num_processes = strtol(argv[1], NULL, 10); // Make sure the number of processes the user specified is more than // 0 and print an error to stderr if not, then exit with status 2: // // "bankers: num processes must be greater than 0\n" + if (num_processes < 1) + { + fprintf(stderr, "bankers: num processes must be greater than 0\n"); + exit(2); + } + // ^^^^^^^^^^^^^^^^^^ // Start with $10K in the bank. Easy peasy. @@ -145,15 +151,36 @@ int main(int argc, char **argv) int amount = get_random_amount(); int balance; - // vvvvvvvvvvvvvvvvvvvvvvvvv // !!!! IMPLEMENT ME - + fd = open_balance_file(BALANCE_FILE); + int lock = flock(fd, LOCK_EX); + if (lock < 0) + { + fprintf(stderr, "lock failed\n"); + exit(1); + } // Open the balance file (feel free to call the helper // functions, above). - + + read_balance(fd, &balance); // Read the current balance - + if (balance > amount) + { + int new_balance = balance - amount; + write_balance(fd, new_balance); + printf("Withdrew $%d, new balance $%d\n", amount, new_balance); + } + else + { + printf("Only have $%d, can't withdraw $%d\n", balance, amount); + } + int unlock = flock(fd, LOCK_UN); + if (unlock < 0) { + fprintf(stderr, "unlock failed\n"); + exit(1); + } + // Try to withdraw money // // Sample messages to print: @@ -163,7 +190,7 @@ int main(int argc, char **argv) // Close the balance file //^^^^^^^^^^^^^^^^^^^^^^^^^^ - + close_balance_file(fd); // Child process exits exit(0); }