리눅스 환경에서 pthread를 통해 스레드 1000개 이상 실행시키기

allieclan의 이미지

...
#define NUM_THREADS 1000
 
int number;
...
...
void *add(void*data) {
	for (int i=0; i<100; i++) {
		number++;
	}
}
 
int main(int argc, char* argv[]) {
...
...
	number = atoi(argv[1]);
	pthread_t pthread[NUM_THREADS];
/////////////////////
// CREATE THREAD
/////////////////////
	for (i=0; i<NUM_THREADS; i++) {
		status = pthread_create(&pthread[i], NULL, add, NULL);
		if (status) {
			printf("ERROR failed create thread\n");
			exit(-1);
		}
	}
/////////////////////
// JOIN THREAD
/////////////////////
	for (i=0; i<NUM_THREADS; i++) {
		status = pthread_join(pthread[i], (void**)status);
		if (status) {
			printf("ERROR failed join thread\n");
			exit(-1);
		}
	}
...
...

pthread 공부 중, 단순연산을 실행하는 스레드 1000개를 만들어서 실행시키고 싶은데
1000개 생성을 시도하면 380개 정도 생성후에 실행이 종료되버립니다...

찾아보니 메모리 부족문제인 것 같은데 이를 해결할 수 있는 방법이 없을까요??
아니면 생성방법의 문제일까요?

서지훈의 이미지

에러 나고 죽는다면 perror() 추가한 실행결과도 같이 올려주시고.
자세한 작동 환경이랑 전체 소스를 올려 보세요.

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

bushi의 이미지

Lipi의 이미지

실제 argv[1] + 1000 * 100 값보다 number값이 작게 나와서 1000개의 스레드가 다 돌지 못했다고 판단하신 건가요?

만약 그렇게 생각하신거라면 영어지만 아래 링크를 한번 보세요.
https://en.wikipedia.org/wiki/Race_condition#Software

AustinKim의 이미지

스레드를 생성하면 커널에서는 해당 스레드에 대한 스택과 태스크 디스크립터를 부여합니다.
만약 64비트 기반 리눅스인 경우 스택 사이즈는 0x4000 이고 태스크 디스크립터는 0x600 정도 크기가 됩니다.

스레드를 100개 이상 생성하면 커널은 메모리가 부족해 메모리 기근에 시달리게 됩니다.
이를 해결할 방안은 마땅히 떠오르지 않는데요.

(개인블로그)
http://rousalome.egloos.com

라스코니의 이미지

리눅스면 프로세스 스택 사이즈를 키운 다음 다시 해보세요.

ulimit -all 로 현재 스택 사이즈 최대값을 보시고
ulimit -s 65536 로 스택 사이즈를 키운 다음
ulimit -all 로 제대로 스택 사이즈가 커진 것을 확인하시고 다시 해 보세요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.