Unix API 2 — shells / fjle descriptors 1
last time context switch in xv6 (fjnish) POSIX standard — source compatibility fork — copy current process return value in copy (“child”) is 0 return value in original (“parent”) is copy’s process ID (PID) exec — replace program in current process specify new program to load + arguments (+ environment variables) keep same process ID, open fjles, current directory, etc. waitpid — get status of and/or wait for child process(es) can wait for specifjc process or all child processes status int — encodes exit code or other termination reason terminated child process’s pid reserved until it’s waited for (“zombie”) parent exits without waiting? process’s new parent is pid 1 2
POSIX process management essential operations process information: getpid process creation: fork running programs: exec* also posix_spawn (not widely supported), … waiting for processes to fjnish: waitpid (or wait ) process destruction, ‘signaling’: exit , kill 3
shell allow user (= person at keyboard) 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) 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) 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) redirection: ./someprogram >output.txt ./someprogram <input.txt pipelines: ./someprogram | ./somefilter 9 ls -l ≈ /bin/ls -l make ≈ /usr/bin/make
some POSIX command-line features searching for programs (not in assignment) redirection: ./someprogram >output.txt ./someprogram <input.txt pipelines: ./someprogram | ./somefilter 10 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: 11 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() 12
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 13
the fjle interface open before use setup, access control happens here byte-oriented explicit close 13 real device isn’t? operating system needs to hide that
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 14
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 14
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 14
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 14 program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 1 or 2 or
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 14 program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 1 or 2 or
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 14 program 2 from terminal read char waiting for program bufger: keyboard input 1 keypress happens, read disk keyboard operating system 1 or 2 or
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 15
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 15
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 15
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 15
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 15
read/write operations read()/write(): move data into/out of bufger block (make process wait) if bufger is empty (read)/full (write) (default behavior, possibly changeable) actual I/O operations — wait for device to be ready trigger process to stop waiting if needed 16
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 17
why layering? better (?) interface — “read line”, etc. less system calls (bigger reads/writes) sometimes faster 18
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 19
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); 20 int open( const char *path, int flags); int open( const char *path, int flags, int mode);
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() ) 21 int open( const char *path, int flags); int open( const char *path, int flags, int mode);
open: fjle descriptors index into table of open fjle descriptions for each process used by system calls that deal with open fjles 22 int open( const char *path, int flags); int open( const char *path, int flags, int mode); return value = fjle descriptor (or -1 on error)
implementing fjle descriptors in xv6 (1) struct proc { ... // Open files }; ofile[0] = fjle descriptor 0 pointer — can be shared between proceses not part of deep copy fork does null pointers — no fjle open with that number 23 struct file *ofile[NOFILE];
implementing fjle descriptors in xv6 (2) alternate designs: (not meaningful for all fjles) off = location in fjle based on fmags to open should read/write be allowed? needs kept up-to-date (example: on fork ) used to safely delete this struct number of pointers to this struct fjle pointer to list of functions (Linux soln.) class + subclass per type FD_INODE = other kind of fjle struct file { FD_PIPE = to talk to other process }; uint off; char writable; char readable; int ref; // reference count enum { FD_NONE, FD_PIPE, FD_INODE } type; 24 struct pipe *pipe; struct inode *ip;
Recommend
More recommend