《 Unix环境高级编程 》1~5章笔记

【第1章        UNIX基础知识】
一、操作系统为程序提供服务
二、登录名
/etc/passwd目录保存用户信息,现在都转移到其他目录中
三、Shell
/bin/sh Bourne shell
/bin/csh C shell
四、文件系统: 文件和目录的层次安排
目录是一个包含目录项(文件名+属性信息)的文件
文件名: / 和 null 不能当文件名
路径名
*ourhdr.h 标准系统头文件 常数及函数原型 见附录B(021.PDF)
.引用当前目录
..引用父目录
man手册
command(number) man手册中command第number部分 e.g. ls(1)
man 1 用户e.g. man 1 ls
man 2 程序员
man 3 系统管理
工作目录:当前工作目录
起始目录:从口令文件取得
五、文件描述符
小的非负整数,标识特定进程正在存访的文件
标准输入、标准输出和标准出错
ls > file.list 重定向
不用缓存的I/O
<unistd.h>
标准I/O
<stdio.h>
六、程序
存放在磁盘文件中的可执行文件。使用exec函数读程序到存储器中
进程:程序的执行实例。
进程ID:标识进程的非负整数
七、进程控制
fork() 创建一个新进程
exec()
waitpid()
八、
unistd.h包含许多unix系统服务的函数原型
类属指针:void *
原始系统数据类型:以_t结尾的数据类型被称为原始系统数据类型<sys/types.h>中定义
出错处理
返回一个负值
<errno.h>中
九、
用户ID为0 具有超级权限
信号:通知进程已发生某种条件的一种技术
处理信号:忽略 按系统默认方式处理 提供函数处理
UNIX时间值 UTC
十、系统调用和库函数
系统调用:程序向内核请求服务的入口点
库函数调用可以替换,系统调用不可以替换
系统调用通常提供一种最小界面,而库函数通常提供比较复杂的功能

【第2章        UNIX标准化及实现】
UNIX标准化(界面接口) ANSI C、 IEEE POSIX、 X/Open XPG3 FIPS
UNIX实现 SVR4、 4.3+BSD
标准和实现的关系
限制 - 通过方法限制,提高程序可移植性
编译时选择项
编译时限制
运行时限制
<limits.h> <float.h> <stdio.h> <uinstd.h>
ANSI C限制 POSIX限制 XPG3限制
sysconf()、pathconf() 和fpathconf()函数
sysconf() pathconf()其中pathconf()第一个参数为unistd.h文件路径(/usr/include/unistd.h)
FIPS 151-1要求
功能测试宏:feature test macro
基本系统数据类型:<sys/type.h>文件中 某些与实现有关的数据类型(primitive system data type)
标准之间的冲突:ansi c没有考虑宿主操作系统

【第3章        文件I/O】
1 open(),read(),write(),lseek(),close()
2 不带缓存的I/O
3 文件描述符:非负整数,内核返回到进程,用于标识文件
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>
4 open函数
       int open(const char* pathname, int oflag, ... /*,mode_t mode*/)
       O_WRONLY O_RDONLY O_RDWR
       O_APPEND到文件结尾
       O_CREAT重新创建
       O_EXCL测试文件是否存在
       O_TRUNC文件存在截断长度为0
       O_NOCTTY终端设备 则不将此设备分配作为此进程的控制终端
       O_NONBLOCK FIFO、块特殊文件或字符特殊文件 非阻塞方式打开
5 creat函数
       int creat(const char* pathname, mode_t mode);
       <=>open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode);
6 close函数
       #include <unistd.h>
       close(int filedes)
7 seek函数
       #include <sys/types.h>
       #include <unistd.h>
       off_t lseek(int filedes, off_t offset, int whence);
       失败返回-1
       whence: SEEK_SET 开始 SEEK_CUR 当前 SEEK_END 结尾
8 read函数
       #include <unistd.h>
       ssize_t read(int filedes, void* buff, size_t nbytes);
       已到文件尾为0,失败返回-1
9 write函数
       #include <unistd.h>
       ssize_t write(int filedes, const void* buff, size_t nbytes);
       失败返回-1
10 进程表项 文件表 v节点表
11 原子操作(atomic operation):多步操作组成的操作,或者执行完所有步,或者一步也不执行
12 dup和dup2函数
        复制一个现存的文件描述符
        #include <unistd.h>
        int dup(int filedes);<=>fcnt1(filedes, F_DUPFD, 0);
        int dup2(int filedes, int filedes2);<=>close(filedes2);+fcnt1(filedes, F_DUPFD, filedes2);+原子操作
        成功返回新的文件描述符,失败为-1
13 fcntl函数
        #include <sys/types.h>
        #include <unistd.h>
        #include <fcntl.h>

        int fcntl(int filedes, int cmd, .../* int arg */);
        失败返回-1
        改变已打开的文件的性质
        cmd=F_DUPFD 复制描述符
        cmd=F_GETFD或F_SETFD 获取/设置文件描述符标记
        cmd=F_GETFL或F_SETFL 获取/设置文件状态标志
        cmd=F_GETOWN或F_SETOWN 获取/设置异步I/O有权
        cmd=F_GETLK,F_SETLK或F_SETLKW 获取/设置记录锁
        通过O_ACCMODE屏蔽字,取
14 ioctl函数
       I/O杂务箱,完成其他I/O函数不能完成的功能
       #include <unistd.h> //SVR4
       #include <sys/ioctl.h> //4.3_BSD
       int ioctl(int filedes, int request,...);
       出错返回-1



【第4章        文件和目录】

struct stat
{
        mode_t st_mode;        /* file type & mode (permissions) */
        ino_t stino;        /* i-node number (serial number */
        dev_t st_dev;        /* device number (filesystem) */
        nlink_t st_nlink;        /* number of links */
        uid_t st_uid;                /* user ID of owner */
        gid_t stgid;                /* group ID of owner */
        off_t st_size;                /* size in byte, for regular files */
        time_t st_atime;        /* time of last access */
        time_t st_mtime;        /* time of last modification */
        time_t st_ctime;        /* time of last file status change */
        time_t st_blksize;        /* best I/O block size */
        long st_blocks;                /* number of 512-byte blocks allocated */
};
/* linux中位于/usr/include/bits/stat.h中 */
一、stat fstat lstat函数
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char* pathname, struct stat* buf);
给定文件名,返回相关文件的信息结构
int fstat(int filedes, struct stat* buf);
给定文件描述符,返回相关文件的信息结构
int lstat(const char* pathname, struct stat* buf);
给定文件是连接时,返回连接文件信息
二、文件类型
普通文件(二进制,文本)
目录文件(仅仅内核有写权限)
设备文件
字符特殊文件(character special file)-用于系统中某些类型的设备
块特殊文件(block special file) - 用于磁盘设备FIFO
FIFO:命名管道,用于进程通信
套接口(socket):网络通信
符号连接(symbolic link)
文件类型宏<sys/stat.h>中
三、access函数
#include <unistd.h>
int access(const char* pathname, int mode);
成功为0,出错返回-1
mode= R_OK(读) W_OK(写) X_OK(执行)
四、粘住位 S_ISVTX
五、chown fchown lchown函数
用于更改文件的用户ID和组ID
#include <sys/types.h>
#include <unistd.h>
int chown(const char* pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char* pathname, uid_towner, gid_t group);
更改文件的用户ID和组ID
成功0,失败-1
六、文件截短
#include <sys/types.h>
#include <unistd.h>
int truncate(const char* pathname, off_t length);
int ftruncate(int filedes, off_t length);
把文件截短为length,如果比原文件长,则用空洞'/0'补充
失败返回-1成功返回0
七、文件系统 ※
磁盘-分区-文件系统-自举块+超级块+i表(i节点)+目录块和数据块
i节点指向数据(文件在目录中)
目录文件指向i节点(目录文件描述)
#include <unistd.h>
添加,原来存在则失败,失败-1成功0
int link(const char* existingpath, const char* newpath);
删除,连接数为0时才能被删除
int unlink(const char* pathname);
#include <stdio.h>
int remove(const char* pathname);
解除文件连接 文件时,与unlink相同;目录时,与rmdir相同
int rename(const char* oldname, const char* newname); 重命名
八、符号连接
ln -s path symbo e.g. ln -s /no/such/file myfile
#include <unistd.h>
创建连接int symlink(const char* actualpath, const char* smpath);
读连接 int readlink(const char* pathname, char* buf, int bufsize);
九、文件的时间
st_atime 文件数据的最后存取时间 st_mtime 文件数据的最后修改时间 st_ctime i节点状态的最后更改时间
#include <sys/types.h>
#include <utime.h>
修改文件存取和修改时间
int utime(const char* pathname, const struct utimbuf* times);
#include <sys/types.h>
#include <sys/stat.h>
十、目录
创建目录和删除空目录
int mkdir(const char* pathname, mode_t mode);
int rmdir(const char* pathname);
读目录
#include <sys/types.h>
#include <dirent.h>
DIR* opendir(const char* pathname);
struct dirent* readdir(DIR* dp);
?void rewinddir(DIR* dp);
int closedir(DIR* dp);
struct dirent
{
         ino_t d_ino;         //i-node number
         char d_name(NAME_MAX+1);         //null-terminated filename
}
/
e.g.
DIR* dp;
struct dirent* dirp;
if (NULL == (dp = opendir(path)))        /* maybe can't read */
while (NULL != (dirp = readdir(dp)))
{
        //dirp->d_name;
}
closedir(dp);
/
#include <unistd.h>
更改当前工作目录
int chdir(const char* pathname);
int fchdir(int filedes);
得到当前工作目录
char* getcwd(char *buf, size_t size);
十一、设备号
主设备号,次设备号
用宏major()和minor()来取
/
e.g.
struct stat stat;
int major, minor;
lstat(pathname, &stat);
major = major(stat.st_dev);
minor = minor(stat.st_dev);
/
十二、缓存
#include <unistd.h>
void sync(void)将修改的缓存排入队列,执行后不等I/O操作完成立即返回
int fsync(int filedes)确保修改的块直接写入磁盘,执行后等待I/O操作完成后返回

【第5章        标准I/O库】 
一、
标准库中的I/O操作中流与文件相结合;FILE对象结构 用于管理流
预定义的三个流STDIN_FILENO标准输入,STDOUT_FILENO标准输出,STDERR_FILENO标准出错
stdin, stdout, stderr
//
e.g. 
#include <stdio.h>
int main()
{
        char writebuf[] = "this is test stdin, stdout, stderr/n";
        char readbuf[10] = {0};
        fwrite(writebuf, sizeof(writebuf), sizeof(char), stdout);
        fread(readbuf, 9, sizeof(char), stdin);
        printf("readbuf = %s/n", readbuf);
        return 0;
}
//
#inlcude <stdio.h>
二、缓存:全缓存,行缓存,不带缓存
修改缓存
void setbuf(FILE* fp, char* buf);
int setvbuf(FILE* fp, char* buf, int mode, size_t size);
强制刷新流,使未缓存中未写入的数据写入内核
int fflush(FILE* fp);
三、打开和关闭
FILE* fopen(const char* pathname, const char* type);
FILE* freopen(const char* pathname, const char* type, FILE* fp);
FILE* fdopen(int filedes, const char* type);
int fclose(FILE* fp);
四、读写流
读一个字节
int getc(FILE *fp);是宏
int fgetc(FILE *fp);函数
int getchar(void);
成功返回字符,出错或到文件结尾返回EOF
int ferror(FILE* fp);
int feof(FILE* fp);
非0为真 0为假
输出一个字节
int putc(int c, FILE* fp);
int fputc(int c, FILE* fp);
int putchar(int c);
成功为C,出错为EOF
每次一行
char* fgets(char* buf, int n, FILE* fp);
char* gets(char* buf);※不推荐使用
char* fputs(const char* str, FILE* fp);
char* puts(const char* str);
二进制I/O
size_t fread(void* ptr, size_t size, size_t nobj, FILE* fp);
size_t fwrite(const void* ptr, size_t size, size_t nobj, FILE* fp);
成功返回操作对象数,失败-1
定位流
long ftell(FILE* fp);
成功为当前文件位置,出错返回-1L
int fseek(FILE* fp, long offset, int whence);
成功0 失败非0
void rewind(FILE* fp);
设置流到文件的起始位置
int fgetpos(FILE* fp, fpos_t* pos);
int fsetpos(FILE* fp, const fpos_t* pos);
成功为0,失败非0
格式化I/O
int printf(const char* format, ...);
输出到标准输出流
int fprintf(FILE* fp, const char* format, ...);
输出到指定流 成功返回输出字符数,出错为负值
int sprintf(char* buf, const char* format, ...);
输出到数组 返回存入数组的字符数

int vprintf(const char* format, va_list arg);
int vfprintf(const char* format, va_list arg);
int vsprintf(char* buf, const char* format, va_list arg);

int scanf(const char* format, ...);
int scanf(FILE* fp, const char* format, ...);
int sscanf(const char* buf, const char* format, ...);
int fileno(FILE* fp); 返回文件描述符
五、临时文件
char* tmpnam(char* ptr)
产生一个与现在文件名不同的一个有效路径名字符串
FILE* tmpfile(void)
建立临时文件
char* tempnam(const char* directory, const char* prefix);
directory目录 prefix前缀
六、标准I/O的替代软件
标准I/O的不足要复制两次:内核到标准I/O的缓存、标准I/O的缓存到用户程序
替代
fio(快速IO库)
sfio
ASI(Alloc Stream Interface)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书全面介绍了UNIX系统的程序设计界面—系统调用界面和标准C库提供的许多函数。 本书的前15着重于理论知识的阐述,主要内容包括UNIX文件和目录、进程环境、进程控制、 进程间通信以及各种I/O。在此基础上,分别按介绍了多个应用实例,包括如何创建数据库函数库, PostScript 打印机驱动程序,调制解调器拨号器及在伪终端上运行其他程序的程序等。 本书内容丰富权威, 概念清晰精辟,一直以来被誉为UNIX编程的“圣经”,对于所有UNIX程序员—无论是初学者还是专家级人士 —都是一本无价的参考书籍。 目 录 译者序 译者简介 前言 第1 UNIX基础知识 1 1.1 引言 1 1.2 登录 1 1.2.1 登录名 1 1.2.2 shell 1 1.3 文件和目录 2 1.3.1 文件系统 2 1.3.2 文件名 2 1.3.3 路径名 2 1.3.4 工作目录 4 1.3.5 起始目录 4 1.4 输入和输出 5 1.4.1 文件描述符 5 1.4.2 标准输入、标准输出和标准 出错 5 1.4.3 不用缓存的I/O 5 1.4.4 标准I/O 6 1.5 程序和进程 7 1.5.1 程序 7 1.5.2 进程和进程ID 7 1.5.3 进程控制 7 1.6 ANSI C 9 1.6.1 函数原型 9 1.6.2 类属指针 9 1.6.3 原始系统数据类型 10 1.7 出错处理 10 1.8 用户标识 11 1.8.1 用户ID 11 1.8.2 组ID 12 1.8.3 添加组ID 12 1.9 信号 12 1.10 UNIX时间值 14 1.11 系统调用和库函数 14 1.12 小结 16 习题 16 第2 UNIX标准化及实现 17 2.1 引言 17 2.2 UNIX标准化 17 2.2.1 ANSI C 17 2.2.2 IEEE POSIX 18 2.2.3 X/Open XPG3 19 2.2.4 FIPS 19 2.3 UNIX实现 19 2.3.1 SVR4 20 2.3.2 4.3+BSD 20 2.4 标准和实现的关系 21 2.5 限制 21 2.5.1 ANSI C限制 22 2.5.2 POSIX限制 22 2.5.3 XPG3限制 24 2.5.4 sysconf、pathconf 和fpathconf 函数 24 2.5.5 FIPS 151-1要求 28 2.5.6 限制总结 28 2.5.7 未确定的运行时间限制 29 2.6 功能测试宏 32 2.7 基本系统数据类型 32 2.8 标准之间的冲突 33 2.9 小结 34 习题 34 第3 文件I/O 35 3.1 引言 35 3.2 文件描述符 35 3.3 open函数 35 3.4 creat函数 37 3.5 close函数 37 3.6 lseek函数 38 3.7 read函数 40 3.8 write函数 41 3.9 I/O的效率 41 3.10 文件共享 42 3.11 原子操作 45 3.11.1 添加至一个文件 45 3.11.2 创建一个文件 45 3.12 dup和dup2函数 46 3.13 fcntl函数 47 3.14 ioctl函数 50 3.15 /dev/fd 51 3.16 小结 52 习题 52 第4 文件和目录 54 4.1 引言 54 4.2 stat, fstat和lstat函数 54 4.3 文件类型 55 4.4 设置-用户-ID和设置-组-ID 57 4.5 文件存取许可权 58 4.6 新文件和目录的所有权 60 4.7 access函数 60 4.8 umask函数 62 4.9 chmod和fchmod函数 63 4.10 粘住位 65 4.11 chown, fchown和 lchown函数 66 4.12 文件长度 67 4.13 文件截短 68 4.14 文件系统 69 4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值