c使用系统运行自己的程序

fre*_*red 24 c

我正在学习函数system()是stdlib.h并意识到我可以创建一个使用system()运行自己的程序.我写了这段代码并试了一下:

#include <stdio.h>
#include <stdlib.h>

int main(){
    printf("x");
    system("./a.out");
}
Run Code Online (Sandbox Code Playgroud)

它每次运行时都会正常打印563 x,然后才能正常退出(没有错误).我想知道是什么阻止了程序以及这个数字的来源,因为它对我来说似乎很随意.谢谢

感谢您对第一个程序的见解,但我不相信系统正在停止它,因为资源耗尽的原因如下:我刚刚编写了这个新程序,但它还没有停止.

#include <stdio.h>
#include <stdlib.h>

int main(){
    printf("x");
    system("./a.out");
    system("./a.out");
}
Run Code Online (Sandbox Code Playgroud)

此外,当我尝试打开一个新的控制台窗口时,我收到此错误:

/.oh-my-zsh/lib/theme-and-appearance.zsh:24: fork failed: resource temporarily unavailable

/.oh-my-zsh/oh-my-zsh.sh:57: fork failed: resource temporarily unavailable
Run Code Online (Sandbox Code Playgroud)

idl*_*dle 31

我将首先处理第二个程序,因为这是最容易解释的.尝试使用此代码,它将打印出递归深度.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv){
  int depth = argc > 1 ? atoi(argv[1]) : 0;
  printf("%d\n", depth);
  char cmd[128];
  sprintf(cmd, "%s %d", "./a.out", depth+1);
  system(cmd);
  system(cmd);
}
Run Code Online (Sandbox Code Playgroud)

它会长大到你的极限(在我的情况下是538),然后开始在递归树上上下乱.

530 531 532 533 534 535 536 537 538 538 537 538 538 536 537
Run Code Online (Sandbox Code Playgroud)

最终这个过程会结束,但需要很长时间!

至于第一个节目.我相信您只是遇到了用户进程限制.

您可以通过运行找到您的过程限制

ulimit -u
Run Code Online (Sandbox Code Playgroud)

在我的情况下,限制是709.计算我运行的其他进程

ps aux | grep user | wc -l
Run Code Online (Sandbox Code Playgroud)

这给了我171. 171 + 538(程序死亡的深度)给你一个可靠的答案:)

https://superuser.com/questions/559709/how-to-change-the-maximum-number-of-fork-process-by-user-in-linux


R S*_*ahu 5

程序中没有任何东西可以阻止无限递归.

You execute a.out.  
  a.out executes a.out  
    a.out executes a.out  
      a.out executes a.out  
        a.out executes a.out  
Run Code Online (Sandbox Code Playgroud)

等等.

在某些时候,系统运行资源并且不执行下一个system调用,程序以相反的顺序退出.您的计算机似乎在运行程序563次时达到了限制.