-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj02.c
140 lines (105 loc) · 2.88 KB
/
proj02.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
/* POS project 2: simple ticket algorithm
* Author: [email protected]
* Description:
* The appliaction creates a given number of threads which then
* are synchronized using a simple ticket algorithm using
* posix mutexes and conditions.
*
*/
#define _POSIX_C_SOURCE 199506L
#define _XOPEN_SOURCE
#define _XOPEN_SOURCE_EXTENDED 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int current = 0;
int tCount = 0;
int passCounter = 0;
/* random sleep function */
void my_sleep(int id) {
struct timespec ts;
unsigned int seed = getpid() * (id + 1); /* id can be 0! */
ts.tv_nsec = (int)(rand_r(&seed) / ((double)RAND_MAX + 1) * 500000000);
ts.tv_sec = 0;
nanosleep(&ts,NULL);
}
/* a function used to assign a ticket */
int getticket () {
int ticketNum = 0;
pthread_mutex_lock(&mutex);
ticketNum = tCount++;
pthread_mutex_unlock(&mutex);
return ticketNum;
}
/* a function used to enter the critical section */
void await(int aenter) {
pthread_mutex_lock(&mutex);
while (current != aenter)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
return;
}
/* function used to leave the critical section */
void advance () {
pthread_mutex_lock(&mutex);
/* increment the ticket counter */
current++;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
return;
}
/* thread function
* Prints its id and ticket number
*/
void *thread_foo(void *id) {
int myTicket;
while((myTicket = getticket()) < passCounter) {
my_sleep((int)id);
await(myTicket);
/*Crit Section */
printf("%d\t(%d)\n", myTicket, (int)id);
/*Crit Section end*/
advance();
my_sleep((int)id);
}
return (void *) 0;
}
int main(int argc, char* argv[]) {
pthread_t *threadArr;
pthread_attr_t attr;
int threads = 0;
int i = 0;
int retval;
/* arguments */
threads = atoi(argv[1]);
passCounter = atoi(argv[2]);
/* mutex init */
pthread_mutex_init(&mutex, NULL);
/* attr init */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
/* cond init */
pthread_cond_init(&cond, NULL);
threadArr = (pthread_t *) malloc(sizeof(pthread_t) * threads);
/* Create threads */
for (i = 0; i<threads; i++) {
if ((retval = pthread_create(&threadArr[i], &attr, thread_foo, (void *) i)) != 0 ) {
printf("Cannot create the set number of threads. Exiting.\n");
return 1;
}
}
/* Join threads */
for (i = 0; i<threads; i++)
retval = pthread_join(threadArr[i], NULL);
/* Cleanup */
free(threadArr);
pthread_mutex_destroy(&mutex);
pthread_attr_destroy(&attr);
pthread_cond_destroy(&cond);
return 0;
}