我的目的是在 golang 程序中设置 ulimit -n,这样我就不必全局设置它,而是在程序中限制它。
发现系统调用 setrlimit 和 get rlimit 相同。( http://linux.die.net/man/2/setrlimit )
但是当我尝试相同的示例程序时,我在设置值时收到一条错误消息,指出无效参数。
package main
import (
"fmt"
"syscall"
)
func main() {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting Rlimit ", err)
}
fmt.Println(rLimit)
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Setting Rlimit ", err)
}
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting Rlimit ", err)
}
fmt.Println("Rlimit Final", rLimit)
}
得到的输出是:
george@george-Not-Specified ~/work/odesk/progium/trial $ ./getRlimit
{4294963002032703 0}
Error Setting Rlimit invalid argument
Rlimit Final {4294963002032703 999999}
george@george-Not-Specified ~/work/odesk/progium/trial $ sudo ./getRlimit
[sudo] password for george:
{4294963002032703 0}
Error Setting Rlimit invalid argument
Rlimit Final {4294963002032703 999999}
george@george-Not-Specified ~/work/odesk/progium/trial $ uname -a
Linux george-Not-Specified 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012 i686 i686 i686 GNU/Linux
george@george-Not-Specified ~/work/odesk/progium/trial $
所以我能够得到 rlimit 设置限制失败并返回错误。即使它失败了,当我再次取值时 MAX 值发生了变化,但 CUR 值保持不变。这个错误是由于我的内核有问题还是一个坏程序?我在哪里可以找到更多信息以及如何处理此类问题?
郎朗坤
相关分类