前言
什么是系统调用:
由操作系统实现并提供给外部应用程序的编程接口。(Application Programming Interface,API)。是应用程序同系统之间数据交互的桥梁。
下面通过一个案例来说明。
一个 helloworld 如何打印到屏幕。如下图。
printf函数会调用用户区的库,库里面调用write系统函数进入内核区给sys_write函数,sys_write函数将字符串传给驱动代码,驱动代码再发到显卡上进行显示。
一、关于文件的常用系统函数
open/close 函数**
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int close(int fd);
常用参数:
O_RDONLY、O_WRONLY、O_RDWR
O_APPEND、O_CREAT、O_EXCL、 O_TRUNC、 O_NONBLOCK
创建文件时,指定文件访问权限。权限同时受 umask 影响。结论为:
文件权限 = mode & ~umask
使用头文件:<fcntl.h>
open 常见错误 :
1. 打开文件不存在
2. 以写方式打开只读文件(打开文件没有对应权限)
3. 以只写方式打开目录
read/write 函数
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
read 与 write 函数原型类似。使用时需注意:read/write 函数的第三个参数。
peero/strerror 函数
void perror(const char *s);
char *strerror(int errnum);
错误号:errnum
查看错误号:
/usr/include/asm-generic/errno-base.h
/usr/include/asm-generic/errno.h
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */
lseek 函数
Linux 中可使用系统函数 lseek 来修改文件偏移量(读写位置)
off_t lseek(int fd, off_t offset, int whence);
失败返回-1;成功:返回的值是较文件起始位置向后的偏移量。
特别的:lseek 允许超过文件结尾设置偏移量,文件会因此被拓展。
fcntl 函数
int fcntl(int fd, int cmd, ... /* arg */ );
改变一个【已经打开】的文件的 访问控制属性。重点掌握两个参数的使用,F_GETFL 和 F_SETFL。
ioctl 函数
int ioctl(int fd, unsigned long request, ...);
对设备的 I/O 通道进行管理,控制设备特性。(主要应用于设备驱动程序中)。通常用来获取文件的【物理特性】(该特性,不同文件类型所含有的值各不相同)
传入传出参数概念
传入参数:
const 关键字修饰的 指针变量 在函数内部读操作。 char *strcpy(const char *src, char *dst);
传出参数:
1. 指针做为函数参数
2. 函数调用前,指针指向的空间可以无意义,调用后指针指向的空间有意义,且作为函数的返回值传出
3. 在函数内部写操作。
传入传出参数:
调用前指向的空间有实际意义 2. 调用期间在函数内读、写(改变原值)操作 3.作为函数返回值传出
二、阻塞、非阻塞
读常规文件是不会阻塞的,不管读多少字节,read 一定会在有限的时间内返回。从终端设备或网络读则不一定,如果从终端输入的数据没有换行符,调用 read 读终端设备就会阻塞,如果网络上没有接收到数据包,调用 read 从网络读就会阻塞,至于会阻塞多长时间也是不确定的,如果一直没有数据到达就一直阻塞在那里。同样,写常规文件是不会阻塞的,而向终端设备或网络写则不一定。现在明确一下阻塞(Block)这个概念。当进程调用一个阻塞的系统函数时,该进程被置于睡眠(Sleep)状态,这时内核调度其它进程运行,直到该进程等待的事件发生了(比如网络上接收到数据包,或者调用 sleep 指定的睡眠时间到了)它才有可能继续运行。与睡眠状态相对的是运行(Running)状态,在 Linux 内核中,处于运行状态的进程分为两种情况:
正在被调度执行
CPU 处于该进程的上下文环境中,程序计数器(eip)里保存着该进程的指令地址,通用寄存器里保存着该进程运算过程的中间结果,正在执行该进程的指令,正在读写该进程的地址空间。
就绪状态。
该进程不需要等待什么事件发生,随时都可以执行,但 CPU 暂时还在执行另一个进程,所以该进程在一个就绪队列中等待被内核调度。系统中可能同时有多个就绪的进程,那么该调度谁执行呢?内核的调度算法是基于优先级和时间片的,而且会根据每个进程的运行情况动态调整它的优先级和时间片,让每个进程都能比较公平地得到机会执行,同时要兼顾用户体验,不能让和用户交互的进程响应太慢。
注意,阻塞与非阻塞是对于文件而言的。而不是 read、write 等的属性。read 终端,默认阻塞读。