-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_dest.c
147 lines (110 loc) · 3.39 KB
/
fs_dest.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
145
146
147
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define true 1
#define MAX_PATH_LENGTH 50
#define DELAY 1000
char * pathname;
int file_num;
int rows;
void * thread_func(void * arg)
{
int thread_num = (int)arg;
char tPath[MAX_PATH_LENGTH];
snprintf(tPath, MAX_PATH_LENGTH, "%sThread%d", pathname, thread_num);
char cPath[MAX_PATH_LENGTH];
strncpy(cPath, tPath, MAX_PATH_LENGTH);
int counter = 1;
while(true)
{
int fd = open(cPath, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd == -1)
{
char errMsg[] = "Could not open a file!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
for (int i = 0; i < rows; i++)
{
char buff[50];
time_t t = time(NULL);
char * temp = ctime(&t); //ctime function may add newline at the end of this returned string
snprintf(buff, sizeof (buff), "The Linux programming interface %s", temp);
if (write(fd, buff, strlen(buff)) == -1)
{
char errMsg[] = "Could not write to a file!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
usleep(DELAY);
}
if (close (fd))
{
char errMsg[] = "Could not close a file!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
snprintf(tPath, MAX_PATH_LENGTH, "%sThread%d.%d", pathname, thread_num, counter);
if (!access(tPath, F_OK))
{
if (unlink(tPath))
{
char errMsg[] = "Could not unlink a file!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
}
if (rename(cPath, tPath))
{
char errMsg[] = "Could not rename the file!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
counter = (counter + 1) % file_num;
}
return (void *)NULL;
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
char errMsg[] = "Missing command line arguments!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
else if (argc == 2)
{
if (strcmp(argv[1], "--help") == 0)
{
char helpMsg[] = "[pathname][thread number][file number][row number]";
write(STDOUT_FILENO, helpMsg, strlen(helpMsg));
_exit(EXIT_SUCCESS);
}
char errMsg[] = "Missing command line arguments!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
int threadNum = atoi(argv[2]);
file_num = atoi(argv[3]);
pathname = argv[1];
rows = atoi(argv[4]);
// pthread_attr_t attr;
// pthread_attr_init(&attr);
// pthread_attr_setdetachstate(&attr, 1);
pthread_t tID;
for (int i = 0; i < threadNum - 1; i++)
{
if (pthread_create((void *)&tID, (void *)NULL, thread_func, (void *)i))
{
char errMsg[] = "Could not create a new thread!";
write(STDERR_FILENO, errMsg, strlen(errMsg));
_exit(EXIT_FAILURE);
}
}
thread_func((void *)threadNum - 1);
return 0;
}