Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55102
  • 博文数量: 21
  • 博客积分: 1440
  • 博客等级: 上尉
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 14:47
文章分类

全部博文(21)

文章存档

2010年(9)

2009年(12)

我的朋友

分类: C/C++

2009-09-06 09:56:38

Low level input and output

1.文件描述符是一个非负整数,打开一个现存文件或创建新文件时,内核向进程返回一个文件描述符。当读写一个文件时,用open或create返回的文件描述符标识该文件,将其作为参数传给read或write。unix shell使文件描述符0与进程的标准输入相结合,文件描述符1与标准输出相结合,文件描述符2与标准出错输出相结合。

在POSIX.1应用程序中,幻数0,1,2应被代换成符号常数:STDIN_FILEN0,STDOUT_FILEN0,STDERR_FILEN0(unistd.h) if you want to do control operations that are specific to a particular kind of device,you must use a file descriptor ,there are no facilities to use streams in this way.You must also use file decriptors if your program needs to do input or output in special modes,such as nonblocking(or poll)input.

与设备有关的文件操作大多数都是要基于文件描述符的I/O,如果你的设备是串口,要设置波特率之类的,就要使用low level I/O即低级I/O。基于流的和基于描述符的I/O各有各的好处。如果想写的东西尽量可移植,最好是就流的I/O,若与硬件打交道,就只能使用低级I/O即基于描述符的I/O。

2.函数声明

int open(const char *filename,int flags[ ,mode_t mode]);

int close(int filedes);

The file position indicator for the file is at the beginning of the line.The argument mode is used only when a file is created,but it doesn't hurt to supply the argument in any case.The flags argument control how the file is to be opened,it is a bit mask.

flags:O_RDONLY,O_WRONLY,O_RDWR,O_CREAT,O_EXCL,O_NOCTTY

3.input and output primitives

ssize_t read(int filedes,void *buffer,size_t size);

ssize_t write(int filedes,const void *buffer,size_t size);

4.setting the file position of a descriptor

off_t lseek(int filedes,off_t offset,int whence);

whence: SEEK_SET,SEEK_CUR,SEEK_END

5.监控I/O

int select(int numfds,fd_set *readfds,fd_set *writefds,

              fd_set *exeptfds,struct timeval *timeout);

6.some macro

void FD_SET(int filedes,fd_set *set);

void FD_IERO(fd_set *set);

int FD_SETSIZE;

void FD_CLR(int filedes,fd_set *set);

int FD_ISSET(int filedes,const fd_set *set);

阅读(800) | 评论(0) | 转发(0) |
0

上一篇:基于流的I/O

下一篇:C实现网卡状态检测

给主人留下些什么吧!~~