posix fjles pipe
play

POSIX fjles /pipe 1 last time creating new threads for swtch - PowerPoint PPT Presentation

POSIX fjles /pipe 1 last time creating new threads for swtch trick: write what would be on stack during call to swtch POSIX and Unix fork copy process to create child process exec replace program being run by current process waitpid


  1. POSIX fjles /pipe 1

  2. last time creating new threads for swtch trick: write what would be on stack during call to swtch POSIX and Unix fork — copy process to create child process exec — replace program being run by current process waitpid — wait for child process typical pattern: fork; if (child) exec; else wait 2

  3. exercise (1) A and C A and B B. L1 (newline) L2 (newline) L2 E. C. (newline) L2 L2 (newline) L1 F. all of the above G. something else D. L1 int main() { A. for ( int i = 0; i < 2; ++i) { pids[i] = fork(); if (pids[i] == 0) { args[1] = extra[i]; execv("/bin/echo", args); } } for ( int i = 0; i < 2; ++i) { waitpid(pids[i], NULL, 0); } } Assuming fork and execv do not fail, which are possible outputs? 3 pid_t pids[2]; const char *args[] = {"echo", "ARG", NULL}; const char *extra[] = {"L1", "L2"};

  4. exercise (2) (newline) 0 (newline) 1 (newline) 0 (newline) 2 F. C and D C. 1 (newline) 0 (newline) 2 B. G. all of the above D. 1 (newline) 0 (newline) 2 (newline) 0 H. something else 0 A, B, and C int main() { for ( int i = 0; i < 2; ++i) { pid_t pids[2]; for ( int i = 0; i < 2; ++i) { pids[i] = fork(); if (pids[i] == 0) { execv("/bin/echo", args); } } printf("1\n"); fflush(stdout); waitpid(pids[i], NULL, 0); E. } printf("2\n"); fflush(stdout); } Assuming fork and execv do not fail, which are possible outputs? A. 0 (newline) 0 (newline) 1 (newline) 2 4 const char *args[] = {"echo", "0", NULL};

  5. shell allow user (= person at keyboard) to run applications user’s wrapper around process-management functions upcoming homework — make a simple shell 5

  6. aside: shell forms POSIX: command line you have used before also: graphical shells e.g. OS X Finder, Windows explorer other types of command lines? completely difgerent interfaces? 6

  7. some POSIX command-line features searching for programs (not in assignment) running in background (not in assignment) ./someprogram & redirection: ./someprogram >output.txt ./someprogram <input.txt pipelines: ./someprogram | ./somefilter 7 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make

  8. some POSIX command-line features searching for programs (not in assignment) running in background (not in assignment) ./someprogram & redirection: ./someprogram >output.txt ./someprogram <input.txt pipelines: ./someprogram | ./somefilter 8 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make

  9. searching for programs POSIX convention: PATH environment variable example: /home/cr4bd/bin:/usr/bin:/bin list of directories to check in order environment variables = key/value pairs stored with process by default, left unchanged on execve, fork, etc. one way to implement: [pseudocode] for (directory in path) { } 9 execv(directory + "/" + program_name, argv);

  10. some POSIX command-line features searching for programs (not in assignment) running in background (not in assignment) ./someprogram & redirection: ./someprogram >output.txt ./someprogram <input.txt pipelines: ./someprogram | ./somefilter 10 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make

  11. some POSIX command-line features searching for programs (not in assignment) running in background (not in assignment) ./someprogram & redirection: ./someprogram >output.txt ./someprogram <input.txt pipelines: ./someprogram | ./somefilter 11 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make

  12. shell assignment implement a simple shell that supports redirection and pipeline (for Linux or another POSIX system — not xv6) …and prints the exit code of program in the pipeline simplifjed parsing: space-seperated: 12 okay: /bin/ls � -1 � > � tmp.txt not okay: /bin/ls � -l � >tmp.txt okay: /bin/ls � -1 � | � /bin/grep � foo � > � tmp.txt not okay: /bin/ls � -1 � |/bin/grep � foo � >tmp.txt

  13. POSIX: everything is a fjle the fjle: one interface for devices (terminals, printers, …) regular fjles on disk networking (sockets) local interprocess communication (pipes, sockets) basic operations: open(), read(), write(), close() 13

  14. the fjle interface open before use setup, access control happens here byte-oriented real device isn’t? operating system needs to hide that explicit close 14

  15. the fjle interface open before use setup, access control happens here byte-oriented explicit close 14 real device isn’t? operating system needs to hide that

  16. 1 or 2 or …via bufger 3 …via bufger data from disk bufger: recently read 2 read block of data from disk 1 from fjle read char 3 kernel bufgering (reads) program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 15

  17. 1 or 2 or …via bufger 3 …via bufger data from disk bufger: recently read 2 read block of data from disk 1 from fjle read char 3 kernel bufgering (reads) program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 15

  18. 1 or 2 or …via bufger 3 …via bufger data from disk bufger: recently read 2 read block of data from disk 1 from fjle read char 3 kernel bufgering (reads) program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 15

  19. kernel bufgering (reads) …via bufger 3 …via bufger data from disk bufger: recently read 2 read block of data from disk 1 from fjle read char 3 15 program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 1 or 2 or

  20. kernel bufgering (reads) …via bufger 3 …via bufger data from disk bufger: recently read 2 read block of data from disk 1 from fjle read char 3 15 program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 1 or 2 or

  21. kernel bufgering (reads) …via bufger 3 …via bufger data from disk bufger: recently read 2 read block of data from disk 1 from fjle read char 3 15 program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 1 or 2 or

  22. kernel bufgering (writes) to remote machine to be written on disk bufger: data waiting write block of data from disk (when ready) to fjle write char print char program waiting for network bufger: output send data (when ready) disk network operating system 16

  23. kernel bufgering (writes) to remote machine to be written on disk bufger: data waiting write block of data from disk (when ready) to fjle write char print char program waiting for network bufger: output send data (when ready) disk network operating system 16

  24. kernel bufgering (writes) to remote machine to be written on disk bufger: data waiting write block of data from disk (when ready) to fjle write char print char program waiting for network bufger: output send data (when ready) disk network operating system 16

  25. kernel bufgering (writes) to remote machine to be written on disk bufger: data waiting write block of data from disk (when ready) to fjle write char print char program waiting for network bufger: output send data (when ready) disk network operating system 16

  26. kernel bufgering (writes) to remote machine to be written on disk bufger: data waiting write block of data from disk (when ready) to fjle write char print char program waiting for network bufger: output send data (when ready) disk network operating system 16

  27. read/write operations read()/write(): move data into/out of bufger possibly wait if bufger is empty (read)/full (write) actual I/O operations — wait for device to be ready trigger process to stop waiting if needed 17

  28. layering application standard library system calls kernel’s fjle interface device drivers hardware interfaces kernel’s bufgers read/write cout/printf — and their own bufgers 18

  29. why the extra layer better (but more complex to implement) interface: read line formatted input (scanf, cin into integer, etc.) formatted output less system calls (bigger reads/writes) sometimes faster bufgering can combine multiple in/out library calls into one system call more portable interface cin, printf, etc. defjned by C and C++ standards 19

  30. fjlesystem abstraction regular fjles — named collection of bytes also: size, modifjcation time, owner, access control info, … directories — folders containing fjles and directories hierarchical naming: /net/zf14/cr4bd/fall2018/cs4414 mostly contains regular fjles or directories 20

  31. open ... int read_fd = open("dir/file1", O_RDONLY); int write_fd = open("/other/file2", O_WRONLY | O_CREAT | O_TRUNC, 0666); int rdwr_fd = open("file3", O_RDWR); 21 int open( const char *path, int flags); int open( const char *path, int flags, int mode);

  32. open path = fjlename e.g. "/foo/bar/file.txt" file.txt in directory bar in directory foo in “the root directory” e.g. "quux/other.txt other.txt in directory quux in “the current working directory” (set with chdir() ) 22 int open( const char *path, int flags); int open( const char *path, int flags, int mode);

Recommend


More recommend