많은 수의 쓰레드에서 전역변수값을 출력할 때 segment fault

systemfile의 이미지

테스트를 하다가 문제를 발견했습니다.

아래 코드를 실행하면
g_iData가 경쟁에 의해 값이 어찌 되던 상관은 없는데 fprintf부분에서 에러가 나는군요.
딱히 segment fault가 날 일이 없다고 생각되는데요.
이유가 뭔지 궁굼합니다.

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
int g_iData=0;
void *threadfunction()
{
    g_iData++;
    fprinf(stderr, "%d\n", g_iData);
    pthread_exit(0);
}
 
int main()
{
    pthread_t pThread[65535];
    int iRet;
    int i;
 
    for(i=0;i<65535;i++)
    {
        iRet = pthread_create(&pThread[i], NULL, threadfunction, NULL);
        if (iRet < 0)
        {
            perror("thread create error : ");
            exit(0);
        }
        pthread_detach(pThread[i]);
    }
 
    return 0;
}

그리고
RH9 Kernel2.4.20-8 512mem
에서 아래 코드를 테스트 하면 255~318 사이에 위치한 pThread 배열로 스레드 생성시 segment fault를 내네요..
이건 하드웨어 이상일까요..ㅡㄴㅡ.....

harshlad의 이미지

stderr 쪽을 잡고 write 하려는 부분에서 문제가 생기는 게 아닐까 합니다만.
정확하진 않습니다. 추정일 뿐...

- 嚴 -

- 嚴 -

mirr187의 이미지

kernel 2.4.21-50에서는 잘 돌아가는군요....

한가지 문제가 있었다면...올려주신 코드에서

*threadfunction()에서 fprintf 관련된 오타가 있었다는것뿐.. -_-;;

컴파일 옵션에 디버깅 옵션 주시고 valgrind -v --trace-children=yes program으로 함 돌려보시면 어떨까 싶군요..

systemfile의 이미지

gdb를 이용해서 continue로 돌리거나 valgrind로 돌리면
에러없이 돌아간답니다.(....이를 어쩌지..;)

: D

: D

익명 사용자의 이미지

특별히 fprintf()에 문제가 있지는 않습니다.
다만 메인에서

for(i=0;i<65535;i++)

쓰레드 생성하는 부분에서 루프가 거의 직빵으로 65535개의 쓰레드를 생성하다 보니
메모리 에러가 나는 겁니다.

에러가 나다보니 threadfunction()쓰레드펑션을 추정하는 것일 뿐.

그렇습니다.

systemfile의 이미지

이러한 에러를 피하기 위해서는 스레드 생성을 살살하는 방법 밖에는 없는건가요.;;?

: D

: D

익명 사용자의 이미지

컴파일시 -D_REENTRANT는 옵션으로 추가했겠지요?

systemfile의 이미지

옵션 추가해도 마찬가지 였습니다.;ㅁ;

: D

: D

lovian의 이미지

fedora core 6 2.6.22.9-61에서 돌려보니까
9000번대를 넘어가면서 에러가 발생했구요.
fprintf에 나는 것이 아니고,
pthread_detach에서 발생합니다.

그래서 pthread_create의 리턴 값을 확인 해보았더니.
성공할때는 0을 반환하더군요. errno를 셋팅하지 않는 것으로 보이네요.

코드에 적용하신 것처럼 iRet < 0 인 조건도 문제가 될거 같아요~
제 경우에는 iRet에 12가 리턴되었고,

ENOMEM      12  /* Out of memory */

이네요.
-----------------
한글을 사랑합니다.

-----------------
한글을 사랑합니다.

김일영의 이미지

int pthread_create()의 리턴값으로 0, EINVAL, EAGAIN 외의 다른 값도 나올 수 있는건지 궁금합니다. 혹시 OS에 따라 리턴값이 추가되거나 할 수 있는지요?

익명 사용자의 이미지

google에서 man pthread_create 하고 찾아보면 solaris에서 다른 경우가 금방 보이네요.

hongwoo의 이미지

fprintf 가 thread-safe 한가요 ???

잘 몰라서 "fprinf thread safe" 로 구글링 해서 처음 나오는 링크 따라가보니까.,
http://sourceware.org/ml/glibc-linux/1999-q3/msg00129.html
여기에서는 fprintf가 thread-safe 하지 않다고 나오네요..,

mutex 하나 만들어서 테스트 해보시는건 어떨까요 ??

-----------------------------
in the real-time scheduler !

댓글 달기

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