發問時間: 電腦與網際網路程式設計 · 1 0 年前

shared memory(20點喔)

請教諸位高手,如何利用shared memory的方式來存取同一個變數 ?已經困擾好久了,請幫忙!!

希望能有範例程式!!

亦即兩隻獨立ㄉAP,一隻去執行寫入的動作,另外一隻AP去執行讀出的動作 看變數是否有被寫入該記憶體位址?

2 個解答

評分
  • HPP
    Lv 5
    1 0 年前
    最佳解答

    Note! The followings are simplified Pseudo code for UNIX-based OS!!

    Assume:

    (1) char * mapped_file_name = "MyFile";

    (2) data structure on the shared memory:

    typedef struct

    {

    pthread_mutex_t mutex;

    int x;

    unsigned char other_data[512];

    } MySharedData_t;

    (3) mapped_file_size=1024;

    =============================

    Process A: Create a shared memory

    =============================

    #define SHM_FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)

    char * pUserSpace=NULL; // pointer to shared memory

    int fd; // file descriptor

    if ((fd = open(mapped_file_name , (O_RDWR | O_CREAT) , SHM_FILE_MODE)) == -1)

    {

    return -1; // error

    }

    ftruncate(fd, mapped_file_size); // set file size

    // pUserSpace points to the starting point of shared memory

    if ((pUserSpace = (char *)mmap(NULL, mapped_file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == -1)

    {

    return -1; // error

    }

    //

    // Init the mutex in the shared memory

    //

    pthread_mutexattr_t mattr;

    MySharedData_t * pMySharedData= (MySharedData_t *) pUserSpace;

    // Init mutex attribute

    pthread_mutexattr_init(&mattr);

    pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);

    pthread_mutex_init(p&pMySharedData->mutex, &mattr);

    2008-09-09 13:35:13 補充:

    ===========================================

    Process B: Map to the shared memory for read and write

    ===========================================

    2008-09-09 13:35:19 補充:

    int fileAccess = O_RDWR;

    int fd;

    if ((fd = open(mapped_file_name , fileAccess, SHM_FILE_MODE)) == -1)

    {

    return -1; // error

    }

    2008-09-09 13:35:56 補充:

    //---- Map the file to memory

    int protMode = PROT_READ|PROT_WRITE;

    int mapMode = MAP_SHARED;

    char * pUserSpace = NULL;

    int mapped_file_size=1024;

    2008-09-09 13:36:04 補充:

    if ((pUserSpace = (char *)mmap(NULL, mapped_file_size, protMode, mapMode, fd, 0)) == -1)

    {

    return -1; // error

    }

    2008-09-09 13:36:13 補充:

    =======================================

    Process A: Write a value to the shared memory

    =======================================

    2008-09-09 13:36:22 補充:

    MySharedData_t * pMySharedData= (MySharedData_t *) pUserSpace;

    pthread_mutex_lock(&pMySharedData->mutex); // lock the mutex

    pMySharedData->x = 100; // write data to shared memory

    pthread_mutex_lock(&pMySharedData->mutex);

    2008-09-09 13:36:30 補充:

    =====================================

    Process B: read a value from the shared memory

    =====================================

    2008-09-09 13:36:44 補充:

    MySharedData_t * pMySharedData= (MySharedData_t *) pUserSpace;

    pthread_mutex_lock(&pMySharedData->mutex); // lock the mutex

    2008-09-09 13:36:49 補充:

    if (pMySharedData->x ==100)

    printf("From process B, x becomes 100\n");

    pthread_mutex_lock(&pMySharedData->mutex); // unlock the mutex

    2008-09-09 13:47:47 補充:

    Correction:

    To unlock a mutex:

    pthread_mutex_unlock(&pMySharedData->mutex); // unlock the mutex

    參考資料: 經驗
  • 1 0 年前

    那是平行問題

    問題不在 how to R/W

    問題在 how to R/W them correctly

    你會平行程式?

還有問題?馬上發問,尋求解答。