2

I have a bug in my Linux app that is reproducable only on single-core CPUs. To debug it, I want to start the process from the command line so that it is limited to 1 CPU even on my multi-processor machine.

Is it possible to change this for a particular process, e.g. to run it so that it does not run (its) multiple threads on multiple processors?

  • It's up to the program running in the process how many threads it creates. – Johan Myréen Sep 8 '18 at 8:39
  • How does your application determine how many threads to spawn? Depending on how your app checks the number of cores (e.g. via std::thread::hardware_concurrency, sysconf(_SC_NPROCESSORS_ONLN), /proc/cpuinfo, cpuid, etc) there may be different approaches to fake the core count. That said, as suggested by @JohanMyréen, at that point it may be easier to hardcode how many threads to spawn. – undercat Sep 8 '18 at 9:33
  • By the way, it may be worth mentioning what language and threading library you are using. – undercat Sep 8 '18 at 9:37
  • Would not be easier changing you own sources? (....) – Rui F Ribeiro Sep 8 '18 at 17:13
4

You can use taskset from util-linux.

The masks may be specified in hexadecimal (with or without a leading "0x"), or as a CPU list with the --cpu-list option. For example,

       0x00000001  is processor #0,

       0x00000003  is processors #0 and #1,

       0xFFFFFFFF  is processors #0 through #31,

       32          is processors #1, #4, and #5,

       --cpu-list 0-2,6
                   is processors #0, #1, #2, and #6.

   When  taskset returns, it is guaranteed that the given program has been
   scheduled to a legal CPU.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.