2012/07/13

避風港

真的有避風港,還在淡水...


檢視較大的地圖

2012/07/06

Get cpu cores number in linux

Output:
cpu count: 4

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main() {
  5.     int num = 1;
  6.     char key[512], deli[512], value[512];
  7.     FILE* fd = fopen("/proc/cpuinfo", "r");
  8.     if(fd != 0)
  9.     {
  10.         while(!feof(fd))
  11.         {
  12.             memset(key, 0, sizeof(key));
  13.             memset(value, 0, sizeof(value));
  14.             fscanf(fd, "%s%s%[^\n]", key, deli, value);
  15.             if(strcmp(key, "processor") == 0)
  16.             {
  17.                 sscanf(value, "%d", &num);
  18.                 num++; /* processor index is 0, 1.... should add 1 */
  19.             }
  20.         }
  21.         fclose(fd);
  22.     }
  23.     printf("cpu count: %d\n", num);
  24.     return 0;
  25. }