Output:
cpu count: 4
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main() {
- int num = 1;
- char key[512], deli[512], value[512];
- FILE* fd = fopen("/proc/cpuinfo", "r");
- if(fd != 0)
- {
- while(!feof(fd))
- {
- memset(key, 0, sizeof(key));
- memset(value, 0, sizeof(value));
- fscanf(fd, "%s%s%[^\n]", key, deli, value);
- if(strcmp(key, "processor") == 0)
- {
- sscanf(value, "%d", &num);
- num++; /* processor index is 0, 1.... should add 1 */
- }
- }
- fclose(fd);
- }
- return 0;
- }
沒有留言:
張貼留言