c语言获取当前程序运行的cpuid
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <syscall.h> #define ULONG unsigned long #define CHAR char #define VOID void #define gettid() syscall(SYS_gettid) ULONG drvgetprocessorid(VOID) { ULONG ultaskcpu=0; ULONG uloopi = 0; char szFilename[50]; char cflag=0; CHAR szBuf[512]; FILE *pstFp=NULL; memset(szfilename,0,(ULONG)50); snprintf(szfilename,sizeof(szfilename),"/proc/%d/task/%d/stat",getpid(),gettid()); pstfp=fopen(szfilename,"r"); if(null==pstfp) { return 30; } while(0 ==feof(pstfp)) { if(uloopi == 38) { fgets(szbuf,sizeof(szbuf),pstfp); sscanf(szbuf,"%lu",&ultaskcpu); break; } cflag=fgetc(pstfp); if(‘ ’ == cflag) { uloopi++; } } fclose(pstfp); return ultaskcpu; }