最近这一个月在看《Linux/Unix系统编程手册》,在学习关于Linux的系统编程。之前学习Linux的时候就打算写关于Linux的学习记录,因为觉得自己学得不好,老是写不出东西。但是现在觉得学习记录应该坚持写,慢慢就会有收获,坚持写才可以锻炼自己的表达能力。
《Linux/Unix系统编程手册》这本书的评价很高,但是个人觉得翻译得不太好。其实终究是因为自己的英文阅读能力太差和没什么钱,只能看翻译版。看了接近一个月,觉得这本书介绍的接口很详细,程序清单基本会提及到介绍的接口。个人觉得作为入门书应该是可以的,起码看完一遍会有初步的认识,而且作为参考手册也不错。打算看完这本书之后,继续学习《UNIX环境高级编程》,学无止境。
第1章:
介绍了一些历史,关于Linux、Unix和C;还有一些标准POSIX、SUS
第2章:
介绍Linux和Unix的一些基本概念。
1.操作系统的两个含义:一,包含管理和分配计算机资源的核心层软件和附带的所有标准软件;二,仅仅指管理和分配计算机资源的核心层软件。
2.内核的作用:进程调度、内存管理、文件系统、创建和终止进程、对设备的访问、联网、提供API。其实就是抽象,用户通过内核来使用硬件,而不用直接与硬件打交道。
3.用户态和内核态。
。。。。。。。。。。。。。。。。还有一堆的概念。。。。。。。。。。。。。。。。。。。
第3章
介绍系统编程的概念。
系统调用(systerm calls)是用户程序与操作系统之间的接口,系统调用在内核态。库函数(library functions),一些库函数是通过系统调用来实现,好处是提供了比底层系统调用更方便的接口。
还有本书的代码所需的一些函数、头文件。。。。。。。。。。。
第4章
介绍了文件I/O。
文件描述符,用来表示已经打开的文件(所有类型),是一非负整数。最基本的文件描述符0(标准输入)、1(标准输出)、2(标准错误)。
I/O操作:首先通过调用open获取一个文件描述符,再对该文件描述符进行read或者write操作,最后使用close释放文件描述符和相关资源。
open()调用,打开一个文件或者创建一个文件。
#include <sys/stat.h>
#include <fcntl.h> int open(const char *pathname, int flags, .../* mode_t mode */);
成功调用返回文件描述符,失败返回-1;
1、pathname为打开文件的文件名; flags为位掩码,用于指定文件的访问模式;mode为位掩码参数,用于指定文件的访问权限(可选)。
2、flags位掩码的标志分为文件访问模式标志、文件创建标志、已打开文件的状态标志;其中文件访问模式标志如下表,这些标志不能同时使用。
访问模式 | 描述 |
O_RDONLY | 以只读方式打开文件 |
O_WRONLY | 以只写方式打开文件 |
O_RDWR | 以读写方式打开文件 |
比较常用的标志还有O_APPEND(总是在文件尾部添加数据)、O_CREAT(如果要打开的文件不存在就新建一个空文件)、O_EXCL(与O_CREAT结合使用表明如果文件已经存在,就不会打开文件)。
3、open()函数的错误:省略。
read()调用,从文件描述符对应的文件中读取数据。
#include <unistd.h> ssize_t read(int fd, void *buffer, size_t count);
fd为文件描述符;buffer为存储数据的缓冲区;count为最多能读取的字节数。
成功调用返回实际读取字节数,遇到文件结束符返回0,出现错误返回-1。
PS:要在缓冲区最后添加一个表示终止的空字符。
write()调用,往文件描述符对应的文件写入数据。
#include <unistd.h> ssize_t write(int fd, void *buffer, size_t count);
fd为文件描述符;buffer为存储数据的缓冲区;count为准备从buffer写入到文件的数据的字节数。
成功调用返回实际写入文件的字节数, 失败返回-1。
close()调用,关闭已经打开的文件描述符。
#include <unistd.h> int close(int fd);
fd为文件描述符。
成功调用返回0,失败返回-1 。
所有类型的文件和设备驱动都实现了相同的I/O接口,所以无需针对特殊的文件编写代码。
文件偏移量:读写偏移量或指针,是执行下一个read和write操作的文件起始位置(相对与文件头部起始点)。
lseek(),改变文件偏移量。
#include <unistd.h> off_t lseek(int fd, off_t offset, int whence);
fd为文件描述符,offset为偏移量(字节),whence为表明解释offset参数的方法。
whence参数:
SEEK_SET | 将文件偏移量设置为从文件头起始点开始的offset个字节 |
SEEK_CUR | 相对于当前文件偏移量,再加上offset个字节 |
SEEK_END | 将文件偏移量设置为起始于文件尾部的offset个字节 |
文件空洞 :当文件的偏移量大于文件的当前长度时,文件结尾到新写入数据之间的空间称为文件空洞。读取文件空洞的内容会返回以0填充的缓冲区。
“空洞是否占用磁盘空间由文件系统决定”----《百度百科》
“如果空洞的边界落在块内,而非恰好落在块的边界上,则会分配一个完整的块来存储数据,块中与空洞相关的部分则以空字节填充”----书本提示
练习:
4-1:tee命令是从标准输入中读取数据,直至文件结尾,随后将数据写入标准输入和命令行参数所指定的文件。请使用I/O系统调用实现tee命令,默认情况下,若已存在命令行参数指定文件同名的文件tee命令会将其覆盖。如文件以存在,请实现-a命令行选项(tee -a file)在文件结尾出追加数据。
/*
* =====================================================================================
*
* Filename: my_tee.c
*
* Description: tee
*
* Version: 1.0
* Created: 2014年03月14日 18时23分04秒
* Revision: none
* Compiler: gcc
*
* Author: alan (), alan19920626@gmail.com
* Organization:如果输入为my_tee -a会出现错误而不是程序自动停止而且报错!!!!需要改进!!!
*
* =====================================================================================
*/ #include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include "tlpi_hdr.h" #define MAX_READ 20 int main(int argc, char *argv[]){
int fd, opt;
char buf[MAX_READ + ];
ssize_t numRead;
off_t offset; if(argc < || strcmp(argv[], "--help") == )
usageErr("%s [-a] file"); fd = open(argv[argc-], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(fd == -)
errExit("open"); offset = lseek(fd, , SEEK_SET);
while((opt = getopt(argc, argv, ":a")) != -){
switch(opt){
case 'a':
//file = optarg;
offset = lseek(fd, -, SEEK_END);
break;
case '?':
default:
fprintf(stderr, "%s option: '-%c' is invalid: ignore\n", argv[], optopt);
exit(EXIT_FAILURE);
break;
}
} while((numRead = read(STDIN_FILENO, buf, MAX_READ)) > ){
buf[numRead] = '\0'; if(write(STDOUT_FILENO, buf, numRead+) != (numRead+))
fatal("couldn't write whole buf");
if(write(fd, buf, numRead) != (numRead))
fatal("couldn't write whole buf");
} if(numRead == -)
errExit("read"); if(close(fd) == -)
errExit("close file"); exit(EXIT_SUCCESS);
}
测试:
lancelot@debian:~/Code/tlpi$ cat > t1
This is the first line.
lancelot@debian:~/Code/tlpi$ cat t1
This is the first line.
lancelot@debian:~/Code/tlpi$ ./my_tee t1
This is the second line.
This is the second line.
This is the third line.
This is the third line. lancelot@debian:~/Code/tlpi$ cat t1
This is the second line.
This is the third line. lancelot@debian:~/Code/tlpi$ ./my_tee -a t1
This is the append line.
This is the append line.
lancelot@debian:~/Code/tlpi$ cat t1
This is the second line.
This is the third line.
This is the append line.
4-2:编写一个类似cp命令的程序,当使用该程序复制一个包含空洞(连续的空字节)的普通文件时,要求目标文件的空的与源文件保持一致。
/*
* =====================================================================================
*
* Filename: my_cp.c
*
* Description:
*
* Version: 1.0
* Created: 2014年04月03日 17时02分43秒
* Revision: none
* Compiler: gcc
*
* Author: alan (), alan19920626@gmail.com
* Organization:
*
* =====================================================================================
*/ #include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include "tlpi_hdr.h" #define BUF_SIZE 1024 int main(int argc, char *argv[]){
int inputFd, outputFd, openFlags;
mode_t filePerms;
ssize_t numRead;
char buf[BUF_SIZE]; if(argc != || strcmp(argv[], "--help") == )
usageErr("%s old-file new-file\n", argv[]); inputFd = open(argv[], O_RDONLY);
if(inputFd == -)
errExit("open file %s", argv[]); openFlags = O_CREAT | O_WRONLY | O_TRUNC;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
outputFd = open(argv[], openFlags, filePerms);
if(outputFd == -)
errExit("open file %s", argv[]); while((numRead = read(inputFd, buf, BUF_SIZE)) > )
if(write(outputFd, buf, numRead) != numRead)
fatal("couldn't write whole buffer");
if(numRead == -)
errExit("read");
if(close(inputFd) == -)
errExit("close input");
if(close(outputFd) == -)
errExit("close output"); exit(EXIT_SUCCESS);
}
测试:首先通过书本的程序清单提供的seek_io.c来使t2出现空洞。
lancelot@debian:~/Code/tlpi$ cat t2
This is the first line.
This is the second line.
This is the third line.
lancelot@debian:~/Code/tlpi$ gcc -o seek_io seek_io.c error_functions.c get_num.c
lancelot@debian:~/Code/tlpi$ ./seek_io t2 s100000 wabc
s100000: seek succeeded
wabc: wrote bytes
lancelot@debian:~/Code/tlpi$ cat t2
This is the first line.
This is the second line.
This is the third line.
abclancelot@debian:~/Code/tlp./seek_io t2 s50000 R5
s50000: seek succeeded
R5:
可以看到文件偏移量为50000的内容为空字节。。。。。
然后进行复制。
lancelot@debian:~/Code/tlpi$ ./my_cp t2 t3
lancelot@debian:~/Code/tlpi$ ls -l t2 t3
-rw-r--r-- lancelot lancelot 4月 : t2
-rw-r--r-- lancelot lancelot 4月 : t3
lancelot@debian:~/Code/tlpi$ ./seek_io t3 s50000 R5
s50000: seek succeeded
R5:
可以看到两个文件的大小相同,而且从t3文件偏移量为50000开始读取5个字节都为空字节。