简体   繁体   English

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

[英]Segmentation Fault when trying to lock a shared memory mutex

Segmentation fault on a shared memory mutex. 共享内存互斥锁上的分段错误。 I have a producer and a consumer processes respectively, the producer inits the shared memory, the shared memory mutex, and writes the data. 我分别有一个生产者进程和一个使用者进程,生产者初始化共享内存,共享内存互斥量并写入数据。

The consumer opens the shared memory, tries to lock and read the data. 使用者打开共享内存,尝试锁定并读取数据。

Without the mutex, the consumer is able to read the data (the data will be inconsistent, but there are no seg faults) 没有互斥量,使用者就可以读取数据(数据将不一致,但是没有段错误)

There is a seg fault only when the consumer is trying to lock the mutex. 仅当使用者尝试锁定互斥锁时才存在段错误。

Changed the flags and the result is the same, checked the return values of the attr and mutex inits, which are all good. 更改标志,结果相同,检查attr和互斥锁init的返回值,它们都很好。

I am a little lost, and I am quite new to working on shared memory. 我有点迷茫,对共享内存的研究还很新。

Any flaws pointed out would be really helpful. 指出的任何缺陷将非常有帮助。 Thanks! 谢谢!

Stack: 堆:

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) one problem is here: ptr->data = rand(); (1)一个问题在这里:ptr-> data = rand(); data is an int*, and you have replaced that pointer with a random address. data是一个int *,并且您已将该指针替换为随机地址。 You do this in a couple of places and I'm not seeing exactly what is going on, but it's the most obvious mistake I see right now. 您在几个地方执行了此操作,但我看不到发生什么,但这是我现在看到的最明显的错误。

(2) The next thing I see is that in the consumer you open the file read only and map the memory as read only, but pthread_mutex_lock and pthread_mutex_unlock would need to be able to modify the mutex, so that would probably cause some trouble. (2)我看到的第二件事是,在使用者中,您以只读方式打开文件并将内存映射为只读,但是pthread_mutex_lock和pthread_mutex_unlock必须能够修改互斥锁,这样可能会引起一些麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM