#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <limits.h>
int main(void)
{
DIR *dir;
struct dirent *d;
int pid,i=0;
char* s;
dir=opendir("/proc");
while((d=readdir(dir))!=NULL)
{
char exe[PATH_MAX+1];
char path[PATH_MAX+1];
int len;
if((pid=atoi(d->d_name))==0)
{
continue;
}
snprintf(exe,sizeof(exe),"/proc/%s/exe",d->d_name);
if((len=readlink(exe,path,PATH_MAX))<0)
{
continue;
}
path[len]='\0';
s=strrchr(path,'/');
// printf("%s %s %s\n",exe,s,path);
s++;
//printf("%s\n",s);
if(!strncmp("tiwangtest",s,strlen("tiwangtest")))
{
if(s[strlen("tiwangtest")]==' '||s[strlen("tiwangtest")]=='\0')
{
closedir(dir);
printf("%d\n",pid);
}
}
//sleep(1);
}
while(1)
{}
return 0;
}
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5COxgDOxIzNyY2NjRjZjhjMyYzX2ETMwADMwEzLcNDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
在終端使用ps -a指令檢視