当前位置:网站首页>16. System and process information
16. System and process information
2022-07-04 01:59:00 【666QAQ】
/proc file system
Many modern UNIX The implementation provides a /proc
Of Virtual file system . This file system is mainstream in /proc
Directory , Contains various files for displaying kernel information , And it allows you to use regular files I/O To easily read , Sometimes you can also modify this information .
It is called virtual , Because the files and subdirectories it contains are not stored on disk , Instead, the kernel accesses such information when the process , Dynamically created .
Here are Linux Proprietary details .
Get information about the process :/proc/PID
For each process in the system , The kernel is named /proc/PID
( among PID It's a process ID) The catalog of . Various files and subdirectories in this directory contain information about the process .
for example :/proc/1
The files in the directory can be obtained init Information about the process , Of the process ID Always 1.
$cat /proc/1/status
Name: systemd
Umask: 0000
State: S (sleeping)
Tgid: 1
Ngid: 0
Pid: 1
PPid: 0
...
Every /proc/PID An extract of the files in the directory
file | describe ( Process properties ) |
---|---|
cmdline | With \0 Delimited command line arguments |
cwd | Symbolic link to the current working directory |
Environ | NAME=value Key value pair environment list , With \0 Separate |
exe | Symbolic link to the file being executed |
fd | File directory , Contains symbolic links to files opened by the process |
maps | Memory mapping |
mem | Process virtual memory ( stay I/O Must call... Before operation lseek() Move to valid offset ) |
mounts | Installation point of process |
root | Symbolic link to the root directory |
status | All kinds of information ( such as , process ID、 voucher 、 Memory usage 、 The signal ) |
task | Include a subdirectory for each thread in the process ( Began in Linux2.6) |
/proc/PID/fd Catalog
/proc/PID/fd
Each file descriptor opened by the directory for the process contains a symbolic link , The name of each symbolic link matches the value of the descriptor . for example ,/proc/1968/1
yes ID by 1968 Symbolic links to standard output in the process of .
For convenience , Any process can use symbolic links /proc/self
To access their own /proc/PID
Catalog .
Threads :/proc/PID/task Catalog
Linux2.4 Added the concept of thread group , Formal support POSIX Threading model .
Because some properties in the thread group are unique to threads , therefore Linux2.4 stay /proc/PID
Added... To the directory
One task
subdirectories .
For each thread of the process , The kernel provides /proc/PID/task/TID
Named subdirectories , among TID Is the thread of this thread ID( This value is equivalent to calling gettid()
The return value of the function ).
Every /proc/PID/task/TID
Have a set similar to /proc/PID
Files and directories of directory contents . Because threads share multiple properties , So much information in these files is the same for each thread .
/proc System information under directory
Catalog | Information expressed by files in the directory |
---|---|
/proc | All kinds of system information |
/proc/net | Status information about networks and sockets |
/proc/sys/fs | File system related settings |
/proc/sys/kernel | Various general kernel settings |
/proc/sys/net | Network and socket settings |
/proc/sys/vm | Memory management settings |
/proc/sysvipc | of System V IPC Object information |
visit /proc file
Usually use shell Script to access /proc
A file in a directory .
for example :
# echo 100000 > /proc/sys/kernel/pid_max
# cat /proc/sys/kernel/pid_max
1000000
You can also use regular... From the program I/O System call to access /proc
A file in a directory . Ann has some restrictions on access :
- /proc Some files in the directory are read-only , That is, these files are only used to display kernel information , But it cannot be modified ./proc/PID Most files in the directory belong to this type .
- /proc Some files in the directory can only be used by the file owner ( Or privilege level process ) Read . for example ,/proc/PID All files in the directory belong to the user with the corresponding process , And even the owner of the file , Some of these documents ( Such as :/proc/PID/environ file ) Only read permission is granted .
- except /proc/PID Files in subdirectories ,/proc Most of the other files in the directory belong to root user , And only root Users can modify files that can be modified .
visit /proc/PID Files in directory
/proc/PID The contents of the catalogue vary , With the corresponding process ID Created by the process of , With the termination of the process . During the interview , Deal with the situation that the process has been completed .
The sample program
/*proc.c*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#define MAX_LINE 100
int main(int argc, char **argv)
{
int fd;
char line[MAX_LINE];
ssize_t n;
fd = open("/proc/sys/kernel/pid_max", (argc > 1) ? O_RDWR : O_RDONLY);
if( fd == -1)
{
printf("open");
exit(EXIT_FAILURE);
}
n = read(fd, line, MAX_LINE);
if( n == -1)
{
printf("read");
exit(EXIT_FAILURE);
}
if(argc > 1)
printf("Old value:");
printf("%.*s", (int)n, line);
if(argc > 1)
{
if(write(fd, argv[1], strlen(argv[1]) != strlen(argv[1])))
{
printf("write() failed");
exit(EXIT_FAILURE);
}
system("echo /proc/sys/kernel/pid_max now contains"
"cat /proc/sys/kernel/pid_max");
}
exit(EXIT_SUCCESS);
}
System identification : uname()
uname()
The system call returns a series of identification information about the host system , Store in utsbuf
In the structure pointed to .
#include <sys/utsname.h>
int uname(struct utsname *utsbuf);
Returns 0 on success, or -1 on error
utsbuf
The parameter is a point to utsname
Pointer to structure , Its definition is as follows :
#define _UTSNAME_LENGTH 65
struct utsname{
char sysname[_UTSNAME_LENGTH]; /*Implementation name*/
char nodename[_UTSNAME_LENGTH]; /*Node name on network*/
char release[_UTSNAME_LENGTH]; /*Implementation release level*/
char version[_UTSNAME_LENGTH]; /*Release version level*/
char machine[_UTSNAME_LENGTH]; /*Haedware on which system is running*/
#ifdef _GNU_SOURCE /*Following is Linux-specific*/
char domainname[_UTSNAME_LENGTH];/*NIS domain name of host*/
#endif
};
among :
sysname
、release
、version
、machine
Automatically set by the kernel .nodename
fromsethostname()
System call settings . Usually this value is similar to the system DNS Prefix hostname in domain name .domainname
fromsetdomainname()
System call settings . This value is the network information service of the host
(NIS) domain name ( Different from the host domain name ).
The sample program :
/*uname.c*/
#define _GNU_SOURCE
#include <sys/utsname.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
struct utsname uts;
if(uname(&uts) == -1)
{
printf("uname");
exit(EXIT_FAILURE);
}
printf("Node name: %s\n", uts.nodename);
printf("System name: %s\n", uts.sysname);
printf("Release: %s\n", uts.release);
printf("Version: %s\n", uts.version);
printf("Machine: %s\n", uts.machine);
#ifdef _GNU_SOURCE
printf("Domain name: %s\n", uts.domainname);
#endif
exit(EXIT_SUCCESS);
}
$gcc uname.c -o uname
$./uname
Node name: sixqaq
System name: Linux
Release: 5.13.0-27-generic
Version: #29~20.04.1-Ubuntu SMP Fri Jan 14 00:32:30 UTC 2022
Machine: x86_64
Domain name: (none)
Advanced
About /proc In depth information about file systems can be found in proc(5)
Manual page 、 Kernel source files Documentation/filesystems/proc.txt
as well as Documentation/sysctl
Various files in the directory .
边栏推荐
- Chapter 3.4: starrocks data import - Flink connector and CDC second level data synchronization
- Basic editing specifications and variables of shell script
- G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
- Mobile phone battery - current market situation and future development trend
- The reasons why QT fails to connect to the database and common solutions
- It's corrected. There's one missing < /script >, why doesn't the following template come out?
- Related configuration commands of Huawei rip
- Create template profile
- Idea if a class cannot be found, it will be red
- Install the pit that the electron has stepped on
猜你喜欢
Huawei cloud micro certification Huawei cloud computing service practice has been stable
Will the memory of ParticleSystem be affected by maxparticles
[leetcode daily question] a single element in an ordered array
Bacteriostatic circle scanning correction template
Force buckle day32
Maximum likelihood method, likelihood function and log likelihood function
The automatic control system of pump station has powerful functions and diverse application scenarios
Remember a lazy query error
On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
51 MCU external interrupt
随机推荐
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
Douban scoring applet Part-3
The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
Luogu p1309 Swiss wheel
1189. Maximum number of "balloons"
What are the advantages and disadvantages of data center agents?
Sequence sorting of basic exercises of test questions
JVM performance tuning and practical basic theory - medium
Bacteriostatic circle scanning correction template
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
Ceramic metal crowns - current market situation and future development trend
Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
From the 18th line to the first line, the new story of the network security industry
Development of user-defined navigation bar in uniapp
Pyinstaller packaging py script warning:lib not found and other related issues
Maximum likelihood method, likelihood function and log likelihood function
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
Setting function of Jerry's watch management device [chapter]
Will the memory of ParticleSystem be affected by maxparticles