如何在C中與Linux一起使用共享內存我的一個項目有點問題。我一直在試圖找到一個有詳細文檔的使用共享內存的示例。fork()但沒有成功?;旧希斢脩魡映绦驎r,我需要在共享內存中存儲兩個值:電流路徑這是夏爾*和一個文件名這也是夏爾*.根據命令參數,一個新進程將以fork()而該過程需要讀取和修改電流路徑變量存儲在共享內存中,而文件名變量是只讀的。有關于共享內存的示例代碼(如果可能的話)的好教程,您可以指導我嗎?
3 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
#include<sys/ipc.h>#include<sys/shm.h>int shmid;int shmkey = 12222;//u can choose it as your choiceint main(){ //now your main starting shmid = shmget(shmkey,1024,IPC_CREAT); // 1024 = your preferred size for share memory // IPC_CREAT its a flag to create shared memory //now attach a memory to this share memory char *shmpointer = shmat(shmid,NULL); //do your work with the shared memory //read -write will be done with the *shmppointer //after your work is done deattach the pointer shmdt(&shmpointer, NULL);
- 3 回答
- 0 關注
- 812 瀏覽
添加回答
舉報
0/150
提交
取消