3

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?

4
  • It's up to the program running in the process how many threads it creates. Commented Sep 8, 2018 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
    Commented Sep 8, 2018 at 9:33
  • By the way, it may be worth mentioning what language and threading library you are using.
    – undercat
    Commented Sep 8, 2018 at 9:37
  • Would not be easier changing you own sources? (....) Commented Sep 8, 2018 at 17:13

2 Answers 2

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

As mentioned in the answer by @Ipor to limit program to one CPU hardware core/thread one may use taskset 1 prog [args]

Also, there is a way to explicitly prohibit a process from cloning (not sure about apps that use fork though, checked only for clone sys call).

The program to limit number of processes a program can have (threads) is prlimit --nproc=1 prog [args]. I've tried to use it with rsync and got "fork unavailable... error in IPC" and the end - rsync is written not to work as one thread.

strace prlimit --nproc=1 rsync  

Running strace has shown as described in SO link below return value from clone call is -1 EAGAIN (resource temporary unavailable).

P.S. idea taken from https://stackoverflow.com/questions/38637451/is-there-a-way-force-a-program-to-use-only-1-thread.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .