The Linux Programming Interface
System And Process Interface
(01)主要内容,主要讨论/proc目录
In this chapter, we look at ways of accessing a variety of system and process information. The primary focus of the chapter is a discussion of the /proc file system.
(02)/proc/PID file
For each process on the system, the kernel provides a corresponding directory named /proc/PID, where PID is the ID of the process.
Within this directory are avrious files and subdirectories containing information about that process.
wang@wang:~$ cat /proc/1/status Name: systemd State: S (sleeping) Tgid: 1 Ngid: 0 Pid: 1 PPid: 0 TracerPid: 0 Uid: 0 0 0 0 Gid: 0 0 0 0 FDSize: 256 Groups: NStgid: 1 NSpid: 1 NSpgid: 1 NSsid: 1 VmPeak: 185680 kB VmSize: 120036 kB VmLck: 0 kB VmPin: 0 kB VmHWM: 6260 kB VmRSS: 6128 kB VmData: 83788 kB VmStk: 136 kB VmExe: 1392 kB VmLib: 3660 kB VmPTE: 100 kB VmPMD: 12 kB VmSwap: 0 kB HugetlbPages: 0 kB Threads: 1 SigQ: 0/14980 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 7be3c0fe28014a03 SigIgn: 0000000000001000 SigCgt: 00000001800004ec CapInh: 0000000000000000 CapPrm: 0000003fffffffff CapEff: 0000003fffffffff CapBnd: 0000003fffffffff CapAmb: 0000000000000000 Seccomp: 0 Cpus_allowed: ff Cpus_allowed_list: 0-7 Mems_allowed: 00000000,00000001 Mems_allowed_list: 0 voluntary_ctxt_switches: 2034 nonvoluntary_ctxt_switches: 926 (03) /proc/PID/fdThe /proc/PID/fd directory contains one symbolic link for each file descriptor that the process has open.
(04) /proc/PID/task
For each thread in this process, the kernel provides a subdirectory named /proc/PID/task/TID
(05)这个例子展示如何读和修改/proc文件
1 #include <fcntl.h> 2 #include "tlpi_hdr.h" 3 4 #define MAX_LINE 100 5 6 int main(int argc, char *argv[]) { 7 int fd; 8 char line[MAX_LINE]; 9 ssize_t n; 10 11 fd = open("/proc/sys/kernel/pid_max", (argc > 1) ? O_RDWR : O_RDONLY); 12 if (fd == -1) 13 errExit("open"); 14 15 n = read(fd, line, MAX_LINE); 16 if (n == -1) 17 errExit("read"); 18 19 if(argc > 1) 20 printf("Old value: "); 21 printf("%.*s",(int)n, line); 22 23 if(argc > 1) { 24 if(write(fd, argv[1], strlen(argv[1])) != strlen(argv[1])) 25 fatal("write() failed"); 26 system("echo /proc/sys/kernel/pid_max now contains 'cat /proc/sys/kernel/pid_max'"); 27 } 28 exit(EXIT_FAILURE); 29 } 最后的输出有问题,但是结果还是从32768变成10000.(06) The uname() system call returns a range of identifying information abut the host system on which application is running, in the structure pointed to by utsbuf.
#include <sys/utsname.h>
int uname(struct utsname *utsbuf);
struct utsname { char sysname[_UTSNAME_LENGTH]; /* Implementation name */ char nodename[_UTSNAME_LENGTH]; /* Node name on network */ char release[_utsname_LENGTH]; /* Implementation relase level */ ... };(07)uname举例 1 #define _GNU_SOURCE 2 #include <sys/utsname.h> 3 #include "tlpi_hdr.h" 4 5 int main(int argc, char *argv[]) { 6 struct utsname uts; 7 if (uname(&uts) == -1) 8 errExit("uname"); 9 10 printf("Node name: %s\n", uts.nodename); 11 printf("System name: %s\n", uts.sysname); 12 printf("Release: %s\n", uts.release); 13 printf("Version: %s\n", uts.version); 14 printf("Machine: %s\n", uts.machine); 15 16 #ifdef _GNU_SOURCE 17 printf("Domain name: %s\n", uts.domainname); 18 #endif 19 exit(EXIT_FAILURE); 20 } 输出;wang@wang:~/Documents/tlpi-dist/lib$ ./t_uname Node name: wang System name: Linux Release: 4.4.0-64-generic Version: #85-Ubuntu SMP Mon Feb 20 11:50:30 UTC 2017 Machine: x86_64 Domain name: (none)
(08)总结
The /proc file system exposes a range of kernel information to application programs. Each /proc/PID subdirectory contains files and subdirectories that provide information about the processes whose ID match PID. Various other files and directories under /proc expose system-wide information that program can read and, in some cases, modify.
The uname() system call allows us to discover the UNIX implementation and the type of machine on which an applicaton is running.
(09) 习题