当前位置:网站首页>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、machineAutomatically set by the kernel .nodenamefromsethostname()System call settings . Usually this value is similar to the system DNS Prefix hostname in domain name .domainnamefromsetdomainname()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 .
边栏推荐
- IPv6 experiment
- What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
- Setting function of Jerry's watch management device [chapter]
- Neo4j learning notes
- A. Min Max Swap
- How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
- Introduction to superresolution
- Portapack application development tutorial (XVII) nRF24L01 launch C
- Small program graduation project based on wechat reservation small program graduation project opening report reference
- Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
猜你喜欢

51 MCU external interrupt

Huawei cloud micro certification Huawei cloud computing service practice has been stable

Example 073 square sum value judgment programming requires the input of a and B, if a ²+ b ² If the result of is greater than 100, a is output ²+ b ² Value, otherwise output the result of a + B.

Maximum entropy model

Learn these super practical Google browser skills, girls casually flirt

1189. Maximum number of "balloons"

Some other configurations on Huawei's spanning tree

Use classname to modify style properties

Will the memory of ParticleSystem be affected by maxparticles

Should enterprises start building progressive web applications?
随机推荐
LV1 previous life archives
Applet graduation project based on wechat selection voting applet graduation project opening report function reference
How to view the computing power of GPU?
Setting function of Jerry's watch management device [chapter]
Take you to master the formatter of visual studio code
Logical operator, displacement operator
Méthode de calcul de la connexion MSSQL de la carte esp32c3
Huawei cloud micro certification Huawei cloud computing service practice has been stable
Some other configurations on Huawei's spanning tree
A. ABC
A. Div. 7
Skku| autonomous handover decision of UAV Based on deep reinforcement learning
Override and virtual of classes in C #
Conditional statements of shell programming
Magical usage of edge browser (highly recommended by program ape and student party)
Neo4j learning notes
Reading notes - learn to write: what is writing?
Small program graduation project based on wechat examination small program graduation project opening report function reference
Trading software programming
Pyrethroid pesticide intermediates - market status and future development trend