Ask AI on The Internet
Question: In this question, we are going to pretend that we have a web server. Write a program in C++ to simulate a web server. The web server should receive requests from clients about web pages and the web server returns the request web pages. Your C++ program should create one thread that receive the requests from the clients and n threads that will search for the requested pages and send them to the client. This problem can be implemented using the producer-consumer problem. The requests from the client are going to be a structure of the following fields: struct reqestStructure { int request_id; string ip_address; string page_requested; }; The msg_queue is defined as: queue<requestStructure> msg_queue; Create a list of 10 web pages names as: string webPages[10] = {"google.com", "yahoo.com", ...}; The program consists of two functions: 1) first function is called listen: that will do the following steps: sleep for random number between 1-3 seconds using sleep_for instruction. generate a request with request_id from a counter shared, ip_address as an empty string and page_request as a random web page name from the array webPages defined above; then save the request in msg_queue. notify one thread to consume it. 2) second function is called do_request that will do the following steps: if the msg_queue is empty, then wait using conditional variable else get the request from the msg_queue display a message; "thread thread_id completed request request_id requesting webpage page_requested"; where thread_id is the id number of the thread, the request_id is the id number of the request and page_requested as the web page requested.
Here's a C++ program implementing the web server simulation as per your description: ```cpp #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> #include <string> #include <chrono> #include <ctime> #include <cstdlib> #include <vector> using namespace std; struct requestStructure { int request_id; string ip_address; string page_requested; }; queue<requestStructure> msg_queue; mutex mtx; condition_variable cond_var; bool done = false; string webPages[10] = {"google.com", "yahoo.com", "bing.com", "stackoverflow.com", "medium.com", "ask.com", "amazon.com", "github.com", "linkedin.com", "stackoverflow.com"}; void listen(int num_requests) { for (int i = 0; i < num_requests; ++i) { this_thread::sleep_for(chrono::seconds(rand() % 3 + 1)); requestStructure request; request.request_id = i + 1; request.ip_address = ""; request.page_requested = webPages[rand() % 10]; unique_lock<mutex> lock(mtx); msg_queue.push(request); lock.unlock(); cond_var.notify_one(); } done = true; } void do_request(int thread_id) { while (!done || !msg_queue.empty()) { unique_lock<mutex> lock(mtx); cond_var.wait(lock, [](){ return !msg_queue.empty(); }); requestStructure request = msg_queue.front(); msg_queue.pop(); lock.unlock(); cout << "Thread " << thread_id << " completed request " << request.request_id << " requesting webpage " << request.page_requested << endl; } } int main() { srand(static_cast<unsigned>(time(0))); const int num_threads = 4; const int num_requests = 10; vector<thread> threads; for (int i = 0; i < num_threads; ++i) { threads.push_back(thread(do_request, i + 1)); } // Producer thread producer(listen, num_requests); producer.join(); for (auto& t : threads) { t.join(); } return 0; } ``` This program simulates a web server that receives client requests for web pages and returns the requested pages using multiple threads. The producer function `listen` generates requests at random intervals and adds them to the shared queue, while the consumer functions `do_request` process the requests and display the results.
A new large language model has been made available since this question was asked.
To generate an updated answer from the new model, click the button below.
Think your answer is better than the AI's? Post it below.
Other Most Relevant Posts:

Question Tags
If you want your question answered by an AI, click here.
Post your own comment: