Question: 1) Buffering Real-world data from measuring devices or sensors is generally produced asynchronously relative to the processing routine(s). The data may appear in bursts that
1) Buffering Real-world data from measuring devices or sensors is generally produced asynchronously relative to the processing routine(s). The data may appear in bursts that occur faster than they can be individually processed. An effective way to handle this situation is to employ a buffer. A buffer is simply an array into which a datagenerating process writes data, and from which a separate data-processing routine subsequently retrieves the data. In the short-term, if the data is being generated at a faster rate than can be processed, the buffer fills up while the processing routine catches up. In the long-term (assuming the average processing rate exceeds the average data generation rate), the data processing will be able to remain ahead of or keep pace with the datagenerating process. There are a number of ways to implement buffering. One simple way is to employ a double-buffering technique in which a generating process allocates two buffers. At any given time, the data-generating process is filling one of the buffers and allowing the processing routine to read from the other buffer. Once the datagenerating process fills one buffer, it provides the address of the filled buffer to the processing routine and allocates a new buffer to begin filling. The processing routine extracts the data from the filled buffer and disposes the buffer when finished, then awaits a pointer to the next "filled" buffer. In this problem, you will simulate a basic double-buffering scheme using dynamically allocated arrays of integers. You will need to construct a data generation function and a data processing function and then randomly call them from a simulation loop in the main program.
1) Construct a function: double getProb() that will return a pseudo-random value between 0.0 and 5.0.
2) Construct a function: int* generateData(int* &inbuf, int &count) that simulates asynchronous data generation by obtaining a random number between 2 and 20 (you do not need to use getProb() for this) and saving it in an input buffer. Your function should do the following:
Generate a two random integers between 2 and 20 and insert them in the buffer at the array location specified by the count argument. (Note that the buffer is passed as a pointer variable reference.)
Increase the count after each insertion.
If the buffer is full, a) return the address of the full buffer,
b) reset the count to zero,
c) allocate a new buffer, and
d) save its address in the pointer variable passed as the first argument.
If the buffer is not full, then return NULL.
3) Construct a function: void processData(int* &outbuf, int &count, int &total) to simulate "processing" of the asynchronous data. Do the following:
If the output buffer pointer is NULL, do nothing, i.e., just return.
Otherwise, obtain the buffer value at [count] and multiply the total by the buffer value, then increase the count.
If all elements in the buffer have been exhausted, then reset the count to zero, delete the (dynamically allocated) buffer and set the buffer pointer argument to NULL.
4) Finally, include the following code in a main simulation program that will call the generateData and ProcessData functions in a loop.
const int BUFSIZE=10; const int ITERATIONS=50; int main() { int *fillbuffer = new int[BUFSIZE]; int fillcnt=0; int *processbuffer = NULL; int processcnt=0; int tcount = 0; for(int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
