Is This Correct?

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);
}
}
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
Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining...
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 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 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 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 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 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 .
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,...
Java Programming: Make a Java program with two processes, a producer and a consumer. If you want to use another language, clear it with me first. The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random...
I NEED HELP WITH THIS HOMEWORK!!!! What is a characteristic of a bag data type? Elements are added and removed in any order Elements are removed in the same order they were added Distinct elements are added in any order but are removed beginning with the last one added Distinct elements are removed in the reverse order they were added Which scenario illustrates a data stack structure Standing in a line to be serviced Filing documents in alphabetical order Offering...
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...