-1

Can anybody help me to read the processor manufacturer ID in a Qt application using the C programming language.

Here is the code I tried :

#include <stdio.h>
int main()
{
    system("sudo dmidecode");
}

I can't use this because :

  • It is not working in QT application
  • I want to store this information inside a file in qt application.

Can someone suggest me any other way so to store processor information in a side file using a Qt application ?

Gagandeep
  • 11
  • 6
  • 1
    Possible duplicate of [Getting the machine serial number and CPU ID using C/C++ in Linux](https://stackoverflow.com/questions/6491566/getting-the-machine-serial-number-and-cpu-id-using-c-c-in-linux) – klutt May 25 '19 at 10:05
  • Using QT are you sure it is not in C++ ?. However the way is the same in C and C++ – bruno May 25 '19 at 10:13
  • yes, but In qt system() function is not working, so can you help me to implement same thing using qt application – Gagandeep May 25 '19 at 10:33
  • @Gagandeep my answer works for C and C++ including under Qt, the first proposal in it just uses standard C functions – bruno May 25 '19 at 11:03

2 Answers2

0

read the file /proc/cpuinfo and look at the line(s) containing "model name" (at least for a Debian / CentOS / OpenSuse, check for your Linux)

Example on my raspberrypi :

pi@raspberrypi:/tmp $ cat /proc/cpuinfo 
processor   : 0
model name  : ARMv7 Processor rev 4 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part    : 0xd03
CPU revision    : 4

processor   : 1
model name  : ARMv7 Processor rev 4 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part    : 0xd03
CPU revision    : 4

processor   : 2
model name  : ARMv7 Processor rev 4 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part    : 0xd03
CPU revision    : 4

processor   : 3
model name  : ARMv7 Processor rev 4 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part    : 0xd03
CPU revision    : 4

Hardware    : BCM2835
Revision    : a020d3
Serial      : 000000003a871273

Example under CentOS :

[bruno@localhost ~]$ cat /proc/cpuinfo 
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 42
model name  : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
stepping    : 7
cpu MHz     : 3409.658
cache size  : 8192 KB
physical id : 0
siblings    : 8
core id     : 0
cpu cores   : 8
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes xsave avx hypervisor lahf_lm
bogomips    : 6819.31
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 42
model name  : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
...

For instance :

#include <stdio.h>
#include <string.h>

int main()
{
  FILE * fp = fopen("/proc/cpuinfo", "r");

  if (fp == 0)
    fprintf(stderr, "cannot open /proc/cpuinfo !");
  else {
    char line[256];

    while (fgets(line, sizeof(line), fp) != NULL) {
      if (strncmp(line, "model name", 10) == 0) {
        char * model = strchr(line, ':');

        if (model != NULL) {
          model += (model[1] == ' ') ? 2 : 1;
          fputs(model, stdout);
        }

        break;
      }
    }
    fclose(fp);
  }

  return 0;
}

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
ARMv7 Processor rev 4 (v7l)

Or if you are lazy and do not want to parse by yourself :

#include <stdio.h>

int main()
{
  FILE * fp = popen("grep model /proc/cpuinfo | head | awk -F: '{print $2}'", "r");

  if (fp != NULL) {
    char line[256];

    if (fgets(line, sizeof(line), fp)) {
      char * model = (*line == ' ') ? line + 1 : line;

      fputs(model, stdout);
    }

    pclose(fp);
  }

  return 0;
}

Note in both cases the model contains a newline, these solutions are compatible with any CPU.

bruno
  • 32,421
  • 7
  • 25
  • 37
  • user:~$ gcc -Wall -Wextra dmidecode.c -o dmidecode user:~$ ./dmidecode 60 (This o/p am getting can you tell me please what is 60?) – Gagandeep May 25 '19 at 11:28
  • I want processor Manufacture ID number, which we can get by executing the command sudo dmidecode in linux. can you help me for the same – Gagandeep May 25 '19 at 11:31
  • @Gagandeep where do you got dmidecode.c ? On my raspberrypi the standard command _dmidecode_ does not return infos, this is why I did not speak about it and I read /proc/cpuinfo. did you look at the duplicate, may be some answer are compatible with your case (some answer supposes intel cpu) – bruno May 25 '19 at 11:45
  • yes, you code is working but I want processor manufacture id. can you help me to read the processor manufacture id – Gagandeep May 25 '19 at 11:49
  • @Gagandeep why do you not like the model name ? This is is a *alone* way compatible with all the processors. Are you focused on only some processors ? If you do not give more details it is impossible to help you more ... – bruno May 25 '19 at 12:06
  • because processor manufacture ID is unique. I want to run my qt application on specific system only. My qt application should not run on different system. so that is the reason I want to read the processor manufacture id. I appreciate if you have any alternative way to do the same. – Gagandeep May 25 '19 at 12:21
  • @Gagandeep you never answer to my questions, I cannot help you more in these conditions – bruno May 25 '19 at 12:26
  • #bruno Because model name could be common pc to pc. – Gagandeep May 25 '19 at 12:31
0

If you want to store this into a file, this is very easy.

You need to parse the output of /proc/cpuinfo the way you want and redirect the output to a file. You can do this with simple command line tools such as grep and cut (and many others).

Then you get a line like this.

system("cat /proc/cpuinfo | grep something > output_file");

There are other fancier ways to do this, such as libcpuid that you might want to look at.

SOKS
  • 190
  • 3
  • 11
  • It seems you have to learn _popen_ exists, to write in a file is a pity ;-) (I use it in my answer) – bruno May 25 '19 at 11:11
  • can you tell me the procedure to install the libcpuid library in ubuntu – Gagandeep May 25 '19 at 12:29
  • OP : The github page has everything you need to know to get it running. @bruno I do know popen exists, I just wanted to show him an answer based on his code. Your answer is definitely more elaborated than mine. Kudos! – SOKS May 25 '19 at 14:33