当前位置:网站首页>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 .
边栏推荐
- How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
- Some other configurations on Huawei's spanning tree
- The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
- Lightweight Pyramid Networks for Image Deraining
- Small program graduation project based on wechat video broadcast small program graduation project opening report function reference
- Trading software programming
- Force buckle day32
- Experimental animal models - current market situation and future development trend
- Related configuration commands of Huawei rip
- Openbionics exoskeleton project introduction | bciduino community finishing
猜你喜欢
A fan summed up so many interview questions for you. There is always one you need!
Make drop-down menu
Solution to the problem that jsp language cannot be recognized in idea
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
Setting function of Jerry's watch management device [chapter]
Infiltration learning diary day19
IPv6 experiment
Douban scoring applet Part-3
Final consistency of MESI cache in CPU -- why does CPU need cache
Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
随机推荐
Write the first CUDA program
Is Shengang securities company as safe as other securities companies
What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
The reasons why QT fails to connect to the database and common solutions
LeetCode 168. Detailed explanation of Excel list name
51 single chip microcomputer timer 2 is used as serial port
Chinese Mitten Crab - current market situation and future development trend
After listening to the system clear message notification, Jerry informed the device side to delete the message [article]
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
TP5 automatic registration hook mechanism hook extension, with a complete case
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
Basic editing specifications and variables of shell script
Valentine's Day - 9 jigsaw puzzles with deep love in wechat circle of friends
Huawei cloud micro certification Huawei cloud computing service practice has been stable
ThinkPHP uses redis to update database tables
Mobile phone battery - current market situation and future development trend
Small program graduation project based on wechat reservation small program graduation project opening report reference
C import Xls data method summary V (complete code)
Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition