Skip to content

Instantly share code, notes, and snippets.

@dechamps
Created August 28, 2012 15:26
Show Gist options
  • Save dechamps/3499096 to your computer and use it in GitHub Desktop.
Save dechamps/3499096 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static const size_t FILE_SIZE = 512;
int main(int argc, char** argv)
{
const char* name = (argc >= 1) ? argv[0] : "msync";
if (argc < 2)
{
fprintf(stderr, "usage: %s <file>\n", name);
return EXIT_FAILURE;
}
const char* path = argv[1];
int fd = open(path, O_RDWR | O_CREAT, 0777);
if (fd == -1)
{
perror(name);
return EXIT_FAILURE;
}
void* mapping = mmap(NULL, FILE_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
if (mapping == MAP_FAILED)
{
perror(name);
return EXIT_FAILURE;
}
for (size_t pos = 0; pos < FILE_SIZE; ++pos)
((uint8_t*)mapping)[pos] = 0xff;
if (msync(mapping, FILE_SIZE, MS_SYNC) != 0)
{
perror(name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment