When you are using SMP (Symmetric MultiProcessing) you might want to override the kernel’s process scheduling and bind a certain process to a specific CPU(s).

But what is CPU affinity?

CPU affinity is nothing but a scheduler property that “bonds” a process to a given set of CPUs on the SMP system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity:

The scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications. For example, application such as Oracle (ERP apps) use # of cpus per instance licensed. You can bound Oracle to specific CPU to avoid license problem. This is a really useful on large server having 4 or 8 CPUS

Setting processor affinity for a certain task or process using taskset command

taskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity. However taskset is not installed by default. You need to install schedutils (Linux scheduler utilities) package.

Install schedutils

Debian Linux:
# apt-get install schedutils
Red Hat Enterprise Linux:
# up2date schedutils
# rpm -ivh schedutils*
Under latest version of Debian / Ubuntu Linux taskset is installed by default using util-linux package.

The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. For example:

  • 0x00000001 is processor #0 (1st processor)
  • 0x00000003 is processors #0 and #1
  • 0x00000004 is processors #2 (3rd processor)

To set the processor affinity of process 13545 to processor #0 (1st processor) type following command:
# taskset 0x00000001 -p 13545
If you find a bitmask hard to use, then you can specify a numerical list of processors instead of a bitmask using -c flag:
# taskset -c 1 -p 13545
# taskset -c 3,4 -p 13545


  • -p : Operate on an existing PID and not launch a new task (default is to launch a new task)

See also:

UPDATED for accuracy.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 37 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
37 comments… add one
  • triton Oct 5, 2006 @ 1:54

    How can I check if it is is using only 1 cpu?

  • 🐧 nixCraft Oct 5, 2006 @ 7:35

    Use any one of the following commands:
    less /proc/interruptsor
    cd /sys/devices/system/cpu

    See url

  • Ricardo Reis May 8, 2007 @ 16:17

    it should be

    taskset -c -p 1 13545

    options firts, arguments later. I know, it seems stupid but it is what it is. It only worked like this on my machines (debian)

  • Erik Bussink Jun 5, 2007 @ 23:03

    Hiya, thanks for this very interesting subject. I was looking for something like this, to be able to schedule my VMware Workstation 6.0 to run on my HP xw9400 Workstation Dual-Core Opteron (3&4) under RHEL5 (x86-64).

  • Ramanath Jun 21, 2007 @ 16:39

    Suppose I give this command

    root@dialer243:~#taskset -c -p 1 13545

    how do I check that the command worked or not!! I mean the cpu affinity was set or not ?

  • Sutanto Kurniawan Sep 22, 2007 @ 12:11

    You can use the ‘top’ command.

    After loading ‘top’ press 1 (one) to switch to SMP view, which will show each CPU usage.

    Try assigning CPU intensive program to one of your CPU, and see (in ‘top’) whether the affinity is set or not.

    In my system, the program taskset definitely do it’s job. If I don’t use the taskset, the CPU that work on my program is vary from 1 to 4 (I have quad core), when I assign the program to one specific CPU, then only that CPU that has high usage.

  • Boca Dec 24, 2007 @ 11:12

    A little odd, but installing schedutils on ubuntu gutsy would remove essential components which is not recommended. However, taskset is available as part of default install.

  • mike503 Feb 9, 2008 @ 9:34

    Yeah, taskset is from util-linux. The syntax is now taskset -cp $CPU $PID

    Took me a few minutes to figure that one out 🙂 It wants to remove util-linux for schedutils and that has valuable utilities in it. It has taskset anyway. This article might be good to update…

    Ubuntu 7.10 gutsy x86_64.

  • ellis bright Apr 12, 2008 @ 18:27

    this is great – i have a java wrapper on a engine written in C++ that crashes whenever the engine is run on more than 1 processor. since all the newer processors are multi-core, i have been running the OS in uniprocessor mode and basically wasting the other cores!
    ~Ellis http://www.lightspoke.com

  • InQuest May 6, 2008 @ 23:07

    Does the taskset do its job on already running processes. I tried but it didn’t work. First I ran 2 identical processes without taskset. They were homogeneously running on all 8 cpus I had. Now, when I tried assigning 2 cpus to 1 and 6 to another, it didn’t work.
    I killed the process where I assigned 6 cpus. Still the other was using all 8 cpus.

  • franchisu Jul 6, 2008 @ 3:53

    hi guys,
    I tried using taskset and schedtool interchangeably, and I noticed that when I’m running virtualbox, which is a cpu intensive process, then I change proc affinity which doesn’t seem to have that big an effect in ‘top’. I change from one proc to another and the output of ‘top’ seems unchanged, while the process is already running. However, if I begin a new cpu intensive process and assign it an affinity before running it, then I can see changes in the output of ‘top’. any clues to this? perhaps my already running virtualbox is too ‘attached’ to its original proc that the linux kernel decides to override/not pay attention to my schedtool/taskset commands?

    • sobrier Jul 21, 2010 @ 8:27

      Could you tell how you change proc affinity, I have found a long time.
      Thank you.

  • anuu Jul 29, 2008 @ 14:19

    anu@el[~]$ taskset -c -p 1 11574
    pid 11574’s current affinity list: 0,1
    pid 11574’s new affinity list: 1

    it works to me
    debian etch

    great tool to look what proceses are running is

  • franchisu Jul 29, 2008 @ 14:35

    Actually, it evidently works especially when I set a process’ affinity before running the process. I noticed that while running a cpu or memory intensive task such as Virtual box or VMware, alternating/changing processor affinity doesn’t seem as evident as my previous example. Anyone noticed this? Perhaps it has something to do with the Linux kernel overriding the manual affinity changing since the kernel knows how to better handle processes and it probably knows that there will be performance hits when moving processes such as VMware/Virtualbox from one proc to another (e.g. cache hit rate etc) ?

  • Angelina Nov 8, 2008 @ 14:17


    How to use task set before the process started?

    Example: when I start oracle db, I want to use only 2 CPU2 for oracle DB processes. In this point I do not know what is PID.

    Oracle says, if you use 4 CPUs at ANY point of time, you should pay oracle license for 4 CPUs.

    Please help me … I do not want to setup kernel maxcpus parameter because I will loose CPUs …I want to use them for some other processes.


  • 🐧 nixCraft Nov 8, 2008 @ 16:03


    Try following for 2 cpu
    taskset -c 0,1 /etc/init.d/oracle-xe start
    su - oracle
    taskset -c 0,1 lsnrctl start
    taskset -c 0,1 dbstart

    taskset -c 0,1 sqlplus '/ as sysdba'
    And at SQL prompt type:


  • Thiru Dec 12, 2008 @ 0:46

    This is very cool!
    One question though: Once you bind a process to certain processors, are those processors dedicated for this process alone? Do other processes use these processors?

  • Ankit Singh Dec 15, 2008 @ 9:06

    when you run the command
    ankit@hover2:~$ taskset -c -p 0 6369
    pid 6369’s current affinity list: 0,1
    pid 6369’s new affinity list: 0

    0 & 1 is the two cores of my dual core CPU which I changed the affinity to core 0. The above mesg. clearly shows the the affinity was changed to core 0 from core 0,1.

    A great article. Keep it up.

    Ankit Singh

    • kishore Aug 12, 2014 @ 5:43

      how to change affinity

  • reavers Jan 5, 2009 @ 15:37

    How you can unbind a process from the selected processor affinity?
    I mean, if you don’t want keep the affinity on a process all time, how can you unbind the established affinity between the processor and the process?
    Something like:
    taskset -c -p Idprocess?
    taskset -c * -p Idprocess?
    taskset -c 0,1,2,3 -p Idprocess?

  • Jakob Engblom Jan 28, 2009 @ 20:55

    What are the key guts needed to make this work? I want to put this into a busybox-based embedded system… where apt-get won’t help me at all. I need to statically get it compiled onto the never-changing initrd…

  • Bernardo B.L. Mar 18, 2009 @ 15:29


    This was a very helpful post. It solved part of my problems. But I still have another problem:
    How do I know in each processor a current process is running into.
    For instance I have a quadcore machine and want to know if the process with pid 999 is running in the cpu 0, 1, 2 or 3.
    top command shows only the processor average utilization by this pid but don’t show each cpu is this.
    Taskset let me bound a task to a CPU, but yet i don’t know each cpu this process was previously running into.


  • Bernardo B.L. Mar 18, 2009 @ 15:42

    Just found the solution
    ps -eo psr,pid

    this gives the processor in each the pid is current assigned to.

  • mahes Sep 15, 2009 @ 12:03

    kool stuff – it helped me a lot .thankx in advance
    i am in need of running a application wit the same cofnfig file , but in different processor .
    even though i run both pairs in different proccessor (as per ur taskset commands) . both are crashin in the sense if i change something in 1 pair it gets reflected in other..can t they be prevented.

    pls help me throu

  • chango Oct 22, 2009 @ 4:56

    I hava a proccess with forks, child process will have the same affinity ??.

    Thanks 😀

  • JuanPablo Nov 8, 2009 @ 2:03

    how asign a processor to specific user ?

    many thanks.

  • kannan Nov 9, 2009 @ 9:41

    taskset -p option pid
    option 1 means 1st processor
    option 2 means 2nd processor
    option 0xffffffff means all the processor

  • Anderson Jun 27, 2011 @ 13:15

    Thanks nixcraft!
    Very useful!

  • JM Aug 5, 2011 @ 13:23

    I hawe quad core cpu, and if i want to all linux processes use only 2 cores? What is the right syntax?
    Now i have in /etc/init.d/rcS:

    /usr/bin/taskset -p 1 1
    /usr/bin/taskset -p 1 $$

    this sett all proccess to first core, but now i want alocate 2 cores for all proccess

  • Cyenne Feb 7, 2012 @ 8:38

    Hi Tony,I have a CentOS 5.7 w/ cPanel 11.30 + ngxcinp plugin. I want to move over to CloudLinux + LiteSpeed. What would you suggest me:a) setup a new server with CentOS 6 -> Install cPanel -> Move customers over -> Upgrade to CloudLinux 6 orb) stay with CentOS 5.7 and upgrade to CloudLinux directly from there?I’ve read that theres no upgrade path between RHEL5 -> RHEL6 and as such nor for CentOS or CloudLinux. Which version do you run on your servers? 5.x or 6.x?Thanks in advance

  • anbu Jul 20, 2012 @ 12:35

    when tried to bind it’s giving

    taskset 0x00000001 -p 6268
    execvp: No such file or directory
    failed to execute -p

    please help me!

    • kirti Jul 28, 2012 @ 8:34

      the same error was encountered by me also..
      Please try using

      taskset -pc 0,1,2..x(i.e. CPU number) 6268

      it works!!

    • BD Apr 27, 2013 @ 16:46

      I had the same result. The syntax in the example is incorrect.
      It should be as follows:
      taskset -p 0×00000001 6268

  • Kisho Dec 10, 2012 @ 10:34

    On Debian Squeeze and newer you need to use “-pc” instead of “-c cpu -p id”
    For example:
    root@debiansqueeze:~# taskset -c 3 -p 5563
    execvp: No such file or directory
    failed to execute -p

    root@debiansqueeze:~# taskset -pc 3 5563
    pid 5563’s current affinity list: 3
    pid 5563’s new affinity list: 3

  • Ivan Petrov Jan 28, 2013 @ 11:20

    Is it possible by default to assign all specific processes to specific CPU’s.
    For example, I want to assign all java processes to CPU 0-1 and all HTTPD proccesses to 2-7.

  • Pawan Mar 15, 2016 @ 18:20

    To change the process affinity login with admin and use taskset cli.
    taskset -pc 0 2
    pid 2’s current affinity list: 0-3
    pid 2’s new affinity list: 0

    Here 0 is the CPU , it could be a list like 0-3.

  • Zaki Jun 16, 2016 @ 5:39

    I have two programs. I want to execute both program on different core. I did this by setting the affinity (using taskset command). But the core is changing during next execution. How to permanently assign a specific core to a running program.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum