APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Basics > taskset for CPU affinity
Printer Friendly Version




taskset for CPU affinity



SMP operating systems have choices when it comes to scheduling processes: a new or newly rescheduled process can run on any available cpu. However, while it shouldn't matter where a new process runs, an existing process should go back to the same cpu it was running on simply because the cpu may still be caching data that belongs to that process. This is particularly apt to be true if the process is a thread: the other threads in the same program are very likely to have cpu cache of interest to their brethren (though obviously this also diminishes the performance gain that might be seen from multithreading) . For these reasons, scheduling algorithms pay attention to cpu affinity and try to keep it constant.

It is possible to force a process to run only on a certain cpu. There are Linux system calls (sched_setaffinity and sched_getaffinity) and a command line "taskset".

Most of us probably don't need to bother with being heavy handed here. The Linux scheduler probably does a better job assigning processes to cpu's than we would. There may be conditions where you want to effectively dedicate a cpu to a certain process. Usually that means forcing all other processes to another cpu to prevent them from migrating to the cpu you want to keep dedicate. That's not too hard if we are only dealing with two cpu's because processes inherit affinity from their parants. So if you forced "init" to bind to cpu 0, all of its children would inherit that restriction and wouldn't use cpu one unless specifically set. You'd then set your "special" process to use cpu 1. This only gets a little more complicated with more than two processors; available cpu's are just a bit mask, so you would bind init to use more than one cpu but not the one you want to keep clear.


If this page was useful to you, please click to help others find it:  
Your +1's can help friends, contacts, and others on the web find the best stuff when they search.


6 comments




More Articles by Tony Lawrence - Find me on Google+



Click here to add your comments





Tue Feb 16 19:42:30 2010:   RichardJose

gravatar
I am running a Xen Virtual Machine on an 8-core machine. I want to bind guest vm's on different cores. Can you tell me is it possible to do that using taskset or cpuset or any of the other things you mentioned about affinity here?





Tue Feb 16 19:46:53 2010:   TonyLawrence

gravatar
I don't know why not - did you try?



Fri Jul 2 10:33:08 2010:   anonymous

gravatar
If a process is assigned a CPU to run on, do all child processes spawned by it inherit the cpu affinity?



Fri Jul 2 12:25:19 2010:   TonyLawrence

gravatar
' So if you forced "init" to bind to cpu 0, all of its children would inherit that restriction and wouldn't use cpu one unless specifically set.'



Mon Jan 9 23:43:45 2012:   anonymous

gravatar
In qemu virtual machine, I am using taskset -c 0 process1 & taskset -c 1 process2 & taskset -c 2 process3 & taskset -c 3 process4; to simultaneously issue 4 processes and bind them to four cores. However, I find that in-between, some cores (say 1 and 2) do not execute those processes but either idle or do something else. Can you suggest, what could be the reason or way of improvement (making sure, processes don't migrate)?



Tue Jan 10 02:03:16 2012:   TonyLawrence

gravatar
Are you sure they are migrating? I would expect them to bind, but of course Qemu is also involved here, so maybe it is screwing it up. Beyond my knowledge, sorry.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




Buy Kerio from a dealer who knows tech: I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals
pavatar.jpg

This post tagged:

       - Basics
       - CPU
       - Linux
       - Unix




Unix/Linux Consultants

Skills Tests

Guest Post Here