C Process Api
Total Page:16
File Type:pdf, Size:1020Kb
TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology TCSS 422: OPERATING SYSTEMS OBJECTIVES Assignment 0 – Introduction to Linux Three Easy Pieces Active Reading Quiz – Chapter 7 Process API, Feedback from 3/28 Limited Direct Execution, Scheduling Introduction Processes – Ch. 4 C Linux Process API – Ch. 5 Wes J. Lloyd Limited Direct Execution – Ch. 6 Institute of Technology . Virtualizing the CPU University of Washington - Tacoma Introduction to Scheduling – Ch. 7 Multi-level Feedback Queue Scheduler – Ch. 8 TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 April 2, 2018 L3.2 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma VIRTUAL MACHINE SURVEY SELECTED FEEDBACK FROM 3/28 Please complete the Virtual Machine Survey is wanting What is context switching? an Institute of Technology hosted Ubuntu 16.04 VM Most bash scripts I have seen begin with #!/bin/bash https://goo.gl/forms/w9VWqkX756yXBUBt1 . You did not include this in your sample, yet it still worked. Why did it work? and/or why is this usually included if it is Submitting results today… not needed? What is fork used for? Such as in real-world applications? What is CPU virtualization? TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.3 April 2, 2018 L3.4 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma FEEDBACK - 2 How do you schedule processes manualy? . Check out the “nice” command . Does this command, schedule processes? CHAPTER 5: . Why? Why not? C PROCESS API What’s an example of a process state that’s blocked? TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.5 April 2, 2018 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma L3.6 Slides by Wes J. Lloyd L3.1 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology fork() FORK EXAMPLE Creates a new process - think of “a fork in the road” p1.c “Parent” process is the original #include <stdio.h> Creates “child” process of the program from the current #include <stdlib.h> execution point #include <unistd.h> Book says “pretty odd” int main(int argc, char *argv[]){ printf("hello world (pid:%d)\n", (int) getpid()); Creates a duplicate program instance (these are processes!) int rc = fork(); if (rc < 0) { // fork failed; exit Copy of fprintf(stderr, "fork failed\n"); exit(1); . Address space (memory) } else if (rc == 0) { // child (new process) . Register printf("hello, I am child (pid:%d)\n", (int) getpid()); } else { // parent goes down this path (main) . Program Counter (PC) printf("hello, I am parent of %d (pid:%d)\n", rc, (int) getpid()); Fork returns } return 0; . child PID to parent } . 0 to child TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.7 April 2, 2018 L3.8 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma FORK EXAMPLE - 2 :(){ :|: & };: Non deterministic ordering of execution prompt> ./p1 hello world (pid:29146) hello, I am parent of 29147 (pid:29146) hello, I am child (pid:29147) prompt> or prompt> ./p1 hello world (pid:29146) hello, I am child (pid:29147) hello, I am parent of 29147 (pid:29146) prompt> CPU scheduler determines which to run first TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.9 April 2, 2018 L3.10 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma wait() FORK WITH WAIT wait(), waitpid() #include <stdio.h> Called by parent process #include <stdlib.h> #include <unistd.h> Waits for a child process to finish executing #include <sys/wait.h> Not a sleep() function int main(int argc, char *argv[]){ printf("hello world (pid:%d)\n", (int) getpid()); Provides some ordering to multi-process execution int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); } else { // parent goes down this path (main) int wc = wait(NULL); printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc, (int) getpid()); } return 0; } TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.11 April 2, 2018 L3.12 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma Slides by Wes J. Lloyd L3.2 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology FORK WITH WAIT - 2 FORK EXAMPLE Deterministic ordering of execution Linux example prompt> ./p2 hello world (pid:29266) hello, I am child (pid:29267) hello, I am parent of 29267 (wc:29267) (pid:29266) prompt> TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.13 April 2, 2018 L3.14 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma exec() EXEC() - 2 Supports running an external program Common use case: 6 types: execl(), execlp(), execle(), execv(), execvp(), execvpe() Write a new program which wraps a legacy one Provide a new interface to an old system: Web services execl(), execlp(), execle(): const char *arg Legacy program thought of as a “black box” List of pointers (terminated by null pointer) to strings provided as arguments… (arg0, arg1, .. argn) We don’t want to know what is inside… Execv(), execvp(), execvpe() Array of pointers to strings as arguments Strings are null-terminated First argument is name of file being executed TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.15 April 2, 2018 L3.16 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma EXEC EXAMPLE EXEC EXAMPLE - 2 #include <stdio.h> … #include <stdlib.h> execvp(myargs[0], myargs); // runs word count #include <unistd.h> printf("this shouldn’t print out"); #include <string.h> } else { // parent goes down this path (main) #include <sys/wait.h> int wc = wait(NULL); printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", int main(int argc, char *argv[]){ rc, wc, (int) getpid()); printf("hello world (pid:%d)\n", (int) getpid()); } int rc = fork(); return 0; if (rc < 0) { // fork failed; exit } fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); char *myargs[3]; myargs[0] = strdup("wc"); // program: "wc" (word count) prompt> ./p3 myargs[1] = strdup("p3.c"); // argument: file to count hello world (pid:29383) myargs[2] = NULL; // marks end of array hello, I am child (pid:29384) … 29 107 1030 p3.c hello, I am parent of 29384 (wc:29384) (pid:29383) prompt> TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.17 April 2, 2018 L3.18 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma Slides by Wes J. Lloyd L3.3 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology EXEC WITH FILE REDIRECTION (OUTPUT) FILE MODE BITS S_IRWXU read, write, execute/search by owner S_IRUSR read permission, owner #include <stdio.h> S_IWUSR #include <stdlib.h> write permission, owner #include <unistd.h> S_IXUSR #include <string.h> execute/search permission, owner #include <fcntl.h> S_IRWXG #include <sys/wait.h> read, write, execute/search by group S_IRGRP int main(int argc, char *argv[]){ read permission, group int rc = fork(); S_IWGRP if (rc < 0) { // fork failed; exit write permission, group fprintf(stderr, "fork failed\n"); S_IXGRP exit(1); execute/search permission, group } else if (rc == 0) { // child: redirect standard output to a file S_IRWXO close(STDOUT_FILENO); read, write, execute/search by others open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU); S_IROTH … read permission, others S_IWOTH write permission, others TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.19 April 2, 2018 L3.20 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma EXEC W/ FILE REDIRECTION (OUTPUT) - 2 … // now exec "wc"... char *myargs[3]; myargs[0] = strdup("wc"); // program: "wc" (word count) myargs[1] = strdup("p4.c"); // argument: file to count myargs[2] = NULL; // marks end of array execvp(myargs[0], myargs); // runs word count } else { // parent goes down this path (main) int wc = wait(NULL); } return 0; } prompt> ./p4 prompt> cat p4.output 32 109 846 p4.c prompt> TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.21 April 2, 2018 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma L3.22 QUESTION: PROCESS API Which Process API call is used to launch a different program from the current program? (a) Fork() CH. 6: (b) Exec() (c) Wait() LIMITED DIRECT (d) None of the above EXECUTION (e) All of the above TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.23 April 2, 2018 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma L3.24 Slides by Wes J. Lloyd L3.4 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology COMPUTER BOOT SEQUENCE: VIRTUALIZING THE CPU OS WITH DIRECT EXECUTION How does the CPU support running so many jobs What if programs could directly control the CPU / system? simultaneously? OS Program Time Sharing 1. Create entry for process list 2. Allocate memory for Tradeoffs: program . Performance 3. Load program into memory . Excessive overhead 4. Set up stack with argc / argv . Control 5. Clear registers 7. Run main() . Fairness 6. Execute call main() 8.