日期:2014-05-16  浏览次数:21052 次

linux下如何获取另一进程ID(编程实现)
我需要在一个程序中获取另一个进程PID,应该如何实现?
我知道可以用命令:ps -ax | grep "进程名" | awk '{print $1}'
不过需要用C语言实现,应该怎么做?
请大家指教,谢谢!

------解决方案--------------------
system("ps -ax ¦ grep "进程名" ¦ awk '{print $1}' ")
------解决方案--------------------
探讨
system("ps -ax ¦ grep "进程名" ¦ awk '{print $1}' ")

------解决方案--------------------
C/C++ code
 #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define MAXLINE 1024
int isdir(char *path)
{
 struct stat buf;
 if(lstat(path,&buf)<0)
  {
//       printf("lstat err for %s \n",path);
       return 0;
   }
  if(S_ISDIR(buf.st_mode)||S_ISLNK(buf.st_mode))
  {
    return 1;
  }
   return 0;
}
char * getfile(char *path)
{
 static char filename[MAXLINE];
 char *tmp1,*tmp2,filepath[MAXLINE];
 if(path==NULL)return NULL;
 memset(filepath,0,sizeof(filepath));
 strcpy(filepath,path);
 tmp2=tmp1=strtok(filepath,"/");
 while((tmp1=strtok(NULL,"/")))
 {
  tmp2=tmp1;
 }
memset(filename,0,sizeof(filename));


if(tmp2)
{
strcpy(filename,tmp2);
tmp1=filename;
while(*tmp1&&*tmp1++!=' ');
*(tmp1)='\0';
}
else return NULL;
return filename;
}
char* readstatus(char *filename)
{
FILE *file;
static char buf[MAXLINE];
file=fopen(filename,"r");
if(!file)return NULL;
while(fgets(buf,sizeof(buf),file))
{
  if(strncmp(buf,"State",5)==0)
        {
                printf("%s\n",buf);
                break;
        }
}
fclose(file);
}
int main(int argc,char ** argv)
{
char buf1[MAXLINE],buf2[MAXLINE];
char path1[MAXLINE],path2[MAXLINE],*ptr,*str;
DIR *db,*directory;
struct dirent *p;
if(argc!=2)
{
printf("input file name .\nusage %s : filename \n",argv[0]);
return 0;
}

db=opendir("/proc/");
if(db==NULL)return -1;
while ((p=readdir(db)))
  {
    if((strcmp(p->d_name,".")==0)||(strcmp(p->d_name,"..")==0))
       continue;
    else
     {
        memset(buf1,0,sizeof(buf1));
        sprintf(buf1,"/proc/%s",p->d_name);
        if(isdir(buf1))
        {
          memset(buf2,0,sizeof(buf2));
          sprintf(buf2,"%s/exe",buf1);
          if(access(buf2,F_OK)==0)
          {
                memset(path1,0,sizeof(path1));
                if(readlink(buf2,path1,sizeof(path1))==-1)
                continue;
                ptr=getfile(path1);
                if(strcmp(ptr,argv[1])==0)
                {
                       memset(path2,0,sizeof(path2));
                       sprintf(path2,"%s/status",buf1);
                       ptr=readstatus(path2);
                }
          }
        }
     }
 }
closedir(db);
}

------解决方案--------------------
把结果写进临时文件,再读进来吧

不知道有没有什么更好的办法

ps好像也是循环去遍历/proc/目录下的进程的

stat64("/proc/5", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
open("/proc/5/stat", O_RDONLY) = 7
read(7, "5 (kswapd) S 1 1 1 0 -1 2112 0 0"..., 1023) = 131
close(7) = 0
open("/proc/5/status", O_RDONLY) = 7
read(7, "Name:\tkswapd\nState:\tS (sleeping)"..., 1023) = 290
close(7) = 0
open("/proc/5/cmdline", O_RDONLY) = 7
read(7, "", 2047) = 0
close(7) = 0
open("/proc/5/environ", O_RDONLY) = 7
read(7, "", 2047) = 0
close(7) = 0