1. 嵌入式Linux中App开发人员是如何得知可以操控哪些底层硬件的?
- 系统会自动挂载,如果没有自动挂载就需要去手动执行挂载命令;我们怎么直到是否自动挂载呢?在终端执行:
1 | cat /proc/mounts |
- 查看系统设备:
1 | ls /dev/* -l # 查看所有的设备,包括其主设备号和次设备号 |
在Linux内核里面,有两类驱动程序:字符(char)设备驱动程序和块(block)设备驱动程序。
图中,c代表字符设备驱动程序, 89就是设备的主设备号,0/1/2就是设备的次设备号。设备节点对应哪一个设备驱动程序通过主设备号来确定,而次设备号则是决定该设备节点对应的是设备驱动程序中对应的哪一个硬件。
2. Linux中对文件的操作
- 打开文件:open
需要包含的头文件:
1 |
可以调用的API:
1 | int open(const char *pathname, int flags); |
当成功打开一个文件后,open的返回值会是这个文件的描述符句柄,如果打开失败,那么它的返回值就是-1。
参数flags的范围:
flags | 涵义 |
---|---|
O_RDONLY | 只读 |
O_WRONLY | 只写 |
O_RDWR | 可读可写 |
O_APPEND | 以附件的方式打开文件,在对该文件进行写的时候,会偏移到文件的末尾开始写 |
O_ASYNC | Enable signal-driven I/O: generate a signal (SIGIO by default, but this can be changed via fcntl(2)) when input or output becomes possible on this file descriptor. This feature is available only for terminals, pseudoterminals, sockets, and (since Linux 2.6) pipes and FIFOs. |
O_CLOEXEC | Enable the close-on-exec flag for the new file descriptor |
O_CREAT | If pathname does not exist, create it as a regular file.这个Flags会用到mode |
O_DIRECT | Try to minimize cache effects of the I/O to and from this file. |
O_DIRECTORY | If pathname is not a directory, cause the open to fail |
O_DSYNC | Write operations on the file will complete according to the requirements of synchronized I/O data integrity completion. |
O_EXCL | Ensure that this call creates the file: if this flag is specified in conjunction with O_CREAT, and pathname already exists, then open() fails with the error EEXIST. |
O_LARGEFILE | Allow files whose sizes cannot be represented in an off_t (but can be represented in an off64_t) to be opened. |
O_NOATIME | Do not update the file last access time (st_atime in the inode) when the file is read(2). |
O_NOCTTY | If pathname refers to a terminal device—see tty(4)—it will not become the process’s controlling terminal even if the process does not have one. |
O_NOFOLLOW | If pathname is a symbolic link, then the open fails, with the error ELOOP. |
O_NONBLOCK/O_NDELAY | When possible, the file is opened in nonblocking mode. |
O_PATH | Obtain a file descriptor that can be used for two purposes: to indicate a location in the filesystem tree and to perform operations that act purely at the file descriptor level. |
O_SYNC | Write operations on the file will complete according to the requirements of synchronized I/O file integrity completion(by contrast with the synchronized I/O data integrity completion provided by O_DSYNC.) |
O_TMPFILE | Create an unnamed temporary regular file. |
O_TRUNC | If the file already exists and is a regular file and the access mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0. If the file is a FIFO or terminal device file, the O_TRUNC flag is ignored. Otherwise, the effect of O_TRUNC is unspecified. |
当flags支持O_CREAT时,可以选择的mode有:
mode | 作用 |
---|---|
S_IRWXU | user (file owner) has read, write, and execute permission |
S_IRUSR | user has read permission |
S_IWUSR | user has write permission |
S_IXUSR | user has execute permission |
S_IRWXG | group has read, write, and execute permission |
S_IRGRP | group has read permission |
S_IWGRP | group has write permission |
S_IXGRP | group has execute permission |
S_IRWXO | others have read, write, and execute permission |
S_IROTH | others have read permission |
S_IWOTH | others have write permission |
S_IXOTH | others have execute permission |
S_ISUID | set-user-ID bit |
S_ISGID | set-group-ID bit |
S_ISVTX | sticky bit |
其中,user/group/others指代的什么?
示例:打开一个“只读”文件
测试代码:
1 |
|
输出结果:
1 | book@100ask:~/workspace/FileOption$ ./open_ro hello.txt |
- 本文作者: 摘星星的小朋友
- 本文链接: http://slhking.github.io/2022/04/22/LinuxLearnNote/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!