-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainProgs.c
171 lines (140 loc) · 4.53 KB
/
MainProgs.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
#include <stdio.h>
#include "MainPrg.h"
#include "ParamReader.h"
#include "TellerHead.h"
#include "CustomerHead.h"
#include "MsgManageHead.h"
#include "SemManageHead.h"
#include "SharedMemHead.h"
const int false = 0;
const int true = 1;
const int cMAXTELLER = 3;
void errorHandler(int errType)
{
switch (errType)
{
case 1 :
{
fprintf(stderr, "Can not invoke fork\n");
break;
}
case 2 :
{
fprintf(stderr, "Can not read customer_file\n");
break;
}
default : break; //no error, do nothing
}
}
int RunProcess(int ReadInterval, int* LiveTime,
int TimeWithdraw, int TimeDeposit)
{
int ProcessNo, ChildPID, ownProcessNo, timeOut;
int ChildPIDs [cMAXTELLER]; //storing for the child's PID
int ParentProcess = true;
//this is the for indiacting the parent process
int errorLog = 0;//for error checking
//this section is used for initiaizing the shared variables
tShVariables *ShVar;
void *ShVAttach;
int ShVAddress;
FILE *logFile;
logFile=fopen(logFileName, "w");
if (logFile==NULL) logFile=stdout;
fprintf(logFile, "Starting simulation\n");
fprintf(logFile, "-------------------\n\n");
fclose(logFile);
ShVAttach=createSHMem(sizeof(tShVariables), &errorLog, &ShVAddress);
ShVar=(tShVariables *) ShVAttach;
ShVar->noOfChildProcess=0;
// ShVar->noOfMsgQueue=0;
ShVar->moreCustomer=true;
//end of initiaizing shared variables
//initiaising message Queue ID
ShVar->messageQueueID=createMessageQueue(1, &errorLog);
//this is for semaphore is for controling child to wait the parents
ShVar->waitMessageSemID = createSemaphore(IPC_PRIVATE, &errorLog);
setSemValue(ShVar->waitMessageSemID, 1, &errorLog);
//this semaphore is for indicating where to get message
ShVar->getMessageSemID = createSemaphore(IPC_PRIVATE, &errorLog);
setSemValue(ShVar->getMessageSemID, 1, &errorLog);
//this is for semaphore to manage no of process process
ShVar->noOfProcessSemID = createSemaphore(IPC_PRIVATE, &errorLog);
setSemValue(ShVar->noOfProcessSemID, 1, &errorLog);
//this is for semaphore to manage writing file access
ShVar->fileAccessSemID = createSemaphore(IPC_PRIVATE, &errorLog);
setSemValue(ShVar->fileAccessSemID, 1, &errorLog);
/* cout << "parent die sem " << ShVar->parentDieSemID << endl;
cout << "semaphore value " << ShVar->noOfProcessSemID << endl;*/
ownProcessNo=0;//this is for indicating the own process no
//0 means parents, 1..3 means childs
//lock the semaphore here so that the child will be willing to
//wait for the parents to put the message in
lockSemaphore(ShVar->waitMessageSemID);
//initiaisation for the loop
ProcessNo=1;
while ((ProcessNo<=cMAXTELLER) && (ParentProcess)
&& (errorLog==0))
{
//while the parent process is running, no error happens, and
//number of process is < max teller+1
ChildPID=fork();
switch (ChildPID)
{
case -1 :
{
errorLog=1;
break;
}
case 0 :
{
//the child process goes here
ParentProcess=false;
ownProcessNo=ProcessNo;
timeOut=LiveTime[ProcessNo-1];
break;
}
default :
{
//the parent process
ChildPIDs[ProcessNo-1]=ChildPID;
}
}
ProcessNo++;
}
if ((errorLog==0) && (ownProcessNo==0)) //parent process will go here
{
if (Customer(ReadInterval, ShVar, ChildPIDs)==-1)
errorLog=2;
destroyMessageQueue(ShVar->messageQueueID, &errorLog);
removeSemaphore(ShVar->waitMessageSemID, &errorLog);
removeSemaphore(ShVar->getMessageSemID, &errorLog);
removeSemaphore(ShVar->noOfProcessSemID, &errorLog);
removeSemaphore(ShVar->fileAccessSemID, &errorLog);
destroySharedMem(ShVAddress, ShVAttach);
}
else
{ //child process will go here
Teller(ownProcessNo, timeOut, TimeWithdraw,
TimeDeposit, ShVar);
// cout << "Child die" << endl;
}
return errorLog;
}
int main(int argc, char** argv)
{
int ReadInterval, TimeWithdraw, TimeDeposit;
int LiveTime [cMAXTELLER];
int ParamCheck;
ParamCheck=getParameter(&ReadInterval, LiveTime,
&TimeWithdraw, &TimeDeposit,
argc, argv);
if (ParamCheck==cPARAMETERVALID)
{
errorHandler(RunProcess(ReadInterval, LiveTime,
TimeWithdraw, TimeDeposit));
}
else
writeParameterError(ParamCheck);
return 0;
}