易截截图软件、单文件、免安装、纯绿色、仅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的initcalls调用机制

只要看看 include/linux/init.h中的定义就清楚了:
#define core_initcall(fn)        __define_initcall("1",fn)
#define postcore_initcall(fn)        __define_initcall("2",fn)
#define arch_initcall(fn)     &n ......

LINUX进程管理

1. 进程是什么?
一个进程就是出于执行期的程序, 包括:可执行程序代码(代码段), 打开的文件, 挂起的信号, 内核内部数据, 处理器状态, 地址空间, 一个或多个执行线程, 当然还包括用来存放全局变量的数据段, 等等.
 
2. 什么是线程?它和进程的关系是什么样的? 线程在LINUX中具体是怎么样实现的?
是在进程中活动的对象 ......

学习Linux应该掌握的


一.填空题:
1. 在Linux系统中,以文件方式访问设备 。
2. Linux内核引导时,从文件/etc/fstab中读取要加载的文件系统。
3. Linux文件系统中每个文件用i节点来标识。
4. 全部磁盘块由四个部分组成,分别为引导块 、专用块 、 i节点表块 和数据存储块。
5. 链接分为:硬链接 和 符号链接。
6. 超级块包含了i节点表 ......

Linux使用

在linux中userid和usergroup分别存于 /etc/group, /etc/shadow, /etc/passwd
在group 中可以修改sudo group和admin权限
远程登陆设置在 /etc/ssh/ssh-config 和 /etc/ssh/sshd-config里,重设后 /etc/init.d/ssh restart
在webmin中更改了firewall后必须重启网络 : /etc/init.d/networking restart
In Ubuntu, remote d ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号