Screen shorts of program:
![# include<iostream» #include <thread» #include <stdlib.h> # include<time . h> #include <sstream.>//char* to int convert10n #include <mutex> #include <Chrono> #include <condition variable> using namespace std; int *buffer, bufferSize -0, counter0 int counterLimit; std: :mutex mut, cout_mut; std: : condition variable condition: int n 0;//n for number of elements in buffer //remove data from buffer int remove () while (true) std: unique lock<std: :mutex> lock (mut) condition.wait (lock, [1(return n > 0) /remove data from buffer int back buffer[--n]; //unlock mutex lock.unlock () condition.notify_all () return back; //insert data into buffer void insert (int data) while (true) std: :unique_lock<std: :mutex> lock (mut); condition.wait (lock, [1(return n < bufferSize;); //add data into buffer buffer [n]=data; //unlock mutex lock.unlock () condition.notify_all () return;](http://img.homeworklib.com/questions/06098a40-ca20-11ea-b4f9-13bdf75f4e9b.png?x-oss-process=image/resize,w_560)
![void producer( //After counter reaches its limit, both threads should be terminated while (counter<counterLimit) //generates a random number int data std:: rand() % 10; //inserts this into buffer insert (data) cout mut.lock (); //prints the number std: :cout << Producer has produced data: <<data << std::endl; std::this thread: :sleep for (std: :chrono: :milliseconds (50) cout mut.unlock ) //Increment counter countertt // Use current time as seed for random generator void consumer () //After counter reaches its limit, both thread should be terminated while (counter<counterLimit) //takes a number in the proper order int dataremove () cout mut.lock (); //prints data std: :cout << Consumer has consumed data: << data << std: :endl; std::this_thread::sleep_for (std::chrono: :milliseconds (50)); cout mut.unlock ) //increament counter countertt int main (int argc, char *argv[]) f (argc < 2) cout<<nNo Extra Command Line Argument Passed. Enter two numbers on the command line ; return 0](http://img.homeworklib.com/questions/066a4800-ca20-11ea-a740-43c857b5b430.png?x-oss-process=image/resize,w_560)
![istringstream iss (argv [1]) //set buffersize iss>> bufferSize; istringstream is (argv [2]) //set counter limit is > counterLimit; //Initialise buffer buffer new int [bufferSize]; //create thread for producer thread threadProducer (producer) //create thread for consumer thread threadconsumer (consumer); Wait for the threads to finish Wait for thread tl to finish threadProducer.join() threadConsumer.join)i //system (pause) Wait for thread t2 to finish return 0](http://img.homeworklib.com/questions/06c3e780-ca20-11ea-8b32-119b9dad283b.png?x-oss-process=image/resize,w_560)
Sample output:

Code to copy:
#include<iostream>
#include <thread>
#include <stdlib.h>
#include<time.h>
#include <sstream>//char* to int convertion
#include <mutex>
#include <chrono>
#include <condition_variable>
using namespace std;
int *buffer, bufferSize = 0, counter = 0;
int counterLimit;
std::mutex mut, cout_mut;
std::condition_variable condition;
int n = 0;//n for number of elements in buffer
//remove data from buffer
int remove()
{
while (true)
{
std::unique_lock<std::mutex> lock(mut);
condition.wait(lock, []{return n > 0;});
//remove data from buffer
int back = buffer[--n];
//unlock mutex
lock.unlock();
condition.notify_all();
return back;
}
}
//insert data into buffer
void insert(int data)
{
while (true) {
std::unique_lock<std::mutex> lock(mut);
condition.wait(lock, []{return n < bufferSize;});
//add data into buffer
buffer[n]=data;
n++;
//unlock mutex
lock.unlock();
condition.notify_all();
return;
}
}
void producer()
{
//After counter reaches its limit, both threads should be terminated
while(counter<counterLimit)
{
//generates a random number
int data = std::rand() % 10;
//inserts this into buffer
insert(data);
cout_mut.lock();
//prints the number.
std::cout << "Producer has produced data: " << data << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cout_mut.unlock();
//Increment counter
counter++;
}// Use current time as seed for random generator
}
void consumer()
{
//After counter reaches its limit, both thread should be terminated
while(counter<counterLimit)
{
//takes a number in the proper order
int data = remove();
cout_mut.lock();
//prints data
std::cout << "Consumer has consumed data: " << data << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cout_mut.unlock();
//increament counter
counter++;
}
}
int main(int argc, char *argv[])
{
if (argc <= 2)
{
cout<<" No Extra Command Line Argument Passed. Enter two numbers on the command line ";
return 0;
}
istringstream iss(argv[1]);
//set buffersize
iss>> bufferSize;
istringstream is(argv[2]);
//set counter limit
is >> counterLimit;
//Initialise buffer
buffer = new int[bufferSize];
//create thread for producer
thread threadProducer(producer);
//create thread for consumer
thread threadConsumer(consumer);
// Wait for the threads to finish
// Wait for thread t1 to finish
threadProducer.join();
// Wait for thread t2 to finish
threadConsumer.join();
//system("pause");
return 0;
}
Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use...
I need to creatre a C++ program that can modify the producer and consumer. My main programmig language is 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 main...
You are given a sample code that when edited/ improved/completed -will work as a producer consumer synchronized solution. In addition to the counter, you will implement a circular linked list as a buffer of size 200 that stores data items 0 or 1. The producer and consumer take *random* turns to produce and consume- *random* numbers of elements ( turning 1 to a 0 and visa-versa-each time, as discussed in class). After each turn of the producer and/or consumer, your...
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...
Producer and Consumer Code in C++ The program will contain two methods- one each for the producer and consumer. Both methods share access to an integer array buffer of size 50 with all values initialized to 0 at the beginning and an integer variable counter initialized to 0. As given below, the producer accesses the buffer elements and updates the element to 1 ( indicating production). The consumer changes a buffer elements to 0 (indicating consumption). Enhancements and modifications: You...
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...
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,...
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...
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;...
Multi-threaded programming help!!! Can anyone solve this
problem? It has to be written in C, runs on Linux.
his time you need to rewrite a multithreaded Pthread program that works with sleep() or pthread_join() in order to print out Fibonacci sequences properly. Create a folder name, WK6 on your class Linux machine when you work. gcc assignment3.c-Wall -Werror -pthread You are required to develop your own program using C with the following information Need to declare a function that receives...
Write a C program that should run on Linux platform using gcc
compiler. You are required to
simulate threads creation and termination behavior by using POSIX
threads library.
Input:
In the main program, first take the value for total number of
threads and then ask user to provide
the arrival time and CPU time (i.e. running time) for each
thread.
Output:
Simulate the behavior of threads arrival, working and termination
at a specific time interval (i.e.
500ms).
Requirements:
i. Name...