Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Screenshot of assignment if it's easier: https://i.imgur.com/4ud1JDF.png Implement a program that processes batches of messages in a loop as follows: reads a message from the

Screenshot of assignment if it's easier: https://i.imgur.com/4ud1JDF.png

Implement a program that processes batches of messages in a loop as follows:

reads a message from the standard input into input_buffer.

collects the messages in message_cache using the function add_message_to_cache() that takes input_buffer as a parameter,

when needed as explained in the next bullet, invokes a function message_dispatcher() to process a batch of messages that are in the cache one by one using the function process_message(), and continues to take batches of messages from the input until either:

the capacity of the cache (arbitrarily chosen to be some fixed value; for example 16) is reached in which case the message dispatcher should be called to process the messages in the buffer, and then to continue reading messages,

or

a line with a sentinel END is read, in which case the program should process all remaining messages in the cache, and then terminate.

There should be one message per line of input. Messages can be in a number of formats:

TYPE 1: a string of unknown size

TYPE 2: 5 integers

TYPE 3: 4 floating point numbers

TYPE 4: 5 five-character words

The input should indicate the type of the message and the content; e.g.:

abcdefghijklmnopqrst

123 45 6 7890 87592

123.34 23.12345 5439.234 0.000231

aaaaa bbbbb ccccc ddddd eeeee

After receiving the sentinel your program should finish processing the messages in the buffer and print processing statistics:

the number of batches (including the last one even if incomplete),

the total number of messages processed, and

the number of messages of each type.

The process_message() function should be invoked from message_dispatcher() for each message in the cache. It should take a single parameter that is the next message from the cache to process. It should recognize what type the message has, and then print the message type along with its content as follows:

for type 1, the complete string,

for type 2, all integers separated by commas,

for type 3, all floating numbers separated by a slash, and

for type 4, all words separated by spaces.

Example Output:

TYPE 1: abcdefghijklmnopqrst

TYPE 2: 123,45,6,7890,87592

TYPE 3: 123.34/23.12345 5439.234 0.000231

TYPE 4: aaaaa bbbbb ccccc ddddd eeeee

Number of message batches: 1

Messages processed:

Total: 4

Type 1: 1

Type 2: 1

Type 3: 1

Type 4: 1

The cache should be implemented as an array of the struct contructs that include two fields: one for the type of the message and the second should be a union construct to hold the content of the message that is specific to its type. An unsigned short should be used as the data type for the message type field. The type should be used in a switch statement to save the message in the union according to the message type.

processor.c should contain a global declaration of the message cache and a global declaration of a number of messages read, as well as implementations of the functions add_message_to_cache() and message_dispatcher(), and process_message().

add_message_to_cache() should use sscanf() to read the type of the message and the message content from the parameter. To do that, an appropriate pattern for sscanf() needs to be designed. A switch statement should be used to select proper handler for each message type.

The program must use dynamic allocation for storing messages of type 1. The memory allocated for that purpose must be freed after the message is processed.

You should have processor.h file for declarations of data structures and declarations of common functions.

test_processor.c should include the main() with the testing driver that implements the main loop of the application.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions