問:如何從Golang 程式設定ulimit -n 以將其限制在
A:
要從Golang 程式中設定ulimit -n,您可以使用以下步驟:
<code class="go">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) }</code>
注意: 設定值時您可能會收到“無效參數”錯誤。這是因為您需要以提升的權限來執行程式。
$ uname -a Linux peterSO 3.8.0-26-generic #38-Ubuntu SMP Mon Jun 17 21:46:08 UTC 2013 i686 i686 i686 GNU/Linux $ go version go version devel +ba52f6399462 Thu Jul 25 09:56:06 2013 -0400 linux/386 $ ulimit -Sn 1024 $ ulimit -Hn 4096 $ go build rlimit.go $ ./rlimit {1024 4096} Error Setting Rlimit operation not permitted Rlimit Final {1024 4096} $ sudo ./rlimit [sudo] password for peterSO: {1024 4096} Rlimit Final {999999 999999} $
以上是如何從 Golang 程式設定 ulimit -n 而不影響全域限制?的詳細內容。更多資訊請關注PHP中文網其他相關文章!