-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinux-Semaphores-and-Posix-Pthreads.cpp
118 lines (98 loc) · 2.41 KB
/
Linux-Semaphores-and-Posix-Pthreads.cpp
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
// Question 2: Divisible by 3/5 //
// 7 February 2020 //
// Author: Anna DeVries //
/* Libraries */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <vector>
#include <iostream>
/* Defs */
#define DEBUG true
#define PTHREADS 8
#define LARGEST 5000
/* Globals */
sem_t mutex;
pthread_t threads[PTHREADS];
int numbers[LARGEST];
int count = 0;
/* Struct */
struct arg_struct {
int start;
int end;
std::vector<int> frontier;
} args;
/* Utility Functions */
void *f1(void *arguments){
// Protect critical section
sem_wait(&mutex);
// Local Variables
struct arg_struct *args = (struct arg_struct *) arguments;
int i;
int start, end, value;
int INT_NUM = LARGEST / PTHREADS;
// For debuging only
if(DEBUG == true){
printf("Frontier:\n");
for(auto num : args -> frontier){
printf("%d ", num);
}
printf("\n");
}
// Pop off value from frontier
value = args -> frontier[0];
args -> frontier.erase(args -> frontier.begin());
// Initialize start and end points
start = value * INT_NUM;
end = start + INT_NUM;
// Check if divisible by 3 and/or 5
for(i = start; i < end; i++){
if( (i + 1) % 3 == 0 || (i + 1) % 5 == 0){
numbers[i] = 1;
count++;
}
}
// Unlock critical section
sem_post(&mutex);
}
/* Main */
int main(){
// Local Variables
int i;
// Initialize array to be false
for(i = 0; i < LARGEST; i++){
numbers[i] = 0;
}
// Initialize frontier of values
for(i = 0; i < PTHREADS; i++){
args.frontier.push_back(i);
}
// Initialize sem
sem_init(&mutex, 0, 1);
// Create pthread and perform ops
for(i = 0; i < PTHREADS; i++){
// Initialize variables
args.start = 0;
args.end = 0;
// Create pthreads
pthread_create(&threads[i], NULL, f1, (void *) &args);
}
// Join pthreads
for(i = 0; i < PTHREADS; i++){
pthread_join(threads[i], NULL);
}
// Print true values
printf("Numbers divisible by 3 or 5 between 1 and %d:\n", LARGEST);
for(i = 0; i < LARGEST; i++){
if(numbers[i] == 1){
printf("%d ", i + 1);
}
}
printf("\n\n");
printf("Count: %d\n\n", count);
// Clean up
sem_destroy(&mutex);
return 0;
}