1 / 40

ch04: UNIX I/O

ch04: UNIX I/O. Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234. Objectives. Learn the basics of device-independent I/O Experiment with read and write Explore ways to monitor multiple descriptors Use correct error handling

shawn
Download Presentation

ch04: UNIX I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ch04: UNIX I/O Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234

  2. Objectives • Learn the basics of device-independent I/O • Experiment with read and write • Explore ways to monitor multiple descriptors • Use correct error handling • Understand inheritance of file descriptors

  3. 4.1 Device Terminology • A peripheral device is a piece of hardware accessed by a computer system • disks, tapes, CD-ROMs, screens, keyboards, printers, mouse devices and networks • A device driver, which is a hardware control module of operating system, hides the details of device operation and protects the device from unauthorized use • UNIX provide uniform access to most device through five functions • open, close, read, write, ioctl • All devices are represented by files, called special files • Block and character special file

  4. 4.2 Reading and Writing #include <unistd.h> ssize_t read(int fildes, void *buf, size_t nbyte); • size_t is an unsigned integer type • ssize_t is a signed integer type • this can return fewer bytes than requested. • you must allocate a buffer to hold the bytes read. • a return value of -1 with errno set to EINTR is not usually an error.

  5. It is not an error if this returns a value greater than 0 and less than nbyte • You must restart the write if it returns fewer bytes than requested. • a return value of -1 with errno set to EINTR is not usually an error. #include <unistd.h> ssize_t write(int fildes, const void *buf, size_t nbyte);

  6. 4.3 Opening and Closing Files #include <fcntl.h> #include <sys/stat.h> int open(const char *path, int oflag); int open(const char *path, int oflag, mode_t mode); • Possible value of oflag include O_RDONLY: read onlyO_WRONLY: write onlyO_RDWR: read and writeO_APPEND: writes always write to endO_CREAT: create the file if it does not existO_EXCL: used with O_CREAT, return an error if file existsO_NOCTTY: do not become a controlling terminalO_NONBLOCK: do not block if not ready to open, also affects reads and writesO_TRUNC: discard previous contents

  7. You must use the 3-parameter form of open if the O_CREAT flag is used. This specifies permissions

  8. 4.4 the select Function • The handling of I/O from multiple source is an important problem that arises in may different forms • One method of monitoring multiple file descriptor is to use a separate process for each one

  9. How would you print out the total number of bytes read from two files? • multiple thread, process communication • Two process have separate address space and so it is difficult for them to interact • The select provide a method of monitoring file descriptor from a single process

  10. #include <sys/select.h> int select(int nfds,fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, struct timeval *restrict timeout); void FD_CLR(int fd, fd_set *fdset); int FD_ISSET(int fd, fd_set *fdset); void FD_SET(int fd, fd_set *fdset); void FD_ZERO(fd_set *fdset); • nfds: the range of file descriptors to be monitored must be at least one greater than the largest descriptor • read, write and error fds: the set of descriptors to be monitored • timeout: forces a return after a certain period of time has elapsed • return clear all the descriptor except those descriptor that are ready

  11. How long will it blocking? What happen if two files are ready?

  12. 4.5 The poll Function • The poll function is similar to select • It organizes the information by file descriptor rather than by type of condition #include <poll.h> int poll(struct pollfd fds[], nfds_t nfds, int timeout);

  13. 4.6 File Representation: File Descriptor • The File Descriptor Table is part of the user program area and can be thought of as an array of pointers indexed by the file descriptors. • The pointers point to entries in the System File Table. • The System File Table is in the kernel area. • It contains an entry for each open file. • Entries contain pointers to a table of i-nodes kept in memory. • Entries contain other information including the current file offset and a count of the number of file descriptors that are using this entry. • When a file is closed the count is decremented. The entry is freed when the count becomes 0. • The In-Memory i-node Table contains copies of the i-nodes that are being used.

  14. myfd = open (“/home/ann/my.day”, O_RDONLY);

  15. 4.6.2 File Pointer and buffering • The ISO C standard I/O library uses file pointers rather than file descriptor FILE *myfp; if( (myfp=fopen (“/home/ann/my.day”, “w”)) == NULL ) perror(“ Fail to open /home/ann/my.dat”); else fprintf(myfp,”This is a rest”);

  16. I/O using file pointers will read from or write to the buffer. • The buffer will be filled or emptied when necessary. • A write may fill part of the buffer without causing any physical I/O to the file. • The amount of buffer may vary. • If a write is done to standard output and them the program crashes, the data written may not show up on the screen. • Standard error is not buffered. • Interleaving output to standard output and standard error may cause output to appear in an unpredictable order. • You can force the physical output to occur with an fflush.

  17. 4.6.3 Inheritance of file descriptor • When fork creates a child, the child inherits a copy of the parents address space, including the file descriptor table.

  18. Suppose the first few bytes in the file my.dat are abcdef. What output would be generated in the previous example?

  19. open after fork Suppose the first few bytes in the file my.dat are abcdef. What output would be generated in the previous example?

  20. What output would be generated by above two programs?

  21. 4.7 Filter and Redirection • The cat command take a list of file names as a command argument, read each of the files in succession, and echoes the contents of each file to standard output. • If no input file is specified, it takes it input from standard input • A program can modify the file descriptor table entry so that it can points to a different entry in the system file table • This action to the standard I/O is known as redirection cat > my.file

  22. The dup2 function takes tow parameters, fildes and filedes2. • It copies the pointer of entry fildes into entry fildes2 • It closes entry fildes2 of the file descriptor table if it was open #incldue <unistd.h> int dup2(int fildes, int fildes2)

  23. 4.8 File Control • The fcntl function is a general-purpose function for retrieving and modifying the flags associated with an open file descriptor #include <fcntl.h> #include <unistd.h> #include <sys/types.h> int fcntl(int fildes int cmd, /* arg */ …);

  24. When a file descriptor has been set for nonblocking I/O, read and write function return -1 and set errno to EAGAINto report that the process would be delayed if a blocking I/O operation were tried

More Related