Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

IPC 實踐(十一):用 System V 信號量打造 FIFO 共享記憶體 shmfifo

消息佇列天生就是先進先出,但共享記憶體的存取速度更快。若把共享記憶體當成環形緩衝區,再搭配信號量做同步,就能做出一個高效版的 FIFO。

設計思路

先用 C 完成程序化版本的 shmfifo,再包成 C++ 類別 ShmFifo:把「一塊共享記憶體 + 三個信號量」收在同一個物件裡。三個信號量分別是 sem_mutex(初值 1,互斥存取)、sem_full(初值為緩衝塊數,表示空位)、sem_empty(初值 0,表示已有資料)。讀寫雙方就自然成了生產者與消費者。

IPC 實踐(十一):用 System V 信號量打造 FIFO 共享記憶體 shmfifo

類別宣告

class ShmFifo {
public:
    ShmFifo(int _key, int _blksize, int _blocks);
    ~ShmFifo();
    void put(const void *buf);
    void get(void *buf);
    void destroy();
private:
    typedef struct shmhead {
        unsigned int blksize;   // 塊大小
        unsigned int blocks;    // 總塊數
        unsigned int rd_index;  // 讀索引
        unsigned int wr_index;  // 寫索引
    } shmhead_t;
private:
    shmhead_t *p_shm;   // 共享記憶體頭部
    char *p_payload;    // 有效負載起始位址
    int shmid;
    int sem_mutex;
    int sem_full;
    int sem_empty;
};

建構與銷毀

ShmFifo::ShmFifo(int _key, int _blksize, int _blocks) {
    shmid = shmget(_key, 0, 0);
    if (shmid == -1) {
        int size = _blksize*_blocks + sizeof(shmhead_t);
        shmid = shmget(_key, size, IPC_CREAT|0666);
        if (shmid == -1) err_exit("shmget error");
        p_shm = (shmhead_t *)shmat(shmid, NULL, 0);
        if (p_shm == (void *) -1) err_exit("shmat error");
        p_shm->blksize = _blksize;
        p_shm->blocks = _blocks;
        p_shm->rd_index = 0;
        p_shm->wr_index = 0;
        p_payload = (char *)(p_shm+1);
        sem_mutex = sem_create(_key);    sem_setval(sem_mutex, 1);
        sem_full  = sem_create(_key+1);  sem_setval(sem_full, _blocks);
        sem_empty = sem_create(_key+2);  sem_setval(sem_empty, 0);
    } else {
        p_shm = (shmhead_t *)shmat(shmid, NULL, 0);
        if (p_shm == (void *) -1) err_exit("shmat error");
        p_payload = (char *)(p_shm+1);
        sem_mutex = sem_open(_key);
        sem_full  = sem_open(_key+1);
        sem_empty = sem_open(_key+2);
    }
}

ShmFifo::~ShmFifo() {
    shmdt(p_shm);
    p_shm = NULL;
    p_payload = NULL;
}

void ShmFifo::destroy() {
    sem_delete(sem_mutex);
    sem_delete(sem_full);
    sem_delete(sem_empty);
    if (shmctl(shmid, IPC_RMID, NULL) == -1)
        err_exit("remove share memory error");
}

寫入與讀出

void ShmFifo::put(const void *buf) {
    sem_P(sem_full);
    sem_P(sem_mutex);
    char *index = p_payload + (p_shm->wr_index * p_shm->blksize);
    memcpy(index, buf, p_shm->blksize);
    p_shm->wr_index = (p_shm->wr_index+1) % p_shm->blocks;
    sem_V(sem_mutex);
    sem_V(sem_empty);
}

void ShmFifo::get(void *buf) {
    sem_P(sem_empty);
    sem_P(sem_mutex);
    char *index = p_payload + (p_shm->rd_index * p_shm->blksize);
    memcpy(buf, index, p_shm->blksize);
    p_shm->rd_index = (p_shm->rd_index+1) % p_shm->blocks;
    sem_V(sem_mutex);
    sem_V(sem_full);
}

Makefile

.PHONY: clean all
CC = g++
CPPFLAGS = -Wall -g
BIN = write read free
SOURCES = $(BIN.=.cpp)

all: $(BIN)
%.o: %.cpp
	$(CC) $(CPPFLAGS) -c $^ -o $@
write: write.o shmfifo.o ipc.o
	$(CC) $(CPPFLAGS) $^ -lrt -o $@
read: read.o shmfifo.o ipc.o
	$(CC) $(CPPFLAGS) $^ -lrt -o $@
free: free.o shmfifo.o ipc.o
	$(CC) $(CPPFLAGS) $^ -lrt -o $@
clean:
	-rm -rf $(BIN) *.o bin/ obj/ core

共享記憶體相關的函式在部分環境需要即時函式庫,連結時加上 -lrt 即可。

Copyright © Linux教程網 All Rights Reserved