1 Purpose and Background
This project is designed to give you experience in writing multi-threaded programs by
implementing a simpliﬁed MapReduce-style wordcount application. By working on this
• Youwill learn towritemulti-threaded code that correctly dealswith race conditions.
• You will carry out a simple performance evaluation to examine the performance
impact of (i) the degree of parallelism in the mapper stage and (ii) the size of the
shared buffer which the two stages of your application will use to communicate.
Figure 1: Overview of our Mapreduce-style multi-threaded wordcount application.
The wordcount application takes as input a text ﬁle and produces as output the counts
for all uniquely occurring words in the input ﬁle arranged in an alphabetically increas-
ing order. We will assume that the words within our input ﬁles will only contain letters
of the English alphabet and the digits 0-9 (i.e., no punctuation marks or other special
characters). Our wordcount will consist of two stages. The ﬁrst stage, called ”mapper,”
reads individual words and produces key-value pairs of the form (word, 1). The sec-
ond stage, called the ”reducer” consumes these, groups them by key, and sums up the
counts in each group to produce the ﬁnal output. Notice how the ﬁrst stage can be paral-
lelized. That is, the mapper stage can consist of multiple mapper threads, each working
on its own separate portion of the input ﬁle. The reducer stage only contains a single
thread which runs concurrently with the mapper threads. The communication between
the mapper threads and the reducer thread occurs via a shared in-memory FIFO buffer.
Figure 1 summarizes these ideas.
2 Starter code: unsynchronized word-count
We are providing you starter code that implements a preliminary version of wordcount
that works correctly in the absence of multi-threading. To appreciate this, you should
conﬁrm that this code is able to pass the ”serialize” test in which accesses to the buffer
occur in a strictly serial manner (i.e., an operation is issued only upon the completion
of the previous operation). However, our implementation is not thread-safe. That is, it
has race conditions. You will need to enhance this code to create a thread-safe version of
3 What you need to implement
Take the time to skim all the starter source code ﬁles. All your implementation will
be conﬁned to the ﬁles helper.c and helper.h. Speciﬁcally, you need to consider
enhancing the following functions (recall that the provided versions of these functions
work correctly in the absence of multi-threding). Though we have implemented the
buffer_create function for you, you can enhance it to make any kind of initialization.
• state_t* buffer_create(int capacity): creates an in-memory bufferwith
the speciﬁed capacity in bytes). Returns a pointer to state_t, see deﬁnition in
helper.h. This function will be called once at the beginning, you can do any kind
of initialization in it based on your implementation.
• enum buffer_status buffer_send(state_t *buffer, void* data):
writes data to the buffer. In case the buffer is full, the function waits till the buffer
has space towrite the newdata. Returns BUFFER_SUCCESS for successfullywriting
data to the buffer, CLOSED_ERROR if the buffer is closed, and BUFFER_ERROR on
encountering any other error. The size of data can be found out using the function
• enum buffer_status buffer_receive(state_t* buffer, void** data):
Reads data fromthe given buffer and stores it in the function’s input parameter, data
(Note that it is a double pointer). This is a blocking call i.e., the function only returns
on a successful completion of receive In case the buffer is empty, the function waits
till the buffer has some data to read.
• enum buffer_status buffer_close(state_t* buffer): closes the buffer
and informs (you may think of giving signal) all the blocking send/receive calls to
return with CLOSED_ERROR. Once the buffer is closed, send/receive operations will
return CLOSED_ERROR. Returns BUFFER_SUCCESS if close is successful, CLOSED_ERROR
if the buffer is already closed, and BUFFER_ERROR for other errors.
• enum buffer_status buffer_destroy(state_t* buffer)
Frees all the memory allocated to the buffer , using own version of sem ﬂags The
caller is responsible for calling buffer_close and waiting for all threads to ﬁnish
their tasks before calling buffer_destroy Returns BUFFER_SUCCESS if destroy
is successful, DESTROY_ERROR if buffer_destroy is called on an open buffer,
and BUFFER_ERROR in any other error case
4 Programming rules
You are not allowed to take any of the following approaches to complete the assignment:
• Spinning in a polling loop to implement blocking calls.
• Sleeping instead of using condition-wait.
• Trying to change the timing of your code to overcome race conditions.
• Using global variables.
You are only allowed to use the pthreads library, the standard C library functions (e.g.,
malloc/free), and the provided starter code. If you think you have a valid reason to use
some code outside of these, please contact the course staff to determine its eligibility.
本网站支持 Alipay WeChatPay PayPal等支付方式
E-mail: firstname.lastname@example.org 微信号:vipnxx