-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixcalc.c
155 lines (140 loc) · 4.24 KB
/
matrixcalc.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <signal.h>
#define NUMBEROFROWCOLL 20
// def of prototypes
void *creator();
void *totalizer();
void takeInput();
void stpHandler(int);
typedef enum
{
FALSE,
TRUE
} boolean;
pthread_t th_creator, th_totalizer; // definition of thread
pthread_mutex_t mutex_main, mutex_creator, mutex_totalizer; // definition of mutexes
int n; // user's input for the number of matrixes
int matrix[NUMBEROFROWCOLL][NUMBEROFROWCOLL]; // global matrix
boolean flag = FALSE, inMain = FALSE; // for the control of while loop in main
int main(int argc, char const *argv[])
{
srand(time(NULL)); // for random values
signal(SIGTSTP, stpHandler); // def of signal
signal(SIGINT, stpHandler); // def of signal
//initialize the mutexes
pthread_mutex_init(&mutex_creator, NULL);
pthread_mutex_init(&mutex_totalizer, NULL);
pthread_mutex_init(&mutex_main, NULL);
pthread_mutex_lock(&mutex_totalizer);
pthread_mutex_lock(&mutex_creator);
// initialize the threads
if (pthread_create(&th_creator, NULL, creator, NULL) != 0)
{
perror("Error to create thread:");
return 1;
}
if (pthread_create(&th_creator, NULL, totalizer, NULL))
{
perror("Error to create thread:");
return 2;
}
//pthread_detach(th_creator);
//pthread_detach(th_totalizer);
while (1)
{
pthread_mutex_lock(&mutex_main);
if (flag == TRUE)
{
break;
}
inMain = TRUE;
takeInput();
inMain = FALSE;
pthread_mutex_unlock(&mutex_creator); // when user enters something, unlock mutex of creator
}
pthread_mutex_destroy(&mutex_creator);
pthread_mutex_destroy(&mutex_totalizer);
pthread_mutex_destroy(&mutex_main);
return 0;
}
void *creator()
{
while (1) // infinite loop
{
pthread_mutex_lock(&mutex_creator); // if user inputs something, run
// matrix creation
for (size_t i = 0; i < NUMBEROFROWCOLL; i++)
{
for (size_t j = 0; j < NUMBEROFROWCOLL; j++)
{
*(*(matrix + i) + j) = rand() % 10; // fill with random values
printf("%6d", *(*(matrix + i))); // print abowe value
}
printf("\n");
}
pthread_mutex_unlock(&mutex_totalizer); // unlock for senc.
}
}
void *totalizer()
{
while (1) // infinite loop
{
pthread_mutex_lock(&mutex_totalizer); // if user inputs something, run
int sum = 0;
// summation of matrix
for (size_t i = 0; i < NUMBEROFROWCOLL; i++)
{
for (size_t j = 0; j < NUMBEROFROWCOLL; j++)
{
sum += *(*(matrix + i) + j);
}
}
printf("\nSum of the matrix: %d\n", sum);
n--; // when a matrix is done, reduce # of matrix
if (n == 0) // if the last matrix is done, throw a signal for main
{
pthread_mutex_unlock(&mutex_main);
}
else
{
pthread_mutex_unlock(&mutex_creator);
}
}
}
void takeInput()
{
printf("\n\nEnter the number of matrix: ");
scanf("%d", &n);
if (n == 0 || n < 0)
{
exit(0);
}
}
void stpHandler(int dummy)
{
printf("\n\n ----Threads will result... \n\n----");
/*
Don't call pthread_exit() or pthread_cancel() from a signal handler!
It is not required to be async-signal-safe, see signal-safety.
In general, you should do as little as possible in a signal handler.
The common idiom is to just set a flag that is periodically checked
in your main loop like e.g.
*/
if (inMain == TRUE)
{
exit(0); // Program is already in the main func to scan a number or terminate itself.
}
else
{
/*
Thread kullanılan bir uygulamada main() fonksiyonundan return edilirse,
tüm thread'ler de sonlandırılır ve kullanılan tüm kaynaklar sisteme geri verilir.
--FSMVU | OPERARING SYSTEMS COURSE LAB MATERIAL--
*/
flag = TRUE;
pthread_mutex_unlock(&mutex_main);
}
}