Home All Groups Group Topic Archive Search About

is there a "process affinity" switch

Author
17 Oct 2005 6:00 PM
Tony
Hi All,

    Please forgive my ignorance on this issue.

    I need my program to only run on one processor (for some
reason, Hyperthread screws up my text writes).

    In a previous post, a kind person showed me the
code to set my "process affinity."    Before I go and have
this coded into the program, I wanted to make sure there
was no switch in the compiler that would essentially do
the same thing.

Many thanks,
--Tony

Author
17 Oct 2005 8:22 PM
Mike D Sutton
>     Please forgive my ignorance on this issue.
>
>     I need my program to only run on one processor (for some
> reason, Hyperthread screws up my text writes).
>
>     In a previous post, a kind person showed me the
> code to set my "process affinity."    Before I go and have
> this coded into the program, I wanted to make sure there
> was no switch in the compiler that would essentially do
> the same thing.

Not in VB6, have a look at the SetProcessAffinityMask() API call:

'***
Private Declare Function GetCurrentProcess Lib "Kernel32.dll" () As Long
Private Declare Function SetProcessAffinityMask lib "Kernel32.dll" ( _
    ByVal hProcess As Long, ByVal dwProcessAffinityMask As Long) As Long

....

' Only use the first processor
Call SetProcessAffinityMask(GetCurrentProcess(), &H1)
'***

There's a little more in this old thread on the subject:
http://groups.google.co.uk/group/microsoft.public.vb.winapi/msg/83e88190612c4212
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
18 Oct 2005 6:35 PM
Tony
Mike D Sutton wrote:

Show quoteHide quote
> Not in VB6, have a look at the SetProcessAffinityMask() API call:
>
> '***
> Private Declare Function GetCurrentProcess Lib "Kernel32.dll" () As Long
> Private Declare Function SetProcessAffinityMask lib "Kernel32.dll" ( _
>     ByVal hProcess As Long, ByVal dwProcessAffinityMask As Long) As Long
>
> ...
>
> ' Only use the first processor
> Call SetProcessAffinityMask(GetCurrentProcess(), &H1)
> '***
>
> There's a little more in this old thread on the subject:
> http://groups.google.co.uk/group/microsoft.public.vb.winapi/msg/83e88190612c4212
> Hope this helps,
>
>     Mike
>
>
>  - Microsoft Visual Basic MVP -
> E-Mail: ED***@mvps.org
> WWW: Http://EDais.mvps.org/
>
>

Thank you!  Love examples!   :-)
--Tony
Author
17 Oct 2005 8:26 PM
Ken Halter
Show quote Hide quote
"Tony" <T***@invalid.com> wrote in message
news:dj0oqi$ft9$2@domitilla.aioe.org...
> Hi All,
>
>    Please forgive my ignorance on this issue.
>
>    I need my program to only run on one processor (for some
> reason, Hyperthread screws up my text writes).
>
>    In a previous post, a kind person showed me the
> code to set my "process affinity."    Before I go and have
> this coded into the program, I wanted to make sure there
> was no switch in the compiler that would essentially do
> the same thing.
>
> Many thanks,
> --Tony

Since VB6 came out long before Hyperthreading, I doubt you'll find a
compiler switch. All compiler options are shown by clicking 'Project/<your
project name> Properties/Compile'

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
18 Oct 2005 1:28 PM
Michael D. Ober
Show quote Hide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:utpSHk10FHA.3256@TK2MSFTNGP09.phx.gbl...
> "Tony" <T***@invalid.com> wrote in message
> news:dj0oqi$ft9$2@domitilla.aioe.org...
> > Hi All,
> >
> >    Please forgive my ignorance on this issue.
> >
> >    I need my program to only run on one processor (for some
> > reason, Hyperthread screws up my text writes).
> >
> >    In a previous post, a kind person showed me the
> > code to set my "process affinity."    Before I go and have
> > this coded into the program, I wanted to make sure there
> > was no switch in the compiler that would essentially do
> > the same thing.
> >
> > Many thanks,
> > --Tony
>
> Since VB6 came out long before Hyperthreading, I doubt you'll find a
> compiler switch. All compiler options are shown by clicking 'Project/<your
> project name> Properties/Compile'
>

However, since VB 6 came out after the advent of multi-processor systems,
this isn't a valid case against such a switch.  The issue is simply that VB
6, at it's heart, is single threaded and was never designed for
multi-processing.  Even ActiveX controls are internally single threaded.
The COM subsystem in Windows is what allows them to appear to be
multi-threaded.  This is also one of the sources of numerous posts I've seen
over the years that run along the lines of "I've subclassed my form, but the
IDE keeps crashing when I'm debugging them."

Mike Sutton's solution will work on Windows 2000, but it may fail on XP and
Server 2003.  The difference is that W2K treats HT enabled processors as if
they are seperate processors while XP and later are HT aware and somtimes
treats them as a single processor.  When true multi-core processors start
arriving en-masse later this year and next year, the problem will get even
worse.

OP - please post your code that is having the problems.  I suspect the real
solution would be to create a Critical Section around your file writes.
This way only a single thread could write to the file at a time.  If, as I
suspect, the writes are coming from two seperate processes, the Critical
Thread section will requre the use of a system wide semaphore, making it a
little more difficult, but not too much so.

Mike Ober.


Show quoteHide quote
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
>
>
Author
18 Oct 2005 6:30 PM
Tony
> OP - please post your code that is having the problems.  I suspect the real
> solution would be to create a Critical Section around your file writes.
> This way only a single thread could write to the file at a time.  If, as I
> suspect, the writes are coming from two seperate processes, the Critical
> Thread section will requre the use of a system wide semaphore, making it a
> little more difficult, but not too much so.
>
> Mike Ober.

Hi Mike,

    I am having (WinAPI) flushes added to each (WinAPI) WriteFile
statement.  I am also going to add the other Mike's
SetProcessAffinityMask statement at the beginning of the program.

    It will be a couple of weeks before I get results back.
If Happy Camping is not achieved, I will repost the section
of code that writes to the text file.

    I appreciate your help and comments.

Many thanks,
--Tony
Author
18 Oct 2005 7:02 PM
Mike D Sutton
> Mike Sutton's solution will work on Windows 2000, but it may fail on XP and
> Server 2003.  The difference is that W2K treats HT enabled processors as if
> they are seperate processors while XP and later are HT aware and somtimes
> treats them as a single processor.

If hyperthreading is not enabled in the BIOS then Windows will see a single processor (and treat it as a single
processor), however as long as it's enabled then the OS should see two processors.  I've tested and verified this on 3
machines running XP or Server 2003 as AFAIK hyperthreading is not supported in Win2K.
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
19 Oct 2005 2:20 PM
Michael D. Ober
In W2K, hyperthreaded CPUs are reported as distinct CPUs.  The OS itself
doesn't know that they are actually a single HT enabled CPU.  The problem is
that the SetAffinityMask API doesn't know either.  There is an additional
API that deals with HT and multi-core CPUs.

Mike Ober.

"Mike D Sutton" <ED***@mvps.org> wrote in message
news:uhUAGaB1FHA.2072@TK2MSFTNGP14.phx.gbl...
> > Mike Sutton's solution will work on Windows 2000, but it may fail on XP
and
> > Server 2003.  The difference is that W2K treats HT enabled processors as
if
> > they are seperate processors while XP and later are HT aware and
somtimes
> > treats them as a single processor.
>
> If hyperthreading is not enabled in the BIOS then Windows will see a
single processor (and treat it as a single
> processor), however as long as it's enabled then the OS should see two
processors.  I've tested and verified this on 3
> machines running XP or Server 2003 as AFAIK hyperthreading is not
supported in Win2K.
Show quoteHide quote
> Hope this helps,
>
>     Mike
>
>
>  - Microsoft Visual Basic MVP -
> E-Mail: ED***@mvps.org
> WWW: Http://EDais.mvps.org/
>
>
>