Skip to content

Instantly share code, notes, and snippets.

@keyurdg

keyurdg/pwrite.c Secret

Created April 7, 2014 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyurdg/961c19175b81c73fdaa3 to your computer and use it in GitHub Desktop.
Save keyurdg/961c19175b81c73fdaa3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#define BLOCK_SIZE 524288
int main(int argc, char **argv) {
int64_t s, off;
char *data;
int64_t fd, rc, i;
data = (char *) malloc(sizeof(char) * BLOCK_SIZE);
if (!data) {
rc = 2;
goto err;
}
memset(data, 'b', BLOCK_SIZE);
rc = 0;
for (i=0; i < 10000; i++) {
fd = open("/var/lib/mysql/plain_pwrite.werr", O_CREAT | O_RDWR);
if (fd < 0) {
rc = 1;
goto err;
}
off = lseek(fd, 0, SEEK_END);
s = pwrite(fd, data, BLOCK_SIZE, off);
printf("wrote %lld bytes, file offset %lld\n", s, off);
s = fsync(fd);
printf("fsync success %lld\n", s);
close(fd);
}
err:
close(fd);
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment