質問

質問

質問

  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 

環境

  • Ubuntu18.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

ベストアンサー1

おすすめ記事