Linux Shared Memory

Source: Internet
Author: User
Tags mutex posix semaphore

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) *, Prot_read | Prot_write,
map_shared, FD, 0);

Close (FD);

temp = ' a ';

for (i = 0; i <; i++)
{
temp + = 1;
memcpy ((* (P_map+i)). Name, &temp, 2);
(* (P_map+i)). Age = + 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 is 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'll use the 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! = ten; ++i)
{
printf ("name:%s, Age:%d\n", (* (addr + i)). Name, (* (addr + i)).
}

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

Exit (0);
}

Server:

/*
* Demo of Shared memory
*
* There is 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'll use the 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) *) < 0)
{
printf ("Ftruncate shm error!\n");
Exit (-1);
}
/*
* Setup Mapping
*/
if (addr = (person*) mmap (NULL, sizeof (person) *, 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! = ten; ++i)
{
TMP + = 1;
memcpy ((* (addr + i)). Name, &tmp, 2);
(* (addr + i)). Age = + I;
}

Sem_post (mutex);

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

Exit (0);
}



Linux Shared Memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.