我想在win7中使用Go将进程设置为CPU,代码如下:package mainimport ( "fmt" "math" "runtime" "syscall" "unsafe")func SetAffinity(pid int, mask *int64) { syscall.Syscall(syscall.SYS_SCHED_SETAFFINITY, uintptr(pid), 8, uintptr(unsafe.Pointer(mask)))}func GetAffinity(pid int, mask *int64) { syscall.Syscall(syscall.SYS_SCHED_GETAFFINITY, uintptr(pid), 8, uintptr(unsafe.Pointer(mask)))}var cpuNum = float64(runtime.NumCPU())var setx = []struct { args int expected int64}{ {0, int64(math.Pow(2, cpuNum)) - 2},}func main() { for _, ca := range setx { var cpuSet int64 GetAffinity(ca.args, &cpuSet) cpuSet = cpuSet & 0XFFFFFFE SetAffinity(ca.args, &cpuSet) fmt.Println(cpuSet) GetAffinity(ca.args, &cpuSet) fmt.Println(cpuSet) }}当我使用时go run affinity.go,获取以下信息:# command-line-arguments.\affinity.go:12: undefined: syscall.SYS_SCHED_SETAFFINITY.\affinity.go:13: not enough arguments in call to syscall.Syscall.\affinity.go:17: undefined: syscall.SYS_SCHED_GETAFFINITY.\affinity.go:18: not enough arguments in call to syscall.Syscall我发现SYS_SCHED_SETAFFINITY它只在 linux 中使用。那么,我想在Windows(Win7)中使用Go将进程设置为cpu,我该怎么办?
1 回答
凤凰求蛊
TA贡献1825条经验 获得超4个赞
您必须调用 WinAPI SetProcessAffinityMask。
这样的事情应该工作:
func setProcessAffinityMask(h syscall.Handle, mask uintptr) (err error) {
r1, _, e1 := syscall.Syscall(syscall.NewLazyDLL("kernel32.dll").NewProc("SetProcessAffinityMask").Addr(), 2, uintptr(h), mask, 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
h当然,它是进程句柄,并且mask是所需的亲和掩码。
这是从BSD 许可下的Go benchmarks 中获取的。
- 1 回答
- 0 关注
- 188 浏览
添加回答
举报
0/150
提交
取消