共享内存

2018-08-21  本文已影响0人  0X7C00
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
int main()
{
    char * file = "test.data";
    int fd = open(file, O_RDWR|O_CREAT | O_TRUNC, 00666);
    if (fd < 0)
    {
        perror("open file error");
    }
    char *ptr = mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if (ptr == MAP_FAILED)
    {
        perror("mmap error");  
    }
    ftruncate(fd,4);
    //int * a = (int *)ptr; *a = 3;
    *ptr = 3;
    return 0;
}
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
int main()
{
    char * file = "test.data";
    int fd = open(file, O_RDWR|O_CREAT | O_TRUNC, 00666);
    if (fd < 0)
    {
        perror("open file error");
    }
    char *ptr = mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if (ptr == MAP_FAILED)
    {
        perror("mmap error");  
    }
    ftruncate(fd,4);
    //int * a = (int *)ptr; *a = 3;
    *ptr = 3;
    return 0;
}

上一篇 下一篇

猜你喜欢

热点阅读