gpt4 book ai didi

c# - sched_setscheduler 的 P/Invoke 签名

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:41 27 4
gpt4 key购买 nike

我需要为 Linux 提供 P/Invoke 签名 sched_setaffinity函数,从在 Mono 上运行的 C# 代码调用。

int sched_setaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);

ThreadAffinity “open-hardware-monitor”项目中的类将其定义为:

[DllImport(LIBC)]
public static extern int sched_setaffinity(int pid, IntPtr maskSize,
ref ulong mask);

然而,Mono.LibRt项目使用不同的签名:

[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="sched_setaffinity")]
protected static extern int sched_setaffinity (long pid, int num_bytes,
byte[] affinity);

两个签名都正确,还是一个错误?它们是否可以跨 32 位和 64 位架构移植?

我假设第一个参数应该是 int,而不是 long,因为 pid_t 在 32-和 64 位平台(根据 Joe Shaw )。同样,根据 Patrick McDonald,第二个应该是 UIntPtrIntPtr .但是,我不知道如何处理第三个参数。

编辑:基于 Simon Mourier 的评论:我从 cpu_set_t * 类型中了解到,第三个参数应该是一个指针。 ref ulong 符合条件吗?我很想使用那种类型,因为它使宏更容易实现;例如,单个处理器 id 的掩码可以计算为:

ulong mask = 1UL << id;

我在 sched.h 中找到了 cpu_set_t 的定义,它作为 unsigned long int 数组实现。

最佳答案

如您所见,cpu_set_t 只是一个 unsigned long int 数组。所以我会这样声明函数:

[DllImport("libc.so.6", SetLastError=true)]
private static extern int sched_setaffinity(
int pid,
IntPtr cpusetsize,
ulong[] cpuset
);

关于c# - sched_setscheduler 的 P/Invoke 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118251/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com