Linux Shared Memory

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);
}



转载于:https://www.cnblogs.com/qingxueyunfeng/p/4711189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值