I need a help to solve this problem
I want to create a code that implement bounded buffer problem by
using semaphore.
so use int buffer[10]; // buffer is global variable and buffersize
is 10
and there is only one producer and one consumer.
1) Producer()
- At every 200msec, generates random positive integer and add to
the buffer.
- Then print current state of buffer (numbers and in/out
indices)
-Also print current state of two semaphore which are full and
empty.
- After producing 20 numbers, producer ends
2) Consumer()
-At every 170msec, removes one integer from the buffer.
-And then print current state of buffer (that is numbers and in/out
indices)
-Also print current state of two semaphore full and empty.
-After reading 20 numbers, consumer ends
So I want to use three semaphore that are full, empty, mutex and also use asleep() for delaying producer and consumer.
#include <iostream>
#include <windows.h>
#include <thread>
#include <time.h>//time(0)
#include<stdlib.h>
using namespace std;
int full = 0, mutex = 1, Empty = 10, buffer[10] = { 0 }, to_produce
= 20, to_consume = 20;
//initialize variables to_produce and to_consume are variables to
note quantity produced and consumed, don't confuse with Empty
void wait(int& S)//wait of semaphore
{
while (S <= 0) {}
S--;
}
void signal(int& S)//signal of semaphore
{
S++;
}
void Producer()//producer
{
do
{
Sleep(200);//every 200 ms
wait(Empty);//decreememt
Empty
wait(mutex);//decreememt
mutex
to_produce--;//decreement
to_produce
buffer[full] = rand();
cout << "\nProduced\t"
<< buffer[full] << endl;
cout << endl;
cout << "Full\t" <<
full+1 << endl;
cout << "Empty\t" <<
Empty << endl;
signal(mutex);//increement
mutex
signal(full);//increement
full
} while (to_produce > 0);
}
void Consumer()
{
do
{
Sleep(170);//every 170 ms
wait(full);//decreememt full
wait(mutex);//decreememt
mutex
to_consume--;//decreememt
to_consume
cout << "\nConsumed\t"
<< buffer[full] << endl;
buffer[full] = 0;//reset
buffer[full]
cout << endl;
cout << "Full\t" <<
full << endl;
cout << "Empty\t" <<
Empty+1 << endl;
signal(mutex);//increement
mutex
signal(Empty);//increement
Empty
} while (to_consume > 0);
}
int main()
{
srand(time(0));
thread producer(Producer);
thread consumer(Consumer);
producer.join();
cout << "\nproducer ended\n";
consumer.join();
cout << "\nconsumer ended\n";
}
COMMENT DOWN FOR ANY QUERY
PLEASE GIVE A THUMBS UP
I need a help to solve this problem I want to create a code that implement bounded buffer problem by using semaphore. so...
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,...
Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows: 1.The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through...
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...
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); } } Instructions Programming Assignment Four In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of...
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...
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...
I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...
I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...
I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...
Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...