c - 试图找出我可以打开的最大进程数是多少

标签 c unix

我是 C 语言的初学者。 我有这个任务要编写一个程序,该程序将找到我可以打开的最大进程数。

我得出了这段代码:

int main() {

while (1){
    pid_t pid = fork();
    if(pid) {
        if ( pid == -1){
            fprintf(stderr,"Can't fork,error %d\n",errno);
            exit(EXIT_FAILURE);
        }else{
            int status;
            alarm(30);
            if(waitpid(pid, &status, 0)==pid) {
                alarm(0);
                // the child process complete within 30 seconds
                printf("Waiting.");
            }else {
                alarm(0);
                // the child process does not complete within 30 seconds
                printf("killed");
                kill(pid, SIGTERM);
            }
        }
    }
    else{
        alarm(30);
        printf("child");
    }
}
}

问题是这个程序导致我的笔记本电脑崩溃..:-|

我假设当程序无法打开更多进程时,我将从 fork() 中得到 -1,然后退出程序。 好吧,这并没有发生。

有什么想法吗? 我在这里缺少什么?

谢谢!

最佳答案

如果您确实想知道可以打开多少个进程,您可以使用 sysconf 调用来查找 _SC_CHILD_MAX 变量。 Check here .

关于c - 试图找出我可以打开的最大进程数是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13642917/

相关文章:

c - 在 C 中,字符数组是否始终且仅用于表示字符串?

c - 如何将 "%99s "克隆到 : fscanf(file, "%d %99s", &id, name) == 2)

python - 将 UNIX python 程序转换为在 Windows 中运行

linux - 如何在没有临时文件的情况下将一些文本附加到管道

c - 如何阻止 gcc 将带有标准库路径的 -L 传递给链接器

c++ - avcodec_receive_packet 中的错误(gdi screenshot + ffmpeg)

改变线程执行的函数

python - 在 I/O 重定向中执行 Python 脚本时出现 EOFError

date - 系统日期到可变unix

linux:如何对文本输入执行类似于 awk 的统计?