-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstats_file_write_with_o_sync.c
68 lines (57 loc) · 1.39 KB
/
stats_file_write_with_o_sync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Author: NagaChaitanya Vellanki
*
* program to demonstrate the effect of O_SYNC on file write with
* varying buffer size
*
* Use run_stats_file_write_with_buf_sizes_o_sync.sh to run this program
* with varying buffer size
*/
#define NUM_BYTES 500000
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
void write_data(int buf_size) {
FILE *file;
int fd;
char *buf;
int i = 0;
fd = open("bar.txt", O_CREAT | O_WRONLY | O_SYNC | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
if(fd != -1) {
file = fdopen(fd, "w");
if(file != NULL) {
buf = (char *)malloc(sizeof(char) * buf_size);
if(setvbuf(file, buf, _IOFBF, buf_size) != 0) {
perror("Error on setvbuf,");
exit(EXIT_FAILURE);
}
for(i = 0; i <= NUM_BYTES; i++) {
fputc('0', file);
}
free(buf);
fclose(file);
} else {
perror("Error on fdopen,");
}
} else {
perror("Error on open,");
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
int buf_size;
if(argc < 2 || strcmp(argv[1], "--help") == 0) {
printf("Usage: %s buf_size\n", argv[0]);
exit(EXIT_FAILURE);
}
buf_size = atoi(argv[1]);
if(buf_size > 0) {
write_data(buf_size);
} else {
printf("buf_size should be greater than zero\n");
}
exit(EXIT_SUCCESS);
}