-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
220 lines (173 loc) · 4.57 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#define MAX_THREADS 12
#define MSG_SIZE 256
#define MAX_MSG 128
#define CLEAR_SCREEN printf("\e[1;1H\e[2J")
#define SHIFT_OUT printf("==========================================================\n")
uint32_t action_counter[MAX_THREADS];
uint32_t rx_counter = 0;
typedef struct node {
char message[MSG_SIZE];
struct node *next;
int pos;
int val;
} node;
static node *head = NULL;
static pthread_t threads_pool[MAX_THREADS];
static pthread_t sender_thread;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t sender_cond, reciever_cond;
void *sender(void *arg);
void *reciever(void *arg);
static void sigusr1_handler(int signum);
int list_init(void);
int pop(char *buf);
int push(char *data);
int main(int argc, char **argv)
{
if(signal (SIGUSR1, sigusr1_handler) == SIG_ERR)
{
perror("inpossible handle signal!\n");
exit(EXIT_FAILURE);
}
int status = 0;
if(list_init() == -1)
{
perror("cannot allocate memory");
exit(EXIT_FAILURE);
} else printf("list init ok!\n");
pthread_cond_init(&sender_cond, 0);
pthread_cond_init(&reciever_cond, 0);
//create reciver threads
for(int i = 0; i < MAX_THREADS; ++i)
{
status = pthread_create(&threads_pool[i], NULL, reciever, &i);
if(status != 0)
{
printf("pthread_create reciever return error %d\n", status);
exit(-1);
}
}
//create sender
status = pthread_create(&sender_thread, NULL, sender, 0);
if(status != 0)
{
printf("pthread_create sender return error %d\n", status);
exit(-1);
}
for(int i = 0; i < MAX_THREADS; i++)
{
pthread_join(threads_pool[i], NULL);
}
pthread_join(sender_thread, NULL);
pthread_cond_destroy(&reciever_cond);
pthread_cond_destroy(&sender_cond);
pthread_mutex_destroy(&mutex);
exit(EXIT_SUCCESS);
}
void *reciever(void *arg)
{
int thread_number = *(int*) arg;
char t_buf[MSG_SIZE];
printf(": start reciever thread %d\n", thread_number);
for(;;)
{
pthread_mutex_lock(&mutex);
//if queue is empty - waiting
int x = pop(t_buf);
if((x == 1) || x == -1)
{
//perror("reciever wait - list is empty");
pthread_cond_wait(&reciever_cond, &mutex);
} else
{
//perror("reciever getting data");
printf("# thread %d %s", thread_number, t_buf);
action_counter[thread_number]++;
rx_counter++;
}
pthread_cond_signal(&sender_cond);
pthread_mutex_unlock(&mutex);
}
}
void *sender(void *arg)
{
char t_buff[MSG_SIZE];
time_t t;
uint32_t tx_counter = 0;
list_init();
printf(": start sender thread\n");
for(;;)
{
//get random number for delay
int delay = 1000 + (rand() % 1000000);
usleep(delay);
time(&t);
pthread_mutex_lock(&mutex);
snprintf(t_buff, MSG_SIZE, "%d message at %s", tx_counter, ctime(&t));
if(push(t_buff) == -1)
{
//if no more place for message - waiting
perror("no more place for message - waiting");
pthread_cond_wait(&sender_cond, &mutex);
}
pthread_cond_signal(&reciever_cond);
pthread_mutex_unlock(&mutex);
tx_counter++;
usleep(100000);
}
}
static void sigusr1_handler(int signum)
{
CLEAR_SCREEN;
printf("stat: for all threads recived messages = %d \n", rx_counter);
for(int i = 0; i < MAX_THREADS; i++)
{
printf("stat: for thread %d recived message = %d \n", i, action_counter[i]);
}
SHIFT_OUT;
}
int pop(char *buf)
{
if(head->next == NULL)
return 1;
node *prev = (node*) malloc(sizeof(node));
if(head == NULL || prev == NULL)
return -1;
prev = head;
strncpy(buf, prev->message, MSG_SIZE);
head = head->next;
free(prev);
return 0;
}
int push(char *data)
{
node *temp = (node*) malloc(sizeof(node));
if(temp == NULL)
return -1;
strncpy(temp->message, data, MSG_SIZE);
temp->pos = head->pos;
temp->next = head->next;
head->pos = head->pos + 1;
head->next = temp;
return 0;
}
int list_init(void)
{
head = (node*) malloc(sizeof(node));
if(head == NULL)
return -1;
head->pos = 0;
head->next = NULL;
return 0;
}
//end