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...
- Ch.8 - The UNIX System Interface
- The Linux Programming Interface - A Linux and UNIX System Programming Handbook
- The UNIX Time-Sharing System
- For the End User, the Interface Is the System
- POSIX——Portable Operating System Interface of UNIX
- 《Learning the UNIX Operating System》 第一章:入门
- The Evolution of the Unix Time-sharing System
- Yandex Big Data Essentials Week1 Unix Command Line Interface File System exploration
- Migrating to the Solaris Operating System: The Discipline of UNIX-to-UNIX Migrations
- The UNIX system family tree: Research and BSD
- The Design of Unix Operating System (2) kernel
- The Design Of Unix Operating System (4) Buffer cache
- What are the UNIX system calls for I/O?
- Could you tell something about the Unix System Kernel?
- CSE hands-on#2 The UNIX Time-Sharing System
- The Differences Between BSD and System V Unix
- ch
- The design of Unix Operating System (1) overview of System V
- 多文档窗口如何最大化视图视图窗口
- 如何C#中实现在TreeView查找某一节点
- 国台办再次重申欢迎民进党成员赴大陆访问
- 关于String.valueOf()和toString
- 出租司机给我上的MBA课
- Ch.8 - The UNIX System Interface
- VLAN简介
- 微软日文输入法
- 我在网上见到的一篇文章,深有同感
- 又是一本AJAX的书
- 网易,你怎么了。为什么如此让国人伤心、愤怒??
- SQL SERVER2005連接字串中的@3/29
- tomcat的wap服务器设置
- COM组件设计与应用12 - 错误与异常处理