Ch.8 - The UNIX System Interface

来源:互联网 发布:知乎日报下载 编辑:程序博客网 时间:2024/05/21 10:58

    This chapter describes how to use some of the most important system calls from C program.

8.1 File Descriptors

    In the UNIX operating system, all input and output is done by reading or writing files, because all peripheral devices, even keyboard and screen, are files in the file system. The file descriptor is used instead of the name to identify the file,just as the file handle of MS-DOS.The user of a program can redirect I/O to and from files with < and >:
    prog <infile >outfile
in this case, the shell changes the default assignments for file descriptor 0 and 1 to the named files.

8.2 Low Level I/O - Read and Write

    Input and output uses the read and write system calls, which are accessed from C programs through two functions called read and write.
    int n_read = read(int fd, char *buf, int n);
    int n_written = write(int fd, char *buf, int n);
Each call returns a count of the number of bytes transferred. Any number of bytes can be read or written in one call. Larger sizes will be more efficient because fewer system calls will be made.

8.3 Open, Creat, Close, Unlink

    Other than the default standard input, output and error, you must explicitly open files in order to read or write them. There are two system calls for this, open and creat [sic].Open returns a file descriptor, which is just a int. it returns -1 if any error occurs.
    int open(char *name, int flags, int perms);  // flags = O_RDONLY, O_WRONLY or O_RDWR. perms = 0.
    It is an error to try to open a file that does not exist. The system call creat is provided to create new files, or to re-write old ones.
    int creat(char *name, int perms);
If the file already exists, creat will truncate it to zero length, thereby discarding its previous contents. It creates it with the permissions specified by the perms argument.
    There is alimit (often about 20) on the number of files that a program may have open simultaneously. Accordingly, any program that intends to process many files must be prepared to re-use file descriptors. The function close(int fd) breaks the connection between a file descriptor and an open file, and frees the file descriptor for use with some other file; is corresponds to fclose in the standard library except that there is no buffer to flush. The function unlink(char *name) removes the file name from the file system. It corresponds to the standard library function remove.

8.4 Random Access - Lseek

    The system call lseek provides a way to move around in a file without reading or writing any data:
    long lseek(int fd, long offset, int origin);
sets the current position in the file whose descriptor is fd to offset, which is taken relative to the location specified by origin.The return value from lseek is a long that gives the new position in the file, or -1 if an error occurs. The standard library function fseek is similar to lseek except that the first argument is a FILE * and the return is non-zero if an error occurred.

 to be continued...

原创粉丝点击