proc 文件系统
课程资源
linux source code bootlin.com
proc 文件系统
/proc 是一个伪文件系统,被内核生成,并不存储真实文件
保存有关系统的相关信息, 使用简单的文件读取获得信息, 而不需要再创建以及使用其他syscall
-
许多系统程序使用这个接口进行运行
- ps
- top
- free
/proc 文件结构
-
proc文件夹的数字目录是对应每个进程 -- 每个进程都有一个对应的文件夹 -- 一般是只读的,因为是由kernel抽象出的进程信息. 强行更改某些文件会造成进程错误
-
/proc/[pid]/stat
- PID (1 st field)
- Command run by this process, in parenthesis (2 nd field)
- Process state (3 rd field)
- PID of parent process (4 th field)
- Time spent in user mode (14 th field)
- Time spent in kernel mode (15 th field)
- Size of virtual memory in bytes (23 rd field)
-
/proc/[pid]/status
- Name – the command run by this process
- Uid – the real, effective, saved set, and file system user IDs (UID)
-
-
proc文件夹中的self文件夹,显示当前调用proc 接口的进程自己的resources
-
proc文件夹中的其他文件 -- 系统的相关信息
-
/proc/version -- 保存关于kernel的信息
- 名称
- 发布版本
- 编译方式
- 编译时间
-
/proc/uptime -- 系统已经开机的秒数以及再idle process 中花费的时间
- idle process -- 一个特殊的系统状态, 用于标记目前没有 process 需要schedule
-
/proc/cpuinfo -- 关于电脑CPU的相关信息, 会被lscpu进行调用
-
/proc/meminfo 有关于内存使用的数据, 被free调用
- MemTotal
- MemFree
- MemAvailable
-
/proc/stat -- 系统的统计变量
- 有关cpu在各种状态使用的时间的属性
-
/proc/stat
- ctxt -- number of context switches
- btime –- boot time in seconds since 1970-01-01 00:00:00 +0000 (UTC)
- processes –- number of processes created (by fork())
-
更多相关属性 : man 5 proc https://man7.org/linux/man-pages/index.html
命令行的C语言编程复习
文件I/O
-
若文件不存在则 Returns NULL
-
char *fgets(char *restrict s, int n, FILE *restrict stream); -- 信息读取
- 读到n-1个字符是停止或者是读到第一个 \n
-
fclose
printf()
-
• %d – signed integer
- • %5d – left-pad the output with spaces to at least 5 characters
- • %-5d – right-pad the output with spaces to at least 5 characters
- • %05d – left-pad the output with zeroes to at at least 5 characters
-
• %u – unsigned integer
-
• %f – floating-point number
- • %.4f – floating-point number with 4 decimal places
-
• %c – character
-
• %s – string
scanf()
-
int ret = scanf("%s %d", s, &a);
- 读取输入, map到s和a
-
fscanf -- file
-
sscanf -- string
命令行命令读取
- int main(int argc, char *argv[]); -- argc>=1 包括运行主程序的命令行./a.out
XMind - Trial Version