-
Notifications
You must be signed in to change notification settings - Fork 1
/
testAppend.c
148 lines (116 loc) · 2.51 KB
/
testAppend.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
char *filePath;
int numThreads = 5;
int PIPE_BUF = 4096;
struct thread_info *infoArr;
void *threadFunc(void *arg);
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Usage: %s file-path\n", argv[0]);
return 1;
}
filePath = argv[1];
pthread_attr_t attr;
int initReturn = pthread_attr_init(&attr);
if (initReturn != 0)
{
printf("Failed to init thread attr with error %\n", initReturn);
return 4;
}
pthread_t threads[numThreads];
for (int i = 0; i < numThreads; i++)
{
int threadStartVal = pthread_create(&threads[i], &attr, threadFunc, (void *) i);
if (threadStartVal != 0)
{
printf("failed to create thread %d with error %d\n", i, threadStartVal);
return 6;
}
}
int fileSize = 0;
for (int i = 0; i < numThreads; i++)
{
void * result;
int joinResult = pthread_join(threads[i], &result);
if (joinResult != 0)
{
printf("Joining thread %d failed with error code %d\n", i, joinResult);
}
else
{
printf("Thread %d wrote %d bytes\n", i, (int)result);
}
if ((int)result > 0)
{
fileSize += (int)result;
}
}
int readFd = open(filePath, O_RDONLY);
void *readBuffer = malloc(PIPE_BUF);
for (int i = 0; i < fileSize;)
{
int readReturn = read(readFd, readBuffer, PIPE_BUF);
if (readReturn <= 0)
{
printf("read failed");
return 7;
}
printf("read %d bytes from file offset %d\n", readReturn, i);
char threadChar = ((char *)readBuffer)[i];
for (int j = 1; j < readReturn; j++)
{
if (((char*)readBuffer)[i+j] != threadChar)
{
printf("found an inconsistency");
}
}
i += readReturn;
}
}
volatile int fd = 0;
void *threadFunc(void *arg)
{
int threadNumber = (int)arg;
void *buffer = malloc(PIPE_BUF);
if (!buffer)
{
printf("Couldn't allcate IO buffer\n");
return (void *)-2;
}
for (int i = 0; i < PIPE_BUF; i++)
{
*((char *)buffer + i) = ('0' + threadNumber);
}
while (fd == 0)
{
int openAttempt = open(filePath, O_WRONLY | O_CREAT | O_DSYNC | O_SYNC | O_APPEND);
if (openAttempt > 0)
{
fd = openAttempt;
}
}
if (fd <= 0)
{
printf("Couldn't open the specified path\n");
return (void *)-3;
}
int amountWritten = 0;
for (int i = 0; i < 5; i++)
{
int thisWrite = write(fd, buffer, PIPE_BUF);
printf("wrote %d bytes out of %d\n", amountWritten, PIPE_BUF);
if (thisWrite > 0)
{
amountWritten += thisWrite;
}
}
free(buffer);
return (void *)amountWritten;
}