码迷,mamicode.com
首页 > 系统相关 > 详细

《Linux/Unix系统编程手册》读书笔记7 (/proc文件的简介和运用)

时间:2014-05-02 12:43:09      阅读:708      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

《Linux/Unix系统编程手册》读书笔记 目录

第11章

这章主要讲了关于Linux和UNIX的系统资源的限制。

关于限制都存在一个最小值,这些最小值为<limits.h>文件中的常量。

通过cat 命令查看:

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ cat /usr/include/limits.h 
/* Copyright (C) 1991, 1992, 1996, 1997, 1998, 1999, 2000, 2005
   Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

/*
 *    ISO C99 Standard: 7.10/5.2.4.2.1 Sizes of integer types    <limits.h>
 */

#ifndef _LIBC_LIMITS_H_
#define _LIBC_LIMITS_H_    1

#include <features.h>


/* Maximum length of any multibyte character in any locale.
   We define this value here since the gcc header does not define
   the correct value.  */
#define MB_LEN_MAX    16


/* If we are not using GNU CC we have to define all the symbols ourself.
   Otherwise use gcc‘s definitions (see below).  */
#if !defined __GNUC__ || __GNUC__ < 2

/* We only protect from multiple inclusion here, because all the other
   #include‘s protect themselves, and in GCC 2 we may #include_next through
   multiple copies of this file before we get to GCC‘s.  */
# ifndef _LIMITS_H
#  define _LIMITS_H    1

#include <bits/wordsize.h>

/* We don‘t have #include_next.
   Define ANSI <limits.h> for standard 32-bit words.  */

/* These assume 8-bit `char‘s, 16-bit `short int‘s,
   and 32-bit `int‘s and `long int‘s.  */

/* Number of bits in a `char‘.    */
#  define CHAR_BIT    8

/* Minimum and maximum values a `signed char‘ can hold.  */
#  define SCHAR_MIN    (-128)
#  define SCHAR_MAX    127

/* Maximum value an `unsigned char‘ can hold.  (Minimum is 0.)  */
#  define UCHAR_MAX    255

/* Minimum and maximum values a `char‘ can hold.  */
#  ifdef __CHAR_UNSIGNED__
#   define CHAR_MIN    0
#   define CHAR_MAX    UCHAR_MAX
#  else
#   define CHAR_MIN    SCHAR_MIN
#   define CHAR_MAX    SCHAR_MAX
#  endif

/* Minimum and maximum values a `signed short int‘ can hold.  */
#  define SHRT_MIN    (-32768)
#  define SHRT_MAX    32767

/* Maximum value an `unsigned short int‘ can hold.  (Minimum is 0.)  */
#  define USHRT_MAX    65535

/* Minimum and maximum values a `signed int‘ can hold.  */
#  define INT_MIN    (-INT_MAX - 1)
#  define INT_MAX    2147483647

/* Maximum value an `unsigned int‘ can hold.  (Minimum is 0.)  */
#  define UINT_MAX    4294967295U

/* Minimum and maximum values a `signed long int‘ can hold.  */
#  if __WORDSIZE == 64
#   define LONG_MAX    9223372036854775807L
#  else
#   define LONG_MAX    2147483647L
#  endif
#  define LONG_MIN    (-LONG_MAX - 1L)

/* Maximum value an `unsigned long int‘ can hold.  (Minimum is 0.)  */
#  if __WORDSIZE == 64
#   define ULONG_MAX    18446744073709551615UL
#  else
#   define ULONG_MAX    4294967295UL
#  endif

#  ifdef __USE_ISOC99

/* Minimum and maximum values a `signed long long int‘ can hold.  */
#   define LLONG_MAX    9223372036854775807LL
#   define LLONG_MIN    (-LLONG_MAX - 1LL)

/* Maximum value an `unsigned long long int‘ can hold.  (Minimum is 0.)  */
#   define ULLONG_MAX    18446744073709551615ULL

#  endif /* ISO C99 */

# endif    /* limits.h  */
#endif    /* GCC 2.  */

#endif    /* !_LIBC_LIMITS_H_ */

 /* Get the compiler‘s limits.h, which defines almost all the ISO constants.

    We put this #include_next outside the double inclusion check because
    it should be possible to include this file more than once and still get
    the definitions from gcc‘s header.  */
#if defined __GNUC__ && !defined _GCC_LIMITS_H_
/* `_GCC_LIMITS_H_‘ is what GCC‘s file defines.  */
# include_next <limits.h>
#endif

/* The <limits.h> files in some gcc versions don‘t define LLONG_MIN,
   LLONG_MAX, and ULLONG_MAX.  Instead only the values gcc defined for
   ages are available.  */
#if defined __USE_ISOC99 && defined __GNUC__
# ifndef LLONG_MIN
#  define LLONG_MIN    (-LLONG_MAX-1)
# endif
# ifndef LLONG_MAX
#  define LLONG_MAX    __LONG_LONG_MAX__
# endif
# ifndef ULLONG_MAX
#  define ULLONG_MAX    (LLONG_MAX * 2ULL + 1)
# endif
#endif

#ifdef    __USE_POSIX
/* POSIX adds things to <limits.h>.  */
# include <bits/posix1_lim.h>
#endif

#ifdef    __USE_POSIX2
# include <bits/posix2_lim.h>
#endif

#ifdef    __USE_XOPEN
# include <bits/xopen_lim.h>
#endif
View Code

我们还可以通过在shell用getconf获取限制:

PS:OPEN_MAX为进程同时最多可以打开的文件描述符的数量。

lancelot@debian:~/Code/tlpi$ getconf OPEN_MAX
1024

第12章

这章主要将了系统和进程信息

/proc虚拟文件系统:维基

首先我们来看看/proc有些什么

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ ls /proc/
1      11994  1652  200   252   2712  32    3869  4156  4297  4442  4577  530   acpi       execdomains  kpageflags    softirqs
10     12     17    2001  2551  2775  33    3870  4165  4299  4451  4579  5318  asound     fb           loadavg       stat
10002  12017  19    202   26    28    3345  3927  4168  4401  4457  4580  589   ati        filesystems  locks         swaps
10008  13     191   203   2609  2815  3346  396   4169  4403  4464  4583  6     buddyinfo  fs           meminfo       sys
10010  14     194   21    2610  2869  34    4     4173  4404  4483  4584  6410  bus        interrupts   misc          sysrq-trigger
10235  140    195   22    2612  2879  3410  40    4177  4407  4485  4622  6415  cgroups    iomem        modules       sysvipc
10575  15     1956  23    2641  29    35    4000  4179  4410  4496  4627  667   cmdline    ioports      mounts        timer_list
11     158    196   2384  2668  2970  3798  4085  4181  4412  4500  4632  7     consoles   irq          mtrr          timer_stats
11377  16     198   2399  2669  2980  3828  4102  4186  4415  4503  4637  7733  cpuinfo    kallsyms     net           tty
11640  1644   1987  24    2670  2982  3864  4103  4190  4422  4511  4642  8     crypto     kcore        pagetypeinfo  uptime
11659  1645   1992  2480  2671  3     3865  4107  4196  4424  4516  4647  8167  devices    keys         partitions    version
11704  1646   1994  25    2672  30    3866  4108  4204  4436  4518  5248  8168  diskstats  key-users    sched_debug   vmallocinfo
11967  1647   2     251   27    3011  3867  4109  4211  4438  4526  5254  8169  dma        kmsg         self          vmstat
11988  1651   20    2515  2710  31    3868  4115  4217  4439  4570  529   9     driver     kpagecount   slabinfo      zoneinfo
bubuko.com,布布扣

那些数字是进程ID(PID),每个进程ID都有其独自的子文件夹,里面包含进程的相关信息。

通过PID为1的进程(init)来说明。

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ sudo ls -l /proc/1
[sudo] password for lancelot: 
总用量 0
dr-xr-xr-x 2 root root 0  4月 28 22:40 attr
-rw-r--r-- 1 root root 0  4月 28 22:40 autogroup
-r-------- 1 root root 0  4月 28 22:40 auxv
-r--r--r-- 1 root root 0  4月 28 22:40 cgroup
--w------- 1 root root 0  4月 28 22:40 clear_refs
-r--r--r-- 1 root root 0  4月 28 12:26 cmdline
-rw-r--r-- 1 root root 0  4月 28 22:40 comm
-rw-r--r-- 1 root root 0  4月 28 22:40 coredump_filter
-r--r--r-- 1 root root 0  4月 28 22:40 cpuset
lrwxrwxrwx 1 root root 0  4月 28 22:40 cwd -> /
-r-------- 1 root root 0  4月 28 22:40 environ
lrwxrwxrwx 1 root root 0  4月 28 12:26 exe -> /sbin/init
dr-x------ 2 root root 0  4月 28 22:40 fd
dr-x------ 2 root root 0  4月 28 22:40 fdinfo
-r-------- 1 root root 0  4月 28 22:40 io
-r--r--r-- 1 root root 0  4月 28 12:27 limits
-rw-r--r-- 1 root root 0  4月 28 22:40 loginuid
-r--r--r-- 1 root root 0  4月 28 22:40 maps
-rw------- 1 root root 0  4月 28 22:40 mem
-r--r--r-- 1 root root 0  4月 28 22:40 mountinfo
-r--r--r-- 1 root root 0  4月 28 22:40 mounts
-r-------- 1 root root 0  4月 28 22:40 mountstats
dr-xr-xr-x 6 root root 0  4月 28 22:40 net
dr-x--x--x 2 root root 0  4月 28 22:40 ns
-r--r--r-- 1 root root 0  4月 28 22:40 numa_maps
-rw-r--r-- 1 root root 0  4月 28 22:40 oom_adj
-r--r--r-- 1 root root 0  4月 28 22:40 oom_score
-rw-r--r-- 1 root root 0  4月 28 12:29 oom_score_adj
-r--r--r-- 1 root root 0  4月 28 22:40 pagemap
-r--r--r-- 1 root root 0  4月 28 22:40 personality
lrwxrwxrwx 1 root root 0  4月 28 22:40 root -> /
-rw-r--r-- 1 root root 0  4月 28 22:40 sched
-r--r--r-- 1 root root 0  4月 28 22:40 sessionid
-r--r--r-- 1 root root 0  4月 28 22:40 smaps
-r--r--r-- 1 root root 0  4月 28 22:40 stack
-r--r--r-- 1 root root 0  4月 28 12:26 stat
-r--r--r-- 1 root root 0  4月 28 22:40 statm
-r--r--r-- 1 root root 0  4月 28 12:26 status
-r--r--r-- 1 root root 0  4月 28 22:40 syscall
dr-xr-xr-x 3 root root 0  4月 28 22:40 task
-r--r--r-- 1 root root 0  4月 28 22:40 wchan
bubuko.com,布布扣

其中

cmdline是以‘\0‘分隔的命令参数

cwd是指向当前工作目录的符号链接,可以看出init当前工作目录为根目录。

environ是环境列表,以‘\0‘分隔

HOME=/init=/sbin/initTERM=linuxBOOT_IMAGE=/vmlinuz-3.2.0-4-amd64PATH=/sbin:/usr/sbin:/bin:/usr/binPWD=/rootmnt=/root

exe是指向正在执行文件的符号链接,正在执行文件为/sbin/init。

fd是文件目录,包含指向由该进程打开文件的符号链接

lancelot@debian:~/Code/tlpi$ sudo ls -l /proc/1/fd
总用量 0
lrwx------ 1 root root 64  4月 28 22:52 10 -> /run/initctl

maps为内存映射

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ sudo cat /proc/1/maps 
00400000-00409000 r-xp 00000000 08:08 9306173                            /sbin/init
00608000-00609000 rw-p 00008000 08:08 9306173                            /sbin/init
01e15000-01e36000 rw-p 00000000 00:00 0                                  [heap]
7f5109159000-7f510915b000 r-xp 00000000 08:08 1838382                    /lib/x86_64-linux-gnu/libdl-2.13.so
7f510915b000-7f510935b000 ---p 00002000 08:08 1838382                    /lib/x86_64-linux-gnu/libdl-2.13.so
7f510935b000-7f510935c000 r--p 00002000 08:08 1838382                    /lib/x86_64-linux-gnu/libdl-2.13.so
7f510935c000-7f510935d000 rw-p 00003000 08:08 1838382                    /lib/x86_64-linux-gnu/libdl-2.13.so
7f510935d000-7f51094df000 r-xp 00000000 08:08 1835094                    /lib/x86_64-linux-gnu/libc-2.13.so
7f51094df000-7f51096de000 ---p 00182000 08:08 1835094                    /lib/x86_64-linux-gnu/libc-2.13.so
7f51096de000-7f51096e2000 r--p 00181000 08:08 1835094                    /lib/x86_64-linux-gnu/libc-2.13.so
7f51096e2000-7f51096e3000 rw-p 00185000 08:08 1835094                    /lib/x86_64-linux-gnu/libc-2.13.so
7f51096e3000-7f51096e8000 rw-p 00000000 00:00 0 
7f51096e8000-7f5109706000 r-xp 00000000 08:08 1835054                    /lib/x86_64-linux-gnu/libselinux.so.1
7f5109706000-7f5109905000 ---p 0001e000 08:08 1835054                    /lib/x86_64-linux-gnu/libselinux.so.1
7f5109905000-7f5109906000 r--p 0001d000 08:08 1835054                    /lib/x86_64-linux-gnu/libselinux.so.1
7f5109906000-7f5109907000 rw-p 0001e000 08:08 1835054                    /lib/x86_64-linux-gnu/libselinux.so.1
7f5109907000-7f5109908000 rw-p 00000000 00:00 0 
7f5109908000-7f5109947000 r-xp 00000000 08:08 1835059                    /lib/x86_64-linux-gnu/libsepol.so.1
7f5109947000-7f5109b46000 ---p 0003f000 08:08 1835059                    /lib/x86_64-linux-gnu/libsepol.so.1
7f5109b46000-7f5109b47000 r--p 0003e000 08:08 1835059                    /lib/x86_64-linux-gnu/libsepol.so.1
7f5109b47000-7f5109b48000 rw-p 0003f000 08:08 1835059                    /lib/x86_64-linux-gnu/libsepol.so.1
7f5109b48000-7f5109b68000 r-xp 00000000 08:08 1838390                    /lib/x86_64-linux-gnu/ld-2.13.so
7f5109d4a000-7f5109d4e000 rw-p 00000000 00:00 0 
7f5109d65000-7f5109d67000 rw-p 00000000 00:00 0 
7f5109d67000-7f5109d68000 r--p 0001f000 08:08 1838390                    /lib/x86_64-linux-gnu/ld-2.13.so
7f5109d68000-7f5109d69000 rw-p 00020000 08:08 1838390                    /lib/x86_64-linux-gnu/ld-2.13.so
7f5109d69000-7f5109d6a000 rw-p 00000000 00:00 0 
7fffc8fe9000-7fffc900a000 rw-p 00000000 00:00 0                          [stack]
7fffc9191000-7fffc9192000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
View Code

mem为进程虚拟内存

mounts为进程的安装点

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ sudo cat /proc/1/mounts
rootfs / rootfs rw 0 0
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=10240k,nr_inodes=958260,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=767824k,mode=755 0 0
/dev/disk/by-uuid/c2a06636-4fcf-4701-8f46-6415015254a0 / ext4 rw,relatime,errors=remount-ro,user_xattr,barrier=1,data=ordered 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
tmpfs /run/shm tmpfs rw,nosuid,nodev,noexec,relatime,size=1535640k 0 0
/dev/sda7 /boot ext4 rw,relatime,user_xattr,barrier=1,stripe=4,data=ordered 0 0
/dev/sda5 /home ext4 rw,relatime,user_xattr,barrier=1,data=ordered 0 0
/dev/sda6 /opt ext4 rw,relatime,user_xattr,barrier=1,data=ordered 0 0
rpc_pipefs /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
View Code

root为指向根目录的符号链接

status记录着进程的ID、内存使用情况、信号等信息。

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ sudo cat /proc/1/status 
Name:    init
State:    S (sleeping)
Tgid:    1
Pid:    1
PPid:    0
TracerPid:    0
Uid:    0    0    0    0
Gid:    0    0    0    0
FDSize:    64
Groups:    
VmPeak:       10652 kB
VmSize:       10648 kB
VmLck:           0 kB
VmPin:           0 kB
VmHWM:         788 kB
VmRSS:         788 kB
VmData:         188 kB
VmStk:         136 kB
VmExe:          36 kB
VmLib:        2052 kB
VmPTE:          44 kB
VmSwap:           0 kB
Threads:    1
SigQ:    0/59891
SigPnd:    0000000000000000
ShdPnd:    0000000000000000
SigBlk:    0000000000000000
SigIgn:    fffffffe57f0d8fc
SigCgt:    00000000280b2603
CapInh:    0000000000000000
CapPrm:    ffffffffffffffff
CapEff:    ffffffffffffffff
CapBnd:    ffffffffffffffff
Cpus_allowed:    f
Cpus_allowed_list:    0-3
Mems_allowed:    00000000,00000001
Mems_allowed_list:    0
voluntary_ctxt_switches:    7947
nonvoluntary_ctxt_switches:    33
View Code

task下存放着该进程的每个线程的子目录(记录线程的信息)

lancelot@debian:~/Code/tlpi$ sudo ls -l /proc/1/task
总用量 0
dr-xr-xr-x 6 root root 0  4月 28 23:07 1

子目录的信息:

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ sudo ls -l /proc/1/task/1
总用量 0
dr-xr-xr-x 2 root root 0  4月 28 23:08 attr
-r-------- 1 root root 0  4月 28 23:08 auxv
-r--r--r-- 1 root root 0  4月 28 23:08 cgroup
--w------- 1 root root 0  4月 28 23:08 clear_refs
-r--r--r-- 1 root root 0  4月 28 23:08 cmdline
-rw-r--r-- 1 root root 0  4月 28 23:08 comm
-r--r--r-- 1 root root 0  4月 28 23:08 cpuset
lrwxrwxrwx 1 root root 0  4月 28 23:08 cwd -> /
-r-------- 1 root root 0  4月 28 23:08 environ
lrwxrwxrwx 1 root root 0  4月 28 23:08 exe -> /sbin/init
dr-x------ 2 root root 0  4月 28 23:08 fd
dr-x------ 2 root root 0  4月 28 23:08 fdinfo
-r-------- 1 root root 0  4月 28 23:08 io
-r--r--r-- 1 root root 0  4月 28 23:08 limits
-rw-r--r-- 1 root root 0  4月 28 23:08 loginuid
-r--r--r-- 1 root root 0  4月 28 23:08 maps
-rw------- 1 root root 0  4月 28 23:08 mem
-r--r--r-- 1 root root 0  4月 28 23:08 mountinfo
-r--r--r-- 1 root root 0  4月 28 23:08 mounts
dr-x--x--x 2 root root 0  4月 28 23:08 ns
-r--r--r-- 1 root root 0  4月 28 23:08 numa_maps
-rw-r--r-- 1 root root 0  4月 28 23:08 oom_adj
-r--r--r-- 1 root root 0  4月 28 23:08 oom_score
-rw-r--r-- 1 root root 0  4月 28 23:08 oom_score_adj
-r--r--r-- 1 root root 0  4月 28 23:08 pagemap
-r--r--r-- 1 root root 0  4月 28 23:08 personality
lrwxrwxrwx 1 root root 0  4月 28 23:08 root -> /
-rw-r--r-- 1 root root 0  4月 28 23:08 sched
-r--r--r-- 1 root root 0  4月 28 23:08 sessionid
-r--r--r-- 1 root root 0  4月 28 23:08 smaps
-r--r--r-- 1 root root 0  4月 28 23:08 stack
-r--r--r-- 1 root root 0  4月 28 23:08 stat
-r--r--r-- 1 root root 0  4月 28 23:08 statm
-r--r--r-- 1 root root 0  4月 28 23:08 status
-r--r--r-- 1 root root 0  4月 28 23:08 syscall
-r--r--r-- 1 root root 0  4月 28 23:08 wchan
View Code

通过访问/proc文件可以获取很多有用的信息。

 

练习:

12-1.编写一个程序,以用户名作为命令行参数,列表显示该用户下所有正在运行的进程ID和命令名。通过分析系统中/proc/PID/status文件的Name和UID各行信息,可以思想此功能。

bubuko.com,布布扣
 1 /*
 2  * =====================================================================================
 3  *
 4  *       Filename:  12.1.c
 5  *
 6  *    Description:  
 7  *
 8  *        Version:  1.0
 9  *        Created:  2014年05月01日 21时03分09秒
10  *       Revision:  none
11  *       Compiler:  gcc
12  *
13  *         Author:  alan (), alan19920626@gmail.com
14  *   Organization:  
15  *
16  * =====================================================================================
17  */
18 
19 #include <fcntl.h>
20 #include <dirent.h>
21 #include <pwd.h>
22 #include <ctype.h>
23 #include <sys/stat.h>
24 #include "tlpi_hdr.h"
25 #include "ugid_functions.h"
26 
27 #define MAX_LINE 1000
28 
29 int main(int argc, char *argv[]){
30     DIR *dirp;
31     struct dirent *dp;
32     int fd;
33     FILE *fp;
34     char path[MAX_LINE], cmd[MAX_LINE], line[MAX_LINE];
35     char *p, *q;
36     int numRead;
37     uid_t id, p_uid;
38     pid_t pid;
39 
40     if(argc != 2 || strcmp(argv[1], "--help") == 0)
41         usageErr("%s username");
42 
43     id = userIdFromName(argv[1]);        //获取用户名对应的UID
44     if(id == -1)
45         errExit("userIdFromName");
46 
47     dirp = opendir("/proc");            //打开/proc文件
48     if(dirp == NULL)
49         errExit("opendir");
50 
51     printf("Uid: %ld\n", id);
52     while((dp = readdir(dirp)) != NULL){
53         if(dp->d_type != DT_DIR || !isdigit((unsigned char) dp->d_name[0]))     //跳过类型文件类型不是目录和文件名不是数字的
54             continue;
55 
56         snprintf(path, MAX_LINE, "/proc/%s/status", dp->d_name);                
57         fp = fopen(path, "r");                                                 //打开进程对应的status文件的文件流
58         p_uid = 0;
59         while(fgets(line, MAX_LINE, fp) != NULL){                              //逐行获取信息
60             if(strncmp(line, "Name:", 5) == 0){                                //当字符串的开始包含Name:为进程名
61                 for(p = line + 5; *p != \0 && isspace((unsigned char)*p); ++p)
62                     ;
63                 for(q = p; *q != \0 && isgraph((unsigned char)*q); ++q)
64                     ;
65                 strncpy(cmd, p, (int)(q-p));
66                 cmd[(int)(q-p)] = \0;
67             }
68 
69             if(strncmp(line, "Pid:", 4) == 0){                                 //获取进程号
70                 pid = strtol(line+4, NULL, 10);
71             }
72 
73             if(strncmp(line, "Uid:", 4) == 0){                                 //获取有效用户号
74                 p_uid = strtol(line+4, NULL, 10);
75                 break;
76             }
77         }
78         fclose(fp);
79         if(p_uid == id)
80             printf("%5ld %s\n", p_uid, cmd);
81     }
82     exit(EXIT_SUCCESS);
83 }
bubuko.com,布布扣

刚开始打算用open和read来读取status的信息,但是不知道为什么只能测试是否包含"Name:",不能获取进程号和有效用户号,所以修改成通过fopen和fgets来读取status文件的信息。感觉这个练习比之前的难度大了,综合了比较多的系统调用和库函数,感觉还是用得不熟练。除了把书上的练习做了,还要找些东东来搞搞,练习一下。

测试结果:

bubuko.com,布布扣
lancelot@debian:~/Code/tlpi$ ./12.1 lancelot
Uid: 1000
 1000 bash
 1000 startx
 1000 xinit
 1000 Xorg
 1000 ck-launch-sessi
 1000 ssh-agent
 1000 x-session-manag
 1000 dbus-launch
 1000 dbus-daemon
 1000 ibus-daemon
 1000 ibus-gconf
 1000 python
 1000 ibus-x11
 1000 gvfsd
 1000 gconfd-2
 1000 ibus-engine-sun
 1000 gnome-settings-
 1000 gnome-keyring-d
 1000 pulseaudio
 1000 gvfs-gdu-volume
 1000 gvfs-afc-volume
 1000 gvfs-gphoto2-vo
 1000 gnome-shell
 1000 gsd-printer
 1000 nm-applet
 1000 tracker-miner-f
 1000 gdu-notificatio
 1000 nautilus
 1000 tracker-store
 1000 gnome-screensav
 1000 evolution-alarm
 1000 gvfsd-trash
 1000 evolution-calen
 1000 gvfsd-burn
 1000 evolution-addre
 1000 goa-daemon
 1000 gvfsd-metadata
 1000 gnome-shell-cal
 1000 mission-control
 1000 chrome
 1000 chrome
 1000 chrome-sandbox
 1000 chrome
 1000 nacl_helper
 1000 chrome
 1000 chrome
 1000 chrome
 1000 chrome
 1000 chrome
 1000 chrome
 1000 chrome
 1000 chrome
 1000 chrome
 1000 chrome
 1000 dconf-service
 1000 guake
 1000 gnome-pty-helpe
 1000 bash
 1000 gvim
 1000 gedit
 1000 gnome-pty-helpe
 1000 bash
 1000 bash
 1000 12.1
bubuko.com,布布扣

 

-----------------------还有两题。。。又来挖坑了-------感觉对这些系统调用和库函数的熟练程度严重不够

-----------------------还要继续努力啊!!!!加油!!!!!---------------------------------------------------

-----------------------要把学习记录的进度加快,下册也快看了一个月,还有一大部分没看,而且代码也很多没实现

《Linux/Unix系统编程手册》读书笔记7 (/proc文件的简介和运用),布布扣,bubuko.com

《Linux/Unix系统编程手册》读书笔记7 (/proc文件的简介和运用)

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/alan-forever/p/3698192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!