# # taskset for CPU affinity
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

taskset for CPU affinity

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© October 2005 Tony Lawrence

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 you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> taskset for CPU affinity

6 comments


Inexpensive and informative Apple related e-books:

Take Control of Automating Your Mac

Take Control of iCloud, Fifth Edition

Take control of Apple TV, Second Edition

Digital Sharing Crash Course

Sierra: A Take Control Crash Course





More Articles by © Tony Lawrence







Tue Feb 16 19:42:30 2010: 8095   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: 8096   TonyLawrence

gravatar


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



Fri Jul 2 10:33:08 2010: 8772   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: 8773   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: 10456   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: 10457   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.

------------------------


Printer Friendly Version

Have you tried Searching this site?

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.

Contact us


Printer Friendly Version





Much to the surprise of the builders of the first digital computers, programs written for them usually did not work. (Rodney Brooks)




Linux posts

Troubleshooting posts


This post tagged:

Basics

CPU

Linux

Unix



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode