所以我对pthread连接有这个问题,它会给我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;
}
答案 0 :(得分:4)
查看pthread_join(3)
的签名:
int pthread_join(pthread_t thread, void **retval);
但是你传给了pthread_t
的指针。
您应该考虑使用更高的警告选项进行编译,例如-Werror -Wall -pedantic
。
答案 1 :(得分:-1)
检查是否要加入从未创建的线程。线程通常是有条件创建的。您可能为从未创建的线程调用pthread_join。在这种情况下,您会遇到细分错误。