Chinaunix首页 | 论坛 | 博客
  • 博客访问: 734670
  • 博文数量: 235
  • 博客积分: 4309
  • 博客等级: 中校
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-17 11:25
个人简介

If you don\\\\\\\\\\\\\\\'t wanna do it, you find an EXCUSE; if you do, you\\\\\\\\\\\\\\\'ll find a WAY :-)

文章分类

全部博文(235)

文章存档

2014年(3)

2013年(2)

2012年(31)

2011年(199)

分类: LINUX

2011-01-17 17:22:20

Earlier in the chapter we mentioned that Linux treats most things as files and that there are entries in the
file system for hardware devices. These /dev files are used to access hardware in a specific way using
low-level system calls.

The software drivers that control hardware can often be configured in certain ways, or are capable of
reporting information. For example, a hard disk controller may be configured to use a particular DMA
mode. A network card might be able to report whether it has negotiated a high-speed, duplex connection.
Utilities for communicating with device drivers have been common in the past. For example, hdparm
is used to configure some disk parameters and ifconfig can report network statistics. In recent years,
there has been a trend toward providing a more consistent way of accessing driver information, and, in
fact, to extend this to include communication with various elements of the Linux kernel.
Linux provides a special file system, procfs, that is usually made available as the directory /proc. It
contains many special files that allow higher-level access to driver and kernel information. Applications
can read and write these files to get information and set parameters as long as they are running with the
correct access permissions.

The files that appear in /proc will vary from system to system, and more are included with each Linux
release as more drivers and facilities support the procfs file system. Here, we look at some of the more
common files and briefly consider their use.

A directory listing of /proc on the computer being used to write this chapter shows the following entries:
1/ 10514/ 20254/ 6/ 9057/ 9623/ ide/ mtrr
10359/ 10524/ 29/ 698/ 9089/ 9638/ interrupts net/
10360/ 10530/ 2983/ 699/ 9118/ acpi/ iomem partitions
10381/ 10539/ 3/ 710/ 9119/ asound/ ioports scsi/
10438/ 10541/ 30/ 711/ 9120/ buddyinfo irq/ self@
10441/ 10555/ 3069/ 742/ 9138/ bus/ kallsyms slabinfo
10442/ 10688/ 3098/ 7808/ 9151/ cmdline kcore splash
10478/ 10689/ 3099/ 7813/ 92/ config.gz keys stat
10479/ 10784/ 31/ 8357/ 9288/ cpuinfo key-users swaps
10482/ 113/ 3170/ 8371/ 93/ crypto kmsg sys/
10484/ 115/ 3171/ 840/ 9355/ devices loadavg sysrq-trigger
10486/ 116/ 3177/ 8505/ 9407/ diskstats locks sysvipc/
10495/ 1167/ 32288/ 8543/ 9457/ dma mdstat tty/
10497/ 1168/ 3241/ 8547/ 9479/ driver/ meminfo uptime
128

10498/ 1791/ 352/ 8561/ 9618/ execdomains misc version
10500/ 19557/ 4/ 8677/ 9619/ fb modules vmstat
10502/ 19564/ 4010/ 888/ 9621/ filesystems mounts@ zoneinfo
10510/ 2/ 5/ 8910/ 9622/ fs/ mpt/

In many cases, the files can just be read and will give status information. For example, /proc/cpuinfo
gives details of the processors available:
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 2
model name : Intel(R) Pentium(R) 4 CPU 2.66GHz
stepping : 8
cpu MHz : 2665.923
cache size : 512 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat
pse36 clflush dts acpi mmx fxsr sse sse2 ss up
bogomips : 5413.47
clflush size : 64

Similarly, /proc/meminfo and /proc/version give information about memory usage and kernel version,
respectively:
$ cat /proc/meminfo
MemTotal: 776156 kB
MemFree: 28528 kB
Buffers: 191764 kB
Cached: 369520 kB
SwapCached: 20 kB
Active: 406912 kB
Inactive: 274320 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 776156 kB
LowFree: 28528 kB
SwapTotal: 1164672 kB
SwapFree: 1164652 kB
Dirty: 68 kB
Writeback: 0 kB
AnonPages: 95348 kB
Mapped: 49044 kB
Slab: 57848 kB
SReclaimable: 48008 kB
SUnreclaim: 9840 kB
PageTables: 1500 kB
129

NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 1552748 kB
Committed_AS: 189680 kB
VmallocTotal: 245752 kB
VmallocUsed: 10572 kB
VmallocChunk: 234556 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
Hugepagesize: 4096 kB
$ cat /proc/version
Linux version 2.6.20.2-2-default (geeko@buildhost) (gcc version 4.1.3 20070218
(prerelease) (SUSE Linux)) #1 SMP Fri Mar 9 21:54:10 UTC 2007
The information given by these files is generated each time the file is read. So rereading the meminfo file
at a later time will give up-to-the-second results.
You can find more information from specific kernel functions in subdirectories of /proc. For example,
you can get network socket usage statistics from /proc/net/sockstat:
$ cat /proc/net/sockstat
sockets: used 285
TCP: inuse 4 orphan 0 tw 0 alloc 7 mem 1
UDP: inuse 3
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0
Some of the /proc entries can be written to as well as read. For example, the total number of files that all
running programs can open at the same time is a Linux kernel parameter. The current value can be read
at /proc/sys/fs/file-max:
$ cat /proc/sys/fs/file-max
76593
Here the value is set to 76,593. If you need to increase this value, you can do so by writing to the same file.
You may need to do this if you are running a specialist application suite—such as a database system that
uses many tables—that needs to open many files at once.
Writing /proc files requires superuser access. You must take great care when writing /proc files; it’s possible
to cause severe problems including system crashes and loss of data by writing inappropriate values.
To increase the system-wide file handle limit to 80,000, you can simply write the new limit to the filemax
file:
# echo 80000 >/proc/sys/fs/file-max
Now, when you reread the file, you see the new value:
$ cat /proc/sys/fs/file-max
80000
130

The subdirectories of /proc that have numeric names are used to provide access to information about
running programs. You learn more about how programs are executed as processes in Chapter 11.
For now, just notice that each process has a unique identifier: a number between 1 and about 32,000. The
ps command provides a list of currently running processes. For example, as this chapter is being written:
neil@suse103:~/BLP4e/chapter03> ps -a
PID TTY TIME CMD
9118 pts/1 00:00:00 ftp
9230 pts/1 00:00:00 ps
10689 pts/1 00:00:01 bash
neil@suse103:~/BLP4e/chapter03>
Here, you can see several terminal sessions running the bash shell and a file transfer session running the
ftp program. You can get more details about the ftp session by looking in /proc.
The process identifier for ftp here is given as 9118, so you need to look in /proc/9118 for details about it:
$ ls -l /proc/9118
total 0
0 dr-xr-xr-x 2 neil users 0 2007-05-20 07:43 attr
0 -r-------- 1 neil users 0 2007-05-20 07:43 auxv
0 -r--r--r-- 1 neil users 0 2007-05-20 07:35 cmdline
0 -r--r--r-- 1 neil users 0 2007-05-20 07:43 cpuset
0 lrwxrwxrwx 1 neil users 0 2007-05-20 07:43 cwd -> /home/neil/BLP4e/chapter03
0 -r-------- 1 neil users 0 2007-05-20 07:43 environ
0 lrwxrwxrwx 1 neil users 0 2007-05-20 07:43 exe -> /usr/bin/pftp
0 dr-x------ 2 neil users 0 2007-05-20 07:19 fd
0 -rw-r--r-- 1 neil users 0 2007-05-20 07:43 loginuid
0 -r--r--r-- 1 neil users 0 2007-05-20 07:43 maps
0 -rw------- 1 neil users 0 2007-05-20 07:43 mem
0 -r--r--r-- 1 neil users 0 2007-05-20 07:43 mounts
0 -r-------- 1 neil users 0 2007-05-20 07:43 mountstats
0 -rw-r--r-- 1 neil users 0 2007-05-20 07:43 oom_adj
0 -r--r--r-- 1 neil users 0 2007-05-20 07:43 oom_score
0 lrwxrwxrwx 1 neil users 0 2007-05-20 07:43 root -> /
0 -rw------- 1 neil users 0 2007-05-20 07:43 seccomp
0 -r--r--r-- 1 neil users 0 2007-05-20 07:43 smaps
0 -r--r--r-- 1 neil users 0 2007-05-20 07:33 stat
0 -r--r--r-- 1 neil users 0 2007-05-20 07:43 statm
0 -r--r--r-- 1 neil users 0 2007-05-20 07:33 status
0 dr-xr-xr-x 3 neil users 0 2007-05-20 07:43 task
0 -r--r--r-- 1 neil users 0 2007-05-20 07:43 wchan
Here, you can see various special files that can tell us what is happening with this process.
You can tell that the program /usr/bin/pftp is running and that its current working directory is
/home/neil/BLP4e/chapter03. It is possible to read the other files in this directory to see the command
line used to start it as well as the shell environment it has. The cmdline and environ files provide
this information as a series of null-terminated strings, so you need to take care when viewing them.
We discuss the Linux environment in depth in Chapter 4.
$ od -c /proc/9118/cmdline
0000000 f t p \0 1 9 2 . 1 6 8 . 0 . 1 2
131
阅读(672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~