-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.c
More file actions
99 lines (78 loc) · 2.76 KB
/
Copy pathinterface.c
File metadata and controls
99 lines (78 loc) · 2.76 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
/**
* @file: interface.c
* @author: Brandon Dedolph
* @date: February 14, 2017
* CSCI2 Account: cs311118
*/
#include <stdio.h>
#include "serverFunctions.h"
int main() {
int error;
int Status = 0;
int writeError, readError;
int serverPipe[PIPE_SIZE]; //Array that holds the read and write pipe to the server
int interfacePipe[PIPE_SIZE]; //Array that holds the read and write pipe to the interface
char argument1[20];
char argument2[20];
char ReadBuffer[BUFFER_SIZE];//Input
char WriteBuffer[BUFFER_SIZE];
int processID;
//Create Pipes
pipe(serverPipe);
pipe(interfacePipe);
//fork child process
processID = fork();
errcheck(processID);
if (processID == 0) {
//in child process
//Close unused pipe ends
close(serverPipe[PIPE_WRITE]);
close(interfacePipe[PIPE_READ]);
//Assign args
sprintf(argument1, "%d", serverPipe[PIPE_READ]);
sprintf(argument2, "%d", interfacePipe[PIPE_WRITE]);
//Execute Server Program
error = execl("./server", argument1, argument2, NULL);
errcheck(error);
//DB is initialized.
} else { // in Parent process
//Close unused pipe ends
close(serverPipe[PIPE_READ]);
close(interfacePipe[PIPE_WRITE]);
//Reading from the Server
readError = read(interfacePipe[PIPE_READ], ReadBuffer, 99);
printf("Response: %s", ReadBuffer);
clearBuffer(ReadBuffer);
errcheck(readError);
//Begin menu loop
bool done = false;
while (done == false) {
printf("\n:");
//receive input from user, send to Server.
clearBuffer(WriteBuffer);
scanf("%s", WriteBuffer);
writeError = write(serverPipe[PIPE_WRITE], WriteBuffer, BUFFER_SIZE);
errcheck(writeError);
//Read Response from DB
readError = read(interfacePipe[PIPE_READ], ReadBuffer, BUFFER_SIZE);
errcheck(readError);
//Check for exit return by Server
if (atoi(ReadBuffer) == 1) {
done = true;
clearBuffer(ReadBuffer);
sprintf(ReadBuffer, "%s", "Server complete.\n");
printf("Response: %s", ReadBuffer);
printf("Interface: child process (%d) completed.\n", processID);
break;
}
printf("Response: \n%s ", ReadBuffer);
clearBuffer(ReadBuffer);
}
//Wait for child process to finish;
error = waitpid(-1, &Status, 0);
errcheck(error);
printf("Interface: child process exit status = %d.\n", Status);
printf("Interface: Complete.\n");
}//End of Parent Instructions
return 0; //Exit
}