如何在Windows中使用Go将进程设置到特定的CPU上?

huangapple go评论126阅读模式
英文:

How set process to a CPU using Go in windows?

问题

我想在Windows 7中使用Go语言将进程绑定到CPU。以下是代码:

  1. package main
  2. import (
  3. "fmt"
  4. "runtime"
  5. "syscall"
  6. "unsafe"
  7. )
  8. func SetAffinity(pid int, mask *int64) {
  9. // 在Windows中设置进程绑定的CPU
  10. // 这里需要使用Windows API来实现
  11. // 可以使用syscall.SetProcessAffinityMask函数来设置进程的亲和性掩码
  12. }
  13. func main() {
  14. // 在这里调用SetAffinity函数来设置进程绑定的CPU
  15. }

在Windows中,你可以使用syscall.SetProcessAffinityMask函数来设置进程绑定的CPU。你需要将进程的句柄和亲和性掩码作为参数传递给该函数。具体的实现细节可以参考Windows API文档或者相关的Go语言库。

英文:

I want set a process to a CPU using Go in win7, the below code:

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. "runtime"
  6. "syscall"
  7. "unsafe"
  8. )
  9. func SetAffinity(pid int, mask *int64) {
  10. syscall.Syscall(syscall.SYS_SCHED_SETAFFINITY,
  11. uintptr(pid), 8, uintptr(unsafe.Pointer(mask)))
  12. }
  13. func GetAffinity(pid int, mask *int64) {
  14. syscall.Syscall(syscall.SYS_SCHED_GETAFFINITY,
  15. uintptr(pid), 8, uintptr(unsafe.Pointer(mask)))
  16. }
  17. var cpuNum = float64(runtime.NumCPU())
  18. var setx = []struct {
  19. args int
  20. expected int64
  21. }{
  22. {0, int64(math.Pow(2, cpuNum)) - 2},
  23. }
  24. func main() {
  25. for _, ca := range setx {
  26. var cpuSet int64
  27. GetAffinity(ca.args, &cpuSet)
  28. cpuSet = cpuSet & 0XFFFFFFE
  29. SetAffinity(ca.args, &cpuSet)
  30. fmt.Println(cpuSet)
  31. GetAffinity(ca.args, &cpuSet)
  32. fmt.Println(cpuSet)
  33. }
  34. }

When I use go run affinity.go, get the follow info:

  1. # command-line-arguments
  2. .\affinity.go:12: undefined: syscall.SYS_SCHED_SETAFFINITY
  3. .\affinity.go:13: not enough arguments in call to syscall.Syscall
  4. .\affinity.go:17: undefined: syscall.SYS_SCHED_GETAFFINITY
  5. .\affinity.go:18: not enough arguments in call to syscall.Syscall

I find SYS_SCHED_SETAFFINITY that it only used in linux.
So, I want to set a process to a cpu using Go in Windows(Win7), what can I do?

答案1

得分: 2

你需要调用WinAPI的SetProcessAffinityMask函数。

类似下面的代码应该可以工作:

  1. func setProcessAffinityMask(h syscall.Handle, mask uintptr) (err error) {
  2. r1, _, e1 := syscall.Syscall(syscall.NewLazyDLL("kernel32.dll").NewProc("SetProcessAffinityMask").Addr(), 2, uintptr(h), mask, 0)
  3. if r1 == 0 {
  4. if e1 != 0 {
  5. err = error(e1)
  6. } else {
  7. err = syscall.EINVAL
  8. }
  9. }
  10. return
  11. }

其中,h是进程句柄,mask是所需的亲和性掩码。

这段代码摘自Go的基准测试,使用的是BSD许可证。

英文:

You'll have to invoke the WinAPI SetProcessAffinityMask.

Something like this should work:

  1. func setProcessAffinityMask(h syscall.Handle, mask uintptr) (err error) {
  2. r1, _, e1 := syscall.Syscall(syscall.NewLazyDLL("kernel32.dll").NewProc("SetProcessAffinityMask").Addr(), 2, uintptr(h), mask, 0)
  3. if r1 == 0 {
  4. if e1 != 0 {
  5. err = error(e1)
  6. } else {
  7. err = syscall.EINVAL
  8. }
  9. }
  10. return
  11. }

h being the process handle, and mask being the desired affinity mask, of course.

This is taken from Go benchmarks, under the BSD license.

huangapple
  • 本文由 发表于 2015年5月7日 18:18:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/30098205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定