您的位置:首页 > 其它

打印pid,写着玩。

2013-12-12 12:08 204 查看
1 #include <stdio.h>
2 #include <string.h>
3 #include <dirent.h>
4 #include <limits.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9
10 #define PROC "/proc/"
11
12
13 typedef int Myfunc(int pid) ;
14 static Myfunc  myfunc;
15
16
17 #define P()     printf("%s:%d\n",__FUNCTION__,__LINE__)
18 static int dopath(Myfunc *func);
19
20 int main(int argc,char *argv[])
21 {
22     return dopath(myfunc);
23 }
24 static int
25 dopath(Myfunc *func)
26 {
27     //struct stat         statbuf;
28     struct dirent        *dirp;
29     DIR                    *dp;
30     int                 ret;
31     char                *ptr;
32
33     //if()
34     if((dp = opendir(PROC)) == NULL)
35     {
36         perror("open");
37         return -1;
38     }
39
40     while((dirp = readdir(dp)) != NULL)
41     {
42         if(strcmp(dirp->d_name,".") == 0 ||strcmp(dirp->d_name,"..")==0)
43         {
44             continue;
45         }
46         int pid = atoi(dirp->d_name);
47         //printf("pid =%d\n",pid);
48         if(pid == 0)
49         {
50             continue;
51         }
52         int ret = myfunc(pid);
53         if(ret != 0)
54         {
55             printf("parse error!\n");
56             return -1;
57         }
58     }
59     if(closedir(dp) < 0)
60     {
61         printf("cant't close directory /proc");
62         return -1;
63     }
64     return 0;
65 }
66 static int
67 myfunc(int pid)
68 {
69     char path[128]={0};
70     const char *name="Name:";
71     const char *state = "State:";
72     char pname[32] = {0};
73
74     sprintf(path,"/proc/%d/status",pid);
75     if(access(path,F_OK)!=0)
76     {
77         printf("%s not exist.\n",path);
78         return -1;
79     }
80
81     int fd = open(path,O_RDONLY);
82     if(fd<0)
83     {
84         perror("open");
85         return -1;
86     }
87
88     char buf[1024]={0};
89     int length = read(fd,buf,sizeof(buf));
90     if(length<0)
91     {
92         perror("read");
93         close(fd);
94         return -1;
95     }
96     char *p0 =strstr(buf,name);
97     char *p1 =strstr(buf,state);
98     memcpy(pname,p0+strlen(name)+1,p1-p0-strlen(name)-2);
99     printf("pname:%s\n",pname);
100
101
102
103     close(fd);
104
105     return 0;
106 }


写几行代码来打印pid对应的进程名。

java版本:

1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.Reader;
8
9 /**
10  * @author jevan (jevan.cnblogs.com)
11  * @version 1.0 at 2013-11-27
12  *
13  */
14 public class PrintPID {
15
16     /**
17      * @param args
18      */
19     public static void main(String[] args) {
20         printPid();
21     }
22
23     private static void printPid()
24     {
25
26         File proc = new File("/proc");
27         for(File f:proc.listFiles())
28         {
29             if(f.getName().equals(".")||f.getName().equals(".."))
30                 continue;
31
32             if(f.isDirectory())
33             {
34                 int pid = toInt(f.getName());
35                 if(pid == 0)
36                     continue;
37
38                 File fname = new File("/proc/"+f.getName()+"/status");
39
40                 if(!fname.exists())
41                     continue;
42
43
44                 String line0 =read0Line("/proc/"+f.getName()+"/status");
45
46                 String[] tmp = line0.split(":",2);
47                 if(tmp==null||tmp.length!=2)
48                 {
49                     continue;
50                 }
51                 p("==>"+tmp[1]);
52
53             }
54         }
55     }
56
57     public static void p(String msg)
58     {
59         System.out.println(msg);
60     }
61
62     public static String read0Line(String filePath)
63     {
64         FileInputStream fis = null;
65         try {
66             fis = new FileInputStream(filePath);
67         } catch (FileNotFoundException e) {
68             e.printStackTrace();
69         }
70         if(fis == null)
71         {
72             return null;
73         }
74         BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
75         try {
76             String line0 = reader.readLine();
77             //p(line0);
78             return line0;
79         } catch (IOException e) {
80             e.printStackTrace();
81         }finally{
82             try {
83                 reader.close();
84                 fis.close();
85             } catch (Exception e2) {
86             }
87         }
88         return null;
89     }
90
91     public static int toInt(String str)
92     {
93         int ret = 0;
94         try{
95             ret = Integer.valueOf(str);
96         }catch(Exception e)
97         {
98             ret = 0;
99         }
100         return ret;
101     }
102
103 }


执行:

1 [jevan@media pname]$ time java PrintPID
2
3 real    0m0.150s
4 user    0m0.098s
5 sys     0m0.056s
6 [jevan@media pname]$ time ./dir
7
8 real    0m0.019s
9 user    0m0.000s
10 sys     0m0.005s
11 [jevan@media pname]$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: