DOC PREVIEW
Harvey Mudd CS 105 - Ring Buffer

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 105, Fall 2005Ring BufferNovember 27, 20051 IntroductionA ring buffer, also called a circular buffer, is a common method of sharinginformation between a producer and a consumer. In class, we have seen anelegant implementation of a ring buffer using semaphores.In this lab, you will implement a simple producer/consumer program withoutusing semaphores. In so doing, you will learn some of the basics of synchroniza-tion and threads.As usual, you are to work in groups of two. It is an Honor Code violationto work on the program without your partner present.2 SpecificationsYou are to write a program named ringbuf.c from scratch. Be sure to doc-ument the names of both team members in comments at the top ofthe file.Your program must be implemented using POSIX threads. There will betwo threads: a producer and a consumer. The producer will read informationfrom standard input (see below) and place it into the ring buffer. The consumerwill 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 moreprimitive thread constructs.The main program must create a thread to run the consumer, and then callthe producer directly. After the consumer terminates, the main thread shouldcollect it with pthread_join and then exit. (Alternatively, the main programcould create and collect two new threads. However, the consumer thread mustbe created first or your output might not match our test cases.)All library and system calls should be error-checked. If an error occurs, printan informative message and terminate the program.12.1 The Shared BufferThe producer and consumer will communicate through a shared buffer that has10 slots (this should be given by a #define). Each slot in the buffer has thefollowing structure:struct message{int value; /* Value to be passed to consumer */int consumer_sleep; /* Time (in ms) for consumer to sleep */int line; /* Line number in input file */int print_code; /* Output code; see below */int quit; /* 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 con-sumer will sum the values passed in.consumer sleep A time (expressed in milliseconds) that the consumer will ex-pend in consuming the buffer entry.line The line number in the input file that this data c ame from. Line numbersstart at 1.print code A code indicating whether the consumer should print a status re-port after consuming this line.quit For all buffer entries except the last, this value should be zero. For thelast entry, it should be nonzero. The consumer should not look at any ofthe other fields in the mess age if quit is nonzero.Besides the shared buffer itself, you will need a number of auxiliary variablesto keep track of the buffer status. These might include things such as the indexof 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 ProducerThe basic tas k of the producer is to read one line at a time from the standardinput. For each line, it will sleep for a time given in the line, and then pass thedata to the consumer via the ring buffer. Finally, after the message has beenplaced in the ring buffer, the producer will optionally print a s tatus message.Since printing is slow, the producer must not hold any mutexes while it isprinting.Each input line consists of four numbers, as follows:• The value to be passed to the consumer.2• An amount of time the pro ducer should sleep, given in milliseconds. Notethat the s leep 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 “manscanf” for more information.When scanf returns an EOF indication, your program should enter onemore message in the ring buffer, without sleeping first. This message shouldcontain 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 me ss ages .The producer’s status message should be generated after the data has beenpassed to the consumer. It must be produced by calling printf with the fol-lowing format argument:"Produced %d from input line %d\n"2.3 The ConsumerThe consumer waits for messages to appear in the buffer, extracts them, andthen executes them. Note that the consumer do es not act on the message untilafter it has been removed from the buffer, so that the producer can continue towork while the consumer is processing the message.If the extracted message has a nonzero quit field, the consumer prints thetotal 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 fieldto a running total (initialized to zero), and optionally prints a status messageif the print_code is 2 or 3. The status message must be generated by callingprintf with the following format argument:"Consumed %d from input line %d; sum = %d\n"33 Useful InformationYou will need to make use of a number of Unix system and C library calls. Youcan read the documentation on these calls by using “man”. For example, tolearn 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 youwill need to use are all documented in sections 2 and 3 of the manual.)3.1 DownloadingAs usual, the lab is available by downloading a tar file. Unpacking the file with“tar xvf ringbuf.tar” will create a subdirectory named ringbuf containingthe writeup and test files.3.2 Pthreads Fe atur esYou will need to familiarize yourself with the following pthreads functions, at aminimum:• pthread_create• pthread_join• pthread_mutex_lock• pthread_mutex_unlock• pthread_cond_wait• pthread_cond_signalYou may choose to use other functions as well. Remember


View Full Document

Harvey Mudd CS 105 - Ring Buffer

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Ring Buffer
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?