thread_mutex_lock()上的段错误

时间:2018-03-16 16:41:22

标签: c multithreading pthreads mutex

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

#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(&amp; printf_mutex)上遇到了段错误,但是我的互斥锁被初始化了。

2 个答案:

答案 0 :(得分:1)

感谢您的帮助,但我解决了我的问题。 我的问题在于变量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));

答案 1 :(得分:0)

Buonjorno fratello! 您的代码的主要问题(以及其他)是该线程

  

void * consumatore

没有任何pthread_exit()实现(也是产品)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.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 != NULL){
        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");

        }
    }
    pthread_exit(0);
}

void *produttore(buffer_t *buffer){
    int* 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--;
    }
    pthread_exit(0);
}

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);
    return 0;
}