pthread 여러개 생성 후 join 시 세그먼트 오류...

iksciting의 이미지

리눅스로 프로그램을 짜던 도중 막히는 부분이 있어서
제가 짜던 소스에서 문제가 되는 부분만 따로 떼어왔습니다

간단히 설명을 하자면 pthread_create로 예를 들어 10개의 스레드를 생성한 후
pthread_join을 10번을 해주고 각 스레드에서 리턴 값을 받아오는 부분인데요

10개의 생성이 이상 없이 완료되고 join도 하나씩 하고 리턴도 하는데 그러다가
마지막 쓰레드의 join은 수행이 안되고 segmentation fault 가 발생해버리네요

도대체 왜 그러는지 알 수가 없어서 너무 답답하네요요

#include
#include
#include
#include

pthread_mutex_t mutex_lock;

struct arg_struct {
int a;
int b;
};

void *t_function(void *data) {
pthread_mutex_lock(&mutex_lock);

struct arg_struct *arg = (struct arg_struct *)data;
long int s;

s = arg->a;
printf("s에 %d를 저장했습니다\n", s);

pthread_mutex_unlock(&mutex_lock);

return (void **)s;
}

int main()
{
int i;

pthread_t p_thread[2];
int thr_id;
int status;

struct arg_struct arg[2];

for(i = 1; i >= 0; i--) {
arg[i].a = i;
arg[i].b = i;
}

pthread_mutex_init(&mutex_lock, NULL);

printf("스레드 %d 생성 전\n", 1);
thr_id = pthread_create(&p_thread[1],NULL, t_function, (void *)&arg[1]);
printf("스레드 %d 생성 후\n", 1);

usleep(1000);

printf("스레드 %d 생성 전\n", 0);
thr_id = pthread_create(&p_thread[0],NULL, t_function, (void *)&arg[0]);
printf("스레드 %d 생성 후\n", 0);

int temp[2];

printf("스레드 %d 조인 전\n", 1);
pthread_join(p_thread[1], (void**)&status);
printf("스레드 %d 조인 후\n", 1);
temp[1] = status;

printf("스레드 %d 조인 전\n", 0);
pthread_join(p_thread[0], (void**)&status);
printf("스레드 %d 조인 후\n", 0);
temp[0] = status;

printf("%d%d", temp[1], temp[0]);

pthread_mutex_destroy(&mutex_lock);

return 0;
}

이 소스를 돌려도 마찬가지지만 제가 짜던 프로그램에서 비슷하게 돌려보면
..
..
..
값 저장 완료
스레드 3 조인 후
스레드 2 조인 전
값 저장 완료
스레드 2 조인 후
스레드 1 조인 전
값 저장 완료
스레드 1 조인 후
스레드 0 조인 전
세그멘테이션 오류 (core dumped)

이렇게 종료가 되어버립니다

jick의 이미지

status가 int인데 &status를 void**로 캐스팅해서 넘겨 버렸으니, 64비트 머신이면 void*에 해당하는 8바이트 영역에 리턴값이 돌아오겠죠. status는 4바이트일 테니 메모리가 깨집니다.

iksciting의 이미지

그것이 제 코드의 문제였군요
하지만 왜 마지막 join에서만 문제가 발생하는 걸까요?
그 이전의 join은 제대로 수행이 되는 것 같거든요

iksciting의 이미지

감.. 사.. 합니다 ㅠㅠㅠ 그 부분은 왜 제 눈에 들어오지 않았을까요
t_function 안에는 long int로 해놓고... status를 long int로 바꾸고 나니 모두 해결입니다! 감사합니다!

댓글 달기

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