共享内存

#include 
#include 
#include 
#include 
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 
#include 
#include 
#include 
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;
}

你可能感兴趣的:(共享内存)