Linux process and threads, CPU Affinity

Silviu Popescu silviupopescu1990 at gmail.com
Fri Sep 13 04:21:15 EDT 2013


On Fri, Sep 13, 2013 at 10:43 AM, manty kuma <mantykuma at gmail.com> wrote:
> I have few queries related to threads and Process scheduling in Linux.
>
> When my process goes into sleep and wakes back, is it always that it will be
> scheduled on the same CPU that it got scheduled before?
> When i create a thread from the process, Will it also be executed on the
> same CPU always? Even if other CPU's are free and sleeping.
>
> I would like to know the mechanism in Linux in specific. Also i am creating
> the threads through pthread library. I am facing a random hangup issue which
> is always not reproducible. Need this information to proceed in the right
> direction.
>
>
> I have also posted a query for the same SO. But no constructive reply. Hence
> re-posting it here.
>
> http://stackoverflow.com/questions/18779947/linux-threads-and-process-cpu-affinity

Hello,

The answer you received on the SO thread seems accurate to me.
User-space processes and threads both map to something called tasks in
the kernel. They are both seem as execution units, but threads share
everything (code, data, open files) except registers and stack. So
they are treated the same by the kernel in terms of scheduling.

If a task sleeps as a result of I/O, signal or time slice exceeded it
is no longer run on the CPU and when it resumes running it is the
scheduler policy that decides how the task is rescheduled. I believe
the policy decides how to take into account the affinity, meaning that
you could set the affinity and the scheduler might ignore it.

The article at [1] demonstrates how to use CPU affinity and [2] and
[3] show how to change the affinity and scheduler policy.

[1] http://www.ibm.com/developerworks/linux/library/l-affinity/index.html
[2] http://man7.org/linux/man-pages/man2/sched_setaffinity.2.html
[3] http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html

-- 
Silviu Popescu



More information about the Kernelnewbies mailing list