-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomerFile.c
136 lines (103 loc) · 3.31 KB
/
CustomerFile.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
#include "MainPrg.h"
#include <stdio.h>
#include "MsgManageHead.h"
#include "TimeManageHead.h"
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include "SemManageHead.h"
const char *customerFileName = "customer_file";
int readCustomerFile(FILE *customerFile, tCustomer *NextCustomer)
{
int cNo, success=0;
char cType[9];
if (EOF!=fscanf(customerFile, "%d %s", &cNo, cType))
{
NextCustomer->customerNo=cNo;
NextCustomer->Transaction=cType[0];
NextCustomer->arrivalTime=time(NULL);
//only character w or d is enough to differentiate between
//deposit or withdrawal
}
else
success=-1;
return success;
}
void writeCustomerLog(tCustomer Customer, int fileAccessSemID)
{
int errorLog;
FILE *logFile;
char *buf;
lockSemaphore(fileAccessSemID);
logFile=fopen(logFileName, "a");
// logFile=stdout; // for debug
//if cannot open for output then set output to standard output
if (logFile==NULL) logFile=stdout;
fprintf(logFile, "-------------------------------------\n");
fprintf(logFile, "Customer %d: ", Customer.customerNo);
switch (Customer.Transaction)
{
case 'd' : fprintf(logFile, "Deposit\n"); break;
case 'w' : fprintf(logFile, "Withdrawal\n"); break;
}
fprintf(logFile, "Arrival time: ");
buf=formatTime(Customer.arrivalTime);
fprintf(logFile, buf);
free(buf);
fprintf(logFile, "\n-------------------------------------\n\n");
fclose(logFile);
unlockSemaphore(fileAccessSemID);
}
int Customer(int ReadInterval, tShVariables *ShVar, int *ChildPIDs)
{
FILE *customerFile;
tCustomer nextCustomer;
CustomerQueueMsg custBuf;
int success = 0;
int errorLog = 0;
int waitLoop;
int runtime=0; //this is used to approximate the run time of the customer
customerFile=fopen("customer_file", "r");
if (customerFile!=NULL)
{
while (readCustomerFile(customerFile, &nextCustomer)==0)
{
custBuf.mtype=1;
custBuf.mtext[0]=nextCustomer.Transaction;
custBuf.customerNo=nextCustomer.customerNo;
custBuf.arrivalTime=nextCustomer.arrivalTime;
writeCustomerLog(nextCustomer, ShVar->fileAccessSemID);
sendMessages(ShVar->messageQueueID, custBuf, &errorLog);
lockSemaphore(ShVar->getMessageSemID);
//critical section
// ShVar->noOfMsgQueue++;
//increment the number of message in the queue
//child process can go now
if (getMessageQueueNumber(ShVar->messageQueueID)==1)
unlockSemaphore(ShVar->waitMessageSemID);
//end of critical section
unlockSemaphore(ShVar->getMessageSemID);
//put the customer in the shared memory
sleep(ReadInterval);
}
}
else
{
success=-1;
}
// lockSemaphore(ShVar->parentDieSemID);//just try to lock
//if the semaphore can be released, then parents can be killed
//or just exit as normal
// unlockSemaphore(ShVar->parentDieSemID);
//this is used to notify whether there were more customer or not
//it is no need to mutually exclusive, since the other file just
//reading
ShVar->moreCustomer=0;
// fprintf(logFile, "parent die %d\n", ShVar->moreCustomer);
for (waitLoop=0; waitLoop<cMAXTELLER; waitLoop++)
waitpid(ChildPIDs[waitLoop], NULL, 0);
//wait until each of the child die
fclose(customerFile);
return success;
}