How can I force my program use this or this core?

Windows specific questions.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

How can I force my program use this or this core?

Post by Tourist Trap »

Hello,

I have 8 logical processors, but my programs seem to love running only on the first. Is there a way to force an executable on Windows, execute on the processor we tell it to? If so, how does it work?

Thanks.
SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How can I force my program use this or this core?

Post by SARG »

Hi TT,

Found that :

the first trick is via task manager
https://www.howtogeek.com/121775/how-to ... cific-cpu/

The second one via line command

Code: Select all

c:\windows\system32\cmd.exe /C start /affinity 1 notepad.exe
Maybe it could possible programatically. Not tested I don't know if it's for real processors or also logical ones.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I force my program use this or this core?

Post by Tourist Trap »

SARG wrote:Hi TT,

Found that :

the first trick is via task manager
https://www.howtogeek.com/121775/how-to ... cific-cpu/

The second one via line command

Code: Select all

c:\windows\system32\cmd.exe /C start /affinity 1 notepad.exe
Maybe it could possible programatically.
Thanks a lot for the quick answer. I will give this a try :)
SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How can I force my program use this or this core?

Post by SARG »

Tourist Trap wrote: Thanks a lot for the quick answer. I will give this a try :)
j'aime bien ce genre de chose :-) I like this sort of stuff.

For doing by program use SetProcessAffinityMask(ProcInfo.hProcess, dword(cpuset))
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I force my program use this or this core?

Post by Tourist Trap »

SARG wrote:j'aime bien ce genre de chose :-)
Alors je me permets poser une autre question ;)
For doing by program use SetProcessAffinityMask(ProcInfo.hProcess, dword(cpuset))
I'll have to test this one first, but I have in fact more questions in the same domain. Sometimes we have a child process that gets stuck, how can we kill a child without killing the parent? It rarely works in the task manager, when you kill a child everything is dropped with it.
Any idea about this?
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: How can I force my program use this or this core?

Post by marcov »

Tourist Trap wrote: I'll have to test this one first, but I have in fact more questions in the same domain. Sometimes we have a child process that gets stuck, how can we kill a child without killing the parent? It rarely works in the task manager, when you kill a child everything is dropped with it.
Any idea about this?
Note that unnecessarily binding to a core can frustrate CPU's heat management, causing it to clock down. The scheduler rotates heavy CPU using processes across cores to move the source of heat around, heating it more evenly with a lower peak temperature.

To let the child be independent, it has to be executed in a certain way. Sometimes the child itself also has to perform certain things (like closing handles from the parent). It is not so much as that you can choose how to kill it externally, the bond must be broken by the programs themselves.

This process is called "daemonization" on Unix. On Windows it is done by passing DETACHED_WINDOW to the createprocess instance (as a quick example I found https://codereview.stackexchange.com/qu ... ft-windows)

OR use shelleexecute to execute the program.
SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How can I force my program use this or this core?

Post by SARG »

Tourist Trap wrote:I'll have to test this one first, but I have in fact more questions in the same domain. Sometimes we have a child process that gets stuck, how can we kill a child without killing the parent? It rarely works in the task manager, when you kill a child everything is dropped with it.
Any idea about this?
I don't know if there is a solution for all the cases. It should depend of how the parent and child are 'linked'.
Post an example and I'll look for (later, busy by gas64) a way.

About setting affinity, as marcov said and also written in the text of the link it isn't a good thing to manage yourself what cpus are to be used.
Post Reply