Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Set thread quantum for process under Windows

by chessgui (Scribe)
on Jan 05, 2012 at 09:44 UTC ( #946347=perlquestion: print w/ replies, xml ) Need Help??
chessgui has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I would like to know if there is a foolproof way in perl to set the thread quantum of a process under Windows.

The process is opened as a bidirectional pipe and I have the pid.

This is necessary for me because I want a chess engine to make calculations on a position in the background while I'm able to do other things on the computer. Currently it eats up CPU time so much that everything else becomes painstakingly slow, web pages time out etc.

Comment on Set thread quantum for process under Windows
Re: Set thread quantum for process under Windows
by BrowserUk (Pope) on Jan 05, 2012 at 09:59 UTC

    Type start /? on your command line for how to start a command with a low or background priory.

    Hit ctrl-alt-del to bring up the task manager, right-clock the name on the processes tab and use "set Priority" ona running process.

    See Win32::Process and use the appropriate constant from:

    HIGH_PRIORITY_CLASS IDLE_PRIORITY_CLASS NORMAL_PRIORITY_CLASS REALTIME_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL THREAD_PRIORITY_BELOW_NORMAL THREAD_PRIORITY_ERROR_RETURN THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_IDLE THREAD_PRIORITY_LOWEST THREAD_PRIORITY_NORMAL THREAD_PRIORITY_TIME_CRITICAL ABOVE_NORMAL_PRIORITY_CLASS BELOW_NORMAL_PRIORITY_CLASS

    if you are starting the process from within perl.

    Personally, I find the first option the simplest even when starting a process from with a perl script:

    system q[start /low notepad.exe];;

    as it also allows me to set the affinity mask.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thanks a lot. These seem to be useful starting points. But my main goal is to do this on a constant basis (ie. not having to look my process up in the table manually every time rather do it automatically from the script) and having a look at the Win32::Process module it seems to me that if I open the process that way I won't have handles to the STDIN and STDOUT of the process, while if I open it through Open2 (as I do currently) there is not a Win32::Process object on which I can perform $ProcessObj->SetPriorityClass($class). There seems to be no method for creating a Win32::Process object from a pid.
        There seems to be no method for creating a Win32::Process object from a pid.

        Actually there is:

        Win32::Process::Open( $obj, $pid, $iflags );

        $pid is an existingprocess id as return by open2(), $obj is a variable you pass in which gets set to a Win32::Process object handle. You can the call ->SetPriority() on that. Eg.

        use Win32::Process;; $pid = system 1, 'notepad';; ## The return from open2 should work just + as well. Win32::Process::Open( $o, $pid, 0 );; print $o;; Win32::Process=SCALAR(0x4073208) $o->SetPriorityClass( IDLE_PRIORITY_CLASS );;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: Set thread quantum for process under Windows
by sundialsvc4 (Monsignor) on Jan 06, 2012 at 14:06 UTC

    It would surprise me greatly, indeed, if it proved to be the over-consumption of CPU time that is causing “everything else to become painstakingly slow” such that “web pages time out,” etc.   It is almost axiomatic to me that such disruption actually comes from the over-consumption of virtual memory (leading to thrashing), or very inefficient use of the file system, or sometimes, simply having too many processes or threads fighting for available milliseconds.   If this be so, then changing the dispatching-priority of a thread won’t help much at all, if at all.

    Now, thread priorities do make a noticeable difference in some GUI systems, most notably Microsoft Windows, which still to this day is very sensitive to main-threads which do not timely respond to GUI messages.   But this manifests itself as “visual jitterbugs,” such as mouse cursors that don’t move smoothly, even as other activities which do not directly involve the GUI continue apace.

      It is true that a chess engine tends to use a great amount of memory for its hash to store generated moves (of which there are tipically tens of millions). On the other hand once started it never 'sleeps' so consumes CPU resources as well - all the way long.

      Still: lowering the priority of the engine process has worked for me. Now I can open web pages even play online with the engine running in the background.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946347]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-07-31 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (253 votes), past polls