All pastes #1283782 Raw Edit

Stuff

public text v1 · immutable
#1283782 ·published 2008-12-12 19:09 UTC
rendered paste body
#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h> 
#include <unistd.h>



/*
1) open() a file over an ext3 filesystem
2) mmap() the file with the MAP_SHARED flag
3) Write records to some areas of the memory mapped file. Records may be less than a page in size or span multiple pages
4) msync(... , 0) the memory area that was modified (see note below)
*/

		 /* This file should be on ext3 */ 
#define FILENAME "/tmp/file.img"
#define FILESIZE 4096
#define SLEEPTIME 300

int main(void) {

	int fd, pagesize; 
	int counter; 

	struct tm *ptr;
	time_t tm;
	
	char *data; 
	char *tmp; 

	pagesize = getpagesize();

	printf("Starting msync test\n");

	/* 1) open() a file over an ext3 filesystem */
 	if ((fd = open(FILENAME, O_RDWR|O_CREAT)) == -1) {
            perror("open");
            exit(1);
        }

	/* Lets artificially create a large file */
	lseek(fd, FILESIZE * 1024, SEEK_SET ) ; 

	/* 2) mmap() the file with the MAP_SHARED flag */
        if ((data = mmap(NULL , pagesize * 1024 , PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == NULL) {
            perror("mmap");
            exit(1);
        }

	lseek(fd, 0, SEEK_SET) ; 


	/* For troubleshooting, we need to find the time */ 

	tm = time(NULL);
	ptr = localtime(&tm);
	tmp = asctime(ptr);


	/* 3) Write records to some areas of the memory mapped file. Records may be less than a page in size or span multiple pages */
	memcpy(data, tmp, strlen(tmp) );  // <- in this case its way less than a page.
	
	/* 4) msync(... , 0) the memory area that was modified */

	/* Lets sync a full page size */
        if (-1 == msync(data, pagesize , 0 )) {
		perror("msync"); 
		exit(1); 
	}

	sleep(SLEEPTIME);

	close(fd); 
	printf("Finishing msync test\n");
	printf("Test case should have rebooted the system and you probably shouldnt see this");
	return 0;
}