pthread_join()会导致分段故障。

问题描述 投票:1回答:2

所以我在pthread join上遇到了这个问题,它会给我segfault或者永远在那里等待。我试图在这里做的是一个pthreaded TCP客户端服务器,其中pthread在客户端。我把所有的东西都注释了,除了连接还是不行。

问题主要出现在pthreads上,它会在pthread_join上停止,再也无法继续。下面是我试着进行4个连接,连接成功后,经过pthread什么也不做。

调试测试运行。

./test 128.114.104.230 4443 5

begins
on top of forloop
forloop #0
forloop top of strcat #0
forloop top of create #0
forloop End #0
in thread, top of write #0
in thread, write errored #0
forloop #1
forloop top of strcat #1
forloop top of create #1
forloop End #1
in thread, top of write #1
in thread, write errored #1
forloop #2
forloop top of strcat #2
forloop top of create #2
forloop End #2
in thread, top of write #2
in thread, write errored #2
forloop #3
forloop top of strcat #3
forloop top of create #3
forloop End #3
in thread, top of write #3
in thread, write errored #3
forloop #4
forloop top of strcat #4
forloop top of create #4
forloop End #4
I am here2
start wait #0
in thread, top of write #4
in thread, write errored #4

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>  //Socket data types
#include <sys/socket.h> //socket(), connect(), send(), and recv()
#include <netinet/in.h> //IP Socket data types
#include <arpa/inet.h>  //for sockaddr_in and inet_addr()
struct thread_info{    /* Used as argument to thread_start() */
   pthread_t   thread_id;        /* ID returned by pthread_create() */
   int         thread_num;       /* Application-defined thread # */
   int         sockfd;
   char *      stringArg;
};

static void * thread_start(void *arg){
   struct thread_info *threadInfo = arg;

   char *recvBuff;
   //char *sendBuff = "This is thread #";
   //char *Temp = &(*threadInfo->thread_num +'0');

   //strcat(sendBuff, Temp);
   //strcat(sendBuff, "\n");
   printf("in thread, top of write #%d\n", threadInfo->thread_num);
   //if(write(threadInfo->sockfd, threadInfo->stringArg, strlen(threadInfo->stringArg)) != strlen(threadInfo->stringArg));
      printf("in thread, write errored #%d\n", threadInfo->thread_num);
   //read(threadInfo->sockfd, recvBuff, 1024);

   close(threadInfo->sockfd);
   pthread_exit((void*) arg);
}

int main(int argc, char* argv[]){

   printf("begins\n");

   struct sockaddr_in servAddr;
   pthread_attr_t attr;

   int numCon = atoi(argv[3]);
   int serNum;
   struct thread_info *pThreads;
   int s;
   int end;

   pThreads = calloc(numCon, sizeof(struct thread_info));

   if (pThreads== NULL){
      fprintf(stderr, "Error : Could not memory for thread_info Structure\n");
      return EXIT_FAILURE;
   }

   servAddr.sin_family = AF_INET;
   servAddr.sin_port = htons(atoi(argv[2]));
   servAddr.sin_addr.s_addr = inet_addr(argv[1]);

   printf("on top of forloop\n");
   //reads and stores all IP and Port in the list of servers for later use


   for(serNum = 0; serNum< numCon; serNum++){

      if((pThreads[serNum].sockfd = socket(AF_INET, SOCK_STREAM,0))< 0){
         fprintf(stderr, "Error : Could not create socket #%d \n", serNum);
         return EXIT_FAILURE;
      }

      if(connect(pThreads[serNum].sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr))< 0){
         fprintf(stderr, "Error : Connect to socket #%d Failed \n", serNum);
         return EXIT_FAILURE;
      }

      printf("forloop #%d\n", serNum);
      pThreads[serNum].thread_num = serNum;
      pThreads[serNum].stringArg = "I am thread ";

      printf("forloop top of strcat #%d\n", serNum);
      //strcat(pThreads[serNum].stringArg, &pThreads[serNum].thread_num);

      printf("forloop top of create #%d\n", serNum);
      s = pthread_create(&pThreads[serNum].thread_id, NULL, thread_start, &pThreads[serNum]);
      printf("forloop End #%d\n", serNum);

   }


   printf("I am here2\n");

   for(serNum = 0; serNum< numCon; serNum++){

      printf("start wait #%d\n",serNum);
      pthread_join(&pThreads[serNum].thread_id, (void **) &end);
      printf("end wait #%d\n",serNum);

   }

   printf("Main: program completed. Exiting.\n");
   free(pThreads);
   pthread_exit(NULL);
   return 0;

}
c pthreads
2个回答
4
投票

请看签名 pthread_join(3):

int pthread_join(pthread_t thread, void **retval);

但你给它传递的是一个指向 pthread_t.

你应该考虑用更高的警告选项来编译,例如 -Werror -Wall -pedantic.


-1
投票

检查你是否试图加入一个从未创建的线程。线程通常是有条件地创建的。有可能你正在为一个从未创建的线程调用pthread_join。在这种情况下,你会得到一个分段故障。

© www.soinside.com 2019 - 2024. All rights reserved.