Linux进程间通信 -3内存共享

来源:互联网 发布:图片识别算法 编辑:程序博客网 时间:2024/05/17 12:48

内存共享允许两个或多个不相关的进程,访问同一个逻辑内存,共享内存的具体实现,由不同进程之间共享的内存安排为同一物理内存。
过个进程就像通过malloc获取的内存一样去使用,但是需要额外的小消息来同队内存的访问。

可以通过信号量,传递消息(消息队列),生成信号来同步对内存的访问。

1 shmget

       #include <sys/ipc.h>       #include <sys/shm.h>       int shmget(key_t key, size_t size, int shmflg);

shmget() returns the identifier of the System V shared memory segment associated with the value of the argument key. A new shared memory segment, with size equal to the value of size rounded up to a multiple of PAGE_SIZE, is created if key has the value IPC_PRI‐VATE or key isn’t IPC_PRIVATE, no shared memory segment corresponding to key exists, and IPC_CREAT is specified in shmflg.

2 shmat

把创建的内存地址空间连接到进程的地址空间。

       #include <sys/types.h>       #include <sys/shm.h>       void *shmat(int shmid, const void *shmaddr, int shmflg);       int shmdt(const void *shmaddr);

3 shmdt

       #include <sys/types.h>       #include <sys/shm.h>       void *shmat(int shmid, const void *shmaddr, int shmflg);       int shmdt(const void *shmaddr);

把共享的内存地址从进程中分离。

4 shmctl

控制共享内存

       #include <sys/ipc.h>       #include <sys/shm.h>       int shmctl(int shmid, int cmd, struct shmid_ds *buf);

控制共享内存;

5 使用共享内存

/* Our first program is a consumer. After the headers the shared memory segment (the size of our shared memory structure) is created with a call to shmget, with the IPC_CREAT bit specified. */#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/shm.h>#include "shm_com.h"int main(){    int running = 1;    void *shared_memory = (void *)0;    struct shared_use_st *shared_stuff;    int shmid;    srand((unsigned int)getpid());        shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);    if (shmid == -1) {        fprintf(stderr, "shmget failed\n");        exit(EXIT_FAILURE);    }/* We now make the shared memory accessible to the program. */    shared_memory = shmat(shmid, (void *)0, 0);    if (shared_memory == (void *)-1) {        fprintf(stderr, "shmat failed\n");        exit(EXIT_FAILURE);    }    printf("Memory attached at %X\n", (int)shared_memory);/* The next portion of the program assigns the shared_memory segment to shared_stuff, which then prints out any text in written_by_you. The loop continues until end is found in written_by_you. The call to sleep forces the consumer to sit in its critical section, which makes the producer wait. */    shared_stuff = (struct shared_use_st *)shared_memory;    shared_stuff->written_by_you = 0;    while(running) {        if (shared_stuff->written_by_you) {            printf("You wrote: %s", shared_stuff->some_text);            sleep( rand() % 4 ); /* make the other process wait for us ! */            shared_stuff->written_by_you = 0;            if (strncmp(shared_stuff->some_text, "end", 3) == 0) {                running = 0;            }        }    }/* Lastly, the shared memory is detached and then deleted. */    if (shmdt(shared_memory) == -1) {        fprintf(stderr, "shmdt failed\n");        exit(EXIT_FAILURE);    }    if (shmctl(shmid, IPC_RMID, 0) == -1) {        fprintf(stderr, "shmctl(IPC_RMID) failed\n");        exit(EXIT_FAILURE);    }    exit(EXIT_SUCCESS);}
/* The second program is the producer and allows us to enter data for consumers. It's very similar to shm1.c and looks like this. */#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/shm.h>#include "shm_com.h"int main(){    int running = 1;    void *shared_memory = (void *)0;    struct shared_use_st *shared_stuff;    char buffer[BUFSIZ];    int shmid;    shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);    if (shmid == -1) {        fprintf(stderr, "shmget failed\n");        exit(EXIT_FAILURE);    }    shared_memory = shmat(shmid, (void *)0, 0);    if (shared_memory == (void *)-1) {        fprintf(stderr, "shmat failed\n");        exit(EXIT_FAILURE);    }    printf("Memory attached at %X\n", (int)shared_memory);    shared_stuff = (struct shared_use_st *)shared_memory;    while(running) {        while(shared_stuff->written_by_you == 1) {            sleep(1);                        printf("waiting for client...\n");        }        printf("Enter some text: ");        fgets(buffer, BUFSIZ, stdin);        strncpy(shared_stuff->some_text, buffer, TEXT_SZ);        shared_stuff->written_by_you = 1;        if (strncmp(buffer, "end", 3) == 0) {                running = 0;        }    }    if (shmdt(shared_memory) == -1) {        fprintf(stderr, "shmdt failed\n");        exit(EXIT_FAILURE);    }    exit(EXIT_SUCCESS);}
0 0
原创粉丝点击