C programming work -- 2
Budget: ₹12,500 – ₹37,500 INR
File Structure:
You will work with the provided producer_template.c and consumer_template.c source files. Rename them to producer.c and consumer.c after writing your code.
Do not modify the low-level functions for reading from and writing to shared memory; use the provided functions.
Compilation:
Compile your programs using the following commands:
Copy code
gcc producer.c -lrt -o producer
gcc consumer.c -lrt -o consumer
Producer Program:
Accept three command-line arguments as integers:
bufSize: Size of the bounded buffer (2 to 500).
itemCnt: Number of items to produce/consume (greater than 0).
randSeed: Seed for the random number generator.
Create a shared memory block with a fixed size of 4K, named OS_HW1_yourName.
Initialize the shared memory block's header with:
bufSize
itemCnt
in: Index of the next item to be produced
out: Index of the next item to be consumed
Fork a child process and load the consumer executable into it.
Use the Producer() function to generate random integers (5 to 2500) and write them into the bounded buffer. If the buffer is full, wait until the consumer consumes an item.
Consumer Program:
Open and map the shared memory block created by the producer, named OS_HW1_yourName.
Read the four integer values from the header.
Read all the items written by the producer to the shared memory buffer (itemCnt). If the buffer is empty, wait until the producer produces more items.
Coding Guidelines:
Follow the provided pseudo-code for bounded-buffer producer/consumer.
Use provided functions for reading and writing shared variables.
Utilize the GetRand() function for generating random numbers.
You will work with the provided producer_template.c and consumer_template.c source files. Rename them to producer.c and consumer.c after writing your code.
Do not modify the low-level functions for reading from and writing to shared memory; use the provided functions.
Compilation:
Compile your programs using the following commands:
Copy code
gcc producer.c -lrt -o producer
gcc consumer.c -lrt -o consumer
Producer Program:
Accept three command-line arguments as integers:
bufSize: Size of the bounded buffer (2 to 500).
itemCnt: Number of items to produce/consume (greater than 0).
randSeed: Seed for the random number generator.
Create a shared memory block with a fixed size of 4K, named OS_HW1_yourName.
Initialize the shared memory block's header with:
bufSize
itemCnt
in: Index of the next item to be produced
out: Index of the next item to be consumed
Fork a child process and load the consumer executable into it.
Use the Producer() function to generate random integers (5 to 2500) and write them into the bounded buffer. If the buffer is full, wait until the consumer consumes an item.
Consumer Program:
Open and map the shared memory block created by the producer, named OS_HW1_yourName.
Read the four integer values from the header.
Read all the items written by the producer to the shared memory buffer (itemCnt). If the buffer is empty, wait until the producer produces more items.
Coding Guidelines:
Follow the provided pseudo-code for bounded-buffer producer/consumer.
Use provided functions for reading and writing shared variables.
Utilize the GetRand() function for generating random numbers.