一、实验代码
#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); } }
二、测试
sfl@ubuntu:~/mytest/proc_test$ tree
.
├── abc
│ └── aaa.txt
├── pp
└── test.c
# gcc test.c -o pp
# ./pp
# ps -aux | grep pp
sfl 4156 93.0 0.0 2036 512 pts/0 R 08:34 0:07 ./pp
1.查看进程的路径:
sfl@ubuntu:/proc/4156$ ls cwd -l
lrwxrwxrwx 1 sfl sfl 0 Sep 27 08:34 cwd -> /home/sfl/mytest/proc_test
2.查看可执行程序路径名
sfl@ubuntu:/proc/4156$ ls exe -l
lrwxrwxrwx 1 sfl sfl 0 Sep 27 08:34 exe -> /home/sfl/mytest/proc_test/pp
3.查看进程运行环境
sfl@ubuntu:/proc/4156$ strings environ 可查看进程运行的环境,包括pwd和cmdline等信息
4.查看进程打开的文件描述符
sfl@ubuntu:/proc/4156$ 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