獲取進(jìn)程的線程數(shù)目
int GetCurThreadsNum(pid_t pid) {
char aszPath[1024] = {0};
snprintf(aszPath, sizeof(aszPath), "/proc/%d/status", pid);
FILE *pstFile = fopen(aszPath, "r");
if (pstFile == NULL) {
return -1;
}
char aszLine[256];
char* pcSearchStr = "Threads:";
int iNumThreads = 0;
while (fgets(aszLine, sizeof(aszLine), pstFile)) {
if (strstr(aszLine, pcSearchStr) != NULL) {
sscanf(aszLine, "Threads: %d", &iNumThreads);
break;
}
}
fclose(pstFile);
return iNumThreads;
}