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

Windows specific questions.
Tourist Trap
Posts: 2853
Joined: Jun 02, 2015 16:24

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

Postby Tourist Trap » Mar 28, 2020 14:52

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: 1046
Joined: May 27, 2005 7:15
Location: FRANCE

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

Postby SARG » Mar 28, 2020 15:12

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: 2853
Joined: Jun 02, 2015 16:24

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

Postby Tourist Trap » Mar 28, 2020 15:14

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: 1046
Joined: May 27, 2005 7:15
Location: FRANCE

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

Postby SARG » Mar 28, 2020 15:23

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: 2853
Joined: Jun 02, 2015 16:24

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

Postby Tourist Trap » Mar 28, 2020 15:33

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: 2945
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

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

Postby marcov » Mar 28, 2020 16:10

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: 1046
Joined: May 27, 2005 7:15
Location: FRANCE

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

Postby SARG » Mar 29, 2020 9:18

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.

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 7 guests