Home  >  Article  >  Backend Development  >  Why does setting `ulimit -n` from a Go program on Linux 32-bit distributions result in an \"invalid argument\" error?

Why does setting `ulimit -n` from a Go program on Linux 32-bit distributions result in an \"invalid argument\" error?

DDD
DDDOriginal
2024-11-03 06:51:02811browse

Why does setting `ulimit -n` from a Go program on Linux 32-bit distributions result in an

How to set ulimit -n from a Go program?

Problem

A user attempted to set ulimit -n from within a Go program to restrict it within the program rather than globally, using the setrlimit and getrlimit system calls. However, an error occurred when trying to set the value, stating "invalid argument."

Solution

It was discovered that the issue was due to Go bugs in Getrlimit and Setrlimit for Linux 32-bit distributions. These bugs have since been fixed, and the following steps can be taken to resolve the problem:

  1. Update to the Go default branch tip. This will include the bug fixes.
  2. Rebuild the program using the updated Go version.

Results

After updating to the Go default branch tip and rebuilding the program, the expected behavior is observed:

$ ./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}

Note: For Linux/amd64 distributions, the issue was not encountered. The problem was specific to Linux 32-bit distributions.

The above is the detailed content of Why does setting `ulimit -n` from a Go program on Linux 32-bit distributions result in an \"invalid argument\" error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn