-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
250 lines (226 loc) · 6.11 KB
/
Copy pathclient.c
File metadata and controls
250 lines (226 loc) · 6.11 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdio.h>
#include <sys/ipc.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/msg.h>
#include "message.h"
#include <time.h>
void checkUsableClient(int msqid);
void choiceClient(int msqid);
void *makeCar(void *ptr);
void *paintCar(void *ptr);
void *inspectCar(void *ptr);
void getComponents();
static sem_t sync_sem;
int components = 0;
int client;
typedef struct Car
{
bool isCreated;
bool isPainted;
bool isInspected;
struct Car *next;
} Car;
void main()
{
int initsetval = 0;
key_t key = (key_t)60110;
int msqid;
struct message msg;
struct timespec time;
struct timespec begin;
struct timespec end;
// 메시지큐 연결 (없으면 오류)
if ((msqid = msgget(key, IPC_EXCL | 0666)) == -1)
{
printf("서버에서 메시지를 생성해주세요\n");
}
checkUsableClient(msqid);
choiceClient(msqid);
// 맥북테스트용
// if ((sync_sem = sem_open("/semaphore", O_CREAT, 0644, 1)) == SEM_FAILED)
// {
// perror("sem_open");
// ;
// }
if (sem_init(&sync_sem, 0, 1) == -1)
{
perror("sem_init");
exit(1);
}
pthread_t thread[3];
Car *headCar = (Car *)malloc(sizeof(Car));
headCar->next = NULL;
int err_code = pthread_create(&thread[0], NULL, makeCar, (void *)headCar); // 차를 만든다.
if (err_code)
{
printf("ERROR code is %d\n", err_code);
exit(1);
}
// printf("point 6\n");
err_code = 0;
err_code = pthread_create(&thread[1], NULL, paintCar, (void *)headCar); // 차를 도색한다.
if (err_code)
{
printf("ERROR code is %d\n", err_code);
exit(1);
}
// printf("point 7\n");
err_code = 0;
err_code = pthread_create(&thread[2], NULL, inspectCar, (void *)headCar); // 차를 검사한 후 출고한다.
// printf("point 8\n");
if (err_code)
{
printf("ERROR code is %d\n", err_code);
exit(1);
}
while (1)
{
if (components < 10)
{
// printf("component get ! \n");
sem_wait(&sync_sem);
// 부품 요청하기
msg.msg_type = 2;
msg.data.client_num = client;
msg.data.attr = 1;
msg.data.time.tv_nsec = 0;
msg.data.time.tv_sec = 0;
if (msgsnd(msqid, &msg, sizeof(struct clientData), 0) == -1)
{
printf("msgsnd failed\n");
exit(0);
}
// 요청한 부품 받기
if (msgrcv(msqid, &msg, sizeof(struct clientData), 3, 0) == -1)
{
printf("msgrcv failed\n");
exit(0);
}
components++;
sem_post(&sync_sem);
}
}
}
void choiceClient(int msqid)
{
struct message msg;
printf("사용할 키 입력 : ");
scanf("%d", &client);
for (int i = 0; i < 10; i++)
{
// 사용가능여부 메시지 받기
if (msgrcv(msqid, &msg, sizeof(struct clientData), 1, 0) == -1)
{
printf("msgrcv failed\n");
exit(0);
}
if (client == msg.data.client_num)
{
// 선택한 클라이언트 사용가능 처리해서 다시 큐에 넣기
msg.data.attr = 1;
if (msgsnd(msqid, &msg, sizeof(struct clientData), 0) == -1)
{
printf("msgsnd failed\n");
exit(0);
}
}
else
{
// 이외 받은메시지 다시 큐에 넣기
if (msgsnd(msqid, &msg, sizeof(struct clientData), 0) == -1)
{
printf("msgsnd failed\n");
exit(0);
}
}
}
}
void checkUsableClient(int msqid)
{
struct message msg;
for (int i = 0; i < 10; i++)
{
// 사용가능여부 메시지 받기
if (msgrcv(msqid, &msg, sizeof(struct clientData), 1, 0) == -1)
{
printf("msgrcv failed\n");
exit(0);
}
// 받은메시지 다시 큐에 넣기
if (msgsnd(msqid, &msg, sizeof(struct clientData), 0) == -1)
{
printf("msgsnd failed\n");
exit(0);
}
if (msg.data.attr == 0)
{
printf("%d - 사용 가능\n", msg.data.client_num);
}
else
{
printf("%d - 사용 불가\n", msg.data.client_num);
}
}
}
void *makeCar(void *ptr)
{
int i = 0;
Car *current_made_car = ptr;
for (;;)
{
if (components > 0)
{
sleep(3);
sem_wait(&sync_sem);
components--; // 부품 갯수 빼주는 녀석
sem_post(&sync_sem);
current_made_car->next = (Car *)malloc(sizeof(Car));
current_made_car = current_made_car->next;
current_made_car->isCreated = true;
current_made_car->isPainted = false;
current_made_car->isInspected = false;
current_made_car->next = NULL;
// printf("car %d is created, client have %d components\n", i, components);
i++;
}
}
}
void *paintCar(void *ptr)
{
int i = 0;
Car *current_painted_car = ptr;
for (;;)
{
if ((current_painted_car->next != NULL) && current_painted_car->next->isCreated)
{
sleep(1);
current_painted_car = current_painted_car->next;
current_painted_car->isPainted = true;
// printf("car %d is painted\n", i);
i++;
}
}
}
void *inspectCar(void *ptr)
{
int i = 0;
Car *current_inspect_target = ptr;
for (;;)
{
if (current_inspect_target->next != NULL && current_inspect_target->next->isPainted)
{
sleep(2);
Car *inpected_car = current_inspect_target;
current_inspect_target = current_inspect_target->next;
current_inspect_target->isInspected = true;
free(inpected_car);
printf("client%d: %d cars inspected\n", client, i);
i++;
}
}
}