Overview: You are to write two c programs, Interface and Server. After initializ
ID: 3668382 • Letter: O
Question
Overview: You are to write two c programs, Interface and Server. After initialization, the Interface will use the fork system call and an exec system call to start the execution of the Server. The Interface will accept commands from the console and send them to the Server for execution. After executing the command, the Server will send the output to the Interface for display on the console.
REQUIREMENTS:
1. Pipes: The Interface will need to create the pipes to be used for communication between itself and the Server prior to doing the fork. Use two pipes, one for sending commands to the Server and one for sending the response to the Interface.
2. Interface: In addition to tasks described elsewhere, the Interface must pass the pipe file descriptor values to the Server. Use the arguments on the exec command for this purpose. The Interface should also detect the final exit status of the Server and display an appropriate message.
3. Server: When the Server initializes, it must do two things before entering its loop accepting commands. First, it must initialize an array of structures (records) using the data in a local file called gasData. Second, it should display the contents of the array so it is easy to verify that the initialization occurred correctly. The records will keep track of the gas consumption of a set of cars. Each structure in the array is to consist of the following fields:
• id: this is the identifier of the car (a character string of up to 8 characters). • odometer: the reading on the odometer at the time of fill-up (integer).
• gallons: Number of gallons to fill up the tank (float). You are to create and use a typedef for the record structure.
4. gasData: each line of this ASCII file is to contain the data for one record of the array of structures. Each line is expected to be in the form: id odometer gallons where a single space is used as a delimiter.
5. Makefile: Write a makefile to control the compilation of your programs.
6. Commands:
Three commands must be processed:
• mpg,<id> This is to compute the average miles per gallon for the vehicle identified by the label <id>. It is to be the average over all records maintained by Server for the designated vehicle.
• list,<id> This will list all of the entries identified by the label <id> in increasing order of the odometer reading.
• exit After processing this command, the Server should terminate in a normal manner. After detecting the termination of the Server, the Interface should print relevant information and exit normally.
Required system calls to be used in the program:
7. System calls: At a minimum, you are expected to use the following system calls: fork, some type of exec call (such as execve), pipe, read, write, exit, waitpid, close. In addition your code is to check the response from the system calls for errors, display an appropriate error message and take appropriate action. In many cases that action may be just to call exit with a unique number selected.
Explanation / Answer
1. Pipes:
int main()
2. Interface:
3. Server :
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "PASSWORD"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s ", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s ", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database: ");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s ", row[0]);
mysql_free_result(res);
mysql_close(conn);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.