#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <map>
#include <memory>
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "pthread.h"
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)
#define BUF_SIZE 1024
void getPidByName(char* task_name)
{
DIR *dir;
struct dirent *ptr;
FILE *fp;
char filepath[50];//大小随意,能装下cmdline文件的路径即可
char cur_task_name[50];//大小随意,能装下要识别的命令行文本即可
char buf[BUF_SIZE];
dir = opendir("/proc"); //打开路径
if (NULL != dir)
{
while ((ptr = readdir(dir)) != NULL) //循环读取路径下的每一个文件/文件夹
{
//如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0))
continue;
if (DT_DIR != ptr->d_type)
continue;
sprintf(filepath, "/proc/%s/status", ptr->d_name);//生成要读取的文件的路径
fp = fopen(filepath, "r");//打开文件
if (NULL != fp)
{
if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
fclose(fp);
continue;
}
sscanf(buf, "%*s %s", cur_task_name);
//如果文件内容满足要求则打印路径的名字(即进程的PID)
if (!strcmp(task_name, cur_task_name))
printf("PID: %s\n", ptr->d_name);
fclose(fp);
}
}
closedir(dir);//关闭路径
}
}
/*
*void getNameByPid(pid_t pid, char *task_name) {
* char proc_pid_path[BUF_SIZE];
* char buf[BUF_SIZE];
*
* sprintf(proc_pid_path, "/proc/%d/status", pid);
* printf("/proc/%d/status", pid);
* FILE* fp = fopen(proc_pid_path, "r");
* if(NULL != fp){
* if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
* fclose(fp);
* }
* fclose(fp);
* sscanf(buf, "%*s %s", task_name);
* }
*}
*/
char* getNameByPid(pid_t pid,pid_t tid) {
char proc_pid_path[BUF_SIZE];
char buf[BUF_SIZE];
char *task_name = new char ;
//char task_name[50];
sprintf(proc_pid_path, "/proc/%d/task/%d/status haha yaya ooa ", pid,tid);
sscanf(proc_pid_path, "/proc/%d/task/%d/status haha yaya ooa ", pid,tid);
printf(proc_pid_path);
printf("pmain : /proc/%d/task/%d/status", pid,tid);
FILE* fp = fopen(proc_pid_path, "r");
if(NULL != fp){
if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
fclose(fp);
}
fclose(fp);
sscanf(buf, "%*s %s", task_name);
//printf("pmain : /proc/%d/task/%d/status |task_name:%s|", pid,tid,task_name);
return task_name;
}
else{
//printf("pmain : +++++++++++++++++++++++++++++++++++faild! +++++++++++++++++++++++++++++++++++++");
//delete task_name;
return (char*)"name-unknown";
}
}
int main(int argc, char** argv)
{
//char task_name[50];
//pid_t pid = getpid();
//printf("pid of this process:%d\n", pid);
//getNameByPid(pid, task_name);
//printf("task name is %s\n", task_name);
//getPidByName(task_name);
char * NAME = getNameByPid( (int)getpid(), (int)gettid() );
printf("+++++++++ NAME:%s",NAME);
//sleep(15);
return 0;
}
//%[^&]
/*
*int main()
*{
* char buf1[512] = {0};
* char buf2[512] = {0};
*
* sscanf(" 123456 i mk dhhhd kkkk ","%[^\n]%s", buf1);
* sscanf(" 123456 I mk dhhhd kkkk ","%*s%5[^&]%s", buf2);
*
* printf("%s\n", buf1);
* printf("%s\n", buf2);
* return 0;
*}
*
*/