1 / 7

Shared memory 補充說明

Shared memory 補充說明. Segmentation fault. 許多人在工作站測試時,遇到 segmentation fault 的問題,原因為 shmget () 沒有成功建立空間 無法建立的原因大部分是因為工作站沒有足夠的空間 來 建立 shared memory 而空間不夠的原因為 系統給予程式一塊空間,不過程式沒有歸還系統,因此這塊空間就一直被佔據著。隨著程式越來越多,工作站的空間就不夠用 了. 空間不夠的解決辦法. 目前只能先麻煩同學換別的工作站 (bsd1~6 or linux1~6) 測試 看看. Shared Memory.

lisbet
Télécharger la présentation

Shared memory 補充說明

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shared memory 補充說明

  2. Segmentation fault • 許多人在工作站測試時,遇到segmentation fault的問題,原因為shmget()沒有成功建立空間 • 無法建立的原因大部分是因為工作站沒有足夠的空間來建立shared memory • 而空間不夠的原因為 • 系統給予程式一塊空間,不過程式沒有歸還系統,因此這塊空間就一直被佔據著。隨著程式越來越多,工作站的空間就不夠用了

  3. 空間不夠的解決辦法 目前只能先麻煩同學換別的工作站(bsd1~6 or linux1~6)測試看看

  4. Shared Memory • 請務必在程式最後加上以下兩項: • intshmdt(const void* shmaddr) • Detach a shred memory, shmaddr is the value return by shmat() • intshmctl(intshmid, int IPC_RMID, NULL) • Remove a shared memory

  5. 如何刪除之前程式建立的SHM? • % ipcs –m • 查看正在使用的shared memory空間 • 記下OWNER是屬於你的ID號碼 • % ipcrm –m 327680 • 刪除此shared memory

  6. shared memory範例更新 #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> int main() { intShmID, *ShmPTR, status; pid_tpid; ShmID = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT|0666); if (ShmID < 0) { printf ("The shmget call failed, error number = %d\n", errno); exit(1); } ShmPTR = (int *) shmat(ShmID, NULL, 0); if ((int) ShmPTR == -1) { printf("The shmat call failed\n"); exit(1); }

  7. shared memory範例更新(續) Important!!! printf("Server has attached the shared memory\n"); pid = fork(); if (pid == -1) printf("failure!\n"); else if (pid == 0) for(;;) { if(ShmPTR[0] != 0) { printf("child get number = %d\n",ShmPTR[0]); exit(1); } } else { srand(pid); ShmPTR[0] = rand()%10+1; printf("parent rand number = %d\n",ShmPTR[0]); } shmdt((void *) ShmPTR); shmctl(ShmID, IPC_RMID, NULL); exit(0); }

More Related