gpt4 book ai didi

c - Linux C : segfault error 4 in libmysqlclient. so.18.0.0

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:29 26 4
gpt4 key购买 nike

我正在尝试使用以下代码实现多线程 tcp 服务器。

int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");

pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;

if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}

//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}

if (client_sock < 0)
{
perror("accept failed");
return 1;
}

return 0;
}

/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int newsockfd = *(int*)socket_desc;
int read_size;
char *message , buffer[2000];
int n;
struct auth details;
char* reply;

char cmd[100] = {0};
//Receive a message from client
while( (read_size = recv(newsockfd , buffer , 2000 , 0)) > 0 )
{
printf("Incoming data: %s", buffer);

if(parseData(cmd, buffer, "/", "-")) /* If received data is compliant */
{
details = authenticate(cmd);
if (details.verified) // If said user can execute this command.
{
reply = execute(cmd); // Execute.
n = write(newsockfd, reply, strlen(reply));
if (n < 0) { error("ERROR writing to socket"); }

free(reply);
reply = NULL;

}
else // Authentication failed.
{
n = write(newsockfd, details.error_msg, strlen(details.error_msg));
if (n < 0) { error("ERROR writing to socket"); }
else{
if(debug) printf("sent: %s\n", details.error_msg);
}
}
}
}


if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}

//Free the socket pointer
free(socket_desc);

return 0;
}

authenticate 函数定义在一个实现文件中:

struct auth authenticate(const char* const command)
{
struct auth data;
char* db_pwd;
char* perm;
char* username = "testUser";
char query[200] = {0};

data.verified = FALSE;
data.error_msg = "";

printf("in auth\n");
MYSQL_ROW row;
snprintf(query, 200, "SELECT password FROM userlist WHERE user='%s'", username);
if ((row = mysql_fetch_row(run_query(query)))) // if user is on list
{
db_pwd = row[0];

if(!strcmp(db_pwd, password))
{
if (!(strcmp(command, "test")))
{
data.verified = TRUE;
}
}
}
return data;
}

我有一个该程序(主要)的工作单线程版本,具有相同的实现文件,段错误仅发生在多线程版本中。

  • 我的代码是不是做错了什么?
  • 我应该如何进行调试?

如有任何想法,我们将不胜感激。

最佳答案

阅读 MySQL C API 的文档, 特别是 § 23.8.12 C API Threaded Function Descriptions它在内部使用到 mysqld 服务器进程的套接字连接,因此您必须序列化所有 MySQL 函数。

因此,定义您的全局互斥体,并保护所有使用该互斥体执行 mysql 调用的函数(从请求到获取所有回复行)。更好的是,设计您的应用程序,以便只有一个线程(通常是主线程)使用 mysql 函数。

编译所有警告和调试信息 (gcc -Wall -g)。使用调试器 (gdb),或许还可以使用 valgrind

如果不熟悉mutexes ,您可能想阅读 pthread tutorial

关于c - Linux C : segfault error 4 in libmysqlclient. so.18.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26140402/

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