从man页面,
MAP_ANONYMOUS
The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require
fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications
我正在使用mmap实现共享内存的IPC。我用于共享的结构是
struct shared{
sem_t P;
sem_t C;
sem_t M;
int prod_status;
char** queue;
int buffer_size;
int queue_start;
int queue_after_last; //pointing to buffer index after the last element in the buffer
int queue_count;
};
缓冲区的大小作为命令行参数传递
我试图用mmap打开一个文件:它在使用MAP_PRIVATE时工作得很好,但是MAP_SHARED会导致一个无效的参数错误:mmap ist读/写文件
int size;
struct stat s;
const char * file_name = argv[1];
int fd = open (argv[1], O_RDWR);
int pagesize = sysconf(_SC_PAGE_SIZE);
/* Get the size of the file. */
int status = fstat (fd, & s);
size = s.st_size;
size +=
考虑这两个程序:
//in
#define MAX 50
int main(int argc, char* argv[]) {
int *count;
int fd=shm_open("/count",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
int ret=ftruncate(fd,sizeof(int));
count=mmap(0,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
while ((*count)==MAX);
我正在尝试为共享内存对象运行程序。我的代码如下:
#include <stdio.h> /*adding standard input output library*/
#include <stdlib.h> /*standard library for four variable types, several macros, and various functions for performing general functions*/
#include <string.h> /*adding string library*/
#include <
基于以下来源,我有几个问题:
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
int g;
int main(void) {
int fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(fd, sizeof(int)); // set size by sizeof(int)
int *p1 = mm