By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

can you do this?

P: 1
hi all

i want a help about my project
this is the discribtion:

This assignment is to be done individually. You are to implement a multi-threaded solution for the bounded buffer producer-consumer problem. There will be one producer thread and multiple consumer threads. The items in the buffer will be inter values. The producer thread will produce (get) the buffer items by reading integers from standard input (stdin) and putting them into the buffer. The consumer threads will consume the buffer items by taking the them from the buffer and printing the integer being consumed. Use a Pthreads mutex lock for mutual exclusion and POSIX semaphores for synchronizing the number of full and empty buffers.
The producer thread should print a message for each item produced, showing the integer value and the slot number where it was placed in the buffer. The consumer threads should print a similar message for each integer consumed in addition to the thread ID that consumed the integer.

2. Implementation
2.1 Source Files
You should implement your program with two separate C language source files. The two source files will be separately compiled, and then the object files will be linked together to create a single executable module. The first source file should be named assign3.c. It will contain the code for the main thread, along with any necessary initialization and cleanup operations. The second file should be named prod_cons.c. It will contain the code for the producer thread routine and the consumer thread routine. The prod_cons.c file could also contain functions for initialization and cleanup that are called from the main program, if that seems the appropriate way to design the program. You will also have a header file named assign3.h which can be included in both assign3.c and prod_cons.c.

2.2 Creating and Terminating Threads
The main thread should create one producer thread and multiple consumer threads. The number of consumer threads that will be created should be controlled by defining a symbolic constant NUM_CONSUMERS. Compile and test your program with different values of NUM_CONSUMERS. When you submit your program for grading use a value of 5 for NUM_CONSUMERS.
The producer thread should terminate itself via pthread_exit() when it reaches end-of-file on the input file. The consumer threads will not be able to determine when to exit, so the main thread should sleep for 5 seconds after the producer thread terminates, and then cancel the consumer threads via pthread_cancel().
After all created threads have terminated or been cancelled, the main() thread terminates with a valid return (status) code.

2.3 Shared Memory
To simplify coding, it is recommended that you use global variables for sharing memory for the synchronization variables among the threads. By using global variables, it should not be necessary to pass any parameters to the producer routine. The parameters that need to be passed to the consumer routines is an integer specifying the number of the consumer thread (i.e., 1, 2, 3, etc.). The consumers will use the thread number passed to them to be used as an identifier in output messages from the thread.
When a global variable is used in a program that is created with multiple source files, only one of the source files that reference the variable should contain a declaration for the variable that causes storage to be allocated. The other source files that reference the global variable must declare the variable with the extern attribute. For example:
mod1.c mod2.c
int globvar; extern int globvar;
In this example, storage for globvar is allocated in mod1, and in mod2 it is declared as an external variable that is allocated in some other module (in this case, mod1.c).

2.4 Mutex Lock and Semaphores
Use a Pthreads mutex lock for mutual exclusion, and POSIX unnamed semaphores to synchronize full and empty buffer slots. The pthread_mutex_init(), pthread_mutex_destroy(), pthread_mutex_lock(), and pthread_mutex_unlock() functions are used to manipulate a Pthreads mutex lock. The sem_init(), sem_destroy(), sem_wait(), and sem_post() functions are used to manipulate POSIX semaphores. See examples in the textbook on pages 239-240.
2.5 Other Issues

a) Define the bounded buffer with 10 slots.

b) Insert a random sleep (using sleep() system call) period of 0-2 seconds between each item produced in the producer thread, and between each item consumed in the consumer threads. Use srand(getpid()) to seed the random number generator, and rand()%3 to generate random numbers in the range 0-2.

c) For all system functions referred to above, there are Unix man pages that describe their operation. Refer to the man pages to get detailed descriptions for each function. For example, execute the command: man pthread_exit to see the description of the function.

d) Get started on the assignment early and ask questions in class if you have some. You can bet that others in the class also have similar questions.

plz help me soon
May 10 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 8,434
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

May 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.