diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100644 index 000000000..e908daecb Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..28dc08c8e 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,23 @@ int main(void) { // Your code here + int x = 100; + int pid = fork(); + + if(pid < 0) { + fprintf(stderr, "Fork failed!\n"); + exit(1); + } + else if(pid == 0) { + printf("Hello, I'm a child process!\n"); + } + else { + printf("Hello, I'm a parent process!\n"); + } + printf("%d %d", 100, x); return 0; } + +/* After initialization, the variable in the child process would be the same as its value in the parent process. When the parent and child change the value of x, we can access both values of x within their respective forks. +If we were to do something like (x * 2) in the parent fork and (x / 2) in the child fork, we could work with both 200 and 50. */ diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100644 index 000000000..e74179507 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..c93f470b5 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,6 +9,25 @@ int main(void) { // Your code here - + FILE * file_pointer; + + file_pointer = fopen("text.txt", "r+"); + int pid = fork(); + + if(pid < 0) { + fprintf(stderr, "Fork failed!\n"); + exit(1); + } + else if(pid == 0) { + fprintf(file_pointer, "%s %s", "Child", "text.\n"); + fclose(file_pointer); + } + else { + fprintf(file_pointer, "%s %s", "Parent", "text."); + fclose(file_pointer); + } + return 0; } + +/* Yes, both child and parent can access the file returned by fopen(). In the case of concurrent attempts, they are saved together as in the example above. */ \ No newline at end of file diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..417ad503a 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1 @@ +Parent text.Child text. diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100644 index 000000000..092bb2a9b Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..14b02bae9 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,23 @@ int main(void) { // Your code here + pid_t pid = fork(); + // waitpid(pid, NULL, 0); + + if(pid < 0) { + fprintf(stderr, "Fork failed!\n"); + exit(1); + } + else if(pid == 0) { + printf("%s", "hello\n"); + // waitpid(pid, NULL, 0); + } + else { + waitpid(pid, NULL, 0); + printf("%s", "goodbye\n"); + } return 0; } + +/* currently testing out the results of implementing waitpid() in various spots in the code */ \ No newline at end of file diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100644 index 000000000..513f4ff3e Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..7cbf93c16 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -11,6 +11,19 @@ int main(void) { // Your code here + pid_t pid = fork(); + char *bin_ref = {"./bin/ls", NULL}; /* might try running another file from this project at some point too */ + exec(bin_ref[0], bin_ref); + // execl(bin_ref[0], bin_ref); + // execle(bin_ref[0], bin_ref); + // execvp(bin_ref[0], bin_ref); return 0; } + +/* The different versions seem to mainly reference parameters. +For example, -l includes individual parameters in the call, whereas -v is useful when +variables are not known in advance because it allows you to pass in a char* array +rather than a specific number of parameters. There is some additional functionality +with things like -e and -p and I'd be lying if I said I understand this fully yet, so +I'm going to keep looking into these differences. */ diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100644 index 000000000..ae913b025 Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..3e235d9fe 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -17,6 +17,33 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here + char inbuf[MSGSIZE]; + int p[2]; + + if(pipe(p) < 0) { + fprintf(stderr, "Pipe failure!\n"); + exit(1); /* yesterday's error handling example applied to the pipe */ + } + pid_t pid = fork(); + + if(pid < 0) { + fprintf(stderr, "Fork failure!\n"); + exit(1); /* fork error handling */ + } + else if(pid == 0) { + /* write the msg entries to the pipe */ + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + else { + for(int i = 0; i < 3; i++) { + /* reads the data from the pipe and outputs it */ + 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..793094656 Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..263ac7b7d 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -12,6 +12,7 @@ and `clock_gettime()` should work just fine. */ #include +#include #include #include @@ -21,6 +22,21 @@ and `clock_gettime()` should work just fine. int main() { // Your code here + uint64_t diff; + struct timespec start, end; + int i; + + /* basic structure */ + clock_gettime(CLOCK_MONOTONIC, &start); + + for(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("Time elapsed: %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..1746da631 --- /dev/null +++ b/stretch/src/balance.txt @@ -0,0 +1 @@ +10000 \ No newline at end of file diff --git a/stretch/src/bankers b/stretch/src/bankers new file mode 100644 index 000000000..f922dbc91 Binary files /dev/null and b/stretch/src/bankers differ diff --git a/stretch/src/bankers.c b/stretch/src/bankers.c index b44aed25c..a8af00cc1 100644 --- a/stretch/src/bankers.c +++ b/stretch/src/bankers.c @@ -90,6 +90,15 @@ int get_random_amount(void) // !!!! IMPLEMENT ME: // Return a random number between 0 and 999 inclusive using rand() + int i, count, lower, upper; /* upper and lower will provide our rand() range */ + lower = 0; + upper = 999; + count = 1; + + for(i = 0; i < count; i++){ + int rng = (rand() % (upper - lower + 1)) + lower; + printf("%d ", rng); + } // ^^^^^^^^^^^^^^^^^^ } @@ -116,10 +125,15 @@ int main(int argc, char **argv) // message to stderr, and exit with status 1: // // "usage: bankers numprocesses\n" - + if(argc == 0) { + fprintf(stderr, "Usage: bankers numprocesses\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 = argc; + + /* learning about getopt() right now to try and parse arguments */ // 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: @@ -151,17 +165,29 @@ int main(int argc, char **argv) // Open the balance file (feel free to call the helper // functions, above). + + int fp = open_balance_file(BALANCE_FILE); // Read the current balance + int read = read_balance(); // Try to withdraw money - // + + int withdrawal = get_random_amount(); + // Sample messages to print: - // + if(withdrawal >= balance) { + printf("Withdrew $%d, new balance $%d\n", withdrawal, balance); + } + else { + printf("Only have $%d, can't withdraw $%d\n", balance, withdrawal); + } // "Withdrew $%d, new balance $%d\n" // "Only have $%d, can't withdraw $%d\n" // Close the balance file + + int fp_close = close_balance_file(BALANCE_FILE); //^^^^^^^^^^^^^^^^^^^^^^^^^^ // Child process exits