Unformatted text preview:

CS 105 Fall 2005 Ring Buffer November 27 2005 1 Introduction A ring buffer also called a circular buffer is a common method of sharing information between a producer and a consumer In class we have seen an elegant implementation of a ring buffer using semaphores In this lab you will implement a simple producer consumer program without using semaphores In so doing you will learn some of the basics of synchronization and threads As usual you are to work in groups of two It is an Honor Code violation to work on the program without your partner present 2 Specifications You are to write a program named ringbuf c from scratch Be sure to document the names of both team members in comments at the top of the file Your program must be implemented using POSIX threads There will be two threads a producer and a consumer The producer will read information from standard input see below and place it into the ring buffer The consumer will extract information from the buffer and perform certain operations You may NOT use semaphores of any type to implement your solution This includes implementing a semaphore construct yourself by building on more primitive thread constructs The main program must create a thread to run the consumer and then call the producer directly After the consumer terminates the main thread should collect it with pthread join and then exit Alternatively the main program could create and collect two new threads However the consumer thread must be created first or your output might not match our test cases All library and system calls should be error checked If an error occurs print an informative message and terminate the program 1 2 1 The Shared Buffer The producer and consumer will communicate through a shared buffer that has 10 slots this should be given by a define Each slot in the buffer has the following structure struct message int value int consumer sleep int line int print code int quit Value to be passed to consumer Time in ms for consumer to sleep Line number in input file Output code see below NZ if consumer should exit These fields have the following purposes value The actual data to be passed to the consumer in this example the consumer will sum the values passed in consumer sleep A time expressed in milliseconds that the consumer will expend in consuming the buffer entry line The line number in the input file that this data came from Line numbers start at 1 print code A code indicating whether the consumer should print a status report after consuming this line quit For all buffer entries except the last this value should be zero For the last entry it should be nonzero The consumer should not look at any of the other fields in the message if quit is nonzero Besides the shared buffer itself you will need a number of auxiliary variables to keep track of the buffer status These might include things such as the index of the next slot to be filled or emptied You will also need some pthreads conditions and mutexes The exact set is up to you 2 2 The Producer The basic task of the producer is to read one line at a time from the standard input For each line it will sleep for a time given in the line and then pass the data to the consumer via the ring buffer Finally after the message has been placed in the ring buffer the producer will optionally print a status message Since printing is slow the producer must not hold any mutexes while it is printing Each input line consists of four numbers as follows The value to be passed to the consumer 2 An amount of time the producer should sleep given in milliseconds Note that the sleep must be done before placing information in the ring buffer An amount of time the consumer should sleep given in milliseconds A print code indicating what sorts of status lines should be printed You can read these four numbers using the C library function scanf see man scanf for more information When scanf returns an EOF indication your program should enter one more message in the ring buffer without sleeping first This message should contain a nonzero quit field the other fields will be ignored The print codes are interpreted as follows 0 No messages are printed for this input line 1 The producer generates a status message 2 The consumer generates a status message 3 Both the producer and consumer generate status messages The producer s status message should be generated after the data has been passed to the consumer It must be produced by calling printf with the following format argument Produced d from input line d n 2 3 The Consumer The consumer waits for messages to appear in the buffer extracts them and then executes them Note that the consumer does not act on the message until after it has been removed from the buffer so that the producer can continue to work while the consumer is processing the message If the extracted message has a nonzero quit field the consumer prints the total it has calculated using the following printf format Final sum is d n It then terminates its thread Otherwise the consumer sleeps for the specified time adds the value field to a running total initialized to zero and optionally prints a status message if the print code is 2 or 3 The status message must be generated by calling printf with the following format argument Consumed d from input line d sum d n 3 3 Useful Information You will need to make use of a number of Unix system and C library calls You can read the documentation on these calls by using man For example to learn about pthread mutex lock type man 3 pthread mutex lock The 3 specifies that the manual page should come from section 3 of the manual which describes the C library You can usually omit it but sometimes man will give you the wrong manual page and you have to be explicit The calls you will need to use are all documented in sections 2 and 3 of the manual 3 1 Downloading As usual the lab is available by downloading a tar file Unpacking the file with tar xvf ringbuf tar will create a subdirectory named ringbuf containing the writeup and test files 3 2 Pthreads Features You will need to familiarize yourself with the following pthreads functions at a minimum pthread create pthread join pthread mutex lock pthread mutex unlock pthread cond wait pthread cond signal You may choose to use other functions as well Remember that you are NOT allowed to use the pthreads semaphore functions sem 3 3 Sleeping For historical reasons there are many ways to get a thread to go to sleep for a specified time period The


View Full Document

Harvey Mudd CS 105 - Ring Buffer

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Loading Unlocking...
Login

Join to view Ring Buffer and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Ring Buffer and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?