当前位置:网站首页>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 .
边栏推荐
- Write the first CUDA program
- Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
- String & memory function (detailed explanation)
- What is the intelligent monitoring system of sewage lifting pump station and does it play a big role
- Three layer switching ①
- Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
- Audio resource settings for U3D resource management
- Force buckle day32
- Software product download collection
- TP5 automatic registration hook mechanism hook extension, with a complete case
猜你喜欢
MPLS③
Douban scoring applet Part-3
Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Yyds dry goods inventory override and virtual of classes in C
MySQL introduction - functions (various function statistics, exercises, details, tables)
Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
Will the memory of ParticleSystem be affected by maxparticles
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
A fan summed up so many interview questions for you. There is always one you need!
随机推荐
Will the memory of ParticleSystem be affected by maxparticles
Small program graduation project based on wechat examination small program graduation project opening report function reference
Yyds dry goods inventory hand-in-hand teach you the development of Tiktok series video batch Downloader
Méthode de calcul de la connexion MSSQL de la carte esp32c3
C import Xls data method summary V (complete code)
All metal crowns - current market situation and future development trend
Idsia & supsi & usi | continuous control behavior learning and adaptive robot operation based on Reinforcement Learning
Human resource management online assignment
Logical operator, displacement operator
A. ABC
Maximum entropy model
Chinese Mitten Crab - current market situation and future development trend
Maximum likelihood method, likelihood function and log likelihood function
Meta metauniverse female safety problems occur frequently. How to solve the related problems in the metauniverse?
C import Xls data method summary IV (upload file de duplication and database data De duplication)
Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]
Applet graduation project based on wechat selection voting applet graduation project opening report function reference
What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
Reading notes - learn to write: what is writing?
Make drop-down menu