This question already has an answer here:

How to limit process to one cpu core ?

Something similar to ulimit or cpulimit would be nice. (Just to ensure: I do NOT want to limit percentage usage or time of execution. I want to force app (with all it's children, processes (threads)) to use one cpu core (or 'n' cpu cores)).

marked as duplicate by Kiwy, Jeff Schaller, Christopher, Rui F Ribeiro linux Apr 25 '18 at 18:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I've pointed out earlier that : Searching taskset on unix.SO gave me those two related questions : 1, 2 , where taskset is used. - It's similar question, that's why I linked this with them, but asked in different way, so I could not find it, when searching for my problem. Those questions where linked together when @DavidSchwartz shown taskset in his comment. – Grzegorz Wierzowiecki Oct 29 '11 at 10:13
  • Related: unix.stackexchange.com/q/247209/135943 – Wildcard Jan 21 '16 at 2:17

Under Linux, execute the sched_setaffinity system call. The affinity of a process is the set of processors on which it can run. There's a standard shell wrapper: taskset. For example, to pin a process to CPU #0 (you need to choose a specific CPU):

taskset -c 0 mycommand --option  # start a command with the given affinity
taskset -c -p 0 1234             # set the affinity of a running process

There are third-party modules for both Perl (Sys::CpuAffinity) and Python (affinity) to set a process's affinity. Both of these work on both Linux and Windows (Windows may require other third-party modules with Sys::CpuAffinity); Sys::CpuAffinity also works on several other unix variants.

If you want to set a process's affinity from the time of its birth, set the current process's affinity immediately before calling execve. Here's a trivial wrapper that forces a process to execute on CPU 0.

#!/usr/bin/env perl
use POSIX;
use Sys::CPUAffinity;
Sys::CpuAffinity::setAffinity(getpid(), [0]);
exec $ARGV[0] @ARGV
  • 8
    There's a good chance you have the taskset command, which allows you to specify CPU affinity when you launch a process and also allows you to change the CPU affinity of a running process. – David Schwartz Oct 23 '11 at 5:34
  • @DavidSchwartz Thanks, I didn't know the tasket command. I've added it to my answer, so that it's mention, but you would have been warranted to post it as a separate answer. – Gilles Oct 24 '11 at 0:26
  • Searching taskset on unix.SO gave me those two related questions : 1, 2 , where taskset is used. – Grzegorz Wierzowiecki Oct 28 '11 at 8:20
  • 1
    On ubuntu it's taskset -pc 0 1234, i.e. limit process 1234 to cpu 0. – qed Nov 3 '13 at 10:03
  • @qed Oh, right, not just on Ubuntu. The CPU list is the first operand to taskset, it needs to go after all the option. Thanks, fixed. – Gilles Nov 3 '13 at 10:06

You can build cpu-sets on the command line as well. man cpuset Later on you can assign (running) processes to these.

  • I've checked out man page. Looks very interesting ! Thanks for this ! What about my question, I'd like to get processes assigned from their birth. Even thou, it's interesting to know how to assign running ones. :) – Grzegorz Wierzowiecki Oct 22 '11 at 22:10
  • It has been a long time that I actually needed that - it was on Solaris. Later on it was on HP-Unix. On Linux - hm. I think I once found an additional package in SLES that provides a shell-api for that stuff. A quick search on CentOS 5 turned out blank. I will come back to you, if I found something in that direction. BTW - wich Linux distribution are we talking about here? – Nils Oct 26 '11 at 19:23
  • I am looking for general solution. If some program is not delivered in standard distro, I can deliver it. What about my desktop: Arch64, Arch. On servers usually Gentoo. On ARM devices, usually small specific ones. – Grzegorz Wierzowiecki Oct 28 '11 at 7:38
  • 1
    My feeling is that you can do all the cpuset-magic with echo in /proc as well. But you are propably better off in your case with the python solution. - I see the command on the shell is taskset. – Nils Oct 28 '11 at 20:53
  • /proc is good tip, I have to check it out. Thanks. – Grzegorz Wierzowiecki Oct 29 '11 at 8:42

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