-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
201 lines (168 loc) · 5.97 KB
/
client.c
File metadata and controls
201 lines (168 loc) · 5.97 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
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
int** readMatrixFromFile (const char* file_name, int i);
int row1Count, col1Count;
int row2Count, col2Count;
// structure for message queue
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main(int argc, char *argv[])
{
int pId = getpid();
printf("Client started with pId: %d\n", pId);
printf("client: Matrices are being read from files....\n");
int** matrix1 = readMatrixFromFile(argv[1], 1);
int** matrix2 = readMatrixFromFile(argv[2], 2);
// Check the suitability of matrices for multiplication
if (col1Count != row2Count){
printf("client: The number of rows of the 2nd matrix is not equal to the number of columns of the 1st matrix!\n");
return -1;
}
key_t key;
int msgIdSent, msgIdRcv;
struct mesg_buffer msgBufferSent, msgBufferRcv;
msgBufferSent.mesg_type = 1;
// Generate unique key with ftok
key = ftok( "progfile", 65);
// Create a message queue to send messages to server
msgIdSent = msgget(key, IPC_CREAT | 0666 );
char msgPId[10];
sprintf(msgPId, "%d", pId);
strcpy(msgBufferSent.mesg_text, msgPId );
// Send pId
if (msgsnd(msgIdSent, &msgBufferSent, sizeof msgBufferSent.mesg_text, 0 ) == -1 ) {
perror( "client: msgsnd failed:" );
exit( 3 );
} else
printf("client: pId sent => %s\n", msgBufferSent.mesg_text);
// Create 2 queues to send and receive messages from server
if ((msgIdRcv = msgget(pId, IPC_CREAT | 0666 ) ) == -1 ) {
perror( "client: Failed to create message queue:" );
exit( 1 );
}
if ((msgIdSent = msgget(pId + 32768, IPC_CREAT | 0666 ) ) == -1 ) {
perror( "client: Failed to create message queue:" );
exit( 1 );
}
// Create message for matrices' sizes.
char matricesSizesMsg[] = "";
char row1CountS[5];
char col1CountS[5];
char row2CountS[5];
char col2CountS[5];
sprintf(row1CountS, "%d", row1Count);
sprintf(col1CountS, "%d", col1Count);
sprintf(row2CountS, "%d", row2Count);
sprintf(col2CountS, "%d", col2Count);
strcat(matricesSizesMsg,row1CountS);
strcat(matricesSizesMsg," ");
strcat(matricesSizesMsg,col1CountS);
strcat(matricesSizesMsg," ");
strcat(matricesSizesMsg,row2CountS);
strcat(matricesSizesMsg," ");
strcat(matricesSizesMsg,col2CountS);
strcpy(msgBufferSent.mesg_text, matricesSizesMsg);
// Send matrices' sizes.
if (msgsnd(msgIdSent, &msgBufferSent, sizeof msgBufferSent.mesg_text, 0 ) == -1 ) {
perror( "client: msgsnd failed:" );
exit( 1 );
} else
printf("client: matrices' sizes sent => %s\n", msgBufferSent.mesg_text);
// Receive message for confirmation of sharing memory is created
if (msgrcv(msgIdRcv, &msgBufferRcv, sizeof msgBufferSent.mesg_text, 1, 0 ) == -1){
perror( "client: msgrcv failed:" );
exit(1);
} else
printf("client: Message received = %s\n", msgBufferRcv.mesg_text );
// Attach the shared memory created by server
int shmSize = row1Count * col1Count * sizeof(int) +
row2Count * col2Count * sizeof(int) +
col1Count * row2Count * sizeof(int);
int shmId;
void *virtualAddr;
int *tempPtr;
shmId = shmget(pId, shmSize, 0);
printf("client: shmId value is %d\n", shmId);
virtualAddr = shmat(shmId, (void *)0, 0); // Attach the shared segment
tempPtr = (int *)virtualAddr; // Put two integer values into it
// Write matrices to shared memory
for (int i = 0; i < row1Count; i++) {
for (int j = 0; j < col1Count; j++) {
*tempPtr = matrix1[i][j];
tempPtr++;
}
}
for (int i = 0; i < row2Count; i++) {
for (int j = 0; j < col2Count; j++) {
*tempPtr = matrix2[i][j];
tempPtr++;
}
}
// Notify server that matrices are written to shared memory
strcpy(msgBufferSent.mesg_text, "The matrices to be multiplied are written to shared memory.");
if (msgsnd(msgIdSent, &msgBufferSent, sizeof msgBufferSent.mesg_text, 0 ) == -1 ) {
perror( "client: msgsnd failed:" );
exit( 3 );
} else
printf("client: message sent => %s\n", msgBufferSent.mesg_text);
//Receive message to confirm matrix multiplication is finished
if (msgrcv(msgIdRcv, &msgBufferRcv, sizeof msgBufferSent.mesg_text, 1, 0) == -1){
perror("client: msgrcv failed:");
exit(1);
} else
printf("client: message received => %s\n", msgBufferRcv.mesg_text);
// Print the result matrix
printf("Result:\n");
for (int i = 0; i < row1Count; i++) {
for (int j = 0; j < col2Count; j++) {
printf("%d ",*tempPtr);
tempPtr++;
}
printf("\n");
}
//Detach the shared memory
shmdt(virtualAddr);
if (shmctl(shmId, IPC_RMID, 0) == -1) {
perror("shmctl");
exit(1);
}
// Remove the message queues
struct msqid_ds msgCtlBuf;
if (msgctl(msgIdSent, IPC_RMID, &msgCtlBuf ) == -1 || msgctl(msgIdRcv, IPC_RMID, &msgCtlBuf ) == -1) {
perror( "client: msgctl failed:" );
exit(1);
}
return 0;
}
//function that reads from the file and puts integer into an array
int** readMatrixFromFile (const char* fileName, int i)
{
FILE* file = fopen (fileName, "r");
int rowCount = 0;
int colCount = 0;
fscanf(file, "%d %d", &rowCount, &colCount);
if (i == 1){
row1Count = rowCount;
col1Count = colCount;
}else if(i == 2){
row2Count = rowCount;
col2Count = colCount;
}
int **arr=(int **)malloc(sizeof(int *) * rowCount);
for (int i = 0; i < rowCount; i++) {
arr[i]=(int *)malloc(sizeof(int) * colCount);
for (int j = 0; j < colCount; j++) {
fscanf(file,"%d", &arr[i][j]);
}
}
fclose (file);
return arr;
}