gpt4 book ai didi

c - pthread_mutex_lock() 上的段错误

转载 作者:行者123 更新时间:2023-11-30 19:05:43 27 4
gpt4 key购买 nike

在我的生产者/消费者练习中,我想构建两个线程,一个用于生产者,另一个用于消费者。他们可以访问缓冲区以在其上读取/写入消息,并且我想使用互斥体来管理此缓冲区。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <pthread.h>

#define BUFFER_LENGHT 10
#define BUFFER_ERROR (msg_t *) NULL
#define TRUE 1
#define FALSE 0

pthread_mutex_t printf_mutex;


typedef struct msg_t{
void* content;

struct msg_t* (*msg_init) (void*);
void (*msg_destroy) (struct msg_t*);
struct msg_t* (*msg_copy)(struct msg_t*);
}msg_t;

void msg_destroy(msg_t* msg){
pthread_mutex_lock(&printf_mutex);
printf("CONSUMER: Message deleted \n");
pthread_mutex_unlock(&printf_mutex);
free(msg);
}

struct msg_t* msg_copy(struct msg_t* message){
struct msg_t* new_message;
new_message = malloc(sizeof(msg_t));
new_message->content = message->content;
new_message->msg_copy = message->msg_copy;
new_message->msg_destroy = message->msg_destroy;
new_message->msg_init = message->msg_init;
return new_message;
}

struct msg_t* msg_init(void* content){
if(content != ""){
struct msg_t* messaggio;
messaggio = malloc(sizeof(msg_t));
messaggio->content = content;
messaggio->msg_init = msg_init;
messaggio->msg_destroy = msg_destroy;
messaggio->msg_copy = msg_copy;
return messaggio;
} else {
return NULL;
}
}

typedef struct buffer_t {

int whereToWrite;
int totMessagesToRead;
int maxMsgs;
pthread_mutex_t mutex;

struct buffer_t* (*buffer_init) (unsigned int maxsize);
struct msg_t* (*put_bloccante)(struct buffer_t*, struct msg_t*);

struct msg_t* messaggio[];
}buffer_t;

_Bool bufferIsFull(buffer_t *buffer){
int size = buffer->maxMsgs;
int nToRead = buffer->totMessagesToRead;

if(nToRead == size)
return TRUE;
else
return FALSE;
}

struct msg_t* put_bloccante(struct buffer_t* buffer, struct msg_t* messaggio){
struct msg_t* new_message;
new_message = messaggio->msg_copy(messaggio);

if(!bufferIsFull(buffer)){
pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Mutex enabled\n");
pthread_mutex_unlock(&printf_mutex);

pthread_mutex_lock(&(buffer->mutex));

buffer->whereToWrite++;

if(new_message == NULL){
pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Error on creating the message \n");
pthread_mutex_unlock(&printf_mutex);
} else{
buffer->messaggio[buffer->whereToWrite] = new_message;
buffer->totMessagesToRead++;
}
pthread_mutex_unlock(&(buffer->mutex));

pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Mutex disabled\n");
pthread_mutex_unlock(&printf_mutex);
} else{
pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Buffer full... \n");
pthread_mutex_unlock(&printf_mutex);
}

return new_message;
}

struct buffer_t* buffer_init (unsigned int maxsize){
if(maxsize > 0){
struct buffer_t* buffer;
buffer = malloc(sizeof(struct buffer_t) + maxsize*sizeof(struct msg_t));
buffer->whereToWrite = 0;
buffer->maxMsgs = maxsize;
buffer->totMessagesToRead = 0;
buffer->put_bloccante = put_bloccante;

pthread_mutex_init(&(buffer->mutex), NULL);

return buffer;
} else {
return NULL;
}

}

void sleep_random(char* whoCall){
srand(time(NULL));
int mills = rand() % 100+1;

pthread_mutex_lock(&printf_mutex);
printf("%s: Waiting for %d milliseconds \n", whoCall, mills);
pthread_mutex_unlock(&printf_mutex);

Sleep(mills);

pthread_mutex_lock(&printf_mutex);
printf("%s: Waiting finished \n", whoCall);
pthread_mutex_unlock(&printf_mutex);
}


void *consumatore(buffer_t *buffer){
pthread_mutex_lock(&printf_mutex);
printf("CONSUMER: Thread created\n");
pthread_mutex_unlock(&printf_mutex);

int life = 500;

while(life > 0){
life--;

if(buffer->totMessagesToRead > 0){

pthread_mutex_lock(&printf_mutex);
printf("CONSUMER: mutex enabled\n");
pthread_mutex_unlock(&printf_mutex);

int whereToRead = (buffer->maxMsgs - (buffer->totMessagesToRead - buffer->whereToWrite)) % buffer->maxMsgs;

pthread_mutex_lock(&printf_mutex);
printf("CONSUMEr: Messagge recived: %s \n", (char *) buffer->messaggio[whereToRead]);
printf("CONSUMER: The message is on the buffer position n. %d \n", whereToRead);
pthread_mutex_unlock(&printf_mutex);

pthread_mutex_lock(&(buffer->mutex));

buffer->messaggio[whereToRead]->msg_destroy(buffer->messaggio[whereToRead]);
buffer->totMessagesToRead--;
pthread_mutex_unlock(&(buffer->mutex));

pthread_mutex_lock(&printf_mutex);
printf("CONSUMER: mutex disabled\n");
pthread_mutex_unlock(&printf_mutex);
sleep_random("CONSUMER");

}
}

}

void *produttore(buffer_t *buffer){
void* messaggio;
int life = 500;

//Here I got the issue
pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Thread created\n");
pthread_mutex_unlock(&printf_mutex);

while(life > 0){
messaggio = (rand() % 1000);

struct msg_t *msg_temp;
msg_temp = malloc(sizeof( msg_t));
msg_temp->msg_init = msg_init;
msg_temp = msg_temp->msg_init(messaggio);
msg_temp = put_bloccante(buffer, msg_temp);

pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: There are %d messages to read \n", buffer->totMessagesToRead);
pthread_mutex_unlock(&printf_mutex);

sleep_random("PRODUCER");
life--;
}
}

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

struct buffer_t *buffer, *buffer_temp;
pthread_t pid_c, pid_p;
int result_consumatore, result_produttore;

pthread_mutex_init(&printf_mutex, NULL);


buffer_temp = malloc(sizeof(buffer_t));
buffer_temp->buffer_init = buffer_init;
buffer_temp = buffer_temp->buffer_init(BUFFER_LENGHT);
if(buffer_temp == NULL){
pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Error on buffer creation\n");
pthread_mutex_unlock(&printf_mutex);
return 0;
}
buffer = buffer_temp;

pthread_mutex_lock(&printf_mutex);
printf("MAIN: Buffer size %d \n", buffer_temp->maxMsgs);
printf("MAIN: Current buffer position %d \n", buffer_temp->whereToWrite);
pthread_mutex_unlock(&printf_mutex);

result_produttore = pthread_create(&pid_p, NULL, (void *) produttore, (void *) buffer);
if(result_produttore != 0){
pthread_mutex_lock(&printf_mutex);
printf("MAIN: Error on producer thread creation, error: %d \n", result_produttore);
pthread_mutex_unlock(&printf_mutex);
return 0;
}

result_consumatore = pthread_create(&pid_c, NULL, (void *) consumatore, (void *)buffer);
if(result_consumatore != 0){
pthread_mutex_lock(&printf_mutex);
printf("MAIN: Error on consumer thread creation, error: %d \n", result_consumatore);
pthread_mutex_unlock(&printf_mutex);
return 0;
}

pthread_join(pid_c, NULL);

}

如果我运行该代码,我会在使用者线程上的 thread_mutex_loc(&printf_mutex) 上遇到段错误,但我的互斥体已经初始化。

最佳答案

感谢您的帮助,但我解决了我的问题。我的问题出在变量 whereToWrite 中,因为它必须指向数组上的下一个空元素,但在我的代码中它指向添加的最后一个元素,所以当我尝试使用以下代码读取时:

int whereToRead = (buffer->maxMsgs - (buffer->totMessagesToRead - buffer->whereToWrite)) % buffer->maxMsgs;

whereToRead 指向一个空位置,所以我在 put_bloccante 中更改了这一行:

pthread_mutex_lock(&(buffer->mutex));

buffer->whereToWrite++;

if(new_message == NULL){
pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Error on creating the message \n");
pthread_mutex_unlock(&printf_mutex);
} else{
buffer->messaggio[buffer->whereToWrite] = new_message;
buffer->totMessagesToRead++;
}
pthread_mutex_unlock(&(buffer->mutex));

使用此代码:

pthread_mutex_lock(&(buffer->mutex));

if(new_message == NULL){
pthread_mutex_lock(&printf_mutex);
printf("PRODUCER: Error on creating the message \n");
pthread_mutex_unlock(&printf_mutex);
} else{
buffer->messaggio[buffer->whereToWrite] = new_message;
buffer->totMessagesToRead++;
buffer->whereToWrite++;
}
pthread_mutex_unlock(&(buffer->mutex));

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

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