- 论坛徽章:
- 0
|
Linux File Structure
The system uses the number of the file’s inode; the directory structure just names the file for our benefit.
A directory is a file that holds the inode numbers and names of other files. Each directory entry is a link to a file’s inode; remove the filename and you remove the link.
Files and Devices
Three important device files found in both UNIX and Linux are /dev/console, /dev/tty, and /dev/null.
/dev/console,
This device represents the system console. Error messages and diagnostics are often sent to this device.Each UNIX system has a designated terminal or screen to receive console messages. At one time, it mighthave been a dedicated printing terminal. On modern workstations, and on Linux, it’s usually the `active’virtual console, while under X, it will be a special console window on the screen.
/dev/tty
The special file /dev/tty is an alias (logical device) for the controlling terminal (keyboard and screen,or window) of a process, if it has one.
Note that while there’s only one /dev/console device, there are effectively many different physical
devices accessed through /dev/tty.
需要注意的是TTY是和 进程相关联的。
/dev/null
This is the null device. All output written to this device is discarded. Unwanted output is often redirected to /dev/null.
System Calls and Device Drivers
The low-level functions used to access the device drivers, the system calls, include:
❑ open: Open a file or device
❑ read: Read from an open file or device
❑ write: Write to a file or device
❑ close: Close the file or device
❑ ioctl: Pass control information to a device driver//需要注意的是ioctl这个函数能够处理特殊设备的不同特性。
Library Functions
系统调用的低效性有下面亮点:
(1) 一个应用程序如果调用了很多次系统调用,则进程老要在用户空间和内核空间进行切换,这样将浪费时间。我们的策略是让一次系统调用尽量多处理数据。
(2) 系统调用对数据块的大小有限制,比方说一个磁带驱动程序如果规定每次写的数据块的大小为10k,如果你写了1k数据,磁带在物理上也要移动1k的距离,这就导致了空间的浪费。
库函数的出现,解决了这些效率低效的问题,库函数对需要I/O的数据进行缓冲和处理,当数据的大小与特定物理设备的系统调用和谐的时候,才进行一次系统调用。
Low-Level File Access
1、三个特殊的文件描叙符
❑ 0: Standard input
❑ 1: Standard output
❑ 2: Standard error
2、四个主要的函数,以及包含他们的头文件
#include
#include
#include
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
注意open函数建立了文件描叙符与各钟类型文件的联系(包括设备文件),其中oflags项定义入下:
❑ O_APPEND: Place written data at the end of the file.
❑ O_TRUNC: Set the length of the file to zero, discarding existing contents.
❑ O_CREAT: Creates the file, if necessary, with permissions given in mode.
❑ O_EXCL: Used with O_CREAT, ensures that the caller creates the file. The open is atomic; that is,
it’s performed with just one function call. This protects against two programs creating the file at
the same time. If the file already exists, open will fail.
<SPAN style="COLOR: #0070c0; FONT-FAMILY: 宋体; mso-bidi-font-family: Palatino-Roman; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt; mso-ascii-theme-font: major-fareast; mso-fareast-theme-font%3
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/69999/showart_1871909.html |
|