如何在golang中调用Linux/UNIX的系统调用

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

How can I invoke system call of Linux/UNIX in golang

问题

由于某些原因,我需要调用系统(Linux)的一些系统调用。
我查看了“syscall”包的文档,但没有找到相关内容。

然后我看到了一个项目(https://github.com/AllenDang/w32/blob/master/kernel32.go),它封装了Windows的API。我稍微阅读了一下它的源代码。

它使用:

modkernel32 = syscall.NewLazyDLL("kernel32.dll")

来加载动态库。然而,对于函数NewLazyDLL()没有文档说明。

我确定在Linux/UNIX中应该有类似的函数。有人可以告诉我Linux中调用系统调用的函数名称或者从libc.so加载函数的方法吗?

更多细节

我想调用系统调用“daemon”或“fork”(我想将进程变为守护进程),因为我找不到提供它们的Golang库。

英文:

For some reasons, I need invoke some system calls of the system(Linux)
I checked the documentation of the 'syscall' package and couldn't find anything about it.

Then I just saw a project (https://github.com/AllenDang/w32/blob/master/kernel32.go) which wraps windows apis. I read the source code of it a bit.

It uses

modkernel32 = syscall.NewLazyDLL("kernel32.dll")

to load the dynamic library. However, there is no documentation for function NewLazyDLL()

I am sure there should be a similar function for Linux/UNIX. Is there any one can tell
me the name of the function or the way to invoke system call of Linux in Golang or
load functions from libc.so ?

More details

I want to invoke system call 'daemon' or 'fork'(I want to daemonize the process) because I cannot find golang library provides them.

答案1

得分: 3

目前Go标准库中没有daemon风格的函数。有一个关于添加此功能的未解决的问题,但它已被推迟到Go 1.1发布之后。我建议阅读该错误报告,了解一些原因,它并不像一开始看起来那么简单。

除了让守护进程自身分叉之外,还有其他方法可以运行守护进程。例如,现代的init守护进程(如Upstart和Systemd)可以为您管理此类守护进程进程。

英文:

There is no daemon style function in the Go standard library at the moment. There is an open bug about adding such a feature, but it has been deferred until after the Go 1.1 release. I would suggest reading the bug report for some of the reasons it isn't quite as simple as it might first appear.

There are other ways to run daemon processes apart from having the daemon fork itself though. Modern init daemons like Upstart and Systemd can manage such daemon processes for you, for instance.

huangapple
  • 本文由 发表于 2013年3月27日 14:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/15652857.html
匿名

发表评论

匿名网友

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

确定