질문] pthread 생성이 이럴땐 어떤 현상인가요?

dadenda의 이미지

안녕하세요.

pthread 에 관해서 질문이 있어서 이렇게 올립니다.
프로그램은 간단한 소켓통신(tcp)프로그램입니다.
큐에서 받아서 값이 있으면 쓰레드를 생성해서 다른서버에
데이터를 주고 받는 프로그램 입니다.

그런데 어느순간에 쓰레드가 생성이 되지 않습니다.
상황을 보면 큐에서 받았을때 로그는 있는데
그다음 단계인 쓰레드 생성함수가 안먹히는것 같습니다.

fncLog("##### Msg Arriving From IR #####");
	fncLog(rcvbuf.c.rcvData);
     thr_id = pthread_create(&p_thread[rcvbuf.c.hd.mchan], NULL, t_function, (void *)&rcvbuf);
    if (thr_id < 0)
    {
		fncLog("**Thread Start FAIL!!");
		fncLog(rcvbuf.c.rcvData);
    }

쓰레드가 실패하지 않았으면 아래 쓰레드에서 로그를 남기는데
로그가 남지 않네요

void *t_function(void *data)  
{                             
    int id;                   
    int i = 0;                
	struct sockaddr_in address;
	int sockfd;
	int result;
	struct accountMsg acctbuf; 
    id = *((int *)data);
   
	fncLog("**Thread Start**");


... ---주저리..

/*    소켓처리부       */
sockfd = socket(AF_INET, SOCK_STREAM,0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("192.9.200.214");
address.sin_port=htons(10011);

len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
							
   if(result==-1){
          //소켓 연결 에러 처리
		fncLog("**Socket Connection Fail**");
         return 0;  
    }
	
  write(sockfd, &rcvbuf.c.rcvData, sizeof(rcvbuf.c.rcvData));
	fncLog("--Sending SUCCESS!!");
	fncLog(rcvbuf.c.rcvData);
  read(sockfd, &acctbuf.sndData, sizeof(acctbuf.sndData));
  	fncLog("--Receiving SUCCESS!!");
	fncLog(acctbuf.sndData);
  close(sockfd);

/* 소켓 처리부 끝 */



break; 
}
}

만약에 성공한다면 start로그라도 남기고 죽어야 하는데
죽지도 않고, 큐에서 값만 계속 읽어 옵니다. (읽어오는 로그만 쌓임)

대충 추측엔.. 쓰레드가 한계치를 넘어서 그런가? 싶기도 한데
그럼 -1이 떨어져서 로그가 남아야 하는데..

혹시 pthread_exit((void*)0); 를 사용하지 않아서
임계치를 넘어선걸까요? 리턴값이 다른?
혹시 아시는 고수분들은 고견 부탁드립니다.

익명 사용자의 이미지

ulimit -a 하시면 파일디스크립터의 한계치가 나옵니다.

아마 통상 1024인 듯 합니다만

소켓이 생성될때마다 카운트를 하나씩 늘려서 파일디스크립터의

한계치와 비교해 보세요 만약 그문제라면 setrlimit 함수로 조절하실

수 있습니다. 솔라리스 5.7에서 기본 파일디스크립터의 한계가 64더군요.

그래서 위와 비슷한 문제에 당해 본 적이 있어서 혹시 남겨봅니다

ps 고수분들의 답변을 기다린다고 하시면 답변달기가 망설여 집니다...쩝

최종호의 이미지

pthread 함수들의 리턴값은 일반적인 시스템호출의 리턴값과 다릅니다.
성공시에는 (일반적으로) 시스템호출들과 마찬가지로 0을 리턴하지만
실패하는 경우에는 곧바로 에러번호를 리턴합니다.
시스템호출의 경우에는 실패시에 -1을 리턴하고 에러번호를 errno 에 설정하는 것과는 동작이 틀리죠.

따라서

    thr_id = pthread_create(&p_thread[rcvbuf.c.hd.mchan], NULL, t_function, (void *)&rcvbuf);
    if (thr_id < 0)
    {
      fncLog("**Thread Start FAIL!!");
      fncLog(rcvbuf.c.rcvData);
    }

부분은
    rc  = pthread_create(&p_thread[rcvbuf.c.hd.mchan], NULL, t_function, (void *)&rcvbuf);
    if (rc != 0)
    {
      fncLog("**Thread Start FAIL!!");
      fncLog(rcvbuf.c.rcvData);
    }

와 같이 수정하셔야 합니다.
pthread_create()가 리턴하는 값은 thread id 가 아니라
성공/실패이므로 thr_id 라는 변수에 리턴값을 받는 것은
불필요한 오해의 소지가 있을 수 있어서 rc 에 받았습니다.

추가:
시스템에 따라 -lpthread 등 필요한 라이브러리를 함께 링크시키지 않는 경우에
링킹은 정상적으로 되는데 실행시에 pthread_create()가 실패하고
-1 을 리턴하는 경우가 있었습니다.

댓글 달기

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