自測之Lesson13:共享內存

題目:建立一個64K的共享內存。函數

 

實現代碼:spa

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define MEM_SIZE 0x10000        // 設置建立的共享內存大小爲64K

int main()
{
        key_t key;
        key = ftok("a", 1); 
        if (key == -1) {
                perror("fail ftok");
                return -1; 
        }

        int shmid;
        shmid = shmget(key, MEM_SIZE, IPC_CREAT | 0664);
        if (shmid == -1) {
                perror("fail shmget");
                return -1; 
        }
        // 打印共享內存的id和key值
        printf("key:%#x\nshmid:%d\n", key, shmid);    

        return 0;
}

 

 

題目:分別完成一個向共享內存讀/寫的程序,要求這兩個程序指向同一共享內存。blog

 

實現代碼:ip

/* 寫共享內存 */
#include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #include <unistd.h> #define MEM_SIZE 0x10000 // 64K 共享內存大小 #define SEG_SIZE 0x100 // 256b 分塊大小 int main() { key_t key; char szBuf[256]; key = ftok("a", 1); if (key == -1) { perror("fail ftok"); return -1; } int shmid; shmid = shmget(key, MEM_SIZE, IPC_CREAT | 0664); if (shmid == -1) { perror("fail shmget"); return -1; } printf("key:%#x\nshmid:%d\n", key, shmid); // shmat() 返回共享內存映射到內存的某一地址 char *addr; addr = shmat(shmid, NULL, 0); if (addr == (char *)-1) { perror("fail shmat"); return -1; } printf("share memory addr:%p\n", addr); // 寫共享內存 int offset = 0; while(1) { fprintf(stderr, "[Write]:"); // scanf("%s", szBuf); 不能讀入空格 read(STDIN_FILENO, szBuf, 256); if (offset == MEM_SIZE / SEG_SIZE) { printf("Share Memory is full!\n"); break; } else { memcpy(addr + SEG_SIZE * offset, szBuf, SEG_SIZE); // 將szBuf的值copy到共享內存 offset++; } } shmdt(addr); return 0; }

 

/* 讀共享內存 */
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>

#define MEM_SIZE 0x10000        // 64K   共享內存大小
#define SEG_SIZE 0x100          // 256b  分塊大小


int main()
{
        key_t key;
        char szBuf[256];
        key = ftok("a", 1);
        if (key == -1) {
                perror("fail ftok");
                return -1;
        }

        int shmid;
        shmid = shmget(key, MEM_SIZE, IPC_CREAT | 0664);
        if (shmid == -1) {
                perror("fail shmget");
                return -1; 
        }

        printf("key:%#x\nshmid:%d\n", key, shmid);    

        // shmat() 返回共享內存映射到內存的某一地址
        char *addr;
        addr = shmat(shmid, NULL, SHM_RDONLY);  
        if (addr == (char *)-1) {
                perror("fail shmat");
                return -1; 
        }

        printf("key:%#x\nshmid:%d\n", key, shmid);    

        // shmat() 返回共享內存映射到內存的某一地址
        char *addr;
        addr = shmat(shmid, NULL, SHM_RDONLY);  
        if (addr == (char *)-1) {
                perror("fail shmat");
                return -1;
        }
        printf("share memory addr:%p\n", addr);         //打印共享內存映射到的內存地址

        // 讀共享內存
        int offset;
        while(1) {
                fprintf(stderr, "[Read Seg-index]:");
                scanf("%d", &offset);
                if (offset == MEM_SIZE / SEG_SIZE) {
                        printf("Read over edge!\n");
                        break;
                }
                else {
                        memcpy(szBuf, addr + SEG_SIZE * offset, SEG_SIZE);
                        printf("->%s\n", szBuf);
                }
        }
        shmdt(addr);
        return 0;
}

 

 

題目:完成一個讀取系統當前共享內存區使用狀況的程序,要求使用shmctl函數完成。內存

 

實現代碼:get

相關文章
相關標籤/搜索