1, / proc file system
The / proc virtual file system provides convenience for accessing kernel information. The file system resides in the / proc directory, contains various files used to display kernel information, and allows processes to easily read and sometimes modify these information through regular file I/O system calls. The reason why the / proc file
The system is called virtual because the files and subdirectories it contains are not stored on disk, but are dynamically created by the kernel when the process accesses such information.
For each process in the system, the kernel provides a corresponding directory named / proc/PID, where PID is the ID of the process. Various files and subdirectories in this directory contain information about the process. For example, by viewing the files in the / proc/1 directory, you can obtain the information of the init process, whose ID is always 1.
book@100ask:/proc/1$ sudo ls [sudo] password for book: arch_status cpuset loginuid numa_maps sched status attr cwd map_files oom_adj schedstat syscall autogroup environ maps oom_score sessionid task auxv exe mem oom_score_adj setgroups timers cgroup fd mountinfo pagemap smaps timerslack_ns clear_refs fdinfo mounts patch_state smaps_rollup uid_map cmdline gid_map mountstats personality stack wchan comm io net projid_map stat coredump_filter limits ns root statm
There is a file named status in each / proc/PID directory, which provides a series of information about the process.
/Common files in proc/pid Directory:
cmdline | Command line arguments separated by \ 0 |
---|---|
cwd | Symbolic link to the current working directory |
Environ | NAME=value key value pair environment list, separated by \ 0 |
exe | Symbolic link to executing file |
fd | File directory, which contains symbolic links to files opened by the process |
maps | Memory mapping |
mem | Process virtual memory (lseek() must be called before I/O operations to move to a valid offset) |
mounts | Installation point for process |
root | Symbolic link to the root directory |
status | Various information (such as process ID, credentials, memory usage, signals) |
task | Contains a subdirectory for each thread in the process |
To access the proc file:
Shell scripts are usually used to access files in the / proc directory (using scripting languages such as Python or Perl, it is easy to parse most files with multiple values in the / proc directory). For example, you can modify and view the file contents in the / proc directory by using the following shell command:
echo 100000 > /proc/sys/kernel/pid_max cat /proc/sys/kernel/pid_max
You can also use regular I/O system calls from the program to access files in the / proc directory. However, there are some restrictions when accessing these files.
① Some files in the / proc directory are read-only, that is, these files are only used to display kernel information, but they cannot be modified/ Most files in the proc/PID directory belong to this type.
② Some files in the / proc directory can only be read by the file owner (or privilege level process). For example, all files in the / proc/PID directory belong to the user with the corresponding process, and even for the owner of the file, some files (such as the proc/PID/environ file) are only granted read permission.
③ In addition to the files in the / proc/PID subdirectory, most of the other files in the / proc directory belong to the root user, and only the root user can modify those modifiable files.
int fd; char line[100]; fd= open("/proc/sys/kernel/pid_max",O_RDWR); read(fd,line,100); write(fd,"1000",5);
System ID: uname
uname system call returns a series of identification information about the host system, which is stored in the structure pointed to by buf.
#include <sys/utsname.h> int uname(struct utsname *buf); struct utsname { char sysname[]; /* Operating system name (e.g., "Linux") */ char nodename[]; /* Name within "some implementation-defined network" */ char release[]; /* Operating system release (e.g., "2.6.28") */ char version[]; /* Operating system version */ char machine[]; /* Hardware identifier */ #ifdef _GNU_SOURCE char domainname[]; /* NIS or YP domain name */ #endif };
Note: refer to the Linux UNIX system programming manual