Close Menu
    X (Twitter)
    Hectic GeekHectic Geek
    • Apps
      • Apps for Mac
      • Apps for PCs
      • Best Apps
    • Concepts
      • Comparison
      • For Geeks
      • IT Careers
    • Gadgets
      • Accessories
      • Best Work Laptops
      • Budget Laptops
    • GNU/Linux
      • Ubuntu
    • How To
      • Virtualization
      • Windows
      • Windows How-To
    Hectic GeekHectic Geek
    Home » How to Assign a Process to a Certain CPU Core in Ubuntu Linux
    Ubuntu

    How to Assign a Process to a Certain CPU Core in Ubuntu Linux

    GayanBy GayanMarch 28, 2012Updated:February 7, 201725 Comments6 Mins Read
    Twitter Reddit Telegram WhatsApp Email
    Share
    Twitter Reddit Telegram WhatsApp Email

    Supposing that you have a multi-core processor, whenever you run a process (program), depending on its requirements, it might be executed using all the available multi-cores (say that it’s a multimedia encoding utility which needs a lot of your CPU cycles). Or, if it doesn’t need a lot of CPU usages and a single core is more than capable of handling it, then still, while executing, it might be switched between the multiple CPU cores by your operating system.

    The OS does this due to various reasons but mainly for achieving a balance between the system’s stability and the performance. And it’s performed by a lower level software utility (Kernel process) called “Scheduler” which decides how to interact a certain process with the available CPU cores, by using a value called the “CPU Affinity Mask”.

    For most users, it’s almost always best for letting the OS handle it. But for the power users, having the ability to make a certain process, to be executed by only using a single CPU core (or a certain number of cores) has a slight benefit concerning the performance.

    This is because, in a multi-core CPU environment, all the individual CPUs have a separate cache which is only accessible by that CPU core (the most lowest levels of CPU cache is known as “L1” and other “L2”, “L3” are usually shared between the cores).

    CPU Core 1 Says: “Oy, that hurts!” 😉 …

    So what happens when executing a process by switching it between various CPU cores is that, when switching a process to a new CPU core, the “L1” cache of the new core has to be updated and the previous core’s “L1” cache might needed to be deleted etc. This results in a somewhat unnecessary cache activity that ultimately reduces the performance.

    So if we only used a certain number of CPU cores while executing that particular program, then we can avoid unnecessary processor cache activities (it’s like with a HDD, if it’s busy deleting/updating files etc, then it’ll be a bit slow while doing other things) thus improving the performance, a bit.

    This can be beneficial, especially while running CPU hungry tasks such as encoding (not be a good idea if the encoder is multi-threaded) or Virtualization for example.

    It all sounds good, but …

    However, it is a bit more complicated than that, and is quite difficult to say when it will be beneficial or non-beneficial from the user’s point of view. For instance, let’s take the same example mentioned above. If while running that program (let’s called it process ‘A’), assigned to a certain core, it was ‘paused’ due to a request from another process with a higher priority, but shortly after, another core was available so that the ‘paused’ process ‘A’ can be run with a minimal delay, yet, since we have assigned it to be executed using a different core, it won’t get that chance.

    So it’ll have to wait till that process with higher priority (on the same core where our process ‘A’ is assigned) is finished running or ‘paused’, and thus decreasing its performance. So …

    Anyhow, without boring you any further with all these technical nonsense ;-), in modern GNU/Linux distributions, there’s a simple tool that lets us bypass the OS’s automated task execution functionality and binds a program (process) to a certain number of CPU cores with ease. It’s called “taskset”.

    You can use this utility to start a process from scratch or it can even “re-map” an existing process to a given number of CPU cores as well.

    I’m using Ubuntu (11.10 Oneiric Ocelot), and since it comes this tool pre-installed, all you gotta do is to open up the Terminal window and use the below commands which should get the job done.

    Starting a new process ….

    Let’s say that I wanted to use a program called “burnP6” (CPU stressing utility) that should only be run/executed using the first CPU core. Then I’ll use the below command in my Terminal window.

    taskset -c 0 burnP6

    The “-c” argument is what enables changing the “CPU Affinity” thus should be untouched.

    The value “0” represents the first CPU Core  (“0” is for the first one, “1” indicates the second one, “2” for the third one etc) and the “burnP6” is the name of the program (process) that I wanted to run. So depending on your needs, you’ll have to replace these two values with yours.

    Let’s say that I wanted to run the same program using the CPU Core number “2”, then I’ll use the below command.

    taskset -c 1 burnP6

    If I wanted to run it using multiple cores (first and the second cores) then I’ll use it in the below format.

    taskset -c 0,1 burnP6

    Binding an existing process …

    As long as you know the “ID” (PID) of the process, as mentioned above, you can even bind an existing process to a certain CPU core (or Cores). For that, I’ll use “taskset” in the below format.

    taskset -pc 0 7022

    Note that we’ve added “p” which indicates PID (process ID).

    Again, you’ll have to replace both “0” (CPU Core number) and “7022” (ID of the process). And I’m sure you know how to get the ID of a running process. If you don’t, then once the process is running, if you use the Unity desktop, then you can use the Gnome System Monitor and under the “Processes” tab, highlight the process and then look under the “ID” value, which is the PID of that particular process (as shown below).

    If you don’t use a desktop environment, then enter the below command in your Terminal window and it should printout a list of all the running processes in your OS and from that list under the “PID” value, you can find a process and its ID as well.

    ps -A

    How do I know if it’s working ?

    Well, if your process is CPU intensive (otherwise it doesn’t make a whole lot of sense to change the CPU Affinity value anyway) and if you use a desktop environment, then the built-in system monitor should be able to show you the CPU load for each individual CPU core, and if a particular Core (or Cores) have a higher CPU usage readings, then you know it’s working 😉 (as shown in the first screenshot).

    But if you’d still prefer the command-line, then you can use another built in system monitor called “top” for that as well. Again, after running the process and changing its “Affinity mask”, use the below command in your Terminal.

    top

    Now when it’s running, press the key “1” of your Keyboard and the output should change and display the load for each individual cores as shown below.

    That’s it!.

    Again, for most users, this is almost always unnecessary. But if you’re some like a system administrator who wants a certain process to be executed by only using a certain number of CPU cores of your PC (in Ubuntu Linux), then I hope that this was a bit helpful. Enjoy!.

    Share. Twitter Reddit Telegram WhatsApp Email
    Gayan

    An RHCE, 'Linux' user with 14+ years of experience. Extreme lover of Linux and FOSS. He is passionate to test every Linux distribution & compare with the previous release to write in-depth articles to help the FOSS community.

    Related Posts

    Debian vs Ubuntu Server: A Brief Comparison for Beginners

    July 2, 2023

    CPU Based Simple System Stability and Benchmark Tester for Ubuntu Linux – systester

    June 16, 2023

    Typespeed: Typing Speed Testing Game for Ubuntu Linux

    March 12, 2023

    Debian vs Ubuntu in 2023- Which Linux OS is Right for You?

    January 6, 2023

    How to Remove PulseAudio & use ALSA in Ubuntu Linux?

    October 2, 2022

    How to Show Hidden Files in Ubuntu 22.04 LTS (and hide them)

    May 25, 2022

    25 Comments

    1. Danny on April 19, 2012 7:05 pm

      Great article!!!

      Reply
      • Gayan on April 19, 2012 7:34 pm

        Thank you! :).

        Reply
    2. PJWDMV on July 23, 2013 11:09 am

      Thanks. It was very helpful.

      Reply
      • Gayan on July 23, 2013 12:43 pm

        You’re welcome.

        Reply
    3. 100RAB on August 17, 2014 1:06 pm

      Nice work. It helped a lot.

      Reply
      • Gayan on August 17, 2014 8:34 pm

        You’re welcome.

        Reply
    4. Ginsea Chen on November 10, 2014 6:17 pm

      Great job! thank you

      Reply
      • Gayan on November 11, 2014 6:48 am

        You’re welcome.

        Reply
    5. crackMEch on December 11, 2014 8:56 pm

      Great guide.. very detailed!

      Thanks a LOT!

      Reply
      • Gayan on December 12, 2014 7:15 am

        You’re welcome!.

        Reply
    6. Alex on January 8, 2015 12:20 pm

      Great article indeed. It saved me a lot of “googling”. Thank you!

      Reply
      • Gayan on January 8, 2015 12:45 pm

        You’re welcome Alex 🙂 .

        Reply
    7. Aaron Franke on January 13, 2015 5:43 pm

      What am I supposed to do if I have a process that cannot be started via a command line, but I need it to only run on one core?

      Reply
      • Gayan on January 13, 2015 6:23 pm

        Quite simple Aaron. As I mentioned in the article, you don’t have to start a process from scratch with ‘taskset’ for it to work, you can ‘bind’ any existing processes (as long as you have the permission to do so) to a certain core (s) with it as well. The process is simple.

        1. First find out the process ID.

        2. Then use ‘taskset’ with the ‘-p’ argument, followed by the number of cores you want that process to be bound to, and then it too followed by the processes ID.

        For instance, if I wanted to bind a process ID called ‘789’ to my first and second cores, then I’d use ‘taskset’ in the below manner:

        taskset -p 01 789

        It’s that simple!. You can read all this in its manual or by reading the more simplified ‘help’ page, both of which can be read using the below commands, respectively:

        man taskset

        taskset --help

        Hope that helped.

        Reply
    8. Denison on September 4, 2015 2:33 am

      Thanks a lot, that is just what I was looking for. Great help!

      Reply
      • Gayan on September 4, 2015 8:28 am

        You’re welcome Denison.

        Reply
    9. tester on March 23, 2016 9:02 pm

      For me it seems to only partially work. I have 4 cores and 7 CPU intensive tasks. I made the affinity of tasks so the cores 0, 1 and 2 have 2 tasks but core 3 – just 1 task. Also I changed the priority of tasks so each core has no CPU intensive tasks with the same priority. In System Monitor in the column for CPU usage I expected to see 4 numbers close to 25. However, what I did see was 1 number fluctuating around 20, and 6 numbers approximately equal to 12.

      Reply
    10. Muhammad Khizar on August 15, 2016 3:21 pm

      need help…..i have a filter program written in C…..i want to assign it to a perticular core….how would i do this??

      Reply
    11. AnveshBagary on July 3, 2017 9:59 am

      I have 8 cores and I want to assign a task on CPU7. Initially, it runs the task on CPU7, but after few seconds, it changes the affinity to CPU0 or CPU1. How can I prevent scheduler from changing the affinity of task?

      Reply
    12. S REDDY on November 30, 2017 4:13 pm

      I need to use one core (or more) exclusively for my program, what is the way ( I understand that “taskset” may allow the user to execute the application on which core he wish to, but not dedicate the entire core for that program, does my understanding is right?).

      Reply
    13. Aman Roy on January 22, 2018 10:08 am

      how to run the code in particular CPU in LINUX OS. i want to do this operation not in command level. i need to execute in programmatic level. anyone will you please help for this.

      Reply
    14. nice on February 13, 2018 10:05 pm

      i need to assign proccess to particular CPU

      Reply
    15. Zhuanhao Wu on September 15, 2018 4:06 am

      Thank you soooo much…
      I run my python code on a 16-core machine but it’s only using 2 cores for some reason I don’t know.
      Now I’m fully utilizing the 16 cores.

      Reply
      • Gayan on September 24, 2018 9:15 am

        You’re welcome.

        Reply
    16. Sergio Montenegro on December 18, 2018 1:31 pm

      It solved my problem with posix semaphores! Maybe there is a bug in the implementation of pthread_mutex_t ??

      Reply

    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Latest Posts

    [Fixed] HDMI No Signal to TV or Monitor on Windows 10/11

    November 7, 2023

    AlmaLinux vs Ubuntu – How to Select the Best One?

    October 22, 2023

    Why Does My Gaming Laptop’s Battery Drain So Fast? The Science Behind It

    October 9, 2023

    Top 10 Best email Encrypting Software for Business and Personal Use

    October 7, 2023

    7 Essential Features A Learning Management System Should Include

    October 2, 2023

    The Importance of DNS for Mac Users: Why You Need to Know How It Works

    September 25, 2023

    Subscribe to Updates

    Get the latest news and updates from Hectic Geek. We won't overload you or sell your data!

    Contact
    • About Us
    • Contact Us
    • Privacy Policy
    • Write for Us
    X (Twitter)
    © 2023 Hectic Geek.

    Type above and press Enter to search. Press Esc to cancel.