-
Notifications
You must be signed in to change notification settings - Fork 0
/
.goutputstream-HRFOFZ
213 lines (129 loc) · 4.23 KB
/
.goutputstream-HRFOFZ
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
#include <stdlib.h>
#include "message.h"
#include "vector.h"
#include <mqueue.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h> /* flags */
#include <sys/stat.h> /* modes */
#include <stdio.h>
//sudo sh -c 'echo 512 > /proc/sys/fs/mqueue/msg_max'
// gcc -g -Wall -pthread -o server server.c -lrt
/*
* cc -c first.c
that would produce an object file you may need to add to the library.
then create an executable using the following command
cc -o first first.c
* */
// gcc -g -Wall -pthread -o server server.c -lrt
//globals:
/* mutex and condition variables for the message copy */
pthread_mutex_t mutex_msg;
int msg_not_copied = 1; /* TRUE = 1 */
pthread_cond_t cond_msg;
//thread function
void process_message(struct request *msg){
printf("process_message() \n");
struct request *msg_local = malloc(sizeof( struct request)); /* local message */
mqd_t q_client; /* client queue */
int result = 1;
//*result = 1;
/* thread copies message to local message*/
pthread_mutex_lock(&mutex_msg);
printf("inside lock \n");
//memcpy((char *) &msg_local, (char *)&msg, sizeof( *msg) );
msg_local = msg;
/* wake up server */
msg_not_copied = 0; /* FALSE = 0 */
printf("msg_not_copied = 0;\n");
pthread_cond_signal(&cond_msg);
pthread_mutex_unlock(&mutex_msg);
printf("Alternate thread message: %s \n", msg_local->str);
q_client = mq_open(msg_local->str, O_WRONLY);
if (q_client == -1){
printf("%s \n", msg_local->str);
perror("Can’t open client queue");
}
else {
mq_send(q_client, (char *) &result, sizeof(result), 0);
printf("\n Response sent \n");
mq_close(q_client);
printf("client queue closed \n");
}
pthread_exit(0);
//return; ->** this gives "stack smashing error"
}
int main(void)
{
//vector test start
int i;
vector v;
vector_init(&v);
for (i = 0; i < 5; i++){
struct request *test = malloc(sizeof(*test));
test->key = i; test->flt = 1.1;
strcpy(test->str , "test");
vector_set(&v,i, (void*)test);
printf("added \n");
}
vector_add(&v, (void *) 2);
vector_add(&v, (void *) 3);
vector_add(&v, (void *)4);
//seg fault here. fix this then on track. can use this and explain how w pointer in docks before continuing or else will be a pain
for (i = 0; i < vector_total(&v); i++){
struct request * resp = (struct request *) vector_get(&v, i);
printf("%d ", resp->key );
printf("\n");
}
vector_free(&v);
//vector test end
mqd_t q_server; /* server queue */
struct request *msg = (struct request *) malloc(sizeof( *msg)); /* message to receive */
//msg->flt = (float * ) malloc(sizeof( float));
//msg->key = (int * ) malloc(sizeof( int));
struct mq_attr q_attr; /* queue atributes */
pthread_attr_t t_attr; /* thread atributes */
q_attr.mq_maxmsg = 20;
q_attr.mq_msgsize = sizeof( *msg);
pthread_t thid; //need reference to thread
//**if error:
//then sudo sh -c 'echo 512 > /proc/sys/fs/mqueue/msg_max'
q_server = mq_open("/server", O_RDONLY | O_CREAT, 0777 , &q_attr);
if (q_server == -1) {
//***
printf("server ");
perror("Can't create server queue");
return 1;
}
pthread_mutex_init(&mutex_msg, NULL);
pthread_cond_init(&cond_msg, NULL);
pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
pthread_attr_init(&t_attr);
//msg = malloc(sizeof(struct request));
while (1) {
printf("waiting for message \n");
ssize_t ret = mq_receive(q_server, (char *)msg, sizeof( *msg), 0);
printf("Recieved! \n");
printf("Main thread Message: %s \n", msg->str);
if(ret==-1){
printf("MESSAGE SIZE: %ld",sizeof(&msg));
perror("ERR:");
}
pthread_create(&thid, &t_attr, (void *)process_message, msg);
//pthread_join(thid,NULL);
//msg_not_cpied first init to true. set to 0 in process message(), after message is succesfully copied.
/* wait for thread to copy message */
pthread_mutex_lock(&mutex_msg);
//work out message not copied stuff they are all off
while (msg_not_copied==1){
printf("copying message \n");
pthread_cond_wait(&cond_msg, &mutex_msg);
printf("after con wait \n");
msg_not_copied = 0;
pthread_mutex_unlock(&mutex_msg);
printf("pthread_mutex_unlock(&mutex_msg); \n");
}
printf("**** WILL NOW WAIT FOR NEXT MESSAGE \n");
}
}//Main done
//** most current**** stuck on only being able to send 2 messages.