【问题标题】:Bash (WSL): Unable to limit the size of files written using ulimit -fBash (WSL):无法限制使用 ulimit -f 写入的文件的大小
【发布时间】:2021-06-27 11:21:07
【问题描述】:

晚上好。

我有一个简单的 C++ 程序 infinite_print.cpp,它写入了 400,000 行 "abcde...xyz"

在另一个 bash 脚本 script.sh 中,我执行了上述 C++ 代码并将输出重定向到 output.txt,即 ./infinite_print.exe > output.txt

我尝试通过设置ulimit -f 10 来限制使用此bash 脚本创建的output.txt 的大小,但失败了。 output.txt 的结果大小为 11MB。

Stackoverflow 说我还不允许在我的问题中附加屏幕截图,因此可以在此链接 here 上找到执行 script.sh 的屏幕截图。

我尝试阅读man bash,然后搜索/ulimit,谷歌搜索等等,但仍然不知道我做错了什么......

出于绝望,我创建了这个帐户并发布了这个问题。非常感谢所有帮助:(

编辑:如果相关,我在 WSL(Linux 的 Windows 子系统)上使用 Bash。

infinite_print.cpp的源代码:

1  #include <bits/stdc++.h>
2  using namespace std;
3
4  int limit = 400000;
5
6  int main() {
7      for (int i = 0; i < limit; ++i) {
8          cout << "abcdefghijklmnopqrstuvwxyz\n";
9      }
10 }

script.sh的源代码:

1  #!/bin/bash
2
3  printf "Current ulimit: "; ulimit -f
4
5  # Set new ulimit.
6  ulimit -f 10
7  printf "New ulimit: "; ulimit -f
8
9  g++ infinite_print.cpp -o inf.exe
10 ./inf.exe > output.txt
11
12 ls -lh output.txt

【问题讨论】:

  • 在我的 Linux 系统中,选项 -f 的单位是 1024 字节。我不知道你的系统怎么样。

标签: bash file redirect size ulimit


【解决方案1】:

bash 的手册页指出: 在允许这种控制的系统上,提供对 shell 和由它启动的进程可用的资源的控制。

查看屏幕截图,这些权限表明您没有在 Linux 或其他 Unix 系统下运行。也许您正在使用 Linux 子系统在 Windows 上运行,而 Windows 不支持这些设置。

这里肯定有用...

【讨论】:

  • 感谢您的回复!我确实在 WSL 上使用 Bash。回家后让我在虚拟机上试一试。可能是 Windows 问题。
  • 我在我的虚拟机上试过了, ulimit -f 确实有效。所以看来我的 WSL 是问题所在。谢谢围兜!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
相关资源
最近更新 更多