I want to limit the processing capability to only a single core in my machine, so I found out taskset
can help to set a single core, say core 0, as the following (Courtesy to this answer):
taskset -c 0 -p 45678
The problem is that how can I determine the process_id pid here when my program is not running yet? Do we just set an arbitrary process id to be picked up by Linux, in this case 45678
? If it is so, is it possible to do that in a shell script as the following:
#!/bin/sh
# Set the processing unit
taskset -c 0 -p 45678
# run python script
python main.py
Best Answer
You can't call
taskset
to record the settings to be used for a future process. This mode of callingtaskset
is only for currently-running processes.To pin a new process to a core, call
taskset
in its direct mode. You calltaskset
and tell it what program to execute.The way it works is:
fork
) to execute (execve
)taskset -c 0 python main.py
.taskset
applies the core settings to itself.execve
again)python main.py
in the same process.This is the way other per-process settings are made: environment variables when you set them for a single process (e.g. with
env
) rather than for a shell session (e.g. withexport
), CPU niceness withnice
, redirections (which is typically done through shell syntax rather than with a separate utility), etc.