Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)).

share|improve this question
possible duplicate of How can I set the processor affinity of a process on Linux? –  Gilles Oct 28 '11 at 9:06
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

2 Answers 2

up vote 15 down vote accepted

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
share|improve this answer
Nice you've pointed out python library as well - my preffered scripting lang :). As I got system_call I can write wrapper, Thanks :). –  Grzegorz Wierzowiecki Oct 22 '11 at 20:23
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
@David Schwartz - Thank you for tasket comman :). It looks really nice and suitable :D. Imho, this tip deserves for separate answer ;). –  Grzegorz Wierzowiecki Oct 28 '11 at 7:37
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

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

share|improve this answer
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
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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