当前位置:网站首页>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 .
边栏推荐
- I don't know why it can't run in the project and how to change it
- Properties of binary trees (numerical aspects)
- Final consistency of MESI cache in CPU -- why does CPU need cache
- Example 072 calculation of salary it is known that the base salary of an employee of a company is 500 yuan. The amount of software sold by the employee and the Commission method are as follows: Sales
- Openbionics exoskeleton project introduction | bciduino community finishing
- A. ABC
- Special copy UML notes
- 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.
- MPLS③
- What are the advantages and disadvantages of data center agents?
猜你喜欢
![Setting function of Jerry's watch management device [chapter]](/img/0b/8fab078e1046dbc22aa3327c49faa7.jpg)
Setting function of Jerry's watch management device [chapter]

Remember another interview trip to Ali, which ends on three sides

Yyds dry goods inventory hand-in-hand teach you the development of Tiktok series video batch Downloader

Idea if a class cannot be found, it will be red

Solution of cursor thickening

Conditional test, if, case conditional test statements of shell script

Solution to the problem that jsp language cannot be recognized in idea

Take you to master the formatter of visual studio code

Use classname to modify style properties

LV1 previous life archives
随机推荐
C import Xls data method summary III (processing data in datatable)
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
Take you to master the formatter of visual studio code
Setting function of Jerry's watch management device [chapter]
SQL statement
Force buckle day32
LeetCode 168. Detailed explanation of Excel list name
51 MCU external interrupt
Openbionics robot project introduction | bciduino community finishing
Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
Why can't it run (unresolved)
2020-12-02 SSM advanced integration Shang Silicon Valley
Hamburg University of Technology (tuhh) | intelligent problem solving as integrated hierarchical reinforcement learning
Life cycle of instance variables, static variables and local variables
Introduction to Tianchi news recommendation: 4 Characteristic Engineering
Write the first CUDA program
TP5 automatic registration hook mechanism hook extension, with a complete case
Portapack application development tutorial (XVII) nRF24L01 launch C
Remember a lazy query error