21
File I/O CS 3113 Fall 2018 CG and AHF: Introduction to Operating Systems 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

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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