c - pthread_join() 导致段错误

标签 c pthreads

所以我遇到了 pthread join 的问题,它会给我段错误或永远在那里等待。我在这里尝试做的是一个 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;

}

最佳答案

pthread_join(3)的签名:

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

但是您向它传递了一个指向 pthread_t 的指针。

您应该考虑使用更高的警告选项进行编译,例如 -Werror -Wall -pedantic

关于c - pthread_join() 导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23317395/

相关文章:

c - C 中的堆头和 free()

c++ - 如何从 C 代码调用 C++ 函数

c - pthreads 传递参数 3 警告

c++ - 使用 pthreads 的线程安全队列

c - 在 C 中使用条件变量和互斥体来同步线程

c - C 代码的 R 内联编译在 Windows 上失败

C qsort 无法正常工作

python - 如何使用 OpenSSL C 库创建退化的 PKCS7 文件?

c++ - 有谁用过Posix pthread win32库,dll文件

c - pthread_sigmask 在信号处理程序中不起作用