Question: In C, write a program called cachememory. This program must: 1. Read 3 arguments at the command line. If there are not 3 arguments or

In C, write a program called cachememory. This program must:

1. Read 3 arguments at the command line. If there are not 3 arguments or if any arguments are invalid, the program should exit and print an error message to stderr:

a. Port, the first argument, must be an integer between 0 and 84109.

b. number_elements, the second argument, must be an integer >= 1.

c. elements_size, the third argument, must be an integer >= 1.

2. The program needs to create a listening TCP socket on the port number specified in the port argument. It should then try to accept a connection.

3. When a connection is made with the programs socket, it will spawn a child process to handle the connection and then go back to accepting connections.

4. If the parent process receives an interrupt signal (SIGINT), it should do a controlled shutdown (first, free/close any resources, wait for children to exit, and then exit).

5. When handling the connection, the child process, should:

a. Read a single line of text from the client. There are three possible formats: SET , GET , DELETE

where

- is a string of only letters (a-z and/or A-Z) and digits (0-9), and should be at most 120 characters (but might be larger)

- is an integer >= 1

b. If one of the above commands is not given, the process should write a line with "ERR INVALID_COMMAND " to the client and close the connection.

c. If a string is larger than 120 characters, the process should write a line with "ERR NAME_TOO_LONG " to the client and close the connection.

d. If a string contains an invalid character, the process should write a line with "ERR BAD_NAME " to the client and close the connection.

e. If the is not an integer or not at least 1, the process should write a line with "ERR INVALID_SIZE " to the client and close the connection.

f. If the command was the SET command, it should read the next bytes from the client.

i) If bytes can't be read, the process should write a line with "ERR TOO_SMALL " to the client and close the connection.

ii) Otherwise, the process should write a line with "OK " to the client and close the connection.

g. If the command was the GET command, the process should write a line with "OK 1 " to the client and close the connection.

h. If the command was the DELETE command, the process should write a line with "OK 1 " to the client and close the connection.

i. If any other errors occur while the child process tries to complete a to h, it should write a line with "ERR OTHER " to the client and close the connection.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!