Question

Write an application in C which implements a reader/writer data stream.

The "writer" threads will be accessing data from an external source (for example, a device

driver) using a serialized function and then placing this data into

a shared buffer. Different writers may be reading from different devices, but the data should

all be placed in the common buffer.

The "reader" threads will be reading this data from the shared buffer and then pseudo

processing the data.

The task is to fill in the body of the reader and writer threads so that they can pass data back

and forth to one another. This situation matches the

synchronization and data management operations required when filling a processing pipeline

with data coming from hardware and passing through to

various processing stages.

Assume the following:

There will be N writers and M readers of the data. The thread creation is specified in the

starter code, but feel free to adjust it to suit your needs

Make-believe API's for data manipulation:

"writer" threads use the API, int get_external_data(char *buffer, int bufferSizelnBytes) where

buffer is a pointer to a storage of bufferSizel

nBytes that can be filled in. The return value from this function indicates the number of bytes

that have been filled in, or <0 on error.

"reader" threads use the API, void process_data(char* buffer, int bufferSizelnBytes) where

buffer is a pointer to the data being

processed that is bufferSizelnBytes in length.

There is no memory constraint on the amount of data that is "in-flight" (totally unrealistic, we

know, but simplifies this a bit) but you must manage

all other memory appropriately. This includes allocating initial buffers for "writers" to read

from the device(s) into.

You should be able to write and compile this code on any POSIX-based Operating System

(i.e., Linux). A stub is provided below:

//TODO Define global data structures to be used

1**

* This thread is responsible for pulling data off of the shared data

* area and processing it using the process_data() API./nfrom the device(s) into.

You should be able to write and compile this code on any POSIX-based Operating System

(i.e., Linux). A stub is provided below:

//TODO Define global data structures to be used

/**

* This thread is responsible for pulling data off of the shared data

* area and processing it using the process_data() API.

*/

void *reader_thread(void *arg) {

//TODO: Define set-up required

while(1) {

//TODO: Define data extraction (queue) and processing

}

}

return NULL;

**

* This thread is responsible for pulling data from a device using

* the get_external_data() API and placing it into a shared area

* for later processing by one of the reader threads.

*/

void *writer_thread (void *arg) {

//TODO: Define set-up required

while(1) {

//TODO: Define data extraction (device) and storage

}

return NULL;/n*/

void *writer_thread(void *arg) {

//TODO: Define set-up required

while(1) {

//TODO: Define data extraction (device) and storage

}

return NULL;

}

#define M 10

#define N 20

int main(int argc, char **argv) {

int i;

for(i = 0; i < N; i++) {

pthread_create(NULL, NULL, reader_thread,NULL);

}

for(i = 0; i < M; i++) {

pthread_create(NULL, NULL, writer_thread, NULL);

} return 0;

}

Question image 1Question image 2Question image 3