#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h> #define PROC "/proc/" typedef int Myfunc(int pid) ;
static Myfunc myfunc; #define P() printf("%s:%d\n",__FUNCTION__,__LINE__)
static int dopath(Myfunc *func); int main(int argc,char *argv[])
{
return dopath(myfunc);
}
static int
dopath(Myfunc *func)
{
//struct stat statbuf;
struct dirent *dirp;
DIR *dp;
int ret;
char *ptr; //if()
if((dp = opendir(PROC)) == NULL)
{
perror("open");
return -;
} while((dirp = readdir(dp)) != NULL)
{
if(strcmp(dirp->d_name,".") == ||strcmp(dirp->d_name,"..")==)
{
continue;
}
int pid = atoi(dirp->d_name);
//printf("pid =%d\n",pid);
if(pid == )
{
continue;
}
int ret = myfunc(pid);
if(ret != )
{
printf("parse error!\n");
return -;
}
}
if(closedir(dp) < )
{
printf("cant't close directory /proc");
return -;
}
return ;
}
static int
myfunc(int pid)
{
char path[]={};
const char *name="Name:";
const char *state = "State:";
char pname[] = {}; sprintf(path,"/proc/%d/status",pid);
if(access(path,F_OK)!=)
{
printf("%s not exist.\n",path);
return -;
} int fd = open(path,O_RDONLY);
if(fd<)
{
perror("open");
return -;
} char buf[]={};
int length = read(fd,buf,sizeof(buf));
if(length<)
{
perror("read");
close(fd);
return -;
}
char *p0 =strstr(buf,name);
char *p1 =strstr(buf,state);
memcpy(pname,p0+strlen(name)+,p1-p0-strlen(name)-);
printf("pname:%s\n",pname); close(fd); return ;
}

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

java版本:

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader; /**
* @author jevan (jevan.cnblogs.com)
* @version 1.0 at 2013-11-27
*
*/
public class PrintPID { /**
* @param args
*/
public static void main(String[] args) {
printPid();
} private static void printPid()
{ File proc = new File("/proc");
for(File f:proc.listFiles())
{
if(f.getName().equals(".")||f.getName().equals(".."))
continue; if(f.isDirectory())
{
int pid = toInt(f.getName());
if(pid == 0)
continue; File fname = new File("/proc/"+f.getName()+"/status"); if(!fname.exists())
continue; String line0 =read0Line("/proc/"+f.getName()+"/status"); String[] tmp = line0.split(":",2);
if(tmp==null||tmp.length!=2)
{
continue;
}
p("==>"+tmp[1]); }
}
} public static void p(String msg)
{
System.out.println(msg);
} public static String read0Line(String filePath)
{
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(fis == null)
{
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
try {
String line0 = reader.readLine();
//p(line0);
return line0;
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
fis.close();
} catch (Exception e2) {
}
}
return null;
} public static int toInt(String str)
{
int ret = 0;
try{
ret = Integer.valueOf(str);
}catch(Exception e)
{
ret = 0;
}
return ret;
} }

执行:

 [jevan@media pname]$ time java PrintPID

 real    0m0.150s
user 0m0.098s
sys 0m0.056s
[jevan@media pname]$ time ./dir real 0m0.019s
user 0m0.000s
sys 0m0.005s
[jevan@media pname]$
04-17 15:36