共享内存

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#define SIZE 500*1024*1024 //50MB
#include <sys/shm.h>
int main(int argc, char** argv)
{
         FILE *fp;
         char c = 'a';
         int i;
         char *pmap;
         int fd;
         char *mem;
         struct timeval t1, t2;
         long spend_us;
         printf("write size: %dMB\n", SIZE/1024/1024);
         if((fp = fopen("disk_file", "w")) == NULL)
         {
                   printf("fopen error!\n");
                   return -1;
         }
         gettimeofday(&t1, NULL);
         for(i = 0; i < SIZE; i++)
         {
                   if(fwrite(&c, sizeof(char), 1, fp) != 1)
                            printf("file write error!\n");
         }
         gettimeofday(&t2, NULL);
         spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
         printf("write disk file,spend time:%ld ms\n", spend_us/1000);
         fclose(fp);
         fd = open("mmap_file", O_RDWR);
         if(fd < 0){
                   perror("open mmap_file");
                   return -1;
         }
         pmap = (char *)mmap(NULL, sizeof(char) * SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
         if(pmap == MAP_FAILED){
                   perror("mmap");
                   return -1;
         }
         gettimeofday(&t1, NULL);
         for(i = 0; i < SIZE; i++)
         {
                   memcpy(pmap + i, &c, 1);
         }
         gettimeofday(&t2, NULL);
         spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
         printf("write mmap file,spend time:%ld ms\n", spend_us/1000);
         close(fd);
         munmap(pmap, sizeof(char) * SIZE);
         mem = (char *)malloc(sizeof(char) * SIZE);
         if(!mem)
         {
                   printf("mem malloc error!\n");
                   return -1;
         }
         gettimeofday(&t1, NULL);
         for(i = 0; i < SIZE; i++)
         {
                   memcpy(mem + i, &c, 1);
         }
         gettimeofday(&t2, NULL);
         spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
         printf("write mem,spend time:%ld ms\n", spend_us/1000);
         free(mem);




        int shmid;
        void *shm = NULL;
        //创建共享内存
        shmid = shmget((key_t)1236, SIZE, 0666|IPC_CREAT);
        if(shmid == -1)
        {
                fprintf(stderr, "shmget failed\n");
                exit(EXIT_FAILURE);
        }
        //将共享内存连接到当前进程的地址空间
        shm = shmat(shmid, (void*)0, 0);
        if(shm == (void*)-1)
        {
                fprintf(stderr, "shmat failed\n");
                exit(EXIT_FAILURE);
        }
        printf("Memory attached at %X\n", (int)shm);
        //设置共享内存
        char * p = (char*)shm;
        gettimeofday(&t1, NULL);
        for(i = 0; i < SIZE; i++)
         {
                   memcpy(p + i, &c, 1);
         }
        gettimeofday(&t2, NULL);
        spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
         printf("write shm,spend time:%ld ms\n", spend_us/1000);
         //把共享内存从当前进程中分离
        if(shmdt(shm) == -1)
        {
                fprintf(stderr, "shmdt failed\n");
                exit(EXIT_FAILURE);
        }
        sleep(2);
         return 0;

}

 

https://blog.csdn.net/foudary/article/details/6575804

https://blog.csdn.net/ljianhui/article/details/10253345

 

 

 

root@node0 ~]# ./test_map
write size: 500MB
write disk file,spend time:7682 ms
write mmap file,spend time:1380 ms
write mem,spend time:1293 ms
Memory attached at 6643B000
write shm,spend time:1406 ms

 

posted @ 2020-09-28 17:16  虾兵  阅读(85)  评论(0编辑  收藏  举报