[Solved] SOLVED:Programming Assignment 3

30 $

File Name: SOLVED:Programming_Assignment_3.zip
File Size: 292.02 KB

SKU: [Solved] SOLVED:Programming Assignment 3 Category: Tag:

Or Upload Your Assignment Here:


In this assignment, you are to write a multi-threaded C program for a bounded-buffer producer/consumer application: file copying. You are required to use semaphores in your program to enforce mutually exclusive access to the buffer and to synchronize the producer and the consumer with regard to buffer slots/items. Make sure that your submitted program compiles and runs on Athena, the ECS Linux machine. 1. Your program should be run with two file names provided at the command line, say: infile and outfile. Otherwise, your program prompts the user with the message: “Correct Usage: prog3 infile outfile” and then terminates. 2. In your program, the main thread verifies the input, opens the infile, and creates the outfile. The main thread then spawns a producer thread and a consumer thread, and waits for both the producer and the consumer to finish before it terminates. The producer and the consumer share a buffer of 9 slots with each slot having a size of 18 bytes. The producer reads a string of 18-byte at a time from the infile and places it into the next available buffer slot. The consumer takes the next available string from a buffer slot and writes it into the outfile. The outfile thus is a verbatim copy of the infile. 3. The buffer can only be accessed in a mutually exclusive fashion, which is enforced through the use of a semaphore variable buf_lock and the sem_wait and sem_post operations. 4. When the buffer is full, the producer must wait until a buffer slot becomes free before it can place a string into it. When the buffer is empty, the consumer must wait for a string to be available. When the producer fills a slot, the consumer is notified of an item available; when the consumer empties a slot, the producer is notified of a slot available. These synchronization conditions between the producer and the consumer are facilitated by two additional semaphore variables slot_avail and item_avail, and the sem_wait and sem_post operations. 5. Because the order in which the semaphore wait operations (sem_wait) are executed can be a cause for deadlock, care must be exercised when using sem_wait. From the producer’s perspective, you want to check to see if there is a buffer slot available (through slot_avail) before attempting to gain exclusive access to the buffer (via buf_lock). Similarly, from the consumer’s standpoint, you need to make sure that there is at least an item (a string) in a buffer slot (via item_avail) before the consumer attempts to have exclusive control of the buffer (via buf_lock). 6. To be able to use semaphores in your program, include

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] SOLVED:Programming Assignment 3
30 $