gpt4 book ai didi

c - pthread_join() 导致段错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:33 36 4
gpt4 key购买 nike

所以我遇到了 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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com