Changelog Changes made in this version not seen in fjrst lecture: 6 September: fjx stray @s on ‘implementing fjle descriptors in xv6 slide’ 6 September: typical pattern with redirection: hilite parts of code more sensibly 6 September: exec preserves open fjles: add slide 6 September: dup2 example: clarify comment, note overall purpose at top 6 September: read’ing one byte at a time: missing ) 6 September: layering: annotate to indicate read/write are system calls, kernel bufgers in layers, user bufgers in layers 0
Unix API 2: fjles 1
last time POSIX — standardized Unix process control blocks fork, exec, waitpid 2
post-quizzes starting this week, post-quizzes link ofg course website same software as CS 3330 box around question turns green: answer recorded no time limits, due before Tuesday’s class released Friday morning or possibly earlier (e.g. Thursday evening) 3
shell allow user (= person at keyborad) to run applications user’s wrapper around process-management functions upcoming homework — make a simple shell 4
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? 5
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 6 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make
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
searching for programs POSIX convention: PATH environment variable example: /home/cr4bd/bin:/usr/bin:/bin checked in order one way to implement: [pseudocode] for (directory in path) { } 8 execv(directory + "/" + program_name, argv);
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 9 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make
running in background $ ./long_computation >tmp.txt & [1] 4049 $ ... [1]+ Done ./long_computation > tmp.txt $ cat tmp.txt the result is ... & — run a program in “background” initially output PID (above: 4049) print out after terminated one way: use waitpid with option saying “don’t wait” 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 11 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make
shell redirection ./my_program ... <input.txt : run ./my_program ... but use input.txt as input like we copied and pasted the fjle into the terminal echo foo >output.txt : runs echo foo , sends output to output.txt like we copied and pasted the output into that fjle (as it was written) 12
exec preserves open fjles the process control block discarded old memory not changed! copy arguments new stack, heap, … executable fjle loaded from memory … user regs … fd 1: … fd 0: (terminal …) open fjles user memory kernel stack ecx=133 init. val. , … eax=42 init. val. , 13
exec preserves open fjles the process control block discarded old memory not changed! copy arguments new stack, heap, … executable fjle loaded from memory … user regs … fd 1: … fd 0: (terminal …) open fjles user memory kernel stack ecx=133 init. val. , … eax=42 init. val. , 13
exec preserves open fjles the process control block discarded old memory not changed! copy arguments new stack, heap, … executable fjle loaded from memory … user regs … fd 1: … fd 0: (terminal …) open fjles user memory kernel stack ecx=133 init. val. , … eax=42 init. val. , 13
exec preserves open fjles the process control block discarded old memory not changed! copy arguments new stack, heap, … executable fjle loaded from memory … user regs … fd 1: … fd 0: (terminal …) open fjles user memory kernel stack ecx=133 init. val. , … eax=42 init. val. , 13
fork copies open fjles eax=420, copy copy child process control block … … fd 1: … fd 0: … open fjles user memory kernel stack ecx=133, … user regs user regs memory parent process control block … … fd 1: … fd 0: … open fjles user memory kernel stack ecx=133, … pid , eax=42 child (new) 14
typical pattern with redirection } else if (pid > 0) { … pid = fork(); if (pid == 0) { open new files; exec…(…); … waitpid(pid,…); … … } … main() { … } } waitpid(pid,…); pid = fork(); … if (pid == 0) { open new files; exec…(…); … } else if (pid > 0) { waitpid(pid,…); } } else if (pid > 0) { … pid = fork(); if (pid == 0) { open new files; exec…(…); … 15
redirecting with exec std output, std error are fjles yes, your terminal is a fjle more on this later after forking, open fjles to redirect …and make them be standard output/error missing pieces: how open fjles becomes default output/input 16
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 17 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make
shell assignment implement a simple shell that supports redirection and pipeline …and prints the exit code of program in the pipeline simplifjed parsing: space-seperated: 18 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
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() 19
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 20
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 20
kernel bufgering (reads) …via bufger …via bufger data from disk bufger: recently read read block of data from disk from fjle read char from terminal program read char waiting for program bufger: keyboard input keypress happens, read disk keyboard operating system 21
kernel bufgering (reads) …via bufger …via bufger data from disk bufger: recently read read block of data from disk from fjle read char from terminal program read char waiting for program bufger: keyboard input keypress happens, read disk keyboard operating system 21
kernel bufgering (reads) …via bufger …via bufger data from disk bufger: recently read read block of data from disk from fjle read char from terminal program read char waiting for program bufger: keyboard input keypress happens, read disk keyboard operating system 21
kernel bufgering (reads) …via bufger …via bufger data from disk bufger: recently read read block of data from disk from fjle read char from terminal program read char waiting for program bufger: keyboard input keypress happens, read disk keyboard operating system 21
kernel bufgering (reads) …via bufger …via bufger data from disk bufger: recently read read block of data from disk from fjle read char from terminal program read char waiting for program bufger: keyboard input keypress happens, read disk keyboard operating system 21
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 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 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 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 22
Recommend
More recommend