Is there a way to lock a process to a CPU?
I am thinking about developing an appli开发者_C百科cation that will, on a six core machine, run six asynchronous tasks, one on each core.
But is it possible to lock the tasks to their own core?
The idea is for them to run mostly by themselves, but to sometimes communicate over a shared memory area. However, I want the tasks to run as undisturbed as possible.
The concept you're looking for is called "thread affinity". How it's implemented and the interfaces to ask for it are OS-specific.
Under Linux, try sched_setaffinity(). glibc may also offer pthread_attr_setaffinity_np().
taskset -c cpunum yourprocess
does what you want.
It is possible to supply PIDs instead, this way you can set single threads to a cpu. If you want to change the cpu affinity from your own program, use sched_setaffinity()
.
Not lock, but it is possible to associate a cpu affinity for a process
Just for the records, another method, not involving programming: Open Task Manager, go to Processes tab, right click your process and choose Set Affinity...
精彩评论