-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect-cpu.c
52 lines (48 loc) · 1.13 KB
/
detect-cpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* program that prints the type of CPU it is running on and the
* number of cores it has.the program is written in C and uses
* the POSIX sysconf() function to get the number of cores.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/sysinfo.h>
// declare and define the function to get type of CPU
// declare and define the function to get number of cores
int get_cpu_type(void)
{
FILE *cpuinfo;
char line[128];
char *cputype;
cpuinfo = fopen("/proc/cpuinfo", "r");
if (cpuinfo == NULL)
{
perror("fopen");
exit(EXIT_FAILURE);
}
while (fgets(line, 128, cpuinfo) != NULL)
{
if (strncmp(line, "model name", 10) == 0)
{
cputype = strchr(line, ':') + 2;
printf("CPU type: %s\n", cputype);
fclose(cpuinfo);
return 0;
}
}
fclose(cpuinfo);
return 0;
}
int get_cpu_cores(void)
{
int cores;
cores = sysconf(_SC_NPROCESSORS_ONLN);
printf("Number of cores: %d\n", cores);
return 0;
}
// main function
int main(void)
{
get_cpu_type();
get_cpu_cores();
return 0;
}