问题

问题

问题

  1. 为什么我会出现这个错误?Resource temporarily unavailable
  2. 如何在没有错误的情况下启动超过 10,000 个线程?
    我想用超过 10,000 个线程运行我的代码。

重现此问题的步骤

  1. 编写代码
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *thread ()
{
  sleep(9999);
}

int main()
{
  int err = 0, count = 0;
  pthread_t tid;

  while (err == 0)
  {
    err = pthread_create (&tid, NULL, thread, NULL);
    count++;
  }
  printf("max thread: %d\n", count);
  getchar();
}
  1. 编译并运行
$ gcc source.c -pthread
$ ./a.out &
max thread: 10800
  1. 运行命令
$ touch abc
-bash: fork: retry: Resource temporarily unavailable

$ vim abc
-bash: fork: retry: Resource temporarily unavailable

root@~# cd /etc
-bash: wait_for: No record of process 21875
root@/etc#

我尝试过什么

  1. 更改内核参数,但未修复
sysctl -w kernel.threads-max=600000
sysctl -w kernel.pid_max=600000
sysctl -w vm.max_map_count=600000
reboot
  1. 改变pids.max,但不是固定的。
$ vim /sys/fs/cgroup/pids/user.slice/user-0.slice/pids.max
999999
$ reboot 

环境

  • 乌班图18.04
  • CPU 32核
  • 内存 64GB
$ ulimit - a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 257435
max locked memory       (kbytes, -l) 16384
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65536
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 257435
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
$ uname -a
Linux localhost 4.15.0-50-generic #54-Ubuntu SMP Mon May 6 18:46:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
$ cat /proc/sys/kernel/threads-max
514870

相关内容