Linux共用記憶體實踐(2)

來源:互聯網
上載者:User

標籤:linux   共用記憶體   ipc   處理序間通訊   shmctl   

shmdt函數

功能:將共用記憶體段與當前進程脫離

原型:

int shmdt(const void *shmaddr);

參數:

    shmaddr: 由shmat所返回的指標

 

傳回值:

    成功返回0;失敗返回-1


//實踐:運行程式,觀察ipcs輸出int main(){    //擷取或者開啟共用記憶體    int shmid = shmget(0x15764221, 1024 * sizeof(int), 0666 | IPC_CREAT);    if (shmid == -1)    {        err_exit("shmget error");    }    //將ID為shmid的共用記憶體串連到該進程    int *pArray = static_cast<int *>(shmat(shmid, NULL, 0));    if (pArray == (void *)-1)    {        err_exit("shmat error");    }    sleep(10);    //將共用記憶體從當前進程分離    if (shmdt(pArray) == -1)    {        err_exit("shmdt error");    }    sleep(10);    return 0;}

注意:將共用記憶體段與當前進程脫離不等於刪除共用記憶體段

 

shmctl函數

功能:用於調控/擷取共用記憶體屬性

原型:

int shmctl(int shmid, int cmd, struct shmid_ds *buf);

參數:

    shmid:由shmget返回的共用記憶體標識碼

    cmd:將要採取的動作(三個取值見下)

    buf:指向一個儲存著共用記憶體的模式狀態和存取權限的資料結構



傳回值:

    成功返回0;失敗返回-1

//樣本:detach & delete, 請在多個終端中啟動該程式,仔細用ipcs觀察,該程式的每一步int main(){    //擷取或者開啟共用記憶體    int shmid = shmget(0x15764221, 1024 * sizeof(int), 0666 | IPC_CREAT);    if (shmid == -1)    {        err_exit("shmget error");    }    //將ID為shmid的共用記憶體串連到該進程    int *pArray = static_cast<int *>(shmat(shmid, NULL, 0));    if (pArray == (void *)-1)    {        err_exit("shmat error");    }    int choice = 0;    cout << "Please input your choice: 0-detach, other-continue: ";    cin >> choice;    if (choice == 0)    {        //將共用記憶體從當前進程分離        if (shmdt(pArray) == -1)        {            err_exit("shmdt error");        }    }    //     int shmctl(int shmid, int cmd, struct shmid_ds *buf);    cout << "\nPlease input your choice: 0-delete, other-quit: ";    cin >> choice;    if (choice == 0)    {        //刪除共用記憶體(shmid的引用計數-1)        if (shmctl(shmid,IPC_RMID,NULL) == -1)        {            err_exit("shmctl error");        }        else        {            cout << "Delete Success!" << endl;        }    }    return 0;}/**  共用記憶體在核心中使用了引用計數技術:必須等到串連到該共用記憶體的數目為0時(等到該共用記憶體的引用計數變為0),才執行真正的刪除該共用記憶體動作!當引用技術不為0時,調用IPC_RMID時,該共用記憶體的key會變成0(變為私人的);*/

/**樣本:查看在其中一個進程執行刪除之後(但此時引用計數還不為0),該共用記憶體是否可讀;執行順序:  writeMemory  readMemory  writeMemory中,鍵入0  readMemory中,鍵入0*/

//writeMemory.cppint main(){    const int NUMBER = 36;    int shmid = shmget(0x15764221, NUMBER * sizeof(int), 0666 | IPC_CREAT);    if (shmid == -1)    {        err_exit("shmget error");    }    int *pArray = static_cast<int *>(shmat(shmid, NULL, 0));    if (pArray == (void *)-1)    {        err_exit("shmat error");    }    for (int i = 0; i < NUMBER; ++i)    {        *(pArray+i) = i+1;    }    int choice = 0;    cout << "Please input your choice: 0-delete, other-quit: ";    cin >> choice;    if (choice == 0)    {        //刪除共用記憶體(shmid的引用計數-1)        if (shmctl(shmid,IPC_RMID,NULL) == -1)        {            err_exit("shmctl error");        }        else        {            cout << "Delete Success!" << endl;        }    }    return 0;}

//2:readMemory.cppint main(){    const int NUMBER = 36;    int shmid = shmget(0x15764221, NUMBER * sizeof(int), 0666);    if (shmid == -1)    {        err_exit("shmget error");    }    int *pArray = static_cast<int *>(shmat(shmid, NULL, 0));    if (pArray == (void *)-1)    {        err_exit("shmat error");    }    int choice = 0;    cout << "Please input your Choice: 0-read, other-quit: ";    cin >> choice;    if (!choice)    {        for (int i = 0; i < NUMBER; ++i)        {            cout << *(pArray+i) << "\t";        }        cout << endl;    }    return 0;}



    由可知:即使其中一個進程執行了IPC_RMID(只要該共用記憶體引用計數不為0),該共用核心還是可讀的(但此時必須該讀進程是已經串連到該共用記憶體的,不然的話,寫進程雖然沒有將共用記憶體真正的刪掉,但是其索引值已經變為0x00000000(Private),此時其他進程是串連不上的)!

 

總結:

    1.共用記憶體被別的程式佔用,則刪除該共用記憶體時,不會馬上刪除

    2.此時會出現一個現象:該共用記憶體的key變為0x00000000,變為私人

    3.此時還可以讀,但必須還有辦法擷取該共用記憶體的ID(shmid),因為此時試圖通過該共用記憶體的key擷取該共用記憶體,都是白費的!


Linux共用記憶體實踐(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.