Isolate processor to run only my program

Has how do I isolate the processor to run only my program in C?

Is another didactic and academic question passed by my A. I. Teacher

He asked us to perform millions of operations of sum of real numbers, and take the time it took the processor to perform them. Then divide, and find the mean that the processor takes to perform 1 sum operation.

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

How do I completely isolate the processor, and it executes only my code? He left it free to do in any language, but I believe that C is the most viable, it can be Assembly too.

If it is possible for Windows to stop all processes to make only that calculation, the OS will not give Dick? And how do I do that?

Author: Maniero, 2017-09-14

3 answers

In relation to GNU/Linux operating systems, there are ways to do this yes, 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 direct to the newly created dedicated cpuset.

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

See More man cpuset.

 3
Author: gfleck, 2017-09-14 13:30:58

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

So it can be done in any language you think best, the only question is to call the API of the OS that defines the scheduling of process.

 3
Author: Maniero, 2017-09-14 00:27:34

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

Thus, although vc does not have exclusive CPU usage, with these statistics, vc can account for CPU usage, as if it were entirely of the program.

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

 3
Author: , 2017-09-14 01:13:20