CSci 4061: Introduction to Operating Systems, Spring 2024 Project #3: MultiThreaded Image Matching Server Intermediate submission due: 11:59pm (CDT), 4, 4, 2023 Final submission due: 11:59pm (CDT), 4. 12, 2023 1. Background The purpose of this lab is to construct a multithreaded client and a multithreaded server usingPOSIX threads (pthreads) in the C language to learn about thread programming and synchronizationmethods. In this project, we will use multithreading to improve the performance of a server that isprogrammed to accept an image from the user, match it against a database of known images, and return the closest matching image. In this programming assignment we will be using the dispatcher-workermodel of threads. There is both concurrency and parallelism at play (the latter if the server is running on a multicore system). Note: even if threads are dispatched to different cores, they still have direct access to all of the process memory.The purpose of this programming assignment is to get you started with thread programming and synchronization. You need to be familiar with POSIX threads, mutex locks and condition variables.2. Project Overview Your project will be composed of two types of threads: dispatcher thread and worker threads.The purpose of the dispatcher threads is to repeatedly accept an incoming connection, read the client request from the connection, and place the request in a queue. We will assume that there will only be one request per incoming connection. The purpose of the worker threads are to monitor the request queue, retrieve requests (in the form. of an input image) and read the image into memory (ie. get the bytes of the image), match the image against a database of images, and serve the best or closest matching image back to the user. The queue is a bounded buffer and will need to be properly synchronized. All client-server communication is implemented for you.3. Server Overview Your server should create a fixed pool of worker and dispatcher threads when the program starts. The worker thread pool size should be num_worker (you can assume that the number of worker threads will be less than the number of requests) and dispatcher thread should be of size num_dispatcher. Your server should bring the database of images into memory when the server starts up.3.1 Server Database: ● The database is a directory filled with images. These images are utilized for comparing input images received from clients, with the closest match subsequently returned to the respective client. It is imperative to load this database into memory upon the server's startup to ensure efficient access during the matching process.3.2 Request Queue Structure: ● Request Queue Structure: Each request inside the queue will contain an image (i.e. a stream of bytes) sent from the client via an image file they specify and file descriptor of where to send the best matching image back. You may use a struct to hold this data before adding it to the queue. The queue structure is up to you. You can implement it as a queue of structs or a linked list of structs, or any other data structure you find suitable.3.3 Dispatcher Thread The purpose of the dispatcher threads is to repeatedly accept an incoming connection, read the client request from the connection (i.e. the image contents), and place the request in a queue. We will assume that there will only be one request per incoming connectio代 写CSci 4061: Introduction to Operating Systems, Spring 2024 Project #3R n. You will use locks and condition variables (discussed Thursday) to synchronize this queue (also known as a bounded buffer). The queue is of fixed size. ● Queue Management: The identified image stream of bytes are added to the request queue along with a file descriptor of where to send the image back. This queue is shared with the workers.● Signaling New Request: Once a request is added to the request queue, the dispatcher thread will signal to all of the worker threads that there is a request in the queue.● Full Queue: Once the queue is full, the dispatcher thread will wait for a signal from any worker thread that there is a space in the queue.● Network Functions the dispatcher will call:○ int socketfd = accept_connection (): returns a file descriptor which should be stored in the queue○ Char * buffer = get_request(int socketfd, size_t *size): Takes the file descriptor as the first argument, and takes a size_t pointer as a second argument which will be set by this function. Returns a char * with the raw image bytes.3.6 Worker Threads The worker threads are responsible for monitoring the request queue, retrieving requests,comparing images from the database with the request image, and serving the best image back to the user. Here's a breakdown of its functionality:● Parameters: The worker thread will take a threadID as a parameter (0, 1, 2, …) which will later be used for logging. You can assign the threads an ID in the order the threads are created. Note that this thread ID is different from the pthread_id assigned to the thread by the pthread_create() function. ● Queue Monitoring: Worker threads continuously monitor the shared request queue. When a new request arrives from the dispatcher thread, one of the worker threads retrieves it for further processing.● Request Handling: Once a request is obtained, a worker thread will compare against the in-memory copy of the database for the best matching image.● Response to request: After finding the image, the worker thread prepares the image to be served back to the user by sending the image bytes. The client then writes the returned image into a file. An example would be: input file is foobar.png output file could be foobar_similar.png.● Empty Queue: Once the queue is empty, the worker thread will wait for a signal from any dispatcher thread that there are now requests in the queue.● Synchronization: Proper synchronization mechanisms such as mutex locks and condition variables are used to ensure that multiple worker threads can safely access and modify shared data structures (queues) and other global variables without race conditions or deadlocks.● Network Function the worker will make: ○ database_entry_t image_match(char *input_image, int size):○ send_file_to_client(int socketFd, char *buffer, int size): Takes the client file descriptor, the matching image memory block, and its size.3.8 Request Logging The worker threads must carefully log each request to a file called “server_log” and also to the terminal (stdout) in the format below. The log file should be created in the same directory where the final executable “server” exists. You must also protect the log file from race conditions. The format is:[threadId][reqNum][fd][Request string][bytes/error] ● threadId is an integer from 0 to num_workers - 1 indicati WX:codehelp