[英]How and where is syscall.Syscall() defined for Windows-specific system calls in Golang's syscall package?
[英]Golang: How to use syscall.Syscall on Linux?
关于在 Windows ( https://github.com/golang/go/wiki/WindowsDLLs ) 上加载共享库和使用 syscall 包调用函数的描述非常好。 但是,本说明中使用的函数LoadLibrary
和GetProcAddress
在 Linux 上的 syscall 包中不可用。 我找不到有关如何在 Linux(或 Mac OS)上执行此操作的文档。
感谢帮助
Linux 系统调用直接使用而不加载库,具体如何取决于您想要执行的系统调用。
我将使用 Linux 系统调用 getpid() 作为示例,它返回调用进程(在本例中为我们的进程)的进程 ID。
package main
import (
"fmt"
"syscall"
)
func main() {
pid, _, _ := syscall.Syscall(syscall.SYS_GETPID, 0, 0, 0)
fmt.Println("process id: ", pid)
}
我在 pid 中捕获了 syscall 的结果,这个特定的调用没有返回错误,所以我对其余的返回值使用空白标识符。 系统调用返回两个 uintptr 和 1 个错误。
正如你所看到的,我也可以只为其余的函数参数传递 0,因为我不需要将参数传递给这个系统调用。
函数签名为:func Syscall(trap uintptr, nargs uintptr, a1 uintptr, a2 uintptr, a3 uintptr) (r1 uintptr, r2 uintptr, err Errno)。
有关更多信息,请参阅https://golang.org/pkg/syscall/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.