The Buffer

Internally, the buffer will consist of a fixed-size array of type buffer_item (which will be defined using a typedef). The array of buffer_item objects will be manipulated as a circular queue. The definition of buffer_item, along with the size of the buffer, can be stored in a header file such as the following:

/* buffer.h */ typedef int buffer_item;
#define BUFFER_SIZE 5

The buffer will be manipulated with two functions, insert_item() and remove_item(), which are called by the producer and consumer threads, respectively. A skeleton outlining these functions appears as:

#include < buffer.h > /* the buffer */
buffer_item buffer[BUFFER_SIZE]; int insert_item(buffer_item item) { /* insert item into buffer return 0 if successful, otherwise
return -1 indicating an error condition */

int remove_item(buffer_item *item) {
/* remove an object from buffer placing it in item
return 0 if successful, otherwise
return -1 indicating an error condition */ }

The insert_item() and remove_item() functions will synchronize the producer and consumer using the algorithms outlined in Slide 42 and 43. The buffer will also require an initialization code section (which is part of the main() function) that initializes the mutualexclusion object mutex along with the empty and full semaphores.

The main()Function

The main() function will initialize the buffer and create the separate producer and consumer threads. Once it has created the producer and consumer threads, the main () function will sleep for a period of time and, upon awakening, will terminate the application. The main () function will be passed three parameters on the command line:

  • How long to sleep before terminating
  • The number of producer threads
  • The number of consumer threads

A skeleton for this function appears as:

int main(int argc, char *argv[]) {
/* 1. Get command line arguments argv[1], argv[2], argv[3]*/
/* 2. Initialize buffer */
/* 3. Create producer thread(s) */ /* 4. Create consumer thread(s) */
/* 5. Sleep */
/* 6. Exit */

Producer and Consumer Threads

The producer thread will alternate between sleeping for a random period of time and inserting a random integer into the buffer. Random numbers will be produced using the rand() function, which produces random integers between 0 and RAND_MAX. The consumer will also sleep for a random period of time and, upon awakening, will attempt to remove an item from the buffer. An outline of the producer and consumer threads appears as:

#include < stdlib.h > I/* required for rand() */
#include < buffer.h > void *producer(void *param) { buffer_item item;
while (TRUE) {
/* sleep for a random period of time */ sleep( ... );
/* generate a random number */ item = rand() ; if (insert_item(rand))
fprintf("report error condition"); else
printf("producer produced %dn",rand); }
void *consumer(void *param) { buffer_item item; while (TRUE) {
/* sleep for a random period of time */ sleep ( ... ) ; if (remove_item(&item)) fprintf("report error condition"); else
printf("consumer consumed %dn",item); }

Test Run

Test run your program, e.g., “./buffer 3 3 10”, and explain the running result. Please create a section called “Running result analysis” in a text file readme.txt and include both the running output and your explanation to the result in this section.

Mutex Locks

The following code sample illustrates how mutex locks available in the Pthread API can be used to protect a critical section:

#include < pthread.h >
pthread_mutex_t mutex;
/* create the mutex lock */
/* acquire the mutex lock */
/*** critical section ***/
/* release the mutex lock */

Pthreads uses the pthread_mutex_t data type for mutex locks. A mutex is created with the pthread_mutex_init(&mutex, NULL) function, with the first parameter being a pointer to the mutex. By passing NULL as a second parameter, we initialize the mutex to its default attributes. The mutex is acquired and released with the pthread_mutex_lock () and pthread_mutex_unlock () functions. If the mutex lock is unavailable when pthread_mutex_lock () is invoked, the calling thread is blocked until the owner invokes pthread_mutex_unlock ().All mutex functions return a value of 0 with correct operation; if an error occurs, these functions return a nonzero error code.

For Java, a utility class org.openide.util.Mutex is provided:


Pthreads provides two types of semaphores-named and unamed. For this assignment, we use unnamed semaphores. The code below illustrates how a semaphore is created:

#include < semaphore.h > sem_t sem;
/* Create the semaphore and initialize it to 5 */ sem_init(&sem, 0, 5);

The sem_init () creates and initializes a semaphore. This function is passed three parameters:

  • A pointer to the semaphore
  • A flag indicating the level of sharing
  • The semaphore's initial value

In this example, by passing the flag 0, we are indicating that this semaphore can only be shared by threads belonging to the same process that created the semaphore. A nonzero value would allow other processes to access the semaphore as well. In this example, we initialize the semaphore to the value 5.


You should ensure that all files used for the assignment sit in a directory called “assignment”. For C code, two files need to be in this directory: buffer.h, and buffer.c. For Java code, a containing the main() function must be in this directory, with other supporting classes/libs. In addition, a file readme.txt that includes a list of all the files in the directory and brief instructions on how to compile and run the program should also be put in this directory. Your answer to “Task 4: Test Run” should also be included in this file.

All code needs to be commented

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.