summaryrefslogtreecommitdiffstats
path: root/c_pgms/threaded_io/thread_fops.c
blob: 80947ae645b8e4984c07b15e42ad064e9ca18a7a (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

#include "thread_read_fstat.h"

main()
{
        int ret = -1;
        pthread_t thread1, thread2, thread3, thread4, thread5, thread6, thread7;
        char *message1 = "Thread 1";
        char *message2 = "Thread 2";
        char *message3 = "Thread 3";
        char *message4 = "Thread 4";
        char *message5 = "Thread 5";
        char *message6 = "Thread 6";
        char *message7 = "Thread 7";

        int  iret1, iret2, iter3, iter4, iter5, iter6, iter7;

        open_t *file = NULL;
        file = (open_t *)calloc(1,sizeof(*file));
        if (!file) {
                fprintf (stderr, "%s:out of memory\n",
                         strerror(errno));
                goto out;
        }

        file->filename = "thread_file";
        file->flags = O_CREAT | O_RDWR;
        file->mode = 0755;

        fstat_t *inode;
        inode = (fstat_t *)calloc(1,sizeof(*inode));
        if (!inode) {
                fprintf (stderr, "%s:out of memory\n",
                         strerror(errno));
                goto out;
        }

        inode->buf = (struct stat *)calloc(1,sizeof(struct stat));
        if (!inode->buf) {
                fprintf (stderr, "%s:Out of memory\n",
                         strerror(errno));
                goto out;
        }

        int fd_main = -1;

        oft *both;
        both = (oft *)calloc(1,sizeof(*both));
        if (!both) {
                fprintf (stderr, "%s:out of memory\n",
                         strerror(errno));
                goto out;
        }
        /* Create independent threads each of which will execute function */

        both->open = file;
        both->fstat = inode;
        iret1 = pthread_create (&thread1, NULL, (void *)open_thread, (void *) both);
        iret2 = pthread_create (&thread2, NULL, (void *)fstat_thread, (void *) both);
        iter3 = pthread_create (&thread3, NULL, (void *)read_thread, (void *)both);
        iter4 = pthread_create (&thread4, NULL, (void *)write_truncate_thread, (void *)both);
        iter5 = pthread_create (&thread5, NULL, (void *)chown_thread, (void *)both);
        iter6 = pthread_create (&thread6, NULL, (void *)write_truncate_thread, (void *)both);
        iter7 = pthread_create (&thread7, NULL, (void *)open_lock_close, (void *)both);

        /* Wait till threads are complete before main continues. Unless we  */
        /* wait we run the risk of executing an exit which will terminate   */
        /* the process and all threads before the threads have completed.   */

        pthread_join( thread1, NULL);
        pthread_join( thread2, NULL);
        pthread_join( thread3, NULL);
        pthread_join( thread4, NULL);
        pthread_join( thread5, NULL);
        pthread_join( thread6, NULL);
        pthread_join( thread7, NULL);

        printf ("%d\n", iret1);
        printf ("%d\n", iret2);
        printf ("%d\n", iter3);
        printf ("%d\n", iter4);
        printf ("%d\n", iter5);
        printf ("%d\n", iter6);
        printf ("%d\n", iter7);

        ret = 0;
out:
        if (both)
                free(both);
        if (inode->buf)
                free (inode->buf);
        if (inode)
                free (inode);
        if (file)
                free (file);

        return ret;
}

void
open_lock_close (void *tmp)
{
        oft *all = (oft *)tmp;
        open_t *file = NULL;
        file = all->open;
        fstat_t *inode = NULL;
        inode = all->fstat;
        int ret = 0;
        int fd = -1;
        struct flock lock;
        char *data = "This is a line";

        while (1) {
                fd = open (file->filename, file->flags, file->mode);
                if (fd == -1) {
                        fprintf (stderr, "%s=>Error: cannot open the file %s "
                                 "(%s)\n", __FUNCTION__, file->filename,
                                 strerror (errno));
                        return;
                }

                lock.l_type = F_RDLCK;
                lock.l_whence = SEEK_SET;
                lock.l_start = 0;
                lock.l_len = 0;
                lock.l_pid = 0;

                ret = fcntl (fd, F_SETLK, &lock);
                if (ret == -1)
                        fprintf (stderr, "Error: cannot lock the file %s (%s)\n",
                                 file->filename, strerror (errno));

                ret = flock (fd, LOCK_SH);
                if (ret == -1)
                        fprintf (stderr, "Error: cannot flock the file %s (%s)\n",
                                 file->filename, strerror (errno));

                ret = write (fd, data, strlen (data));
                if (ret == -1)
                        fprintf (stderr, "Error: cannot write the file %s (%s)\n",
                                 file->filename, strerror (errno));

                lock.l_type = F_UNLCK;
                ret = fcntl (fd, F_SETLK, &lock);
                if (ret == -1)
                        fprintf (stderr, "Error: cannot unlock the file %s"
                                " (%s)\n", file->filename, strerror (errno));

                ret = flock (fd, LOCK_UN);
                if (ret == -1)
                        fprintf (stderr, "Error: cannot unlock the flock on "
                                 "the file %s (%s)\n", file->filename,
                                 strerror (errno));

                close (fd);
        }

        return;
}

int
open_thread(void *tmp)
{
        oft *all = (oft *)tmp;
        open_t *file = NULL;
        file = all->open;
        fstat_t *inode = NULL;
        inode = all->fstat;
        int ret = 0;
        int fd = -1;

        while (1) {
                if (fd = open (file->filename, file->flags, file->mode) == -1) {
                        fprintf(stderr, "%s:open error (%s)\n", __FUNCTION__,
                                strerror(errno));
                        ret = -1;
                        goto out;
                }

                close (fd);
        }
out:
        if (file)
                free(file);
        return ret;
}

int
fstat_thread(void *ptr)
{
        oft *all = (oft *)ptr;
        fstat_t *inode = NULL;
        open_t *file = NULL;
        int ret = 0;
        int fd = -1;

        file = all->open;
        inode = all->fstat;

        fd = open (file->filename, file->flags, file->mode);
        if (fd == -1) {
                fprintf (stderr, "%s: open error (%s)\n", __FUNCTION__,
                         strerror (errno));
                ret = -1;
                goto out;
        }

        while (1) {
                if (fstat(fd, inode->buf) == -1) {
                        fprintf (stderr, "%s:fstat error\n",
                                 strerror(errno));
                        ret = -1;
                        goto out;
                }
        }

out:

        close (fd);
        if (inode->buf)
                free (inode->buf);
        if (inode)
                free(inode);
        return ret;
}

int
read_thread (void *ptr)
{
        oft *all = NULL;
        int fd = -1;
        int ret = -1;
        fstat_t *stat = NULL;
        open_t *file = NULL;
        char buffer[4096];

        all = (oft *)ptr;
        stat = all->fstat;
        file = all->open;

        open_validate_error_goto(file->filename, file->flags, file->mode);

        while (1) {
                ret = read (fd, buffer, 22);
                if (ret == -1) {
                        fprintf (stderr, "%s: read error\n", strerror (errno));
                        goto out;
                }

                if (ret == EOF) {
                        lseek (fd, 0, SEEK_SET);
                }
        }

        ret = 0;
out:
        close (fd);
        return ret;
}

int
write_truncate_thread (void *ptr)
{
        oft *all = NULL;
        open_t *file = NULL;
        fstat_t *stat = NULL;
        int fd = -1;
        int ret = -1;
        char *buffer = "This is a multithreaded environment";
        unsigned int data = 0;
        int bytes_written = -1;

        all = (oft *)ptr;
        file = all->open;
        stat = all->fstat;

        fd = open (file->filename, file->flags | O_APPEND, file->mode);
        if (fd == -1) {
                fprintf (stderr, "%s: open error (%s)\n", __FUNCTION__,
                         strerror (errno));
                ret = -1;
                goto out;
        }

        while (1) {
                ret = write (fd, buffer, strlen (buffer));
                bytes_written = ret;
                if (ret == -1) {
                        fprintf (stderr, "%s: write error\n", strerror (errno));
                        goto out;
                }

                if ((data + bytes_written) >= 4096) {
                        ret = ftruncate (fd, 0);
                        if (ret == -1) {
                                fprintf (stderr, "%s: truncate error\n",
                                         strerror (errno));
                                goto out;
                        }

                        data = 0;
                } else
                        data = data + bytes_written;
        }

out:
        close (fd);
        return ret;
}

int
chown_thread (void *ptr)
{
        oft *all = NULL;
        fstat_t *stat = NULL;
        open_t *file = NULL;
        int ret = -1;
        int fd = -1;
        struct stat stat_buf;

        all = (oft *)ptr;
        stat = all->fstat;
        file = all->open;

        open_validate_error_goto(file->filename, file->flags, file->mode);

        while (1) {
                ret = fstat (fd, &stat_buf);
                if (ret == -1) {
                        fprintf (stderr, "%s: fstat error.(%s)",
                                 strerror (errno), __FUNCTION__);
                        goto out;
                }

                if (stat_buf.st_uid == 1315 && stat_buf.st_gid == 1315) 
                        ret = fchown (fd, 2222, 2222);
                else
                        ret = fchown (fd, 1315, 1315);

                if (ret == -1) {
                        fprintf (stderr, "%s: chown error\n", strerror (errno));
                        goto out;
                }
        }

        ret = 0;
out:
        close (fd);
        return ret;
}