리눅스에서 쓰레드 사용 개수의 한계는?

neomagic의 이미지

안녕하세요. :D
쓰레드를 공부하고 있는 프로그래머입니다.
쓰레드 개수에 대한 고수님들의 조언을 듣고자 합니다.

실행환경
cpu 셀800
ram 256
리눅스 7.2 ( Linux test 2.4.7-10 #1 Thu Sep 6 17:27:27 EDT 2001 i686 unknown )
gcc 버젼 -
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 ( Red Hat Linux 7.1 2.96-98 )

예 1)
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define ADMIN_TIME      5
#define MAX_ADMIN_THR   1000

int     errno;

pthread_t admin_thr[MAX_ADMIN_THR];

void *AdminThread(void *thr_arg)
{
    int     proc_id;

    char    *func_name = "AdminThread";

    proc_id = (int)thr_arg;

    while ( 1 )
    {
        printf("%s : running (%d)\n", func_name, proc_id);

        sleep(ADMIN_TIME);
    }
}


int main()
{
    int     i;
    int     ret_val;

    char    *func_name = "main";

    for ( i = 0 ; i < MAX_ADMIN_THR ; i++ )
    {
        ret_val = pthread_create(&admin_thr[i], NULL, AdminThread, (void *)i);
        if ( ret_val != 0 )
        {
            printf("%s : failed to create administration thread (%d) - error number(%d)\n", func_nam
e, i, errno);
            return -1;
        }
    }

    while ( 1 )
        pause();
}

gcc test_thread.c -o test -lpthread 와 같이 컴파일하여 실행하게 되면

AdminThread : running (0)
AdminThread : running (1)
.
.
AdminThread : running (254)
main : failed to create administration thread (255) - error number(4)

위와 같은 결과값이 나옵니다. 255개까지 밖에 생성이 안되더군요.

그래서 쓰레드 속성 부분을 추가 했습니다.

예 2)
.
.
int     errno;

pthread_t admin_thr[MAX_ADMIN_THR];
pthread_attr_t attr[MAX_ADMIN_THR]; <- 추가
.
.
char    *func_name = "main";

    for ( i = 0 ; i < MAX_ADMIN_THR ; i++ )
    {
        pthread_attr_init(&attr[i]); <-추가

        ret_val = pthread_create(&admin_thr[i], &attr[i], AdminThread, (void *)i);
<- pthread_create(&admin_thr[i], NULL, AdminThread, (void *)i); 던 부분을 위와 같이 변경
.
.
.

위와 같이 변경 해보니 쓰레드가 1000개 생성되고 실행됩니다.

**먼저 쓰레드 속성 설정과 쓰레드 개수와 어떤 관계가 있는지 알고 싶습니다.

**그리고 한가지 또 오라클 8.1.7 클라이언트를 설치하게 되면 예1)으로도 에러 없이 실행 가능합니다.
- 제가 알기로는 오라클 8.1.7을 설치할때 gcc버젼이 2.91로 다운그레이드 되는 걸로 알고 있습니다. 오라클이 gcc 설정을 바꾸는 건지(??)...

**MAX_ADMIN_THR 1000를 MAX_ADMIN_THR 1018를 초과하게 되면
예2) 위에 있는 에러가 납니다. 에러번호는 4(#define EINTR 4 /* Interrupted system call */) 이구요.

**gcc 컴파일러에 설정되어 있는 쓰레드 개수 제한 때문이라면 인위적으로 늘려 줄수 있는 방법(리눅스가 허용하는 개수까지 제가 알기로는 한 4000개 정도 사용 할수 있는 걸로 아는데...)은 있는지 알고 싶습니다. 만약 있다면 죄송하지만 설명 좀 해주셨으면 감사하겠습니다.

** pthread_detach(pthread_self()) 이 함수는 개수와 상관있는지 예전 게시판을 보니 내용이 있기에 위에 코드에 적용 시켜도 똑같은 에러만이 ㅠ.ㅠ

## 고수님들의 조언이 필요합니다. 그럼 이만 (^^)(__) ##

aero의 이미지

http://www.kegel.com/c10k.html

윗 글에서 관련된 부분 입니다.

Limits on threads
On any architecture, you may need to reduce the amount of stack space allocated for each thread to avoid running out of virtual memory. You can set this at runtime with pthread_attr_init() if you're using pthreads.

Solaris: it supports as many threads as will fit in memory, I hear.
Linux 2.4: /proc/sys/kernel/threads-max is the max number of threads; it defaults to 2047 on my Red Hat 8 system. You can set increase this as usual by echoing new values into that file, e.g. "echo 4000 > /proc/sys/kernel/threads-max"
Linux 2.2: Even the 2.2.13 kernel limits the number of threads, at least on Intel. I don't know what the limits are on other architectures. Mingo posted a patch for 2.1.131 on Intel that removed this limit. It appears to be integrated into 2.3.20.
See also Volano's detailed instructions for raising file, thread, and FD_SET limits in the 2.2 kernel. Wow. This document steps you through a lot of stuff that would be hard to figure out yourself, but is somewhat dated.

Java: See Volano's detailed benchmark info, plus their info on how to tune various systems to handle lots of threads

mania12의 이미지

원래 레드햇 8.0 이전 버젼은 glibc-2.2.5/linuxthreads/internals.h 를 보면 #define STACK_SIZE (2 * 1024 * 1024) 로 STACK_SIZE가 2M로 되어 있습니다.

그리고 glibc-2.2.5/linuxthreads/sysdeps/unix/sysv/linux/bits/local_lim.h 에
#define PTHREAD_THREADS_MAX 1024 로 최대 쓰레드가 1024개로 정의 되어 있습니다.

MySQL의 경우 동시접속 500 이상을 처리하기 위해서는 STACK_SIZE를 줄이고 THREADS_MAX를 늘려줘서 처리를 했었습니다.
#define STACK_SIZE (128 * 1024)
#define PTHREAD_THREADS_MAX 4096
이런식으로 재정의해서 다시 컴파일해줬었죠.

그런데 레드햇 8.0의 경우는 glibc-2.2.93 이 들어있는데 위의 작업이 이미 다 되어 있어서 손댈 필요가 없더군요.

MySQL 사이트나 Volano 사이트등을 보면 리눅스 쓰레드에 대한 자세한 이야기가 있습니다. 문태준님의 http://www.tunelinux.pe.kr 에도 리눅스 쓰레드 튜닝에 관한 글이 있으니 참고하세요. :o

neomagic의 이미지

일단 궁급한 건 대부분은 해결 된것 같네요. :D
한가지 쓰레드 속성을 설정해주면 255에서 1024로 늘어 나는 것만
빼구요. ㅜ.ㅠ

계속 문서를 찾아봐야겠죠.^^

이것 저것 해보구 다시 한 번 글을 올릴 생각입니다.

그럼 그때까지 ... (^^)(__)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.