linux共享内存使用实例

三个文件,一个头文件,一个读,一个写,用同一个key值申请共享内存。

//shm.h

#ifndef _SHM_COM_H
#define _SHM_COM_H 1

#define TEXT_SZ 2048

struct shared_use_at
{
    int written_by_you;
    char some_text[TEXT_SZ];
};

struct kts
{
 int power;
 int mode;
 int temp;
 int windspeed;

 int write_flag;
};

#endif

//shm_write.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "shm.h"

int main()
{
 int running = 1;
 void *shared_memory = (void *)0;
 struct shared_use_at *shared_stuff;
 char buffer[BUFSIZ];
 int shmid;

    shmid = shmget((key_t)1234, sizeof(struct shared_use_at), 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_at *)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);
}

//shm_read.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#include "shm.h"

int main()
{
    int running = 1;
 void *shared_memory = (void *)0;
    struct shared_use_at *shared_stuff;
    int shmid;
    srand((unsigned int) getpid());
    shmid = shmget((key_t)1234, sizeof(struct shared_use_at), 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_at *)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);
            shared_stuff->written_by_you = 0;
            if(strncmp(shared_stuff->some_text, "end", 3)==0)
            {
                running = 0;
            }
        }
    }
    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);

阅读更多
文章标签: linux struct buffer
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭