Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1663444
  • 博文数量: 607
  • 博客积分: 10031
  • 博客等级: 上将
  • 技术积分: 6633
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-30 17:41
文章分类

全部博文(607)

文章存档

2011年(2)

2010年(15)

2009年(58)

2008年(172)

2007年(211)

2006年(149)

我的朋友

分类: LINUX

2008-08-21 19:52:59

In Linux, everything is a file. well, almost.

我们可以想对待文件一样访问串口,打印机,磁盘文件,和其他设备。

基本上,只需要5个基本函数:open, close, read, write, ioctl

目录也是特殊的文件。

linux中,不会直接写目录文件。有高级的opendir/readdir 接口来获知目录的信息。

几乎每一个东西都被表述为文件。

甚至硬件设备也被表述为文件。
例如:将CDROM mount为1个文件
mount -t iso9660 /dev/hdc /mnt/cdrom
cd /mnt/cdrom

有3个重要的设备文件
/dev/console:系统控制台
/dev/tty:终端(键盘、鼠标,窗口)的别名
/dev/null:null设备,所有输出到这个终端的信息都会被丢弃。可以用这个文件作为空文件的cp源
  $cp /dev/null empty_file
  $echo "....."> /dev/null

系统调用和设备驱动

内核就是许多设备驱动构成。
设备驱动封装了依赖于硬件的特征。

低级的可以访问设备驱动,系统调用。
包括:
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被用于必要的特定硬件的控制。

库文件
--------------------------
User program
--------------|
Library       |
--------------------------
System calls
--------------------------
---------------|
Device Drivers | kernel
------------------------
Hardware Devices
------------------------

低级文件访问


#include
size_t write(int fildes, const void * buf, size_t nbytes);
int main()
{
  if(18!=write(1, "hello world!\n", 13))
     write(2, "safe\n", 5);
  exit(0);
}
输出到标准输出。当程序推出时,所有打开的文件描述符会自动关闭,所以不需要直接关闭。

#include
size_t read(int fildes, void*buf, size_t nbytes);

#include
#include
int main()
{
    char buffer[128];
    int nread;
    nread=read(0, buffer, 128);
    if(nread==-1)
        write(2, "A read error has occurred\n", 26);
    if((write(1,buffer,nread))!=nread)
        write(2, "A write error has occurred\n", 27);
    exit(0);
}

STDIN:  0
STDOUT: 1
STDERR: 2

#include
#include
#include

int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);

types.h/stat.h对某些unix系统是需要的。
Mode:
O_RDONLY
O_WRONLY
O_RDWR

oflags:
O_APPEND
O_TRUNC
O_CREAT
O_EXCL

umask: user mask
[1][2][3]
[1]:user
[2]:group
[3]:others
Execute 1; Write: 2; Read: 4; can do anything: 0;

#include
int close(int fildes);

#include
int ioctl(int fildes, int cmd, ...)

别的管理文件的系统调用
#include
#include
off_t lseek(int fildes, off_t offset, int whence)
int fstat(int fildes, struct stat *buf);
int stat(const char*path, strucutr stat *buf);
int lstat(const char*path, sturct stat *buf);

dup 提供我们两个或多个文件描述符(同一个文件)
dup2


标准I/O库

fopen,fclose
fread,fwrite
fflush
fseek
fgetc,getc,getchar
fputc,putc,putchar
fgets,gets
printf,fprintf, and sprintf
scanf, fscanf, and sscanf

格式化输入输出


文件和目录维护
#include
int chmod(const char*path, mode_t mode);

#include
int chown(coonst char* path, uid_t owner, gid_t group);

unlink 删除1个文件

mkdir rmdir

chdir getcwd

搜索目录
opendir, readdir, telldir, seekdir,closedir

错误
外部变量errno用来表明一个标准的错误
#include
char* strerror(int errnum);
将1个错误数字转换成1个字符串


/proc文件系统










阅读(964) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~