Thông tin hệ thống với /proc

Có bao giờ các bạn tự hỏi là những chương trình mình hay sử dụng như top, ps, free hay KDE System Guard làm thế nào lấy được thông tin về hệ thống như các process đang chạy, lượng bộ nhớ còn trống, lượng CPU đang được sử dụng … hay không? Các bạn đang học hệ điều hành, đang tiếp xúc với lập trình hệ thống? Và các bạn muốn tự mình viết những chương trình như vậy? Vậy thì kiến thức về hệ thống file /proc là điều thiết yếu các bạn cần nắm. Bài viết này nhằm giới thiệu cho các bạn các thông tin cơ bản về /proc file system để dựa vào đó các bạn có thể tự bắt tay vào “vọc” hệ thống.

1. Hệ thống file /proc ( proc FS)
Proc là hệ thống file ảo (pseudo file system), một hệ thống file thời gian thực (real time) và thường trú trong bộ nhớ (memory resident) để theo dõi các process đang chạy cùng với trạng thái của hệ thống.
Proc là hệ thống file ảo bởi vì trên thực tế nó không tồn tại trong bất kì phương tiện lưu trữ nào. Nó tồn tại dựa trên bộ nhớ ảo và dữ liệu luôn thay đổi động cùng với trạgn thái của hệ thống. Hầu hết các dữ liệu trong proc FS được cập nhật liên tục để phù hợp với trạng thái hiện tại của hệ điều hành. Nội dung của proc FS có thể được đọc bởi user có quyền thích hợp, trong đó một số phần chỉ dó thể đọc bởi owner của process và root. Nếu liệt kê thư mục root (/) ra bạn sẽ thấy

Trích:

# ls –al /
drwxr-xr-x 2 root root 4096 Feb 22 2006 mnt
drwxr-xr-x 2 root root 4096 Feb 22 2006 opt
dr-xr-xr-x 144 root root 0 Oct 2 20:00 proc 

Thư mục proc có kích thước luôn = 0 và thời điểm modify cuối cùng là thời điểm hiện tại.

2. Nội dung thư mục /proc
Bây giờ hãy thử “ls proc” bạn sẽ thấy hàng các tập tin và thư mục.
Sau đây là tên các file và mô tả ngắn gọn của chúng
————————————————————————————————-
file:/proc/cmdline == Kernel Command line (eg . “/rhgb”, “quite” etc.
file:/proc/cpuinfo == used cpus and various info about them
file:/proc/crypto == current password encription (such as MD5)
file:/proc/devices == available character(eg terminal,sound etc.) and block device name and corresponding number
file:/proc/diskstats == status about all diskdevice including ramdrive (such as free space,etc.)
file:/proc/dma == dma status (such as cascade)
file:/proc/execdomains == don’t know, maybe about kernel domain !
file:/proc/fb == maybe about any pending mails
file:/proc/filesystems == available filesystems (such as : ext3,vfat,etc.)
file:/proc/interrupts == available and usable interrupts and their number
file:/proc/iomem == kernel ram info (may be about available memory mapped io space)
file:/proc/ioports == available system io ports (such as timer,pci,vga+ , etc)
file:/proc/kcore == You can not access it as it is used by kernel itself
file:/proc/kmsg == Kernel Messages
file:/proc/loadavg == CPU Loads and uptime
file:/proc/locks
file:/proc/mdstat
file:/proc/meminfo == details about memory , including swap,cache,physical memory size and available free space
file:/proc/misc
file:/proc/modules == loded kernel modules (such as snd,alsa,hw,gameport etc.)
file:/proc/mounts ==mounted partitions
file:/proc/mtrr
file:/proc/partitions == Available Disk Partitions (eg. hda,hda1,…,hdb,.. etc.)
file:/proc/pci == pci devices and their corresponding Bus no.,devive no.,function no. ,irq no. etc.
file:/proc/slabinfo ==
file:/proc/stat == cpu usage info , Includes per cpu load (eg. cpu0,cpu1,…) , context switches , uptime etc.
file:/proc/swaps == Swap partition drive name and size etc.
file:/proc/sysrq-trigger
file:/proc/uptime == Uptime (times elapsed since boot) for individule cpus
file:/proc/version == Linux Kernel version and compiler used to build it
file:/proc/vmstat == Details Virtual memory Info ,including page faults,range,etc.
——————————————————————————————————

Các thư mục được đánh số như 1, 4567, 2385, 112, 40, 41 … chính là các Process ID (PID) của những process đang chạy trong hệ thống. Mỗi thư mục sẽ chứa thông tin về process đó. Bạn có thể dùng lệnh “ps –ef” để liệt kê các process đang chạy và so sánh với tên các thư mục trên để biết thư mục nào chứa thông tin của process nào. Nếu đang dùng giao diện KDE bạn có thể thử phím tắt Ctrl + ESC). Hãy cd vào một thư mục nào đó, số 1 chẳng hạn, và ls ra để xem có gì trong đó.

Trích:

attr cmdline environ fd mem root statm task
auxv cwd exe maps mounts stat status wchan

Đó là những gì các bạn thấy. Ý nghĩa nó thế nào?

——————————————————————————————————
attr == its a folder containing attributes of all child and parent processes
cmdline == it contains the COMMAND LINE used to create the process , so you can get NAME of the process from here
environ == ENVIRONMENT variable passed to it
fd == its a folder containing file descriptor opened by the particular process
mem == memory info
root == its a folder link to root directory
statm == memory status
task == its a folder containing task number ? (same as it’s PID)
auxv == Contains the initial values of the process’s aux vector in
an array of auxv_t structures (see <sys/auxv.h>). The values
are those that were passed by the operating system as
startup information to the dynamic linker.

cwd == symbollic link to current working directory
exe == MEMORY IMAGE of the Process . SO YOU CAN DUMP IT EASILY IN RUNTIME , use it for debugging purpose!
maps == when you print the contents of this named pipe, you can see the
parts of the process’ address space which are currently mapped to a file.
The fields, from left to right, are: the address space associated to this
mapping, the permissions associated to this mapping, the offset from the
beginning of the file where the mapping starts, the device on which the
mapped file is located, the inode number of the file and finally, the name
of the file itself.
mounts == currently mounted devices(mount it used to join different filesystem to root filesystem)
stat == status about the process containg fields such as PID, PPID, VMsize, Process name etc.
status == some of the info from “stat” file but in easily understandable format!
wchan == currently running thread

——————————————————————————————————

Đó là những thông tin cơ bản nhất về proc FS. Ngoài ra còn nhiều điều thú vị nữa, các bạn có thể tự khám phá. Các bạn thử viết một vài chương trình truy xuất các thông tin trên để tự tìm hiểu về hệ thống của mình nhé. Tìm ra gì hay ho thì gửi lên đây cho mọi người xem với.

Đông Thao

Nhân dịp bkitclub.net vừa phục hồi, tìm lại được bài viết này post lại ở đây để lưu trữ và tham khảo.

About dongthao

"Man does not simply exist but always decides what his existence will be, what he will become the next moment"
This entry was posted in Linux, Linux Tutorial. Bookmark the permalink.

2 Responses to Thông tin hệ thống với /proc

  1. hoi dap says:

    may em loi bus no device no func no la loi gi ha anh. giup dum em cach khac phuc voi. em cam on

  2. Tran Thach says:

    Cám ơn bạn bài viết hay,

    Bạn có tài liệu nào nói rõ hơn về chức năng, tham số cho từng file không. Tài liệu tiếng việt thì càng tốt 🙂

Leave a comment