Question: Task 6: Character-based circular buffer (40 marks) Copy your code from Task 5 into a new directory named task6. This task will involve replacing the
Task 6: Character-based circular buffer (40 marks) Copy your code from Task 5 into a new directory named task6. This task will involve replacing the message-oriented approach with a character-based solution that allows for arbitrary message sizes (i.e., greater than 20 characters) and improved parallel access to the shared memory segment. Background The shared memory buffer will need to be modified to function as a FIFO (first in, first out) data structure (a queue). This could be accomplished using a linear array with the producer inserting items at one end and the consumer removing them from the other. However, every time an element is removed from the array, all remaining elements would need to be shuffled down one place to fill the gap, which is inefficient. A more efficient solution is to use a circular buffer that functions as if it were a ring connected end-toend. This can be achieved using two index variables: head and tail. The head variable stores the location (index) where the producer will insert an item into the buffer. The tail variable stores the location (index) where the consumer will remove an item from the buffer. As both index values can change, no data shuffling is required. If the head or tail variables reach the end of the array, they wrap around to the beginning, which can be implemented using a modulus (%) expression. It is important to ensure that when the buffer is full, the head variable does not move past the tail variable and overwrite a character before the consumer has had the chance to read it. Stage 1 Implement a circular buffer in your shared memory segment. Modify your producer so it writes a single character at a time to the buffer instead of a complete line. Each character should be written to the next head index. Modify your consumer so it reads a single character at a time from the buffer. Each character should be read from the tail index. As with the previous task, use semaphores to control access to the buffer and index variables (mutual exclusion/synchronisation). If the buffer is full, the producer should wait until there is sufficient space to continue. If the buffer is empty, the consumer should wait until there are characters to read. This is the essence of the producer-consumer problem (see the lecture material from week 4 for a refresher). Stage 2 Implement character-based sleep control message processing for both the producer and consumer. Modify your algorithm so it works with the circular buffer and is capable of parsing control messages one character at a time rather than as a complete line (string). Add sleep functionality to the consumer. The consumer should process sleep control messages in the same manner as the producer (Task 4); however, the consumer should only adjust its sleep interval when it reads a control message in the format $C:n (a C instead of a P). See the table below for an example. Set the default sleep interval for the producer and consumer to 100 ms. Both should sleep between writing/reading each character from the buffer. As per the previous task, a value of 0 ms bypasses sleeping until another control message is read that resets it. The use of distinct control messages allows the run speed of the producer and consumer to be varied individually, meaning we can explore cases where the producer must wait due to a full buffer or the consumer must wait due an empty buffer. Correct use of semaphores should ensure each responds appropriately in cases where the other speeds up or slows down. COMP9812 Semester 2, 2018 9 Message Target $P:3000 Addressed to the producer (consumer ignores) $C:3000 Addressed to the consumer (producer ignores) Hint: As you are only able to read one character at a time, you will need to ensure you read the three prefix characters ($P: or $C:) consecutively before you attempt to parse the number. Any deviation from the prefix format will mean you have to start checking from the beginning. Once the prefix has been found, the following characters can be analysed to determine whether they are digits. You may find the isdigit function useful for this Stage 3 Test your producer and consumer using the supplied test-message-task6.txt, which includes control messages to alter the run speed of the producer and consumer. Or create your own with a variety of run speeds to simulate full and empty buffer conditions. In either case, ensure your code behaves as expected. Write a summary of your approach to Stage 1 (circular buffer implementation) and Stage 2 (characterbased sleep control message processing) in your report. Be descriptive. Discuss your implementation, testing routine, and any issues you overcame during development.
note c programming please
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
