forked from UrielBender/Multithreading-in-Win32Api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haifaPort.c
282 lines (233 loc) · 7.31 KB
/
haifaPort.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#define BUFFER_SIZE 55
#define MAX_SLEEP_TIME 3000 //Miliseconds (3 second)
#define MIN_SLEEP_TIME 5 // Miliseconds
#define True 1
#define False 0
int numVes;
HANDLE mutex;
HANDLE* semVessel;
HANDLE* vessels;
int* vesselID;
DWORD WINAPI Vessel(PVOID); //Thread function for each vessel
DWORD ThreadId;
TCHAR ProcessName[256];
HANDLE StdInRead, StdInWrite; /* pipe for writing parent to child */
HANDLE StdOutRead, StdOutWrite; /* pipe for writing child to parent */
STARTUPINFO si;
PROCESS_INFORMATION pi;
char buffer[BUFFER_SIZE];
SYSTEMTIME timeToPrint;
DWORD written, read;
//Functions declartion
int initGlobalData();
void cleanupGlobalData();
char* inttostr(int n);
int calcSleepTime(); //generic function to randomise a Sleep time between 1 and MAX_SLEEP_TIME msec
void toSuez(int id);
char* timePrinting();
int main(int argc, char* argv[])
{
//check if the number the client type is positive
if (argc <= 1) {
printf("You did not feed me arguments, I will die now :( ...");
exit(1);
} //otherwise continue on our merry way....
numVes = atoi(argv[1]);
if (numVes >= 51 || numVes <= 2) {
printf("Ships number should be between 2 to 51!!!! ----> ending process");
exit(1);
}
/* set up security attributes so that pipe handles are inherited */
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL,TRUE };
ZeroMemory(&pi, sizeof(pi));
/* create the pipe */
if (!CreatePipe(&StdInRead, &StdInWrite, &sa, 0)) {
fprintf(stderr, "Create Pipe Failed\n");
return 1;
}
/* create second the pipe */
if (!CreatePipe(&StdOutRead, &StdOutWrite, &sa, 0)) {
fprintf(stderr, "Create second Pipe Failed\n");
return 1;
}
/* establish the START_INFO structure for the child process */
GetStartupInfo(&si);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
/* redirect the standard input to the read end of the pipe */
si.hStdInput = StdInRead;
si.hStdOutput = StdOutWrite;
si.dwFlags = STARTF_USESTDHANDLES;
/* we do not want the child to inherit the write end of the pipe */
//SetHandleInformation(StdInWrite, HANDLE_FLAG_INHERIT, 0);
wcscpy(ProcessName, L"..\\..\\EilatPort\\Debug\\EilatPort.exe");
// Start the child process.
if (!CreateProcessW(NULL,
ProcessName, // No module name (use command line).
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
TRUE, // inherite handle.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
fprintf(stderr, "Process Creation Failed\n");
return -1;
}
sprintf(buffer, "%d", numVes);
/* Haifa port now sends ships to the pipe(souze) */
if (!WriteFile(StdInWrite, buffer, BUFFER_SIZE, &written, NULL))
printf(stderr, "Error writing to pipe-father\n");
//Read response from Eilat
if (ReadFile(StdOutRead, buffer, BUFFER_SIZE, &read, NULL)) {
if (atoi(buffer) == 1) {
printf("Sorry, you cant send %d ships to eilat port!", numVes);
/* close all handles */
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
exit(0);
}
}
else
printf(stderr, "haifa: Error reading from pipe\n");
//Initializa global data after confirmation from Eilat
if (initGlobalData() == False)
{
printf("main::Unexpected Error in Global Semaphore Creation\n");
return 1;
}
//Initialise vessels Threads
for (int i = 0; i < numVes; i++)
{
vesselID[i] = i + 1;
vessels[i] = CreateThread(NULL, 0, Vessel, &vesselID[i], 0, &ThreadId);
if (vessels[i] == NULL)
{
printf("main::Unexpected Error in Vessel %d Creation\n", i);
return 1;
}
}
int i = 0;
while (ReadFile(StdOutRead, buffer, BUFFER_SIZE, &read, NULL)) {
if (strcmp(buffer, "") != 0) {
printf("[%s] Vessel %d - done sailing @ Haifa Port \n", timePrinting(), atoi(buffer));
Sleep(calcSleepTime());
if (!ReleaseSemaphore(semVessel[(atoi(buffer) - 1)], 1, NULL)) {
printf("vesselThatCameBack::Unexpected error semVessel.V()\n");
}
i++;
if (i == numVes)
break;
}
}
WaitForMultipleObjects(numVes, vessels, TRUE, INFINITE);
printf("[%s] Haifa Port: All Vessel Threads are done\n", timePrinting());
/* close all pipes */
CloseHandle(StdOutWrite);
CloseHandle(StdInRead);
CloseHandle(si.hStdError);
WaitForSingleObject(pi.hProcess, INFINITE);
WaitForSingleObject(pi.hThread, INFINITE);
/* close all handles */
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
//close all global Semaphore Handles
cleanupGlobalData();
printf("[%s] Haifa Port: Exiting....\n", timePrinting());
}
//thread function for vessels thread by unique ID
DWORD WINAPI Vessel(PVOID Param)
{
int vesselId = *(int*)Param;
printf("[%s] Vessel % d starts sailing @ Haifa Port \n", timePrinting(), vesselId);
Sleep(calcSleepTime());
toSuez(vesselId);
Sleep(calcSleepTime());
return 0;
}
//generic function to randomise a Sleep time between 1 and MAX_SLEEP_TIME msec
int calcSleepTime()
{
srand((unsigned int)time(NULL));
return(rand() % MAX_SLEEP_TIME + MIN_SLEEP_TIME);
}
//function that send the vessels frome Haifa to suez
void toSuez(int id)
{
WaitForSingleObject(mutex, INFINITE);
printf("[%s] Vessel %d - entering Canal: Med. Sea ==> Red Sea \n", timePrinting(), id);
Sleep(calcSleepTime());
int sizeBuf = sizeof(inttostr(id));
if (!WriteFile(StdInWrite, inttostr(id), BUFFER_SIZE, &written, NULL))
fprintf(stderr, "Error writing to pipe-eilat\n");
if (!ReleaseMutex(mutex))
printf("toSuez::Unexpected error mutex.V()\n");
WaitForSingleObject(semVessel[id - 1], INFINITE);
}
//generic function to initial all mutex\semaphore and allocated memory to variable
int initGlobalData()
{
int i;
vessels = (HANDLE*)malloc(numVes * sizeof(HANDLE));
vesselID = (int*)malloc(numVes * sizeof(int));
semVessel = (HANDLE*)malloc(numVes * sizeof(HANDLE));
if ((vessels || vesselID || semVessel) == NULL) {
printf("memory error!!!");
return -1;
}
mutex = CreateMutex(NULL, FALSE, NULL);
if ((mutex) == NULL)
{
return False;
}
for (i = 0; i < numVes; i++)
{
semVessel[i] = CreateSemaphore(NULL, 0, 1, NULL);
if (semVessel[i] == NULL)
{
return False;
}
}
return True;
}
//Close all global semaphore handlers - after all vessels Threads finish.
void cleanupGlobalData()
{
int i;
CloseHandle(mutex);
for (i = 0; i < numVes; i++)
CloseHandle(semVessel[i]);
free(vessels);
free(vesselID);
free(semVessel);
}
//generic function that change int to string
char* inttostr(int n) {
char* result;
if (n >= 0)
result = malloc(floor(log10(n)) + 2);
else
result = malloc(floor(log10(n)) + 3);
sprintf(result, "%d", n);
return result;
}
//generic function to print system time information
char* timePrinting()
{
GetLocalTime(&timeToPrint);
static char currentLocalTime[20];
if (timeToPrint.wHour < 12) // before midday
sprintf(currentLocalTime, "%02d:%02d:%02d am", timeToPrint.wHour, timeToPrint.wMinute, timeToPrint.wSecond);
else // after midday
sprintf(currentLocalTime, "%02d:%02d:%02d pm", timeToPrint.wHour - 12, timeToPrint.wMinute, timeToPrint.wSecond);
return currentLocalTime;
}