o · key i/o system calls |opens the file identified by pathname, returning a file descriptor....
TRANSCRIPT
![Page 1: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/1.jpg)
File I/OCS 3113 Fall 2018
CG and AHF: Introduction to Operating Systems 1
![Page 2: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/2.jpg)
Be sure to install all code prerequisites
CG and AHF: Introduction to Operating Systems2
![Page 3: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/3.jpg)
Download TLPI book code
CG and AHF: Introduction to Operating Systems 3
![Page 4: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/4.jpg)
File Descriptors
| A nonnegative integer that may refer to:regular files, pipes, FIFOs, sockets, terminals or devices.
| Each process has its own assigned set of file descriptors.
| Used by the system to refer to files (not filenames)
| When requested, the lowest-numbered unused file descriptor is assigned
CG and AHF: Introduction to Operating Systems 4
![Page 5: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/5.jpg)
Standard File Descriptors
| When a shell program is run, these descriptors are copied from the terminal to the running program.
| I/O redirection may modify this assignment.
| IDEs may map output to stderr to a red color
| POSIX names are available in <unistd.h>
CG and AHF: Introduction to Operating Systems 5
./myprog <input.txt >output.txt 2>error.txt
New process/program to be run
stdin is mapped to input.txt
stdout is mapped to output.txt
stderr is mapped to error.txt
![Page 6: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/6.jpg)
Key I/O System Calls
| opens the file identified by pathname, returning a file descriptor.
| reads at most count bytes from the open file referred to by fd and stores them in buffer.
| writes up to count bytes from buffer to the open file referred to by fd.
| is called after all I/O has been completed, in order to release the file descriptor fdand its associated kernel resources.
CG and AHF: Introduction to Operating Systems 6
![Page 7: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/7.jpg)
Example: fileio/copy.c
CG and AHF: Introduction to Operating Systems 7
![Page 8: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/8.jpg)
Universality of I/O
same four system calls—open(), read(), write(), and close()—are used to perform I/O on all types of files.
CG and AHF: Introduction to Operating Systems 8
![Page 9: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/9.jpg)
Openopens the file identified by pathname, returning a file descriptor.
CG and AHF: Introduction to Operating Systems 9
![Page 10: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/10.jpg)
CG and AHF: Introduction to Operating Systems 10
![Page 11: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/11.jpg)
Readreads at most count bytes from the open file referred to by fd and stores them in buffer.
CG and AHF: Introduction to Operating Systems 11
![Page 12: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/12.jpg)
CG and AHF: Introduction to Operating Systems 12
![Page 13: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/13.jpg)
Writewrites up to count bytes from buffer to the open file referred to by fd.
CG and AHF: Introduction to Operating Systems 13
![Page 14: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/14.jpg)
CG and AHF: Introduction to Operating Systems 14
![Page 15: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/15.jpg)
Closeis called after all I/O has been completed, in order to release the file descriptor fdand its associated kernel resources.
CG and AHF: Introduction to Operating Systems 15
![Page 16: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/16.jpg)
CG and AHF: Introduction to Operating Systems 16
Always check for errors.
![Page 17: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/17.jpg)
Seeking
CG and AHF: Introduction to Operating Systems 17
![Page 18: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/18.jpg)
File offset
| Also called read- write offset or pointer
| the kernel records a file offset for each open file.
| The first byte of the file is at offset 0.
| The file offset is set to point to the start of the file when the file is opened and is automatically adjusted by each subsequent call to read() or write()
CG and AHF: Introduction to Operating Systems 18
![Page 19: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/19.jpg)
CG and AHF: Introduction to Operating Systems 19
![Page 20: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/20.jpg)
Example: fileio/seek_io.c
20
![Page 21: O · Key I/O System Calls |opens the file identified by pathname, returning a file descriptor. |reads at most count bytes from the open file referred to by fdand stores them in buffer](https://reader034.vdocuments.site/reader034/viewer/2022050416/5f8c356e9e2284314f5f098a/html5/thumbnails/21.jpg)
lseek + read + write
CG and AHF: Introduction to Operating Systems 21
du tfile # The number of blocks used
du tfile # The number of blocks used
./seek_io tfile s10000 wefg # write efg starting at byte point 10000