+ All Categories
Home > Documents > ch04: UNIX I/O

ch04: UNIX I/O

Date post: 14-Jan-2016
Category:
Upload: shawn
View: 41 times
Download: 0 times
Share this document with a friend
Description:
ch04: UNIX I/O. Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] 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 - PowerPoint PPT Presentation
Popular Tags:
40
ch04: UNIX I/O Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] Rm: 1228, Tel: 580-5234
Transcript
Page 1: ch04: UNIX I/O

ch04: UNIX I/O

Ju, Hong TaekComputer Network Lab.

Keimyung [email protected]

Rm: 1228, Tel: 580-5234

Page 2: ch04: UNIX I/O

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

Page 3: ch04: UNIX I/O

4.1 Device Terminology

A peripheral device is a piece of hardware accessed by a computer system disks, tapes, CD-ROMs, screens, keyboards, printers, mo

use devices and networks A device driver, which is a hardware control module of op

erating 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

Page 4: ch04: UNIX I/O

4.2 Reading and Writing

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 n

ot usually an error.

#include <unistd.h>ssize_t read(int fildes, void *buf, size_t nbyte);

Page 5: ch04: UNIX I/O
Page 6: ch04: UNIX I/O

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

Page 7: ch04: UNIX I/O
Page 8: ch04: UNIX I/O
Page 9: ch04: UNIX I/O
Page 10: ch04: UNIX I/O
Page 11: ch04: UNIX I/O

4.3 Opening and Closing Files

Possible value of oflag include O_RDONLY: read only

O_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

#include <fcntl.h> #include <sys/stat.h>int open(const char *path, int oflag);int open(const char *path, int oflag, mode_t mode);

Page 12: ch04: UNIX I/O
Page 13: ch04: UNIX I/O

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

Page 14: ch04: UNIX I/O

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

Page 15: ch04: UNIX I/O
Page 16: ch04: UNIX I/O

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

Page 17: ch04: UNIX I/O

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

#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);

Page 18: ch04: UNIX I/O

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

Page 19: ch04: UNIX I/O
Page 20: ch04: UNIX I/O
Page 21: ch04: UNIX I/O

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

Page 22: ch04: UNIX I/O

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.

Page 23: ch04: UNIX I/O

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

Page 24: ch04: UNIX I/O

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”);

elsefprintf(myfp,”This is a rest”);

Page 25: ch04: UNIX I/O
Page 26: ch04: UNIX I/O

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 phys

ical I/O to the file. The amount of buffer may vary. If a write is done to standard output and them the progra

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

Page 27: ch04: UNIX I/O

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.

Page 28: ch04: UNIX I/O

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

Page 29: ch04: UNIX I/O

open after fork

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

Page 30: ch04: UNIX I/O
Page 31: ch04: UNIX I/O

What output would be generated by above two programs?

Page 32: ch04: UNIX I/O

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 in

put

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

Page 33: ch04: UNIX I/O
Page 34: ch04: UNIX I/O

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 w

as open

#incldue <unistd.h>int dup2(int fildes, int fildes2)

Page 35: ch04: UNIX I/O
Page 36: ch04: UNIX I/O
Page 37: ch04: UNIX I/O

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 */ …);

Page 38: ch04: UNIX I/O

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

Page 39: ch04: UNIX I/O
Page 40: ch04: UNIX I/O

Recommended