Set the number of CPUs used by the System

Source: Internet
Author: User

Some programs run abnormally on multiple CPU Systems. To solve this problem, I wrote a program to change the number of CPU workers of the process.

The source code is as follows:

Unit Main; <br/> interface <br/> uses <br/> Windows, messages, sysutils, variants, classes, graphics, controls, forms, <br/> dialogs, tlhelp32, stdctrls, buttons; <br/> type <br/> tfrmmain = Class (tform) <br/> button1: tbutton; <br/> button2: tbutton; <br/> opendialog1: topendialog; <br/> button3: tbutton; <br/> groupbox1: tgroupbox; <br/> label1: tlabel; <br/> edit1: tedit; <br/> speedbutton1: tspeed Button; <br/> label3: tlabel; <br/> radiobutton1: tradiobutton; <br/> identifier: tradiobutton; <br/> radiobutton4: tradiobutton; <br/> radiobutton5: tradiobutton; <br/> usage: tradiobutton; <br/> radiobutton6: tradiobutton; <br/> procedure button1click (Sender: tobject); <br/> procedure button2click: tobject); <br/> procedure speedbutton1click (Sender: tobject); <br/> Procedure Form Create (Sender: tobject); <br/> procedure button3click (Sender: tobject ); <br/> private <br/> {private Declarations} <br/> Public <br/> {public declarations} <br/> function getcpu_num (): DWORD; <br/> end; <br/> var <br/> frmmain: tfrmmain; <br/> function setprocesscpu_num: Boolean; <br/> Implementation <br/> {$ R *. DFM} </P> <p> function max (A, B: integer): integer; <br/> begin <br/> If A> B then <br/> Result: = A <br/> else <br/> result: = B; <br/> end; </P> <p> function setcpuprocessbyfullpath (modulefullpath: string; appname: string = ''; real_cpu_num: DWORD = 1): integer; <br/> var <br/> lppe: tprocessentry32; <br/> found: Boolean; <br/> processlist: thandle; <br/> modulelist: thandle; <br/> PM: tmoduleentry32; <br/> H: thandle; <br/> A: DWORD; <br/> modulename: string; <br/> appprocessid: DWORD; // Thread id <br/> begin <br/> result: = 0; <br/> appprocessid: = getcurrentprocessid; <br/> If length (appname) = 0 then <br/> appname: = extractfilename (modulefullpath); <br/> lppe. dwsize: = sizeof (tprocessentry32); <br/> PM. dwsize: = sizeof (tmoduleentry32); <br/> processlist: = createconlhelp32snapshot (th32cs_snapprocess, 0); <br/> found: = process32first (processlist, lppe ); <br/> while found do // process column Table <br/> begin <br/> If stricomp (pchar (strpas (lppe. szexefile), pchar (appname) = 0 then // is the application name the same? <Br/> begin <br/> modulelist: = createconlhelp32snapshot (th32cs_snapmodule, lppe. th32processid); <br/> found: = module32first (modulelist, PM); <br/> while found do // module list <br/> begin <br/> modulename: = strpas (PM. szexepath); <br/> modulename: = copy (modulename, max (1, pos (':', modulename)-1), maxint ); <br/> If stricomp (pchar (modulename), pchar (modulefullpath) = 0 then // whether the module to be searched is included <br/> begin <br /> If appprocessid <> lppe. th32processid then <br/> begin <br/> H: = OpenProcess (process_all_access, true, lppe. th32processid); <br/> If setprocessaffinitymask (H, real_cpu_num) Then <br/> Inc (result); <br/> end; <br/> break; <br/> end; <br/> found: = module32next (modulelist, PM); <br/> end; <br/> closehandle (modulelist ); <br/> end; <br/> found: = process32next (processlist, lppe); <br/> end; <br/> Closehandle (processlist); <br/> end; <br/> function tfrmmain. getcpu_num (): DWORD; <br/> begin <br/> If radiobutton1.checked then <br/> result: = $0001 <br/> else if radiobutton2.checked then <br/> result: = $0002 <br/> else if radiobutton3.checked then <br/> result: = $0003 <br/> else if radiobutton4.checked then <br/> result: = $ 000f <br/> else if radiobutton5.checked then <br/> result: = $ 007f <br /> Else if radiobutton6.checked then <br/> result: = $0007; <br/> end; <br/> procedure tfrmmain. button1click (Sender: tobject); <br/> var <br/> I, cpu_num: integer; <br/> begin <br/> // For example, the system has two CPUs, parameter description <br/> // 1: indicates that the process runs on the first CPU <br/> // 2: indicates that the process runs on the second CPU <br/> // 3: indicates that the process runs on both CPUs <br/> cpu_num: = getcpu_num; <br/> I: = setcpuprocessbyfullpath (edit1.text, '', cpu_num); <br/> If I <1 then <br/> showmes Sage ('settings failed! ') <Br/> else <br/> showmessage (' set successfully! ') <Br/> end; <br/> procedure tfrmmain. button2click (Sender: tobject); <br/> var <br/> cpu_num: DWORD; <br/> H: thandle; <br/> begin <br/> cpu_num: = getcpu_num; <br/> H: = getcurrentprocess (); <br/> If setprocessaffinitymask (H, cpu_num) Then <br/> showmessage ('set successfully! '); <Br/> end; <br/> procedure tfrmmain. button3click (Sender: tobject); <br/> begin <br/> close; <br/> end; <br/> procedure tfrmmain. formcreate (Sender: tobject); <br/> var <br/> lpsysteminfo: system_info; <br/> begin <br/> getsysteminfo (lpsysteminfo); <br/> label3.caption: = label3.caption + inttostr (lpsysteminfo. dwnumberofprocessors); <br/> radiobutton1.checked: = true; <br/> If lpsysteminfo. dwnumberofprocessors <= 1 then <br/> begin <br/> radiobutton2.enabled: = false; <br/> radiobutton3.enabled: = false; <br/> radiobutton4.enabled: = false; <br/> radiobutton5.enabled: = false; <br/> radiobutton6.enabled: = false; <br/> end <br/> else if lpsysteminfo. dwnumberofprocessors = 2 then <br/> begin <br/> radiobutton4.enabled: = false; <br/> radiobutton5.enabled: = false; <br/> radiobutton6.enabled: = false; <br/> end <br/> else if lpsysteminfo. dwnumberofprocessors = 4 then <br/> begin <br/> radiobutton6.enabled: = false; <br/> end <br/> else if lpsysteminfo. dwnumberofprocessors = 8 then <br/> begin <br/> // <br/> end; <br/> setprocesscpu_num; <br/> end; <br/> procedure tfrmmain. speedbutton1click (Sender: tobject); <br/> begin <br/> If opendialog1.execute then <br/> begin <br/> edit1.text: = opendialog1.filename; <br/> end; <br/> end; <br/> // when the number of CPU cores of the system exceeds 4, set the number of CPU cores of the process to 4. <br/> function setprocesscpu_num: Boolean; <br/> var <br/> lpsysteminfo: system_info; <br/> cpu_num: DWORD; <br/> H: thandle; <br/> begin <br/> result: = true; <br/> getsysteminfo (lpsysteminfo); <br/> If lpsysteminfo. dwnumberofprocessors> 4 then <br/> begin <br/> cpu_num: = $ 000f; <br/> H: = getcurrentprocess (); <br/> result: = setprocessaffinitymask (H, cpu_num); <br/> end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.