Question

Is This Correct?

Instructions Programming Assignment Four In computing, the producer-consumer problem (also known as the bounded-buffer proble

semaphore fillCount = 0;
// Items produced semahore emptyCount = BUFFER_SIZE;
// remaining space procedure producer()
{   
While (true)
{   
item = produceItem();
down(emptyCount);
// emptyCount is decremented
putItemIntoBuffer(item);
up(fillCount);
// fillcount is incremented
}

}
procedure consumer()
{
While (true)
{   
down(fillCount);
item = removeItem();
up(emptyCount);
// emptyCount is incremented
consumeItem(item);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There is just one mistake in your code. You should use one binary semaphore lock whose initial value is 0, so that any process before changing the value of fillCount and emptyCount, he should acquire the lock and then release lock at the end.

The problem here in your code is that , there is possibility that producer process can just down the emptyCount and they consumer process is swapped into and he get wrong value of emptyCount.

So in order to handle this critical section problem add a binary semaphore lock so your final code will be

binary semaphore lock = 0

semaphore fillCount = 0;
// Items produced semahore emptyCount = BUFFER_SIZE;
// remaining space procedure producer()
{

While (True) /
{   

While(Test_And_Set(lock)); //loop till lock is not 0
item = produceItem();
down(emptyCount);
// emptyCount is decremented
putItemIntoBuffer(item);
up(fillCount);
// fillcount is incremented

lock = 0; //release the lock
}
}


procedure consumer()
{
While (true)
{   

While (Test_And_Set(lock)); //loop till lock is not 0
down(fillCount);
item = removeItem();
up(emptyCount);
// emptyCount is incremented
consumeItem(item);
lock =0; //release the lock
}
}

Now the code is correct

Add a comment
Know the answer?
Add Answer to:
Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to...

    Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the  produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...

  • 1) Producer/Consumer again Consider the bounded buffer Producer/Consumer problem we discussed in class. Assume the buffer...

    1) Producer/Consumer again Consider the bounded buffer Producer/Consumer problem we discussed in class. Assume the buffer size is now 10. Assume the Producer process enters 3 items at a time into the buffer. It will only do this if there are at least 3 empty spots in the buffer. Assume the Consumer process will remove items two at a time, and will only do so if there are at least two items in the buffer. In addition to the Producer...

  • Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multit...

    Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is a common problem that requires cooperating processes or...

  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is...

  • write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size...

    write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size of one thousand. Allow the Producer to generate one million items. Use ten consumers. The program needs to perform a normal exit process after all items are consumed. Both the Producer (singular) and Consumers are to be runs as separate processes generated via fork(). The program must us Linux semaphores. The program must clean up the semaphores used and zombies created before termination. Report...

  • I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I...

    I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I need a source comment next to the line that gives the delay so i can compare please. (Concurrency – Semaphores) 1.- In this assignment you will implement a deadlock free variant of the bounded-buffer producer/consumer using jBACI (C - -). C- - is a subset of C + + that allows you to declare semaphores and apply the operations P and V. In the...

  • operating system engineering , please read the question and solve on the given skeleton code ....

    operating system engineering , please read the question and solve on the given skeleton code . Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem: A bounded buffer is simply a global integer array of size N (2) which can be accessed by multiple threads. • Create two types of threads - Producer (2) and Consumer (2). Producers will write to the buffer, and the consumers will read the buffer. In this scenario,...

  • Question III This question carries 20% of the marks for this assignment. Given the following mix...

    Question III This question carries 20% of the marks for this assignment. Given the following mix of tasks, task lengths and arrival times, compute the completion [5 marks and response time time from the arrival to the finish time) (5 marks for each task, along with the average response time for the FIFO. RR and SJF algorithms. Assume a time slice of 10 milliseconds and that all times are in milliseconds. You are kindly asked to provide the Gantt Chart...

  • 1. Consider a grocery supermarket planning to computerize their inventory management. The items on shelves will...

    1. Consider a grocery supermarket planning to computerize their inventory management. The items on shelves will be marked with Radio Frequency Identification (RFID) tags and a set of RFID reader-devices will be installed for monitoring the movements of the tagged items. Each tag carries a 96-bit EPC (Electronic Product Code) with a Global Trade Identification number, which is an international standard. The RFID readers are installed on each shelf on the sales floor. The RFID system consists of two types...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT