Isolate processor to run only my program

2

How can I isolate the processor to run only my program in C?

It is more a didactic and academic question passed by my teacher of AI.

He asked us to run millions of real-number sum operations, and take the time the processor took to run them. Then divide, and find the media that the processor takes to perform 1 sum operation.

And do this with the other subtraction, multiplication, and comparison operations.

How do I completely isolate the processor, and does it just run my code? He left free to do in any language, but I believe C is the most viable, it can be Assembly as well.

If it is possible for Windows to stop all processes to do just that calculation, the OS will not stick? And how do I do this?

    
asked by anonymous 14.09.2017 / 02:19

3 answers

3

Regarding GNU / Linux operating systems, there are ways to do this, basically you will have to use cpuset .

Example:

$ mkdir /cpuset 
$ mount -t cpuset none /cpuset/
$ cd /cpuset

$ mkdir sys                               # cria um cpuset para o sistema operacional
$ /bin/echo 0-2 > sys/cpuset.cpus         # atribui os cpu cores 0-2
$ /bin/echo 1 > sys/cpuset.cpu_exclusive
$ /bin/echo 0 > sys/cpuset.mems     

$ mkdir rt                                 # cria cpuset para o teu processo
$ /bin/echo 3 > rt/cpuset.cpus             # atribui o cpu core 3

$ /bin/echo 1 > rt/cpuset.cpu_exclusive
$ /bin/echo 0 > rt/cpuset.mems
$ /bin/echo 0 > rt/cpuset.sched_load_balance
$ /bin/echo 1 > rt/cpuset.mem_hardwall

# direciona todos os processos do cpuset default para o cpuset sys
$ for T in 'cat tasks'; do echo "Moving " $T; /bin/echo $T > sys/tasks; done

After this configuration step, start your process (program) and go to the dedicated cpuset newly created.

$ /bin/echo $PID > /cpuset/rt/tasks

See more man cpuset .

    
14.09.2017 / 15:30
3

This is not possible on "normal" operating systems, the operating system is controlling this. What you can do is ask for high priority for the operating system, which will deliver as it wishes. You can determine affinity and with this try to make your process only run on one of the processors, but this is discretionary of the OS.

So you can do whatever language you think is best, the only issue is to call the OS API that defines the scheduling of processes.

    
14.09.2017 / 02:27
3

This all depends on your OS. In modern OS it has APIs that let you get statistics of processor usage, by program. That is, how much CPU time was spent exclusively to run your program, how much time it spent running inside the kernel , how much time it spent total, and so on.

So, although you do not have CPU usage alone, with these statistics, you can count CPU usage as if it were entirely from the program.

In the UNIX world, you can use getrusage . In the case of Windows, you can use GetProcessTimes

    
14.09.2017 / 02:58