一、实验代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> void main()
{
int fd;
char buf = ''; char *pname = "/home/sfl/mytest/proc_test/abc/aaa.txt"; fd = open(pname, O_RDWR);
if (fd < ) {
printf("Open failed: %m");
return;
} while() {
write(fd, &buf, ); usleep(1000);
}
}

二、测试

sfl@ubuntu:~/mytest/proc_test$ tree
.
├── abc
│ └── aaa.txt
├── pp
└── test.c

# gcc test.c -o pp
# ./pp
# ps -aux | grep pp
sfl 4626 93.0 0.0 2036 512 pts/0 R 08:34 0:07 ./pp

1.查看进程的路径:
sfl@ubuntu:/proc/4626$ ls cwd -l
lrwxrwxrwx 1 sfl sfl 0 Sep 27 08:34 cwd -> /home/sfl/mytest/proc_test

2.查看可执行程序路径名
sfl@ubuntu:/proc/4626$ ls exe -l
lrwxrwxrwx 1 sfl sfl 0 Sep 27 08:34 exe -> /home/sfl/mytest/proc_test/pp

3.查看进程运行环境
sfl@ubuntu:/proc/4626$ strings environ 可查看进程运行的环境,包括pwd和cmdline等信息

4.查看进程打开的文件描述符
sfl@ubuntu:/proc/4626$ ls fd -l 查看所有打开的文件的路径
total 0
lrwx------ 1 sfl sfl 64 Sep 27 08:36 0 -> /dev/pts/0 std_in
lrwx------ 1 sfl sfl 64 Sep 27 08:36 1 -> /dev/pts/0 std_out
lrwx------ 1 sfl sfl 64 Sep 27 08:34 2 -> /dev/pts/0 std_error
lrwx------ 1 sfl sfl 64 Sep 27 08:36 3 -> /home/sfl/mytest/proc_test/abc/aaa.txt

5.获取执行时的cmdline
root@ubuntu:/proc/4626# strings cmdline
./pp

6.查看io操作已经读写的字节数
root@ubuntu:/proc/4626# cat io
rchar: 1223
wchar: 57672
syscr: 7
syscw: 57672
read_bytes: 0
write_bytes: 73728

7.查看进程nice值
root@ubuntu:/proc/4626# cat autogroup
/autogroup-311 nice 0

8.comm应该是可执行文件名
root@ubuntu:/proc/4626# cat comm
pp

9.查看系统调用
root@ubuntu:/proc/4626# cat syscall
running
root@ubuntu:/proc/4626# cat syscall
162 0xbf9d71b8 0x0 0xb7762000 0x0 0x0 0xbf9d71f8 0xbf9d718c 0xb777ecf9

10.查看系统对此可执行程序的限制

root@ubuntu:/proc/# cat limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size unlimited bytes
Max core file size unlimited bytes
Max resident set unlimited unlimited bytes
Max processes processes
Max open files files
Max locked memory bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals signals
Max msgqueue size bytes
Max nice priority
Max realtime priority
Max realtime timeout unlimited unlimited us

11.查看文件系统的挂载信息 per-thread的!!

root@ubuntu:/proc/# cat mountinfo
: / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw
: / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
: / /dev rw,relatime - devtmpfs udev rw,size=3097160k,nr_inodes=,mode=
: / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=,mode=,ptmxmode=
: / /run rw,nosuid,noexec,relatime - tmpfs tmpfs rw,size=621552k,mode=
: / / rw,relatime - ext4 /dev/sda1 rw,errors=remount-ro,data=ordered
: / /sys/fs/cgroup rw,relatime - tmpfs none rw,size=4k,mode=
: / /sys/fs/fuse/connections rw,relatime - fusectl none rw
: / /sys/kernel/debug rw,relatime - debugfs none rw
: / /sys/kernel/security rw,relatime - securityfs none rw
: / /run/lock rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=5120k
: / /run/shm rw,nosuid,nodev,relatime - tmpfs none rw
: / /run/user rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=102400k,mode=
: / /sys/fs/pstore rw,relatime - pstore none rw
: / /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime - cgroup systemd rw,name=systemd
: / /run/user//gvfs rw,nosuid,nodev,relatime - fuse.gvfsd-fuse gvfsd-fuse rw,user_id=,group_id= root@ubuntu:/proc/# cat mounts
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime
proc /proc proc rw,nosuid,nodev,noexec,relatime
udev /dev devtmpfs rw,relatime,size=3097160k,nr_inodes=,mode=
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=,mode=,ptmxmode=
tmpfs /run tmpfs rw,nosuid,noexec,relatime,size=621552k,mode=
/dev/sda1 / ext4 rw,relatime,errors=remount-ro,data=ordered
none /sys/fs/cgroup tmpfs rw,relatime,size=4k,mode=
none /sys/fs/fuse/connections fusectl rw,relatime
none /sys/kernel/debug debugfs rw,relatime
none /sys/kernel/security securityfs rw,relatime
none /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k
none /run/shm tmpfs rw,nosuid,nodev,relatime
none /run/user tmpfs rw,nosuid,nodev,noexec,relatime,size=102400k,mode=
none /sys/fs/pstore pstore rw,relatime
systemd /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd
gvfsd-fuse /run/user//gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=,group_id= root@ubuntu:/proc/# cat mountstats
device sysfs mounted on /sys with fstype sysfs
device proc mounted on /proc with fstype proc
device udev mounted on /dev with fstype devtmpfs
device devpts mounted on /dev/pts with fstype devpts
device tmpfs mounted on /run with fstype tmpfs
device /dev/sda1 mounted on / with fstype ext4
device none mounted on /sys/fs/cgroup with fstype tmpfs
device none mounted on /sys/fs/fuse/connections with fstype fusectl
device none mounted on /sys/kernel/debug with fstype debugfs
device none mounted on /sys/kernel/security with fstype securityfs
device none mounted on /run/lock with fstype tmpfs
device none mounted on /run/shm with fstype tmpfs
device none mounted on /run/user with fstype tmpfs
device none mounted on /sys/fs/pstore with fstype pstore
device systemd mounted on /sys/fs/cgroup/systemd with fstype cgroup
device gvfsd-fuse mounted on /run/user//gvfs with fstype fuse.gvfsd-fuse

12.查看进程状态信息

root@ubuntu:/proc/# cat status
Name: pp
State: S (sleeping)
Tgid:
Ngid:
Pid:
PPid:
TracerPid:
Uid:
Gid:
FDSize:
Groups:
NStgid:
NSpid:
NSpgid:
NSsid:
VmPeak: kB 最大虚拟内存峰值
VmSize: kB 虚拟内存
VmLck: kB
VmPin: kB
VmHWM: kB
VmRSS: kB
VmData: kB
VmStk: kB
VmExe: kB
VmLib: kB
VmPTE: kB
VmPMD: kB
VmSwap: kB
HugetlbPages: kB
Threads:
SigQ: /
SigPnd:
ShdPnd:
SigBlk:
SigIgn:
SigCgt:
CapInh:
CapPrm:
CapEff:
CapBnd: 0000003fffffffff
CapAmb:
Seccomp:
Cpus_allowed: ff 允许运行的CPU掩码
Cpus_allowed_list: -
Mems_allowed:
Mems_allowed_list:
voluntary_ctxt_switches:
nonvoluntary_ctxt_switches:

13.查看可执行文件的内存映射

root@ubuntu:/proc/# cat maps
- r-xp : /home/sfl/mytest/proc_test/pp 代码段
-0804a000 r--p : /home/sfl/mytest/proc_test/pp 只读存储区
0804a000-0804b000 rw-p : /home/sfl/mytest/proc_test/pp 数据段
b75b6000-b75b7000 rw-p :
b75b7000-b775f000 r-xp : /lib/i386-linux-gnu/libc-2.19.so
b775f000-b7760000 ---p 001a8000 : /lib/i386-linux-gnu/libc-2.19.so
b7760000-b7762000 r--p 001a8000 : /lib/i386-linux-gnu/libc-2.19.so
b7762000-b7763000 rw-p 001aa000 : /lib/i386-linux-gnu/libc-2.19.so
b7763000-b7766000 rw-p :
b777a000-b777c000 rw-p :
b777c000-b777e000 r--p : [vvar]
b777e000-b7780000 r-xp : [vdso]
b7780000-b77a0000 r-xp : /lib/i386-linux-gnu/ld-2.19.so
b77a0000-b77a1000 r--p 0001f000 : /lib/i386-linux-gnu/ld-2.19.so
b77a1000-b77a2000 rw-p : /lib/i386-linux-gnu/ld-2.19.so
bf9b8000-bf9d9000 rw-p : [stack]

14.task/4626/目录下面又有一层一样的东西

root@ubuntu:/proc/# ls task//
attr clear_refs cwd fdinfo loginuid mounts oom_score projid_map sessionid stat uid_map
auxv cmdline environ gid_map maps net oom_score_adj root setgroups statm wchan
cgroup comm exe io mem ns pagemap sched smaps status
children cpuset fd limits mountinfo oom_adj personality schedstat stack syscall

15.net目录下又有这么多文件

root@ubuntu:/proc/# ls net/
anycast6 dev_mcast icmp ip6_flowlabel ipv6_route netlink ptype rt6_stats snmp6 tcp udplite6
arp dev_snmp6 icmp6 ip6_mr_cache l2cap netstat raw rt_acct sockstat tcp6 unix
bnep fib_trie if_inet6 ip6_mr_vif mcfilter packet raw6 rt_cache sockstat6 udp wireless
connector fib_triestat igmp ip_mr_cache mcfilter6 protocols rfcomm sco softnet_stat udp6 xfrm_stat
dev hci igmp6 ip_mr_vif netfilter psched route snmp stat udplite
05-06 01:16