-
Notifications
You must be signed in to change notification settings - Fork 0
/
dretve.c
84 lines (74 loc) · 1.53 KB
/
dretve.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <pthread.h>
int n; // brojDretvi
int m; // brojIteracija
int a = 0;
bool verbose = false;
void *dretva()
{
for (int i = 1; i <= m; i++)
{
a++;
}
return NULL;
}
void *dretva_verbose(void *rbr)
{
int *d = rbr;
for (int i = 1; i <= m; i++)
{
a++;
printf("Dretva %2d: iteracija %2d\n", *d, i);
}
return NULL;
}
int main(int argc, char *argv[])
{
int *BR;
pthread_t *t;
if (argc < 3)
{
fprintf(stderr, "Koristenje: %s brojDretvi brojIteracija [-v]\n", argv[0]);
exit(1);
}
n = atoi(argv[1]);
m = atoi(argv[2]);
if (argc == 4 && strcmp(argv[3], "-v") == 0)
verbose = true;
BR = malloc(n * sizeof(int));
t = malloc(n * sizeof(pthread_t));
if (verbose)
{
for (int i = 0; i < n; i++)
{
BR[i] = i;
if (pthread_create(&t[i], NULL, dretva_verbose, &BR[i]))
{
fprintf(stderr, "Nesto je poslo po zlu\n");
exit(1);
}
}
}
else
{
for (int i = 0; i < n; i++)
{
if (pthread_create(&t[i], NULL, dretva, NULL))
{
fprintf(stderr, "Nesto je poslo po zlu\n");
exit(1);
}
}
}
for (int j = 0; j < n; j++)
{
pthread_join(t[j], NULL);
}
printf("Glavni: a = %d\n", a);
return 0;
}