- 背景
- 解決
- 剛開始以爲有難度,需要遍歷整個proc/***以及/proc/***/task/***;因爲拿到的是tid而非pid,該tid是由某個pid通過pthread_create或fork、clone出來的,只有找到對應的pid,才能從/proc/***/status下獲取進程名;
- 但是linux下,tid和pid就是一個東西,內核中是完全不區分這兩個概念(即線程和進程);體現在/proc下就是,不論tid還是pid,都可以在根目錄下找到;因此直接讀取/proc/tid/status文件即可獲取進程名
- 當然,如果tid是由某個pid創建出來的,在/proc/pid/task是可以找到對應tid的,這也就是pstree工具的實現原理;
- 從這也可以看出,fork以及pthread_create其實最終到內核中,使用的都是一條創建路徑;由此也可以看出,在linux中沒有所謂的進程和線程之分,內核中,都是進程,就是一個task_struct結構體,重而進行執行調度
- 源碼
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);
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);
}
}
void getPidByName(pid_t *pid, char *task_name)
{
DIR *dir;
struct dirent *ptr;
FILE *fp;
char filepath[50];
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);
if (!strcmp(task_name, cur_task_name)){
sscanf(ptr->d_name, "%d", pid);
}
fclose(fp);
}
}
closedir(dir);
}
}