有两种方法:shmget和mmap。我将讨论mmap,因为它更加现代和灵活,但是如果您更愿意使用旧式工具,可以看看man shmget(or this tutorial)。 mmap()函数可用于分配具有高度可定制参数的内存缓冲区,以控制访问和权限,并在必要时使用文件系统存储来支持它们。 下面的函数创建一个进程可以与其子进程共享的内存缓冲区:
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
void* create_shared_memory(size_t size) {
// Our memory buffer will be readable and writable:
int protection = PROT_READ | PROT_WRITE;
// The buffer will be shared (meaning other processes can access it), but
// anonymous (meaning third-party processes cannot obtain an address for it),
// so only this process and its children will be able to use it:
int visibility = MAP_SHARED | MAP_ANONYMOUS;
// The remaining parameters to `mmap()` are not important for this use case,
// but the manpage for `mmap` explains their purpose.
return mmap(NULL, size, protection, visibility, -1, 0);
}
#include<sys/ipc.h>
#include<sys/shm.h>
int shmid;
int shmkey = 12222;//u can choose it as your choice
int main()
{
//now your main starting
shmid = shmget(shmkey,1024,IPC_CREAT);
// 1024 = your preferred size for share memory
// IPC_CREAT its a flag to create shared memory
//now attach a memory to this share memory
char *shmpointer = shmat(shmid,NULL);
//do your work with the shared memory
//read -write will be done with the *shmppointer
//after your work is done deattach the pointer
shmdt(&shmpointer, NULL);
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/*
* pvtmMmapAlloc - creates a memory mapped file area.
* The return value is a page-aligned memory value, or NULL if there is a failure.
* Here's the list of arguments:
* @mmapFileName - the name of the memory mapped file
* @size - the size of the memory mapped file (should be a multiple of the system page for best performance)
* @create - determines whether or not the area should be created.
*/
void* pvtmMmapAlloc (char * mmapFileName, size_t size, char create)
{
void * retv = NULL;
if (create)
{
mode_t origMask = umask(0);
int mmapFd = open(mmapFileName, O_CREAT|O_RDWR, 00666);
umask(origMask);
if (mmapFd < 0)
{
perror("open mmapFd failed");
return NULL;
}
if ((ftruncate(mmapFd, size) == 0))
{
int result = lseek(mmapFd, size - 1, SEEK_SET);
if (result == -1)
{
perror("lseek mmapFd failed");
close(mmapFd);
return NULL;
}
/* Something needs to be written at the end of the file to
* have the file actually have the new size.
* Just writing an empty string at the current file position will do.
* Note:
* - The current position in the file is at the end of the stretched
* file due to the call to lseek().
* - The current position in the file is at the end of the stretched
* file due to the call to lseek().
* - An empty string is actually a single '\0' character, so a zero-byte
* will be written at the last byte of the file.
*/
result = write(mmapFd, "", 1);
if (result != 1)
{
perror("write mmapFd failed");
close(mmapFd);
return NULL;
}
retv = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, mmapFd, 0);
if (retv == MAP_FAILED || retv == NULL)
{
perror("mmap");
close(mmapFd);
return NULL;
}
}
}
else
{
int mmapFd = open(mmapFileName, O_RDWR, 00666);
if (mmapFd < 0)
{
return NULL;
}
int result = lseek(mmapFd, 0, SEEK_END);
if (result == -1)
{
perror("lseek mmapFd failed");
close(mmapFd);
return NULL;
}
if (result == 0)
{
perror("The file has 0 bytes");
close(mmapFd);
return NULL;
}
retv = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, mmapFd, 0);
if (retv == MAP_FAILED || retv == NULL)
{
perror("mmap");
close(mmapFd);
return NULL;
}
close(mmapFd);
}
return retv;
}
6条答案
按热度按时间zpqajqem1#
有两种方法:
shmget
和mmap
。我将讨论mmap
,因为它更加现代和灵活,但是如果您更愿意使用旧式工具,可以看看man shmget
(or this tutorial)。mmap()
函数可用于分配具有高度可定制参数的内存缓冲区,以控制访问和权限,并在必要时使用文件系统存储来支持它们。下面的函数创建一个进程可以与其子进程共享的内存缓冲区:
下面是一个使用上面定义的函数来分配缓冲区的示例程序。父进程将写入一条消息,fork,然后等待其子进程修改缓冲区。两个进程都可以读写共享内存。
nzk0hqpo2#
下面是一个共享内存的例子:
步骤:
1.使用ftok将路径名和项目标识符转换为SystemVIPC键
1.使用分配共享内存段的shmget
1.使用shmat将shmid标识的共享内存段附加到调用进程的地址空间
1.对内存区域进行操作
1.使用shmdt分离
1szpjjfi3#
这些都是使用共享内存的内容
9njqaruj4#
试试这个代码示例,我测试过了,源码:http://www.makelinux.net/alp/035
xggvc2p65#
下面是一个mmap示例:
iaqfqrcu6#
我简化了@salezica的答案,在父进程/子进程之间读/写一个共享的
int
。同时将 Package 函数重命名为
shared_malloc
。