diff --git a/README.md b/README.md index 4b8dba5fa..ab56a2426 100644 --- a/README.md +++ b/README.md @@ -91,11 +91,11 @@ int main(int argc, char *argv[]) ``` Here, the `waitpid()` function suspends the parent process until the child process pointed at by `rc` terminates. Thus, we ensure that the parent process only runs after the child process has finished its execution. -## `exec()` -The `exec()` system call is used in order to run a program that is different from the calling program (since `fork` only executes copies of the program that called it). -Let's say we wanted to spin up a child process to execute a word count program. Here's how what a program that does that might look like: +## `exehttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout +The `exhttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout program that is different from the calling program (since `fork` only executes copies of the program that called https://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout +Let's shttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdoutexecute a word count program. Here's how what a program that does that might look like: ```c -// p3.c +// p3.chttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout #include #include #include @@ -129,12 +129,12 @@ int main(int argc, char *argv[]) ``` Here, we're doing the same thing as before, forking a new child process from a parent process. Then, inside that child process, we're calling `execvp()` with the arguments it needs to run the word count program. Note that `exec` -does not spin up _another_ child process from the child process in which we called it. It transforms the process -that called it into the new program that was passed to `exec`, in this case, `wc`, the word count process. That's +does not spin up _another_ child process from the child process in which we calledbin/lsit. It transforms the process +that called it into the new program that was passed to `exec`, in this case, `wc`,bin/lsthe word count process. That's why we still had to have the parent process `fork` a new child process. ## `pipe()` -Conceptually, a pipe is a uni-directional channel between two processes that would otherwise be isolated from each other. When a pipe is established between two processes, one process has access to the write end of the pipe, while the other has read access to the other end of the pipe. Thus, if you want two-way communication between two processes, two pipes will have to be created between both processes, one in each direction. +Conceptually, a pipe is a uni-directional channel between two processes that wouldbin/lsotherwise be isolated from each other. When a pipe is established between two processes, one process has access to the write end of the pipe, while the other habin/ls read access to the other end of the pipe. Thus, if you want two-way communication between two processes, two pipes will have to be created between both processes, one in each direction. Some things to keep in mind when working with pipes is that when a process writes to a pipe, the other process receives the data in FIFO order (which makes sense when you think about the pipe analogy in real life). Additionally, if the process with read access tries to read from the pipe before anything has been written to it, the reading process is suspended until there is some data to read. ```c diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..5b7c5e015 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..8c28498e7 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -6,9 +6,22 @@ #include #include +int x; + int main(void) { - // Your code here + x = 50; + + printf("%d", x); + + int child = fork(); + + if(child) { + x = 30; + printf("%d", x); + } + + printf("%d", x); return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..8af7217be Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..b02dd3f3f 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -8,7 +8,20 @@ int main(void) { - // Your code here + + FILE * fp; + + fp = fopen("text.txt", "w+"); + + int child = fork(); + + fprintf(fp, "Testing the parent process."); + + if(child) { + fprintf(fp, "Testing the child process."); + } + fclose(fp); + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..643985351 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1 @@ +Testing the parent process.Testing the child process.Testing the parent process. \ No newline at end of file diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..9d9096217 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..0237aa25b 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,15 @@ int main(void) { - // Your code here + + int child = fork(); + + if(child) { + printf("Hello"); + } else { + wait(NULL); + printf("Goodbye"); + } return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..d231ba747 Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..4d6ca0cd0 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,15 @@ int main(void) { - // Your code here + int child = fork(); + + if(child) { + + execl("/bin/ls", "ls", 0); + execle("/bin/ls", "ls", 0); + execv("/bin/ls", 0); + + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 000000000..d16e42f4e Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..9663e24f3 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -17,6 +17,31 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here + char inbuf[MSGSIZE]; + int i; + + int pipeEnds[2]; + int pipe(pipeEnds); + + + int child = fork(); + + if(child) { + + write(pipeEnds[1], msg1, MSGSIZE); + write(pipeEnds[1], msg2, MSGSIZE); + write(pipeEnds[1], msg3, MSGSIZE); + + } else { + + wait(NULL); + for(i = 0; i < 3; i++) { + + read(pipeEnds[0], inbuf, MSGSIZE); + + } + + } return 0; } diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100755 index 000000000..ee434fe54 Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..09b81d453 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -18,9 +18,30 @@ and `clock_gettime()` should work just fine. #define number_iter 1000000 #define BILLION 1000000000L +int total = 0; +int diff; +int avg; +int i; +struct timespec start, end; + int main() { // Your code here + for(i = 0; i < number_iter; i++) { + + clock_gettime(_POSIX_MONOTONIC_CLOCK, &start); + write(STDOUT_FILENO, NULL, 0); + clock_gettime(_POSIX_MONOTONIC_CLOCK, &end); + + diff = BILLION * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec); + + total = total + diff; + + } + + avg = (total / number_iter); + + printf("%d\n", avg); return 0; } diff --git a/stretch/src/balance.txt b/stretch/src/balance.txt new file mode 100644 index 000000000..2447c9fb6 --- /dev/null +++ b/stretch/src/balance.txt @@ -0,0 +1 @@ +4458 \ No newline at end of file diff --git a/stretch/src/bankers b/stretch/src/bankers new file mode 100755 index 000000000..f12d60d62 Binary files /dev/null and b/stretch/src/bankers differ diff --git a/stretch/src/bankers.c b/stretch/src/bankers.c index b44aed25c..d497d93a0 100644 --- a/stretch/src/bankers.c +++ b/stretch/src/bankers.c @@ -91,6 +91,10 @@ int get_random_amount(void) // Return a random number between 0 and 999 inclusive using rand() + int randomAmount = rand() % 999; + + return randomAmount; + // ^^^^^^^^^^^^^^^^^^ } @@ -116,14 +120,28 @@ 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 numProcesses\n"); + exit(1); + + } + + int num_processes = atol(argv[1]); // Store the number of processes in this variable: // How many processes to fork at once - int num_processes = IMPLEMENT ME + // 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: // + if (num_processes < 1) { + fprintf(stderr, "Bankers: number of processes must be at least 1"); + exit(2); + } + // "bankers: num processes must be greater than 0\n" // ^^^^^^^^^^^^^^^^^^ @@ -152,8 +170,12 @@ int main(int argc, char **argv) // Open the balance file (feel free to call the helper // functions, above). + int balFile = open_balance_file(BALANCE_FILE); + // Read the current balance + read_balance(balFile, &balance); + // Try to withdraw money // // Sample messages to print: @@ -161,8 +183,17 @@ int main(int argc, char **argv) // "Withdrew $%d, new balance $%d\n" // "Only have $%d, can't withdraw $%d\n" + if(amount <= balance) { + balance = balance - amount; + write_balance(balFile, balance); + printf("Withdrew $%d, New Balance: $%d\n", amount, balance); + } else { + printf("Insufficient Funds - Current Available Balance: $%d.\n", balance); + } + // Close the balance file //^^^^^^^^^^^^^^^^^^^^^^^^^^ + close_balance_file(balFile); // Child process exits exit(0);