一、实验代码
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> void main() { int fd; char buf = ‘1‘; char *pname = "/home/sfl/mytest/proc_test/abc/aaa.txt"; fd = open(pname, O_RDWR); if (fd < 0) { printf("Open failed: %m"); return; } while(1) { write(fd, &buf, 1); usleep(1000); } }
二、测试
[email protected]:~/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.查看进程的路径:
[email protected]:/proc/4626$ ls cwd -l
lrwxrwxrwx 1 sfl sfl 0 Sep 27 08:34 cwd -> /home/sfl/mytest/proc_test
2.查看可执行程序路径名
[email protected]:/proc/4626$ ls exe -l
lrwxrwxrwx 1 sfl sfl 0 Sep 27 08:34 exe -> /home/sfl/mytest/proc_test/pp
3.查看进程运行环境
[email protected]:/proc/4626$ strings environ 可查看进程运行的环境,包括pwd和cmdline等信息
4.查看进程打开的文件描述符
[email protected]:/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
[email protected]:/proc/4626# strings cmdline
./pp
6.查看io操作已经读写的字节数
[email protected]:/proc/4626# cat io
rchar: 1223
wchar: 57672
syscr: 7
syscw: 57672
read_bytes: 0
write_bytes: 73728
7.查看进程nice值
[email protected]:/proc/4626# cat autogroup
/autogroup-311 nice 0
8.comm应该是可执行文件名
[email protected]:/proc/4626# cat comm
pp
9.查看系统调用
[email protected]:/proc/4626# cat syscall
running
[email protected]:/proc/4626# cat syscall
162 0xbf9d71b8 0x0 0xb7762000 0x0 0x0 0xbf9d71f8 0xbf9d718c 0xb777ecf9
10.查看系统对此可执行程序的限制
[email protected]:/proc/4626# 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 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 48392 48392 processes Max open files 1024 4096 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 48392 48392 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us
11.查看文件系统的挂载信息 per-thread的!!
[email protected]:/proc/4626# cat mountinfo 18 23 0:17 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw 19 23 0:4 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw 20 23 0:6 / /dev rw,relatime - devtmpfs udev rw,size=3097160k,nr_inodes=204098,mode=755 21 20 0:14 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=000 22 23 0:18 / /run rw,nosuid,noexec,relatime - tmpfs tmpfs rw,size=621552k,mode=755 23 0 8:1 / / rw,relatime - ext4 /dev/sda1 rw,errors=remount-ro,data=ordered 24 18 0:19 / /sys/fs/cgroup rw,relatime - tmpfs none rw,size=4k,mode=755 25 18 0:20 / /sys/fs/fuse/connections rw,relatime - fusectl none rw 26 18 0:7 / /sys/kernel/debug rw,relatime - debugfs none rw 27 18 0:12 / /sys/kernel/security rw,relatime - securityfs none rw 28 22 0:21 / /run/lock rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=5120k 29 22 0:22 / /run/shm rw,nosuid,nodev,relatime - tmpfs none rw 30 22 0:23 / /run/user rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=102400k,mode=755 31 18 0:24 / /sys/fs/pstore rw,relatime - pstore none rw 33 24 0:26 / /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime - cgroup systemd rw,name=systemd 34 30 0:27 / /run/user/1000/gvfs rw,nosuid,nodev,relatime - fuse.gvfsd-fuse gvfsd-fuse rw,user_id=1000,group_id=1000 [email protected]:/proc/4626# cat mounts sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0 proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 udev /dev devtmpfs rw,relatime,size=3097160k,nr_inodes=204098,mode=755 0 0 devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0 tmpfs /run tmpfs rw,nosuid,noexec,relatime,size=621552k,mode=755 0 0 /dev/sda1 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0 none /sys/fs/cgroup tmpfs rw,relatime,size=4k,mode=755 0 0 none /sys/fs/fuse/connections fusectl rw,relatime 0 0 none /sys/kernel/debug debugfs rw,relatime 0 0 none /sys/kernel/security securityfs rw,relatime 0 0 none /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0 none /run/shm tmpfs rw,nosuid,nodev,relatime 0 0 none /run/user tmpfs rw,nosuid,nodev,noexec,relatime,size=102400k,mode=755 0 0 none /sys/fs/pstore pstore rw,relatime 0 0 systemd /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0 gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 [email protected]:/proc/4626# 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/1000/gvfs with fstype fuse.gvfsd-fuse
12.查看进程状态信息
[email protected]:/proc/4626# cat status Name: pp State: S (sleeping) Tgid: 4626 Ngid: 0 Pid: 4626 PPid: 3978 TracerPid: 0 Uid: 1000 1000 1000 1000 Gid: 1000 1000 1000 1000 FDSize: 256 Groups: 4 24 27 30 46 108 124 1000 NStgid: 4626 NSpid: 4626 NSpgid: 4626 NSsid: 3978 VmPeak: 2116 kB 最大虚拟内存峰值 VmSize: 2036 kB 虚拟内存 VmLck: 0 kB VmPin: 0 kB VmHWM: 544 kB VmRSS: 544 kB VmData: 40 kB VmStk: 136 kB VmExe: 4 kB VmLib: 1824 kB VmPTE: 16 kB VmPMD: 12 kB VmSwap: 0 kB HugetlbPages: 0 kB Threads: 1 SigQ: 0/48392 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000000000000 SigCgt: 0000000000000000 CapInh: 0000000000000000 CapPrm: 0000000000000000 CapEff: 0000000000000000 CapBnd: 0000003fffffffff CapAmb: 0000000000000000 Seccomp: 0 Cpus_allowed: ff 允许运行的CPU掩码 Cpus_allowed_list: 0-7 Mems_allowed: 1 Mems_allowed_list: 0 voluntary_ctxt_switches: 296207 nonvoluntary_ctxt_switches: 26
13.查看可执行文件的内存映射
[email protected]:/proc/4626# cat maps 08048000-08049000 r-xp 00000000 08:01 1310779 /home/sfl/mytest/proc_test/pp 代码段 08049000-0804a000 r--p 00000000 08:01 1310779 /home/sfl/mytest/proc_test/pp 只读存储区 0804a000-0804b000 rw-p 00001000 08:01 1310779 /home/sfl/mytest/proc_test/pp 数据段 b75b6000-b75b7000 rw-p 00000000 00:00 0 b75b7000-b775f000 r-xp 00000000 08:01 787745 /lib/i386-linux-gnu/libc-2.19.so b775f000-b7760000 ---p 001a8000 08:01 787745 /lib/i386-linux-gnu/libc-2.19.so b7760000-b7762000 r--p 001a8000 08:01 787745 /lib/i386-linux-gnu/libc-2.19.so b7762000-b7763000 rw-p 001aa000 08:01 787745 /lib/i386-linux-gnu/libc-2.19.so b7763000-b7766000 rw-p 00000000 00:00 0 b777a000-b777c000 rw-p 00000000 00:00 0 b777c000-b777e000 r--p 00000000 00:00 0 [vvar] b777e000-b7780000 r-xp 00000000 00:00 0 [vdso] b7780000-b77a0000 r-xp 00000000 08:01 787721 /lib/i386-linux-gnu/ld-2.19.so b77a0000-b77a1000 r--p 0001f000 08:01 787721 /lib/i386-linux-gnu/ld-2.19.so b77a1000-b77a2000 rw-p 00020000 08:01 787721 /lib/i386-linux-gnu/ld-2.19.so bf9b8000-bf9d9000 rw-p 00000000 00:00 0 [stack]
14.task/4626/目录下面又有一层一样的东西
[email protected]:/proc/4626# ls task/4626/ 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目录下又有这么多文件
[email protected]:/proc/4626# 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
原文地址:https://www.cnblogs.com/hellokitty2/p/9716396.html