2

Suppose I have a 8-CPU machine, which run a single process and one thread program

  while True: report time every hour.

The program run one year alive.

During the passed whole year, did the program run solely on one core, or might visit every core?

Wizard
  • 2,503
  • 1
    What language is this program being run in? – jesse_b Dec 20 '19 at 22:40
  • @Jesse_b almost certainly irrelevant since almost every language implements threads on top of native operating system threads. The code in the question can be run as psudo code. – Philip Couling Dec 20 '19 at 22:45
  • @PhilipCouling: Probably but I'm not familiar with all programming languages and I wouldn't doubt if some of them automatically implement multithreading. Of course OP said it would be a one thread program so is that actually specified in the program or assumed? – jesse_b Dec 20 '19 at 22:46
  • @Jesse_b I'm familiar with a lot of languages and none of the ones I know do. Further its very unlikely that they would go through the pain of implementing their own time slicer & stack management when it would screw with their ability to run different threads on different CPUs. There is a concept of "tasks" in C# and Python that can mess with the concept of threads a little, but they have to be used explicitly in the language and are nothing like threads when you understand them. – Philip Couling Dec 20 '19 at 22:50
  • I want to assure the possibility of the kernel to arrange tasks to CPU so if any language run a program on various cores, I got the answer. @Jesse_b – Wizard Dec 21 '19 at 00:54

2 Answers2

2

The Linux scheduler will try to run the program always on the same CPU, but that is not guaranteed. Cf. CPU affinity.

2

I have seen the scheduler move a process around, too reduce local warming effects (this happens in time of order a second). It is also free to move it for any other reason, however it will try to keep it on the same core, as this will use the same cache, and prevent a cache flush.

In a year there will be so many opportunities for the core/cpu to change that I would say that the probability is close enough to 100% that makes no difference.

You can restrict the cpu/core that a process runs on. If you do this then it is guaranteed.