/
mtsort.c
204 lines (164 loc) · 4.77 KB
/
mtsort.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
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
/******************************************************************************
** CS416 - Operating Systems Theory **
** Test Program for HW0 (Spring 2015) **
** Prepared By: William Katsak <wkatsak@cs.rutgers.edu> **
** Code Written By: Guilherme Cox <cox@computer.org> **
******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h> // printf(..)
#include <signal.h> // signal(..)
#include <stdlib.h> // atoi(..)
#include "mypthread.h"
#include <unistd.h> // usleep(..)
#include <sys/ucontext.h>
////////////////////////////////////////////////////////////////////////////////
// Defines
// Default list size (in terms of number of elements)
#define LISTSIZE (32)
struct pthrarg {
int *num;
int size;
mypthread_mutex_t *mtx;
};
static int quitting = 0;
////////////////////////////////////////////////////////////////////////////////
// Function prototypes
void * fnsort(void *arg);
void * fncheck(void *arg);
void printList(int *p, int size);
////////////////////////////////////////////////////////////////////////////////
void *fnsort(void *arg) {
struct pthrarg *pargs;
int *num, swap;
mypthread_mutex_t *mtx0, *mtx1;
pargs = (struct pthrarg *) arg;
num = pargs->num;
mtx0 = pargs->mtx;
mtx1 = pargs->mtx + 1;
while (!quitting) {
mypthread_mutex_lock(mtx0);
if (mypthread_mutex_trylock(mtx1) != 0) {
mypthread_mutex_unlock(mtx0);
mypthread_yield();
continue;
}
if (num[1] < num[0]) {
swap = num[0];
num[0] = num[1];
num[1] = swap;
}
mypthread_mutex_unlock(mtx0);
mypthread_mutex_unlock(mtx1);
mypthread_yield();
}
mypthread_exit(0);
// I will never get here
return 0;
}
void * fncheck(void *arg) {
struct pthrarg *pargs;
int i, j = 0, size, check;
mypthread_mutex_t *mtx;
pargs = (struct pthrarg *) arg;
mtx = pargs->mtx;
size = pargs->size;
while (!quitting) {
printf(".");
if ((j + 1) % 80 == 0)
printf("\n");
//lock all threads
for (i = 0; i < size; i++)
mypthread_mutex_lock(mtx + i);
check = 1;
for (i = 0; i < size - 1 && check; i++) {
if (pargs->num[i] > pargs->num[i + 1])
check = 0;
}
if (check)
printf("\nQuitting...\n");
quitting = check;
//unlock all threads
for (i = 0; i < size; i++)
mypthread_mutex_unlock(mtx + i);
// j seconds
j = j + 1;
#ifndef MYTHREAD
//sleep( j );
#endif
mypthread_yield();
}
mypthread_exit(0);
return 0;
}
void printList(int *p, int size) {
int i;
for (i = 0; i < size; i++) {
printf("%4d ", p[i]);
if (((i + 1) % 10) == 0)
printf("\n");
}
printf("\n");
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("usage: %s mypthread_policy(0|1|2|3) LISTSIZE\n", argv[0]);
exit(0);
}
mypthread_init(atoi(argv[1]));
int i, *pList = 0, nListSize = LISTSIZE;
mypthread_t *threads, thrcheck;
mypthread_mutex_t *mutexes;
struct pthrarg *pthrargs, pthrargcheck;
if (argc == 3)
nListSize = atoi(argv[2]);
nListSize = nListSize > 0 ? nListSize : LISTSIZE;
// Creating the List of numbers
printf("Number of elements: %d\n", nListSize);
pList = (int *) malloc(sizeof(int) * nListSize);
for (i = 0; i < nListSize; i++)
// pList[i] = random( ) % (nListSize<<1); // random list
pList[i] = nListSize - i; // decreasing list (easier to debug)
printf("[BEFORE] The list is NOT sorted:\n");
printList(pList, nListSize);
threads = (mypthread_t *) malloc(sizeof(mypthread_t) * (nListSize - 1));
mutexes = (mypthread_mutex_t *) malloc(
sizeof(mypthread_mutex_t) * nListSize);
pthrargs = (struct pthrarg *) malloc(
sizeof(struct pthrarg) * (nListSize - 1));
mypthread_mutex_init(&mutexes[0], 0);
for (i = 0; i < nListSize - 1; i++) {
mypthread_mutex_init(&mutexes[i + 1], 0);
pthrargs[i].num = &pList[i];
pthrargs[i].mtx = &mutexes[i];
pthrargs[i].size = nListSize;
if (mypthread_create(&threads[i], 0, &fnsort, &pthrargs[i]) != 0) {
printf("[FATAL] Could not create thread: %d\n", i);
exit(1);
}
}
pthrargcheck.num = pList;
pthrargcheck.mtx = mutexes;
pthrargcheck.size = nListSize;
if (mypthread_create(&thrcheck, 0, &fncheck, &pthrargcheck) != 0) {
printf("[FATAL] Could not create thread: fncheck\n");
exit(1);
}
///////////
// Waiting the threads to complete the sorting
//////////
printf("waiting...\n");
for (i = 0; i < nListSize - 1; i++)
mypthread_join(threads[i], 0);
mypthread_join(thrcheck, 0);
for (i = 0; i < nListSize; i++)
mypthread_mutex_destroy(&mutexes[i]);
printf("[AFTER] The list is sorted:\n");
printList(pList, nListSize);
// Cleaning
free(pthrargs);
free(mutexes);
free(threads);
free(pList);
return 0;
}
// eof