How can I ensure that exactly one process is running on a given processor/core?

3

1

I would like to do this so I can accurately speed test my processes, and just to be able to have the illusion of control.

Maybe I want to reserve one processor for special programs. Maybe I want to write my own process optimizer. Who knows. The possibilities are endless in my mind.

Is this possible in any OS? If not why not? If we are allowed to compartmentalize memory, why can't we have such control over processors?

Thanks.

user420667

Posted 2012-05-01T20:54:00.433

Reputation: 133

possible duplicate of Setting CPU cores off-limits to all threads not specified (preferably in Windows 7) (although I've still never received a good answer for it...)

– Shinrai – 2012-05-01T21:00:44.397

@Shinrai: what did you end up doing? Would autoitv3 have worked for you? – user420667 – 2012-05-01T21:22:59.100

We ended up having to approach it from another direction, honestly, because the Windows requirement for that customer was non-negotiable and we never could figure it out. I can't see how autoitv3 could do it. The problem is that it's trivial to get things to start on the processor you want - the hard part is keeping other things off of them. Modern OSes are very good at figuring out where to put things and they really don't like to let you fool with this - I'm guessing it can be done in Linux variants, but to what extent I don't know. – Shinrai – 2012-05-01T21:33:28.117

@Shinrai I had a look at your previous question, and indeed you can do it with AutoIt (the scripts can also be compiled directly into .EXE files to avoid distributing the AutoIt runtime). Basically, find a program which can list the process' affinity, and modify all of them accordingly. – Breakthrough – 2012-05-01T21:35:13.920

@Breakthrough - But what about new processes that start after that? (I'd add that I don't have any practical concern for this anymore but it is still an interesting intellectual exercise (and that's why I've upvoted this as well)) – Shinrai – 2012-05-01T21:46:00.630

@Shinrai I thought about this, and you could simply have the program run all the time, polling all processes at a given interval (e.g. check all process affinities every 5 seconds). It should be a fairly efficient program, since the task scheduler is always running (and thus, the information of interest is always available in RAM). – Breakthrough – 2012-05-01T22:24:52.550

@Breakthrough - I can see how that would probably work, okay. – Shinrai – 2012-05-01T22:47:05.390

@Breakthrough: these are good ideas. However, I think the ideal case is to disallow new programs from being launched / intercept launching of programs so that before they run their affinity is set. I don't know how to do THAT though. If it's polling, the program may run for x amount of time on multiple processors, which defeats the purpose to some extent. – user420667 – 2012-05-02T21:32:20.643

@user420667 I agree, but when you're dealing with a closed-source operating system, there's not much you can do aside from high-level OS calls, which would imply polling. If you're running Linux, something like grawity's suggestion would be best (and indeed, if you need that level of control, maybe you should be using Linux to begin with...). – Breakthrough – 2012-05-04T00:28:12.580

Answers

4

On Linux, cpusets should work for this.

user1686

Posted 2012-05-01T20:54:00.433

Reputation: 283 655

+1, I have not used linux much. Would it be possible to use cpusets to set the active executables to exist in only one specific processor? – user420667 – 2012-05-01T21:26:01.640

1

This is in theory possible. The easiest way to accomplish something close to this would be with a virtualization platform that lets you assign specific CPU/cores to a specific VM. Then you could run just the process you want on a given VM (of course with the bare minimum of OS overhead).

Flimzy

Posted 2012-05-01T20:54:00.433

Reputation: 4 168

+1 That might work but it's not really what I was looking for. I guess I'm just a control freak. – user420667 – 2012-05-01T21:21:30.740

1

The easiest way to do this is by setting the processor affinity for a given process, or telling the OS which logical CPUs (cores) that process is allowed to run on. If you right-click on a process in the Windows Task manager and select Set Affinity..., you'll find a window that looks something like this:

Setting the processor affinity in windows

In linux-based systems, you can do the same thing with the taskset utility or by setting the appropriate CPU mask flag in the processes' /proc entry.

Sadly, there's no easy way to remove all processes from a certain processor, or to automatically set a specific mask on a process when it starts up-- you'll need a script to monitor for new processes on the system and set the appropriate affinity that you desire.

Darth Android

Posted 2012-05-01T20:54:00.433

Reputation: 35 133