-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrentCounter.c
78 lines (66 loc) · 1.82 KB
/
concurrentCounter.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
/****
*@author: Samuel Bunyan, Faith Kilonzi(testing)
*File: concurrentCounter.c
***Project 4***Operating Systems****
*Brief: This program implements a concurrent Counter
*such that safe multithreading is enabled.
***/
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
//define the counter and the locks
typedef struct __counter_t {
int value;
pthread_mutex_t lock;
} counter_t;
//initialize the counter with lock
void init(counter_t *c) {
c->value = 0;
pthread_mutex_init(&c->lock, NULL);
}
//increment the counter with the locks done
void increment(counter_t *c) {
pthread_mutex_lock(&c->lock);
c->value++;
pthread_mutex_unlock(&c->lock);
}
//decrement the counter with the locks done
void decrement(counter_t *c) {
pthread_mutex_lock(&c->lock);
c->value--;
pthread_mutex_unlock(&c->lock);
}
//get the value of the counter while protecting the critical section
int get(counter_t *c) {
pthread_mutex_lock(&c->lock);
int rc = c->value;
pthread_mutex_unlock(&c->lock);
return rc;
}
/**
*Testing the counter implementation with multithreads
**/
counter_t myCounter;
void *executable(void *args){
int i;
for (i = 0; i < 10000000; i++) {
increment(&myCounter);
}
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t t1, t2;
printf("#Counter with locks :\n");
init(&myCounter);
printf("START (counter = %d)\n", get(&myCounter));
pthread_create(&t1, NULL, executable, &myCounter);
pthread_create(&t2, NULL, executable, &myCounter);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("Counter for 2 threads Done(counter = %d)\n", get(&myCounter));
return 0;
}