0

In a ps1 file I start 2 separate processes like so: As you can see the only difference is the CPU affinity.

$Process1 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process1.ProcessorAffinity = 65535
$Process1.PriorityClass = 'RealTime'

$Process2 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process2.ProcessorAffinity = 4294901760
$Process2.PriorityClass = 'RealTime'

Sometimes, randomly, one of the 2 processes fails with: Problem Event Name: BEX64

It is essential that the process is restarted immediately when a failure like this happens. So I am faced with this problem.

Can I monitor within the PS1 file the 2 processes and if one fails to restart it? The monitoring process must be able to distinguish which of the 2 processes has failed, so that the correct affinity is used to restart it.

If you have a solution involving windows batch script please visit How to RESTART a specific command if it fails in Windows Batch file?

Thanks

3
  • Process.ProcessorAffinity is a bitmask representing the CPUs, so using decimal for it makes zero sense. Use $Process1.ProcessorAffinity = 0xFFFF instead of 65535 Commented Jul 14, 2021 at 0:44
  • actually powershell has asked me to use IntPtr and accepts these values without issue. I am using them because it would not accept 0x00000000FFFF0000 Commented Jul 14, 2021 at 0:52
  • Who said that 0x00000000FFFF0000 isn't accepted? Unless your CPU has fewer than 32 CPUs. 65535 = 0xFFFF, not 0x00000000FFFF0000. To powershell both decimal and hex literals represent the same value but to human readers the decimal value doesn't make sense Commented Jul 14, 2021 at 1:31

1 Answer 1

3
$Process1 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process1.ProcessorAffinity = 1
$Process1.PriorityClass = 'RealTime'
Write-Host Process ID $process1.Id started with CPU affinity $Process1.ProcessorAffinity

$Process2 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process2.ProcessorAffinity = 2
$Process2.PriorityClass = 'RealTime'
Write-Host Process ID $process2.Id started with CPU affinity $Process2.ProcessorAffinity

while($true){
    foreach($process in $Process1,$Process2){
        if($process.hasexited){

            Write-Host "Process ID $($process.id) has exited, restarting."
            $priority = $process.PriorityClass
            $affinity = $process.ProcessorAffinity
            if($newprocess = $process.Start()){
                Write-Host Successfully restarted process ID $process.Id with CPU affinity $process.ProcessorAffinity
                $process.PriorityClass = $priority
                $process.ProcessorAffinity = $affinity
                Write-Host Priority Class set back to $process.PriorityClass
                Write-Host Process affinity set back to $process.ProcessorAffinity
            }
        }
    }
    Start-Sleep -Milliseconds 500
}
Sign up to request clarification or add additional context in comments.

5 Comments

Good answer. only problem is that when you restart the process, the affinity and priority are not retained. correct?
The affinity seemed to be the same in my tests but they were very basic/quick.
@conanDrum It seems you are correct about the PriorityClass, I've updated my answer. Thanks!
I get this:Process ID 9744 started with CPU affinity 65535 Process ID 19312 started with CPU affinity 4294901760 Process ID 19312 has exited, restarting. Successfully restarted as process ID 16120 with CPU affinity 4294967295 Process ID 16120 has exited, restarting. Successfully restarted as process ID 20084 with CPU affinity 4294967295
Affinity is always 4294967295

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.