Armadilloフォーラム

スレッドの生成と、プロセス終了

hakaru_plus

2018年3月24日 15時55分

お世話になります。山下と申します。

現在、Armadilloで動作させるプロセスを、C言語で開発中です。
このプロセスは、Armadilloのシステム起動~終了まで、常時バックグラウンドで動かす予定です。

その中の処理に、pthread_create()関数でスレッドを生成し、仮想メモリを消費する処理があるのですが、pthread_join()関数やpthread_detach()関数は実行しなくとも、システム終了後のArmadillo電源OFFで仮想メモリの消費はリセットされると考えていいのでしょうか?
また、1つのプロセスが生成できるスレッドの上限はいくつなのでしょうか?

以上、アドバイスよろしくお願いします。

コメント

at_mizo

2018年3月26日 9時11分

溝渕です。

> システム終了後のArmadillo電源OFFで仮想メモリの消費はリセットされると考えていいのでしょうか?

良いです。PCと同じで、電源OFFするとメモリの状態は保持されません。

> また、1つのプロセスが生成できるスレッドの上限はいくつなのでしょうか?

上記のような上限があるかは把握していません(私が知らないだけであるかもしれません)。

Linuxシステム全体でのスレッド数の上限は、次のように確認することができます。

cat /proc/sys/kernel/threads-max

y.nakamura

2018年3月26日 9時43分

中村です。

> > また、1つのプロセスが生成できるスレッドの上限はいくつなのでしょうか?
> 上記のような上限があるかは把握していません(私が知らないだけであるかもしれません)。

ulimitコマンドの"-u"でプロセス数制限がわかります。

# ulimit -u
3382

Linuxのスレッドはプロセス数の制限を受けるはずだったと思います。
ulimitが表示する内容は確か・・・
man GETRLIMIT をみると、

RLIMIT_NPROC
The maximum number of processes (or, more precisely on Linux,
threads) that can be created for the real user ID of the calling
process. Upon encountering this limit, fork(2) fails with the
error EAGAIN. This limit is not enforced for processes that
have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capabil‐
ity.

それから、プロセスごとのlimit表示は/proc/[pid]/limitsで、
この"Max processes"がスレッド数にも適用されるようです。

今手元で動いているG3Lでsshdのpidでやってみると・・・

# cat /proc/2578/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             3382                 3382                 processes
Max open files            1024                 4096                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       3382                 3382                 signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

なので、ulimitの結果と同じ3382でした。

ちなみに、
> cat /proc/sys/kernel/threads-max
は 6765 でした。

--
なかむら

at_mizo

2018年3月26日 10時40分

溝渕です。

補足いただき、有難うございます。

> ulimitコマンドの"-u"でプロセス数制限がわかります。
>
> # ulimit -u
> 3382
>

上記、man pageより
「1 人のユーザが使用できる最大のプロセス数」
のようです。

> > cat /proc/sys/kernel/threads-max

は、システム全体での最大スレッド(プロセス)数のようです。

いくつかの環境でそれぞれの値を確認してみましたが、デフォルトだと、1人
のユーザーが使用できる数は、システム全体で使用できる数の半分になってい
るようです。
# ソースを見ていないので多分ですけど

y.nakamura

2018年3月26日 14時12分

中村です。

> 上記、man pageより
> 「1 人のユーザが使用できる最大のプロセス数」
> のようです。

補足、ありがとうございます。
manにはあったのですが、投稿の自分の言葉で書き忘れてました。

ついでなので、情報をもう1つ。
この制限は /etc/security/limits.conf で変更できます。

G3/G3L/X1のこのファイルは次のようになっていて
全部コメント状態ですが、ここを書き換えれば
システムの制限内であれば増やせる|減らせるのだろうと思います。
(自分でこれを修正して試したことはありません)

# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#        - chroot - change root to directory (Debian-specific)
#
#<domain>      <type>  <item>         <value>
#
 
#*               soft    core            0
#root            hard    core            100000
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#ftp             -       chroot          /ftp
#@student        -       maxlogins       4
 
# End of file

--
なかむら