Attached is project 2. Please use project 2 in order to complete project 3.
The parent program will create the threads and necessary data structures but won’t do any of the work. Have the parent program create four threads, one for each of the jobs from Project 2, then wait for all the threads to quit before it exits. Three of the threads will handle the work from project 2: read the input; break the input into tokens; classify the tokens. The new job, handled by the fourth thread, will count the number of tokens in each of the four token categories and display those counts after all the tokens have been processed. It will get tokens from the thread that classifies the tokens, and it will use the same method – an “all-done” token – to know when to exit.
The threads will communicate using shared-memory queues instead of pipes. Use the STL <queue> data structure, and make it a queue of records using the struct’s defined for Project 2 – one queue for the inputStruct’s and two for the tokenStruct’s. You’ll need to protect any access to each queue by any thread using a mutex. In addition, use a counting semaphore to manage each thread’s access – don’t assume that if the queue is empty, there’s nothing for the thread to do: it should block when the queue is empty using the semaphore, *not* the size of the queue. Declare the queues, mutexes, and counting semaphores as global, and have the parent program initialize them.
To use the pthread library, #include <pthread.h> and link with the flag –lpthread. To use the semaphore library, #include <semaphore.h> Here’s a summary of the libraries:
pthread_t An internal pthread struct, whose internals can safely be ignored, used as a thread identifier. Declare one for each thread to be created.
pthread_create(pthread_t * p, NULL, void *function, void *argument) Creates a thread and dispatches it to run function with the given argument. The function must have the signature void *function(void *parameter). If you need to start that function with an actual argument, consider creating a struct variable, filling it with data, and passing it by address but cast to void *.
pthread_join(pthread_t p, NULL) Waits for the indicated thread to complete.
pthread_exit(void *status) Kills the thread making this call.
pthread_kill(pthread p, void** status) Kills the indicated thread.
// in parent:
pthread_create(&p, NULL, fn, NULL); // Use fn( ) as the startup function for p
pthread_join(p, NULL); // Wait for p to finish
// in thread:
// some thread code goes here
pthread_mutex_t A mutex (atomic lock) variable struct, whose contents can safely be ignored. Declare one for each mutex needed. A mutex should be statically initialized by setting it equal to PTHREAD_MUTEX_INITIALIZER.
pthread_mutex_lock(&pthread_mutex_t) Locks the mutex.
pthread_mutex_unlock(&pthread_mutex_t) Unlocks the mutex.
// in parent:
pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
// in thread code:
// ... some critical region code goes here ...
The semaphore library is associated with pthreads, and it provides counting semaphores.
sem_t An internal semaphore struct whose internals can safely be ignored. Declare one for each semaphore needed.
sem_init(sem_t *, 0, unsigned int count) Initializes the indicated semaphore to count (note: ignore the second parameter for our purposes.) That is, set count to the number of shared resources, or the number of threads that can safely share one resource.
sem_wait(sem_t *s) If the semaphore is 0, blocks the thread; otherwise decrements the semaphore’s value.
sem_post(sem_t *s) This is what is usually called “signal”. Increments the semaphore’s value; if the old value was zero, wakes up one blocked thread (if any where actually blocked).
sem_t mysem; // Make this a gl
3 pekerja bebas membida secara purata $150 untuk pekerjaan ini
HI WE ARE HAVING EXPERIENCED TEAM OF C++ DEVELOPERS. ALSO RECENTLY WE WORK WITH RESEARCH INSTITUTE TO PROVIDE THEM OPTIMIZED SOLUTION ITS COMPLETE DEVELOPMENT IN C++ . SO U CAN THINK THIS BID SERIOUSLY. THANKS