-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask3.c
143 lines (109 loc) · 4.08 KB
/
task3.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
/*
I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana
This programs uses multiple threads to read data and process data from files.
The main function will start three threads which all run the "cs392_thread_run" function. We have implemented the main function to start the threads, wait for the threads, and compare the results.
You need to implement the "cs392_thread_run" function. The argument to this function is a path to a file.
* You need to open the file and process each line as follows:
** If the line is “+item1”, please increase the global variable “item1_counter” by 1. If the line is “-item1”, please decrease “item1_counter” by 1.
** If the line is “+item2”, please increase “item2_counter” by 1. If the line is “-item2”, please decrease “item2_counter” by 1.
** If the line is “+item3”, please increase “item3_counter” by 1. If the line is “-item3”, please decrease “item3_counter” by 1.
** Pay attention, you may read the "\n" character when you read a line. If so, you need to ignore that byte when you do string comparison.
* When you change any of the above three global variables, you need to use the mutex called "mlock" (it has been defined and initialized)
* You will need to use "pthread_exit" to terminage the execution of "cs392_thread_run".
*/
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
int item1_counter = 0;
int item2_counter = 0;
int item3_counter = 0;
pthread_t tid[3];
int counter;
pthread_mutex_t mlock;
void * cs392_thread_run(void* filepath){
/* Write down your code here */
//open up file to be read
FILE* fp;
int size;
fp=fopen(filepath, "r+");
//ERROR CHECK
if(fp==NULL){
printf("Error: Cannot read file");
pthread_exit;
exit(1);
}
char str[7];
//fscanf(fp, "%s",str);
//pthread_mutex_lock(&mlock);
//locked and unlocked within each if statement bc it's safer means of editing global variable
while(fscanf(fp,"%s",str)==1){
//add values
if(strcmp(str,"+item1")==0){
pthread_mutex_lock(&mlock);
item1_counter++;
pthread_mutex_unlock(&mlock);
}
if(strcmp(str,"+item2")==0){
pthread_mutex_lock(&mlock);
item2_counter++;
pthread_mutex_unlock(&mlock);
}
if(strcmp(str,"+item3")==0){
pthread_mutex_lock(&mlock);
item3_counter++;
pthread_mutex_unlock(&mlock);
}
//subtract values
if(strcmp(str,"-item1")==0){
pthread_mutex_lock(&mlock);
item1_counter--;
pthread_mutex_unlock(&mlock);
}
if(strcmp(str,"-item2")==0){
pthread_mutex_lock(&mlock);
item2_counter--;
pthread_mutex_unlock(&mlock);
}
if(strcmp(str,"-item3")==0){
pthread_mutex_lock(&mlock);
item3_counter--;
pthread_mutex_unlock(&mlock);
}
}
//pthread_mutex_unlock(&mlock);
fclose(fp);
//exit
pthread_exit;
}
int main(int argc, char **argv){
int i = 0;
int err1, err2, err3;
if (pthread_mutex_init(&mlock, NULL) != 0){
printf("Cannot init mutex lock\n");
return 1;
}
err1 = pthread_create(&(tid[0]), NULL, cs392_thread_run, "./item_file1.txt");
err2 = pthread_create(&(tid[1]), NULL, cs392_thread_run, "./item_file2.txt");
err3 = pthread_create(&(tid[2]), NULL, cs392_thread_run, "./item_file3.txt");
if (err1 || err2 || err3)
printf("Cannot creat new threads\n");
for(i = 0; i < 3; i++)
pthread_join(tid[i], NULL);
pthread_mutex_destroy(&mlock);
printf("========= Results of test cases for task 3 ========= \n");
if(item1_counter == 10055)
printf(" ========= Congrats! You passed test case 1\n");
else
printf(" ========= Sorry! You failed test case 1. Expected results %d; Your result %d\n", 10055, item1_counter);
if(item2_counter == 4884)
printf(" ========= Congrats! You passed test case 2\n");
else
printf(" ========= Sorry! You failed test case 2. Expected results %d; Your result %d\n", 4884, item2_counter);
if(item3_counter == 4995)
printf(" ========= Congrats! You passed test case 3\n");
else
printf(" ========= Sorry! You failed test case 3. Expected results %d; Your result %d\n", 4995, item3_counter);
return 0;
}