0.参考文档
正点原子的:I.MX6U 嵌入式 Linux C 应用编程指南
1. 应用编程的概念
1.1 系统调用
系统调用(system call)是 linux内核提供给应用层的应用编程接口(API),是linux应用层进入内核的入口。应用程序通过系统调用来使用操作系统提供的各种服务。
1.2 库函数
C语言库函数构建于系统调用之上,库函数其实是由系统调用封装而来的。
有些库函数则不调用系统调用:strlen()、strcat()、memcpy()、memset()、strchr()等;
系统调用与库函数的区别:
(1)库函数属于应用层,而系统调用是内核提供给应用层的编程接口,属于系统内核的一部分;
(2)库函数运行在用户空间,而调用系统调用会由用户空间陷入到内核空间;
(3)库函数通常是有缓存的,而系统调用是无缓存的,所以性能、效率上,库函数通常优于系统调用;
(4)可移植性:库函数相比于系统调用具有更好的移植性,C语言库在不同的操作系统上接口定义几乎是一样的;
1.3 标准C语言函数库
glibc的网址:http://www.gnu.org/software/libc/
1.4 main 函数
man函数的2种形式:
int main(void)
{
}
int main(int argc, char **argv)
{
}
2.文件IO基础
Linux下一切皆文件,文件是Linux系统设计思想的核心理念。
对于一个进程来说,文件描述符是一种有限资源,文件描述符从0开始分配。
0:系统标准输入
1:标准输出
2:标准错误
2.1 open
man 2 open // 查看open函数的帮助
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
2.2 write
#include <unistd.h>
ssize_t write(int fd, void *buf, size_t count);
2.3 read
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
2.4 close
#include <unistd.h>
int close(int fd);
2.5 lseek
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);