I’m trying to write a script which could output the id of the core that executes the script.
I have already found a system call getCPU which can achieve my goal.
However, I don’t know how to call this system call and get it work in a bash script.
Any help would be grateful!
Dennis
Unless you have taken action to pin a process to a particular core, the scheduler will move it around as it sees fit. So each time the process is scheduled it is likely to change.
On linux with a kernel >= 2.2.8 you can find the core that the process was last run on by looking at the
/proc/<PID>/statfile. Eee the PROC(5) man page for more information. A simple awk script can be used to get the relevant information e.g. if your shell was PID 2338 thenwill print the CPU number that your shell last ran on. If you run the awk script several times on a multi-core/cpu system you should see the that the process moves from core to core.
If you want to set the affinity your process to a particular core (or group of cores) then you can use the taskset command.
will set the affinity of the process to core 0,
-p 3 2338will set the affinity to cores 0 and 1 etc.