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. HCI层协议概述:
HCI提供一套统一的方法来访问Bluetooth底层。如图所示:
从图上可以看出,Host Controller Interface(HCI) 就是用来沟通Host和Module。Host通常就是PC, Module则是以各种物理连接形式(USB,serial,pc-card等)连接到PC上的bluetooth Dongle。
在Host这一端:application,SDP,L2cap等协议 ......
例一:发送Signaling Packet:
Signaling Command是2个Bluetooth实体之间的L2CAP层命令传输。所以得Signaling Command使用CID 0x0001.
多个Command可以在一个C-frame(control frame)中发送。
如果要直接发送Signaling Command.需要建立SOCK_RAW类型的L2CAP连接Socket。这样才有机会自己填充Command Code,Identi ......
一.填空题:
1. 在Linux系统中,以文件方式访问设备 。
2. Linux内核引导时,从文件/etc/fstab中读取要加载的文件系统。
3. Linux文件系统中每个文件用i节点来标识。
4. 全部磁盘块由四个部分组成,分别为引导块 、专用块 、 i节点表块 和数据存储块。
5. 链接分为:硬链接 和 符号链接。
6. 超级块包含了i节点表 ......
错误印象和认识罗列如下,一一解释:
1。linux下的软件太少
回答:linux 下的软件一点也不少。windows还在娘肚子里的时候,Unix已经如日中天了。要知道微软公司开发的第一个操作系统是什么吗?是一个叫做Xenix的东西,是Unix的一个分支,后来才去搞DOS的。有人又问了,Unix不是Linux阿,要知道,Linux完全重新的实现了Uni ......
下面的代码分析是根据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 ......