码迷,mamicode.com
首页 > 系统相关 > 详细

Linux Shared Memory

时间:2015-08-07 18:31:59      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

Shared memory based mapping file

 

#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

typedef struct {
  char name[4];
  int  age;
} people;

int main(int argc, char **argv)
{
  int    fd, i;
  people *p_map;
  char   temp;

  fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 00777);
  lseek(fd, sizeof(people) * 5 - 1, SEEK_SET);
  write(fd,"",1);

  p_map = (people*) mmap(NULL, sizeof(people) * 10, PROT_READ | PROT_WRITE,
          MAP_SHARED, fd , 0);

  close( fd );

  temp = ‘a‘;

  for ( i = 0; i < 10; i++)
  {
    temp += 1;
    memcpy( ( *(p_map+i) ).name, &temp, 2 );
    ( *(p_map+i) ).age = 20 + i;
  }

  printf(" initialize over \n ");

  sleep(10);

  munmap( p_map, sizeof(people)*10 );

  printf( "umap ok \n" );

  exit(0);
}


 

 

#include <sys/mman.h>

#include <sys/types.h>

#include <fcntl.h>

#include <unistd.h>

typedef struct{

  char name[4];

  int  age;

}people;

main(int argc, char** argv)  // map a normal file as shared mem:

{

  int fd,i;

  people *p_map;

  fd=open( argv[1],O_CREAT|O_RDWR,00777 );

  p_map = (people*)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,

       MAP_SHARED,fd,0);

  for(i = 0;i<10;i++)

  {

  printf( "name: %s age %d;\n",(*(p_map+i)).name, (*(p_map+i)).age );

  }

  munmap( p_map,sizeof(people)*10 );

}

 

 

 

Posix   Shared Memory

Client:

/*
 * Demo of shared memory
 *
 * There are two kinds of ways to implement shared memory:
 * (1) map file:
 *              fd = open(filename...);
 *              ptr = mmap();
 * (2) shared memory(only posix here):
 *              fd = shm_open(shm_name...);
 *              ptr = mmap();
 *
 * Now, we show (2)
 *
 * Also, we will use named semaphore to sync multiple processes.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>

#define SHM_NAME "/shm"
#define SEM_NAME "/shmsem"

typedef struct {
    char name[4];
    int  age;
} person;

int main(int argc, char **argv)
{
    int    fd, i;
    struct stat st;
    sem_t  *mutex;
    person *addr = NULL;
    char   *ptr;

    /*
     * Open semaphore
     */

    if ( (mutex = sem_open(SEM_NAME, O_RDWR, 0, 0)) == SEM_FAILED)
    {
        printf("Open semaphore error!\n");
        exit(-1);
    }

    /*
     * Open shared memory
     */
    if ( (fd = shm_open(SHM_NAME, O_RDONLY, 0)) < 0)
    {
        printf("Open shm error!\n");
        exit(-1);
    }

    if (fstat(fd, &st) < 0)
    {
        printf("Fstat shm error!\n");
        exit(-1);
    }
    /*
     * Setup mapping
     */
    if ( (addr = (person*)mmap(NULL, st.st_size, PROT_READ,
            MAP_SHARED, fd, 0)) == NULL)
    {
        printf("mmap error!\n");
        exit(-1);
    }

    /*close(fd);*/

    /*
     * Send some messages
     */
    sem_wait(mutex);
    for (i = 0; i != 10; ++i)
    {
        printf("Name:%s, Age: %d\n", (*(addr + i)).name, (*(addr + i)).age);
    }

    munmap(addr, st.st_size);
    printf("munmap finished!\n");

    exit(0);
}

 

 

Server:

/*
 * Demo of shared memory
 *
 * There are two kinds of ways to implement shared memory:
 * (1) map file:
 *              fd = open(filename...);
 *              ptr = mmap();
 * (2) shared memory(only posix here):
 *              fd = shm_open(shm_name...);
 *              ptr = mmap();
 *
 * Now, we show (2)
 *
 * Also, we will use named semaphore to sync multiple processes.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/mman.h>

#define SHM_NAME "/shm"
#define SEM_NAME "/shmsem"

typedef struct {
    char name[4];
    int  age;
} person;

int main(int argc, char **argv)
{
    int    fd, i;
    char   tmp;
    sem_t  *mutex;
    person *addr = NULL;
    char   *ptr;

    /*
     * Create semaphore
     */

    if ( (mutex = sem_open(SEM_NAME, O_CREAT | O_RDWR, 0666, 1)) == SEM_FAILED)
    {
        printf("Create semaphore error!\n");
        exit(-1);
    }

    /*
     * Create shared memory
     */
    if ( (fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666)) < 0)
    {
        printf("Create shm error!\n");
        exit(-1);
    }

    if (ftruncate(fd, sizeof (person) * 10) < 0)
    {
        printf("Ftruncate shm error!\n");
        exit(-1);
    }
    /*
     * Setup mapping
     */
    if ( (addr = (person*)mmap(NULL, sizeof (person) * 10, PROT_READ | PROT_WRITE,
            MAP_SHARED, fd, 0)) == NULL)
    {
        printf("mmap error!\n");
        exit(-1);
    }

    /*close(fd);*/

    tmp = ‘a‘;

    /*
     * Send some messages
     */
    for (i = 0; i != 10; ++i)
    {
        tmp += 1;
        memcpy( (*(addr + i)).name, &tmp, 2);
        (*(addr + i)).age = 20 + i;
    }

    sem_post(mutex);

    munmap(addr, sizeof (person) * 10);
    printf("munmap finished!\n");

    exit(0);
}



Linux Shared Memory

标签:

原文地址:http://www.cnblogs.com/qingxueyunfeng/p/4711189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!