Linux System Programming阅读笔记之 read(....)
关于read(...)返回值的正确判断:p30
File I/O 的 read(...)函数用法:
有问题的代码,只判断返回值为-1的情况。
unsigned long word;
ssize_t nr;
/* read a couple bytes into 'word' from 'fd' */
nr = read (fd, &word, sizeof (unsigned long));
if (nr == -1)
/* error */
Indeed, a call to read( ) can result in many possibilities:
• The call returns a value equal to len. All len read bytes are stored in buf. The
results are as intended.
• The call returns a value less than len, but greater than zero. The read bytes are
stored in buf. This can occur because a signal interrupted the read midway, an
error occurred in the middle of the read, more than zero, but less than len bytes’
worth of data was available, or EOF was reached before len bytes were read.
Reissuing the read (with correspondingly updated buf and len values) will read the
remaining bytes into the rest of the buffer, or indicate the cause of the problem.
• The call returns 0. This indicates EOF. There is nothing to read.
• The call blocks because no data is currently available. This won’t happen in nonblocking
mode.
• The call returns -1, and errno is set to EINTR. This indicates that a signal was
received before any bytes were read. The call can be reissued.
• The call returns -1, and errno is set to EAGAIN. This indicates that the read would
block because no data is currently available, and that the request should be reissued
later. This happens only in nonblocking mode.
• The call returns -1, and errno is set to a value other than EINTR or EAGAIN. This
indicates a more serious error.
正确做法:
ssize_t ret;
while (len != 0 && (ret = read (fd, buf, len)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror ("read");
break;
}
len -= ret;
buf += ret;
}
相关文档:
1. 进程是什么?
一个进程就是出于执行期的程序, 包括:可执行程序代码(代码段), 打开的文件, 挂起的信号, 内核内部数据, 处理器状态, 地址空间, 一个或多个执行线程, 当然还包括用来存放全局变量的数据段, 等等.
2. 什么是线程?它和进程的关系是什么样的? 线程在LINUX中具体是怎么样实现的?
是在进程中活动的对象 ......
常常有人问:我想学习内核,需要什么基础吗?Linus Torvalds本人是这样回答的:你必须使用过Linux。 这个……还是有点太泛了吧,我想下面几个基础可能还是需要的,尽管不一定必需:
1, 关于操作系统理论的最初级的知识。不需要通读并理解《操作系统概念》《现代操作系统》等巨著,但总要知道分时(t ......
下面的代码分析是根据linux-2.6.17.14_stm22版本
#define STSYS_WriteRegDev32LE(Address_p, value) writel((u32)value, (void*)Address_p)
在include/asm-sh/io.h
#define writel(v,a) ({__raw_writel((v),(a));mb();})
#define __raw_write(v,a) __writ ......
系统
# uname -a # 查看内核/操作系统/CPU信息
# head -n 1 /etc/issue # 查看操作系统
版本
# cat /proc/cpuinfo # 查看CPU信息
# hostname &n ......