美文网首页
共享内存

共享内存

作者: 0X7C00 | 来源:发表于2018-08-21 23:36 被阅读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;
    }
    
    #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;
    }
    
    

    相关文章

      网友评论

          本文标题:共享内存

          本文链接:https://www.haomeiwen.com/subject/ekgsiftx.html