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:

# Set the processing unit
taskset -c 0 -p 45678

# run python script
python main.py

You can't call taskset to record the settings to be used for a future process. This mode of calling taskset is only for currently-running processes.

To pin a new process to a core, call taskset in its direct mode. You call taskset and tell it what program to execute.

taskset -c 0 python main.py

The way it works is:

  1. The shell (or other calling program) creates a new process (fork) to execute (execve) taskset -c 0 python main.py.
  2. The process running taskset applies the core settings to itself.
  3. The process executes (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. with export), CPU niceness with nice, redirections (which is typically done through shell syntax rather than with a separate utility), etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.