Question: Project 1 Designing a thread pool Thread pools were introduced in Section Thread pools. When thread pools are used, a task is submitted to the
Project Designing a thread pool Thread pools were introduced in Section Thread pools. When thread pools are used, a task is submitted to the pool and executed by a thread from the pool. Work is submitted to the pool using a queue, and an available thread removes work from the queue. If there are no available threads, the work remains queued until one becomes available. If there is no work, threads await notification until a task becomes available. This project involves creating and managing a thread pool, and it may be completed using either Pthreads and POSIX synchronization or Java. Below we provide the details relevant to each specific technology. I. POSIX The POSIX version of this project will involve creating a number of threads using the Pthreads API as well as using POSIX mutex locks and semaphores for synchronization. The client Users of the thread pool will utilize the following API: void poolinitInitializes the thread pool. int poolsubmitvoid somefunctionvoid p void pwhere somefunction is a pointer to the function that will be executed by a thread from the pool and p is a parameter passed to the function. void poolshutdownvoidShuts down the thread pool once all tasks have completed. We provide an example program client.c in the source code download that illustrates how to use the thread pool using these functions. Implementation of the thread pool In the source code download we provide the C source file threadpool.c as a partial implementation of the thread pool. You will need to implement the functions that are called by client users, as well as several additional functions that support the internals of the thread pool. Implementation will involve the following activities: The poolinit function will create the threads at startup as well as initialize mutualexclusion locks and semaphores. The poolsubmit function is partially implemented and currently places the function to be executedas well as its data into a task struct. The task struct represents work that will be completed by a thread in the pool. poolsubmit will add these tasks to the queue by invoking the enqueue function, and worker threads will call dequeue to retrieve work from the queue. The queue may be implemented statically using arrays or dynamically using a linked list The poolinit function has an int return value that is used to indicate if the task was successfully submitted to the pool indicates success, indicates failure If the queue is implemented using arrays, poolinit will return if there is an attempt to submit work and the queue is full. If the queue is implemented as a linked list, poolinit should always return unless a memory allocation error occurs. The worker function is executed by each thread in the pool, where each thread will wait for available work. Once work becomes available, the thread will remove it from the queue and invoke execute to run the specified function. A semaphore can be used for notifying a waiting thread when work is submitted to the thread pool. Either named or unnamed semaphores may be used. Refer to Section POSIX semaphores for further details on using POSIX semaphores. A mutex lock is necessary to avoid race conditions when accessing or modifying the queue. Section POSIX mutex locks provides details on Pthreads mutex locks. The poolshutdown function will cancel each worker thread and then wait for each thread to terminate by calling pthreadjoin Refer to Section Thread cancellation for details on POSIX thread cancellation. The semaphore operation semwait is a cancellation point that allows a thread waiting on a semaphore to be cancelled. Refer to the sourcecode download for additional details on this project. In particular, the README file describes the source and header files, as well as the Makefile for building the project. II Java The Java version of this project may be completed using Java synchronization tools as described in Section Links to an external site.. Synchronization may depend on either a monitors using synchronizedwaitnotifySection Java monitors or b semaphores and reentrant locks Section Reentrant locks and Section Semaphores Java threads are described in Section Java threads. Implementation of the thread pool Your thread pool will implement the following API: ThreadPoolCreate a defaultsized thread pool. ThreadPoolint sizeCreate a thread pool of size size. void addRunnable taskAdd a task to be performed by a thread in the pool. void shutdownStop all threads in the pool. We provide the Java source file ThreadPool.java as a partial implementation of the thread pool in the source code download. You will need to implement the methods that are called by client users, as well as several additional methods that support the internals of the thread pool. Implementation will involve the following activities: The constructor will first create a number of idle t
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
