尝试锁定共享内存互斥锁时出现分段错误

时间:2019-01-22 17:41:35

标签: c mutex shared-memory

共享内存互斥锁上的分段错误。我分别有一个生产者进程和一个消费者进程,生产者初始化共享内存,共享内存互斥量并写入数据。

使用者打开共享内存,尝试锁定并读取数据。

没有互斥量,使用者就可以读取数据(数据将不一致,但是没有段错误)

仅当使用者试图锁定互斥锁时,才会出现段错误。

更改标志,结果相同,检查attr和互斥锁init的返回值,它们都很好。

我有点迷茫,对共享内存的研究还很新。

指出的任何缺陷将非常有帮助。谢谢!

堆栈:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff79c0c8d in pthread_mutex_lock () from /lib64/libpthread.so.0
(gdb) bt
#0  0x00007ffff79c0c8d in pthread_mutex_lock () from /lib64/libpthread.so.0
#1  0x00000000004008ec in main (argc=1, argv=0x7fffffffde18 "\210\341\377\377\377\177") at cons.c:41
(gdb) p cons_ptr
$1 = (test_str_t *) 0x7ffff7ff7000
(gdb) p cons_ptr->data[0] 
$2 = 873422052  
(gdb) p cons_ptr->magic 
$3 = 43981  
(gdb) p cons_ptr->shm_lock
$4 = {__data = {__lock = 0, __count = 0, __owner = 0, __nusers = 0, __kind = 128, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}}, 
  __size = '\000' <repeats 16 times>, "\200", '\000' <repeats 22 times>, __align = 0}
(gdb) 


// Header:  

const char *pathname = "test_shm";

typedef struct test_str_{
    uint64_t            magic;
    int                 data[1024];
    pthread_mutex_t     shm_lock;
    uint64_t            magic2;
}test_str_t;

// Producer:  

#include "test_header.h"
#include <time.h>

test_str_t *ptr;

void init_mutex(pthread_mutex_t *mutex){
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(mutex, &attr);
    pthread_mutexattr_destroy(&attr);
}

int main(int argc, char *argv[]) {
    int fd, i = 0, num_int = 1024, rc = 0;
    int size = num_int * sizeof(int);
    fd = shm_open(pathname, O_CREAT|O_RDWR, 0666);
    if(fd == -1) {
        printf("\n shm_open failed \n");
        exit(0);
    }
    rc = ftruncate(fd, sizeof(test_str_t));
    ptr = (test_str_t *)mmap(NULL, sizeof(test_str_t),
                    PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
    if(ptr == MAP_FAILED) {
        printf("\n mmap failed \n");
        exit(0);
    }
    rc = init_mutex(&(ptr->shm_lock));
    if(rc != 0) {
        printf("Mutex Init failed: rc:[%d]\n", rc);
        exit(0);
    }
    ptr->magic = 0xABCD;
    ptr->magic2 = 0xEF12;
    srand(time(0));
    for(i = 0; i < 5; i++) {
        pthread_mutex_lock(&(ptr->shm_lock));
        ptr->data[i] = i;
        pthread_mutex_unlock(&(ptr->shm_lock));
    }
    return 0;
}

// Consumer:  

#include "test_header.h"
#include <time.h>

test_str_t *cons_ptr;

int main(int argc, char *argv[]) {
    int fd, i = 0, num_int = 1024, rc = 0;
    int try_lock = 0;
    int size = num_int * sizeof(int);
    fd = shm_open(pathname, O_RDONLY, 0666);
    if(fd == -1) {
        printf("\n shm_open failed \n");
        exit(0);
    }
    cons_ptr = (test_str_t *)mmap(NULL, sizeof(test_str_t),
                    PROT_READ, MAP_SHARED, fd, 0);
    if(cons_ptr == MAP_FAILED) {
        printf("\n mmap failed \n");
        exit(0);
    }
    if(cons_ptr == NULL) {
        printf("\n NULL Ptr\n");
        return 0;
    }
    printf("Magic: [0x%x] [0x%x]\n", cons_ptr->magic, cons_ptr->magic2);
    //int try_lock = pthread_mutex_lock(&(cons_ptr->shm_lock)); // SEG FAULT
    for(i = 0; i < 5; i++) {
        printf("\nNumber:[%d]\n", cons_ptr->data[i]);
        sleep(1);
    }
    //pthread_mutex_unlock(&(cons_ptr->shm_lock)); // SEG FAULT
    return 0;
}

1 个答案:

答案 0 :(得分:0)

(1)一个问题在这里: ptr-> data = rand(); data是一个int *,并且您已将该指针替换为随机地址。您在几个地方进行了此操作,但我没有看到确切正在发生什么,但这是我现在看到的最明显的错误。

(2)我接下来要看到的是,在使用者中,您以只读方式打开文件并将内存映射为只读,但是pthread_mutex_lock和pthread_mutex_unlock需要能够修改互斥锁,因此可能会导致一些问题。麻烦。