diff --git a/README.md b/README.md index 4b8dba5fa..937276f97 100644 --- a/README.md +++ b/README.md @@ -3,19 +3,22 @@ Now that we've talked a bit about how processes and system calls work at a high level, it's time to apply these concepts by doing some exercises related to process creation and making system calls. We'll be utilizing the `fork()`, `exec()`, `wait()`, and `pipe()` system calls in order to create processes and even have them pass messages to each other. ## Objective -To introduce and familiarize yourself with some basic system calls pertaining to process + +To introduce and familiarize yourself with some basic system calls pertaining to process creation, to spawn some new processes, and to practice writing more C! ## `fork()` -The `fork()` system call is used by a parent process to create a new child process. Its -actual implementation isn't as intuitive as it could be, though. When a parent process -executes `fork()`, the new child process that is created is an almost exact copy of the -calling process from the operating system's perspective. We say _almost_ an exact copy -to delineate the fact that while the new child process has the same instruction set -(i.e. code) as its parent process, the child process starts executing at the point right -after `fork()` is called in the parent process. + +The `fork()` system call is used by a parent process to create a new child process. Its +actual implementation isn't as intuitive as it could be, though. When a parent process +executes `fork()`, the new child process that is created is an almost exact copy of the +calling process from the operating system's perspective. We say _almost_ an exact copy +to delineate the fact that while the new child process has the same instruction set +(i.e. code) as its parent process, the child process starts executing at the point right +after `fork()` is called in the parent process. Let's look at a program that calls `fork()` to try to give an example of what this means: + ```c // p1.c #include @@ -39,7 +42,9 @@ int main(int argc, char *argv[]) return 0; } ``` + Running this program, one of the possible outputs could be something like this: + ``` prompt> ./p1 hello world (pid: 29146) @@ -48,22 +53,24 @@ hello, child here (pid: 29147) prompt> ``` -So as we can see, the child process doesn't return the exact same output as its parent process, which -is good, because otherwise spawning child processes wouldn't be nearly as useful. Notice that the child -process has its own process identifier (PID); it also has its own address space, program counter, and -execution context that are not the same as its parent's. +So as we can see, the child process doesn't return the exact same output as its parent process, which +is good, because otherwise spawning child processes wouldn't be nearly as useful. Notice that the child +process has its own process identifier (PID); it also has its own address space, program counter, and +execution context that are not the same as its parent's. -More specifically, if we look at the `int rc = fork();` line, which both parent and chlid execute, they -receive different results here. The parent process receives the PID of the child process it just spawned, -while the child process receives 0 if the `fork()` call was successful. +More specifically, if we look at the `int rc = fork();` line, which both parent and chlid execute, they +receive different results here. The parent process receives the PID of the child process it just spawned, +while the child process receives 0 if the `fork()` call was successful. ## `wait()` -So `fork()` allows us to spawn new child processes, but the thing with having multiple processes is that -the order in which they execute is not deterministic, i.e., the parent may execute before the child or -the child may execute before the parent; we can't say for certain. The `wait()` system call (along with -the more complete `waitpid()` system call) allows us to get around this non-determinism if that is something -that needs to be accounted for. Let's add a call to `waitpid()` in our previous example program to ensure that + +So `fork()` allows us to spawn new child processes, but the thing with having multiple processes is that +the order in which they execute is not deterministic, i.e., the parent may execute before the child or +the child may execute before the parent; we can't say for certain. The `wait()` system call (along with +the more complete `waitpid()` system call) allows us to get around this non-determinism if that is something +that needs to be accounted for. Let's add a call to `waitpid()` in our previous example program to ensure that the child always runs before its parent: + ```c // p2.c #include @@ -89,11 +96,14 @@ int main(int argc, char *argv[]) return 0; } ``` -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. + +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). + +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: + ```c // p3.c #include @@ -113,8 +123,8 @@ int main(int argc, char *argv[]) } else if (rc == 0) { // child process satisfies this branch printf("hello, child here (pid: %d) \n", (int) getpid()); char *myargs[3]; // allocate an array of chars to hold 3 bytes - // `strdup` duplicates the given input string - myargs[0] = strdup("wc"); // pass the name of the program we want to run as the first array element + // `strdup` duplicates the given input string + myargs[0] = strdup("wc"); // pass the name of the program we want to run as the first array element myargs[1] = strdup("p3.c"); // argument: the file to count myargs[2] = NULL; // marks the end of the array execvp(myargs[0], myargs); // runs the word count program, passing in the `myargs` array to the word count program as arguments @@ -127,16 +137,19 @@ int main(int argc, char *argv[]) return 0; } ``` -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 -why we still had to have the parent process `fork` a new child process. + +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 +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. -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. +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. + +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 #include #include @@ -151,7 +164,7 @@ char* msg3 = "hello world #3"; int main() { char inbuf[MSGSIZE]; // a buffer that will hold the incoming data that is being written - int p[2]; // a two-element array to hold the read and write file descriptors that are used by the pipe + int p[2]; // a two-element array to hold the read and write file descriptors that are used by the pipe // establish our pipe, passing it the p array so that it gets populated by the read and write file descriptors if (pipe(p) < 0) { @@ -165,7 +178,7 @@ int main() write(p[1], msg3, MSGSIZE); for (int i = 0; i < 3; i++) { - // read 16 bytes of data from the read file descriptor + // read 16 bytes of data from the read file descriptor read(p[0], inbuf, MSGSIZE); printf("% s\n", inbuf); } @@ -173,20 +186,25 @@ int main() return 0; } ``` -This program isn't actually sending data between two different processes. The process that is doing the writing is also doing the reading. This process is essentially just talking to itself. But at least it gives you an idea of how to create pipes and send data between both ends. + +This program isn't actually sending data between two different processes. The process that is doing the writing is also doing the reading. This process is essentially just talking to itself. But at least it gives you an idea of how to create pipes and send data between both ends. ## What You'll be Doing for this Lab + This was your first introduction to making system calls to an operating system kernel. Obviously, there are many more system calls that we'll talk about in a future lesson, but for now we'll just practice with these. Once you've cloned this repo, go into each directory, open up the exercise file, read the prompt, and write some C code! Compile your code with `gcc [NAME OF FILE].c -o [NAME OF EXECUTABLE]`, then run the executable with `./[NAME OF EXECUTABLE]`. ## Further Reading + If you would like to read more on this topic, these chapters from _Operating Systems: Three Easy Pieces_ heavily informed this particular topic and material: [http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-api.pdf](http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-api.pdf) [http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf](http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf) Additionally, it's always a good idea to read the man pages for any system calls you're trying to use: - - The man page for `fork`: [https://linux.die.net/man/2/fork](https://linux.die.net/man/2/fork) - - The man page for `wait` and `waitpid`: [https://linux.die.net/man/3/waitpid](https://linux.die.net/man/3/waitpid) - - The man page for the `exec` family: [https://linux.die.net/man/3/exec](https://linux.die.net/man/3/exec) - - The man page for the `pipe` system call: [https://linux.die.net/man/2/pipe](https://linux.die.net/man/2/pipe) + +- The man page for `fork`: [https://linux.die.net/man/2/fork](https://linux.die.net/man/2/fork) +- The man page for `wait` and `waitpid`: [https://linux.die.net/man/3/waitpid](https://linux.die.net/man/3/waitpid) +- The man page for the `exec` family: [https://linux.die.net/man/3/exec](https://linux.die.net/man/3/exec) +- The man page for the `pipe` system call: [https://linux.die.net/man/2/pipe](https://linux.die.net/man/2/pipe) ## Stretch Goal -Open up the `/stretch` directory. In there, you'll find an involved exercise pertaining to file locking and concurrency. Read the README included in that directory for instructions on what to do. + +Open up the `/stretch` directory. In there, you'll find an involved exercise pertaining to file locking and concurrency. Read the README included in that directory for instructions on what to do. diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..effd6cc1c Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..f0347043e 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -2,6 +2,12 @@ // (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? +// 1. What value is the variable in the child process? +// The value is also the same. It looks like the x variable is in a global scope. + +// 2.What happens to the variable when both the child and parent change the value of x? +// The x variable changes due to the scope of being inside the conditional statements. + #include #include #include @@ -9,6 +15,29 @@ int main(void) { // Your code here + int x = 100; + printf("x value before calling fork: %d \n", x); + + int rc = fork(); + + // child process starts execution here + if (rc < 0) + { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (rc == 0) + { // child process satisfies this branch + x = 200; + printf("hello, child here (pid: %d) \n", (int)getpid()); + printf("x value in child when changed: %d \n", x); + } + else + { + x = 300; + printf("hello, parent here (pid: %d) of child %d \n", (int)getpid(), rc); + printf("x value in parent when changed: %d \n", x); + } return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..c72bc0046 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..21b34cec0 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -1,14 +1,48 @@ -// 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()` system 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? +// 1. Can both the child and parent access the file descriptor returned by `fopen()`? +// Yes, both child and parent had access since you can write to fp. + +// 2. What happens when they are written to the file concurrently? +// They both get saved. + #include #include #include int main(void) { - // Your code here - + // Your code here + FILE *fp; + fp = fopen("text.txt", "w"); + printf("===Opening the file in write mode===\n"); + + if (fp == NULL) + { + printf("Error opening file!\n"); + exit(1); + } + + int rc = fork(); + if (rc < 0) + { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (rc == 0) + { // child process satisfies this branch + fprintf(fp, "%s \n", "child"); + printf("===Finished writing to file (Child)===\n"); + } + else + { + fprintf(fp, "%s \n", "parent"); + printf("===Finished writing to file (Parent)===\n"); + } + + printf("===Closing the file===\n"); + fclose(fp); return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..cf90a34a2 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,2 @@ +parent +child diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..ac055123b Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..d3726284e 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -11,5 +11,24 @@ int main(void) { // Your code here + // starting process + int rc = fork(); + + // child process starts execution here + if (rc < 0) + { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (rc == 0) + { // child process satisfies this branch + printf("hello \n"); + } + else + { + wait(NULL); + printf("goodbye \n"); + } + return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..6b3fdb9ed Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..ff97b7538 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -1,8 +1,11 @@ // 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? +// 1. Why do you think there are so many variants of the same basic call? +// Just many ways to run a program with different arguments, paths, and environmental variables. + #include #include #include @@ -10,7 +13,30 @@ int main(void) { - // Your code here + // Your code here + + // parent asking OS to start new process, 1 process + int rc = fork(); + + // parent and child processes return from fork(), 2 processes + if (rc < 0) + { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (rc == 0) + { // child + printf("From child, PID: %d, PPID: %d \n\n", getpid(), getppid()); + wait(NULL); // wait for child process to join with the parent + + // execl + int proc1 = execl("/bin/ls", "ls", "-ls", NULL); // returns list direction with long form and file size + printf("proc1 %d \n", proc1); + } + else + { // parent + printf("From parent, PID: %d, PPDI: %d \n", getpid(), rc); + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 000000000..bd0b5728f Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..d05c36471 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,51 @@ #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 - + char buffer[MSGSIZE]; // sets up a buffer of 16 bytes + int p[2]; // sets up pipe, returns 2 file descriptors for each end of pipe + + // PIPE FAIL CHECK + if (pipe(p) < 0) + { + fprintf(stderr, "pipe failed\n"); + exit(1); + } + + // FORK + int rc = fork(); + + // FORK FAIL CHECK + if (rc < 0) + { + fprintf(stderr, "fork failed\n"); + exit(1); + } + + // CHILD - reads messages from parent + if (rc == 0) + { + printf("From child, PID: %d, PPID: %d\n", getpid(), getppid()); + for (int i = 0; i < 3; i++) + { + read(p[0], buffer, MSGSIZE); // p[0] is the read end by default + printf("%s \n", buffer); + } + } + // PARENT - write messages for child + else + { + printf("From parent: PID: %d, PPID: %d\n", getpid(), rc); + write(p[1], msg1, MSGSIZE); // p[1] is the write end by default + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + return 0; } diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100755 index 000000000..9653c974c Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..06fe33a2d 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -13,14 +13,32 @@ and `clock_gettime()` should work just fine. #include #include -#include +#include // for clock_gettime -#define number_iter 1000000 +#define number_iter 1000000 // 1 million iterations #define BILLION 1000000000L +// AVERAGE OF 1 MILLION ITERATIONS - CLOCK_MONOTONIC (~99k-102k nanosecs) int main() { // Your code here - + uint64_t diff; // variable for difference between start and end times + long long unsigned int total_times; // variable for total of 1 million iterations + long long unsigned int average_time; // variable to hold average of 1 million iterations + struct timespec start, end; // declare 2 structs to hold start and end times (includes secs and nanosecs in each struct) + + // CLOCK_MONOTONIC - Unspecified starting point for measuring elapsed time + for (int i = 0; i < number_iter; i++) + { // loops through 1 million times + clock_gettime(CLOCK_MONOTONIC, &start); // start the elapse timer + fileno(stdout); // empty write to stdout + clock_gettime(CLOCK_MONOTONIC, &end); // end the elapse timer + + diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; // total up differences of start and end + total_times = total_times + diff; // add the total time to total_times buffer variable + } + + average_time = total_times / number_iter; // calculates average time + printf("CLOCK_MONOTONIC Average Time = %llu nanoseconds\n", average_time); return 0; }