unix api 2 shells fjle descriptors
play

Unix API 2 shells / fjle descriptors 1 last time context switch - PowerPoint PPT Presentation

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


  1. Unix API 2 — shells / fjle descriptors 1

  2. 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

  3. 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

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

  5. 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

  6. 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

  7. 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

  8. 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);

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  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 14

  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 14

  18. 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

  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 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

  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 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

  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 15

  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 15

  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 15

  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 15

  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 15

  26. 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

  27. 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

  28. why layering? better (?) interface — “read line”, etc. less system calls (bigger reads/writes) sometimes faster 18

  29. 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

  30. 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);

  31. 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);

  32. 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)

  33. 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];

  34. 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;

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend