ioctl
是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率、马达的转速等等。
ioctl
经常是IO操作的杂货包(catchall),任何使用本章其他函数(如read,write…)表示的操作就需要ioctl
终端(Terminal)IO就是本章最大的使用者(在chapter 18我们会看到POSIX.1已经用分离的函数替换了终端IO操作)
函数原型如下:
#include <unistd.h>
#include <sys/ioctl.h>
int ioctl(int fd, int requets, ...);
系统给不同的设备提供了各种各样的ioctl
命令
Catgory | Constant names | Header | Number of ioctls |
---|---|---|---|
disk label | DIOxxx | sys/disklabel.h | 6 |
file I/O | FIOxxx | sys/filio.h | 9 |
mag tape I/O | MTIOxxx | sys/mtio.h | 11 |
socket I/O | SIOxxx | sys/sockio.h | 60 |
terminal I/O | TIOxxx | sys/ttycom.h | 44 |
例如mag tape操作,允许我们倒带等操作,这是其余函数无法提供的功能。因此最简单地操作这些设备的方法就是使用ioctl