Sandeep
Sandeep

Reputation: 19452

Linux Threads and process - CPU affinity

I have few queries related to threads and Process scheduling.

  1. 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?
  2. 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.

Upvotes: 2

Views: 4349

Answers (2)

Arno
Arno

Reputation: 5194

On single processor/core systems

  1. Yes
  2. Yes

on multi processor/core systems

  1. No.
  2. No.

use taskset to retrieve or set a processes’s CPU affinity on multicore systems. Setting the CPU affinity to a specific processor/core will change the answers to

  1. Yes
  2. Yes

also for multicore systems.

From within an application you may use sched_setaffinity and/or sched_getaffinity to adjust the CPU affinity.


Edit: Additional details about how/when CPU swaps are managed with respect to cache disadvantages:

The Linux/SMP Scheduler: "... In order to achieve good system performance, Linux/SMP (2.4 kernel) adopts an empirical rule to solve the dilemma ..." Read the details in the linked reference, section The Linux/SMP Scheduler.

For the newer CFS (Completely Fair Scheduler) you'd look at sched_migration_cost. "...if the real runtime of the task is smaller than the values of this parameter then the scheduler assumes that it is still in the cache and tries to avoid moving the task to another CPU during the load balancing procedure ..." (e.g.: Completely Fair Scheduler and its tuning).

Upvotes: 7

user2775341
user2775341

Reputation: 37

when process goes in to sleep and when it wake up ,it is not necessary that it will schedule on same cpu.if u have multiprocessor environment then according to scheduler policy it will schedule on any cpu.When process goes to sleep there are different reason ,it goes to sleep beacause it is waiting for io,any resource.When event will occurs it goes from waiting state to ready state.At that time which cpu will be free scheduler will schedule that process on free cpu.It is not necessary it will schedule on same cpu. for extra information about scheduler open source code of scheduler in linux release tree path.

Upvotes: 0

Related Questions