Because this lab is a little harder than normal for the first lab for a programming assignment, it has an extended due date of Thursday at 8am.
The goal for this lab is to implement the producer-consumer pattern with a single producer and a single consumer. The producer should open the file whose filename is specified by the single commandline argument. It should feed the consumer blocks of this file, one block at a time. The blocks should be 1000 bytes long. The queue through which the producer and the consumer communicate only needs to hold one block. The consumer should simply count the total number of lines it sees in the blocks.
You need to devise a technique for the producer to tell the consumer that there are no more buffers to be consumed. For instance, you might package the block inside a C struct that also contains the length of the block. A zero-length block could indicate that there is no more data. (The length field might also be handy because the length of the file is unlikely to be evenly divisible by 1000, so the last block will be a short one.)
We used C structs in Program 3, but you may not have much experience writing your own struct definitions. Here is a possible struct definition for a struct for a buffer element for this lab:
typedef struct { int blockLength; char block[1050]; } BufferElement;
The producer should allocate this struct
p = malloc(sizeof(BufferElement));and fill it in. The consumer should deallocate it
free(p);
The buffer itself would then be a pointer to a buffer element and a control field that indicates whether the buffer is full or empty. The producer would wait until the buffer is empty, then insert a pointer to the new buffer element that it just filled and mark the buffer as full. The consumer would wait until the buffer is full, then remove the pointer to the next buffer element it should process and mark the buffer as empty.
The main thread should create the producer and consumer threads and wait for them to finish. The main thread should then print the final count of lines computed by the consumer.
Put all your source code in a single file, "lab7.c".
Use the valgrind memcheck facility to validate that you properly free all allocated memory blocks:
Also run the program using the valgrind helgrind facility to check for threading errors:
Turn in this laboratory by typing:
% ~cs520/bin/submit lab7 lab7.c
Submissions can be checked by typing:
% ~cs520/bin/scheck lab7
Remember that there are no late submissions of labs. Submit what you have prior to 8am on Thursday March 26. Please note the extended due date for this lab.
Comments and questions should be directed to hatcher@unh.edu