#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
    int fd;
    char *pm;
    struct stat st;
    pid_t p;
//    p = fork();
    fd = open("/home/ice/mmap.dat", O_RDWR);
    if( fd < 0 ){
        perror("open mmap.dat error");
        exit(1);
    }
    if ( fstat(fd, &st) != 0 ){
        perror("stat error");
        exit(1);
    }
    pm = (char*) mmap( NULL, st.st_size, PROT_WRITE | PROT_READ , MAP_SHARED , fd, 0);
//    pm = (char*) mmap( NULL, st.st_size,  PROT_READ , MAP_SHARED , fd, 0);
    if ( pm < 0 || pm == MAP_FAILED ){
        perror("mmap failed");
        exit(1);
    }
    
    struct timeval tBegin, tEnd;
    bzero( &tBegin, sizeof(tBegin) );
    bzero( &tEnd, sizeof(tEnd) );

    gettimeofday( &tBegin, NULL);
    int i;
    char buffer[4096];
//    read( fd, buffer, 4096);
    while(1){
    for( i = 0; i < 1024*100; ++i )
    {
        memcpy( pm + 4096*i,buffer, 4096 );
//        pwrite( fd, buffer, 4096, i * 4096 );

    }
    sleep(300);
    }
//    mlockall(MCL_CURRENT);
//
    gettimeofday( &tEnd, NULL);
    int liTimeDiff = ((tEnd.tv_sec - tBegin.tv_sec) * 1000000 + tEnd.tv_usec - tBegin.tv_usec ) / 1000;
    printf("%d ms\n", liTimeDiff );
    printf("msync\n");
    msync( pm, st.st_size, MS_SYNC );
    munmap( pm, st.st_size );
//    sleep(600);
}