-
Notifications
You must be signed in to change notification settings - Fork 2
/
zad3.c
188 lines (161 loc) · 4.74 KB
/
zad3.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
/* Rozwiazanie zadania 3 z tutoriala.
*
*/
#include <stdlib.h>
#include <stddef.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#define ERR(source) (perror(source),\
fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
exit(EXIT_FAILURE))
typedef unsigned int UINT;
typedef struct args {
pthread_t tid;
UINT seed;
pthread_mutex_t *mxChecked;
int Checked;
int* Counter;
pthread_mutex_t *mxCounter;
pthread_mutex_t *mxL;
int *L;
int* Quit;
pthread_mutex_t *mxQuit;
} args_t;
typedef struct argsSignalHandler {
pthread_t tid;
sigset_t *pMask;
int *Quit;
pthread_mutex_t *mxQuit;
} argsSignalHandler_t;
void* thread_work(void *voidPtr) {
args_t* args = voidPtr;
int M = rand_r(&args->seed) % 99 + 2;
while(1)
{
pthread_mutex_lock(args->mxChecked);
if(args->Checked == 0)
{
(*args->Counter)++;
args->Checked = 1;
}
else
{
pthread_mutex_unlock(args->mxChecked);
continue;
}
pthread_mutex_unlock(args->mxChecked);
pthread_mutex_lock(args->mxL);
if(*(args->L) % M == 0) printf("L[%d] jest podzielne przez M[%d]\n",*args->L,M);
pthread_mutex_unlock(args->mxL);
pthread_mutex_lock(args->mxQuit);
if(*(args->Quit) == 1)
{
pthread_mutex_unlock(args->mxQuit);
break;
}
pthread_mutex_unlock(args->mxQuit);
}
return NULL;
}
void* signal_handling(void* voidArgs) {
argsSignalHandler_t* args = voidArgs;
int signo;
for (;;) {
if(sigwait(args->pMask, &signo)) ERR("sigwait failed.");
switch (signo) {
case SIGINT:
pthread_mutex_lock(args->mxQuit);
*args->Quit = 1;
pthread_mutex_unlock(args->mxQuit);
return NULL;
default:
printf("unexpected signal %d\n", signo);
exit(1);
}
}
return NULL;
}
int main(int argc, char** argv) {
int n = atoi(argv[1]);
int L = 1;
int Counter = 0;
int Quit = 0;
sigset_t oldMask, newMask;
sigemptyset(&newMask);
sigaddset(&newMask, SIGINT);
if (pthread_sigmask(SIG_BLOCK, &newMask, &oldMask)) ERR("SIG_BLOCK error");
args_t* attr = (args_t*) malloc(n * sizeof(args_t));
argsSignalHandler_t handler_args;
pthread_mutex_t mxChecked = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mxL = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mxCounter = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mxQuit = PTHREAD_MUTEX_INITIALIZER;
handler_args.Quit=&Quit;
handler_args.mxQuit=&mxQuit;
handler_args.pMask = &newMask;
if(pthread_create(&handler_args.tid, NULL, signal_handling, &handler_args))ERR("Couldn't create signal handling thread!");
srand(time(NULL));
for(int i=0;i<n;i++)
{
attr[i].mxQuit = &mxQuit;
attr[i].Quit = &Quit;
attr[i].mxCounter = &mxCounter;
attr[i].Counter = &Counter;
attr[i].mxL = &mxL;
attr[i].Checked = 0;
attr[i].seed = rand();
attr[i].L = &L;
attr[i].mxChecked = &mxChecked;
}
for(int i=0;i<n;i++)
{
int err = pthread_create(&(attr[i].tid), NULL, thread_work,&attr[i]);
if (err != 0) ERR("Couldn't create thread");
}
struct timespec tk = {0,0.1 * 1000000000};
while(1)
{
pthread_mutex_lock(&mxChecked);
if(Counter != n)
{
pthread_mutex_unlock(&mxChecked);
continue;
}
else
{
Counter =0;
}
pthread_mutex_unlock(&mxChecked);
nanosleep(&tk,NULL);
pthread_mutex_lock(&mxL);
L++;
pthread_mutex_unlock(&mxL);
pthread_mutex_lock(&mxChecked);
for(int i=0;i<n;i++)
{
attr[i].Checked = 0;
}
pthread_mutex_unlock(&mxChecked);
pthread_mutex_lock(&mxQuit);
if(Quit == 1)
{
pthread_mutex_unlock(&mxQuit);
break;
}
pthread_mutex_unlock(&mxQuit);
}
if(pthread_join(handler_args.tid, NULL)) ERR("Can't join with 'signal handling' thread");
for (int i = 0; i < n; i++)
{
int err = pthread_join(attr[i].tid, NULL);
if (err != 0) ERR("Can't join with a thread");
}
free(attr);
if (pthread_sigmask(SIG_UNBLOCK, &newMask, &oldMask)) ERR("SIG_BLOCK error");
return EXIT_SUCCESS;
}