易截截图软件、单文件、免安装、纯绿色、仅160KB

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;
}


相关文档:

Linux设备模型之input子系统详解

一:前言
最近在研究android的sensor driver,主要是E-compass,其中用到了Linux input子系统.在网上也看了很多这方面的资料,感觉还是这篇分析的比较细致透彻,因此转载一下以便自己学习,同时和大家分享!
(这篇博客主要是以键盘驱动为例的,不过讲解的是Linux Input Subsystem,可以仔细的研究一下!)
键盘驱动将检 ......

Linux 下串口编程入门[IBM]

级别: 初级
左锦 (zuo170@163.com), 副总裁, 南沙资讯科技园
2003 年 7 月 03 日
Linux 操作系统从一开始就对串行口提供了很好的支持,本文就 Linux 下的串行口通讯编程进行简单的介绍。
串口简介
串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用。常用的串口是 RS-232-C 接口(又称 EIA RS-2 ......

原创:纠正国人对Linux的误解和错误认识

错误印象和认识罗列如下,一一解释:
1。linux下的软件太少
回答:linux 下的软件一点也不少。windows还在娘肚子里的时候,Unix已经如日中天了。要知道微软公司开发的第一个操作系统是什么吗?是一个叫做Xenix的东西,是Unix的一个分支,后来才去搞DOS的。有人又问了,Unix不是Linux阿,要知道,Linux完全重新的实现了Uni ......

Linux 用户层如何调用接口设置寄存器

下面的代码分析是根据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 ......

linux内核导读

一.核心源程序的文件组织:
1.Linux核心源程序通常都安装在/usr/src/linux下,而且它有一个非常简单的编号约定:任何偶数的核心(例如2.0.30)都是一个稳定地发行的核心,而任何奇数的核心(例如2.1.42)都是一个开发中的核心。
本文基于稳定的2.2.5源代码,第二部分的实现平台为 Redhat Linux 6.0。
2.核心源程序 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号