-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSource.c
More file actions
166 lines (133 loc) · 4.03 KB
/
Source.c
File metadata and controls
166 lines (133 loc) · 4.03 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
//Daler_Assignment_4
#define WIN32_LEAN_AND_MEAN
#define NOATOM
#define NOCLIPBOARD
#define NOCOMM
#define NOCTLMGR
#define NOCOLOR
#define NODEFERWINDOWPOS
#define NODESKTOP
#define NODRAWTEXT
#define NOEXTAPI
#define NOGDICAPMASKS
#define NOHELP
#define NOICONS
#define NOTIME
#define NOIMM
#define NOKANJI
#define NOKERNEL
#define NOKEYSTATES
#define NOMCX
#define NOMEMMGR
#define NOMENUS
#define NOMETAFILE
#define NOMSG
#define NONCMESSAGES
#define NOPROFILER
#define NORASTEROPS
#define NORESOURCE
#define NOSCROLL
#define NOSHOWWINDOW
#define NOSOUND
#define NOSYSCOMMANDS
#define NOSYSMETRICS
#define NOSYSPARAMS
#define NOTEXTMETRIC
#define NOVIRTUALKEYCODES
#define NOWH
#define NOWINDOWSTATION
#define NOWINMESSAGES
#define NOWINOFFSETS
#define NOWINSTYLES
#define OEMRESOURCE
#pragma warning(disable : 4996)
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <io.h>
#include <WinSock2.h>
#if !defined(_Wp64)
#define DWORD_PTR DWORD
#define LONG_PTR LONG
#define INT_PTR INT
#endif
typedef struct _RECORD { //My Record
int AccountNumber;
int AccountBalance;
HANDLE AccountHandle;
CRITICAL_SECTION AccUpdating;
} RECORD;
typedef struct _TEMPRECORD { //To hold the temporary balance of an account
int TempAccNumber;
int TempAccBalance;
} TEMPRECORD;
void WINAPI Transactions(char*); //prototype for the Transactions function
LARGE_INTEGER FileSize;
RECORD *AccountArray;
//CMMDLINE: trans1.bin trans2.bin trans3.bin < AccInfo.bin > NewAccountInfo.bin
int _tmain(int argc, LPTSTR argv[])
{
TEMPRECORD Temp;
HANDLE *hThreads;
DWORD BIn;
//STDinput and STDoutpus variables
HANDLE STDInput;
HANDLE STDOutput;
STDInput = GetStdHandle(STD_INPUT_HANDLE);
STDOutput = GetStdHandle(STD_OUTPUT_HANDLE);
GetFileSizeEx(STDInput, &FileSize); //Getting the file size
int NumberOfRecords = FileSize.QuadPart / 8; // / 8 because each record is equal to 8 bytes
//Giving memories to hThreads and AccountArray
hThreads = malloc((argc - 1) * sizeof(HANDLE));
AccountArray = malloc(NumberOfRecords * sizeof(RECORD));
for (int x = 0; x < NumberOfRecords; x++)
{
ReadFile(STDInput, &Temp, sizeof(TEMPRECORD), &BIn, NULL); //Reading the data into Temp
AccountArray[x].AccountNumber = Temp.TempAccNumber; //Storing the data from Temp to my array
AccountArray[x].AccountBalance = Temp.TempAccBalance;
InitializeCriticalSection(&AccountArray[x].AccUpdating); //initializing the Critical Section
}
for (int NumThread = 0; NumThread < argc - 1; NumThread++)
{
hThreads[NumThread] = (HANDLE)_beginthreadex(NULL, 0, Transactions, argv[NumThread + 1], CREATE_SUSPENDED, NULL); //Beginning the threads in SUSPENDED MODE
}
for (int x = 0; x < argc - 1; x++)
{
ResumeThread(hThreads[x]);
}
for (int x = 0; x < argc - 1; x++)
{
WaitForSingleObject(hThreads[x], INFINITE);
}
for (int x = 0; x < argc - 1; x++)
{
CloseHandle(hThreads[x]);
}
//write new account information
for (int x = 0; x < NumberOfRecords; x++)
{
WriteFile(STDOutput, &AccountArray[x], sizeof(TEMPRECORD), &BIn, NULL);
}
return 0;
}
void WINAPI Transactions(char* TransFile) //Read in the files 8 bytes at a time
{
HANDLE TransHandle;
DWORD BIn;
TEMPRECORD Temp;
TransHandle = CreateFile(TransFile,
GENERIC_READ | GENERIC_WRITE, //child can read the files
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
ReadFile(TransHandle, &Temp, sizeof(TEMPRECORD), &BIn, NULL);
while (BIn != 0)
{
EnterCriticalSection(&AccountArray[Temp.TempAccNumber].AccUpdating);
AccountArray[Temp.TempAccNumber].AccountBalance += Temp.TempAccBalance;
// printf("%s changed %d balance to %d \n", TransFile, AccountArray[Temp.TempAccNumber], AccountArray[Temp.TempAccNumber].AccountBalance);
LeaveCriticalSection(&AccountArray[Temp.TempAccNumber].AccUpdating);
ReadFile(TransHandle, &Temp, sizeof(TEMPRECORD), &BIn, NULL);
}
}