-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrw.c
56 lines (47 loc) · 1.19 KB
/
rw.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
/*
* Reader and Writer's Problem for disk scheduler
* CSE 511 Test Program
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "rw_serial.h"
#define NUM_READERS 5
#define NUM_WRITERS 5
extern serial_t* gs;
int main(int argc, char *argv[])
{
// Initialize vars
int i;
int rc;
pthread_t reader[NUM_READERS];
pthread_t writer[NUM_WRITERS];
// Create serializer
create();
// Create the r/w requests (as threads) to the disk
for (i = 0; i < NUM_READERS; i++)
{
rc = pthread_create(&reader[i], NULL, read_func, (void *)i);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for (i = 0; i < NUM_WRITERS; i++)
{
rc = pthread_create(&writer[i], NULL, write_func, (void *)i);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
printf("Readers/Writers were created successfully!\n");
// Wait until all threads are done
for (i = 0; i < NUM_READERS; i++)
pthread_join (reader[i], NULL);
for (i = 0; i < NUM_WRITERS; i++)
pthread_join (writer[i], NULL);
printf("Reads/writes were done successfully!\n");
// Finish
return 0;
}