Question: Question Create a thread worker function to count the number of negative elements in an integer array. i.e. less than zero. The input is the
Question
Create a thread worker function to count the number of negative elements in an integer array. i.e. less than zero.
The input is the following:
array of int16_t, called array
number of elements in the array, called array_size
number of worker threads that will execute the function count_negative_thread_worker(), called number_of_threads
one pthread mutex lock, initially unlocked, called negative_count_lock
as an argument for each thread worker, one integer (int) as the thread identifier, called arg
The output is the following:
defined externally. Set the variable negative_count to represent the final answer
as a return value for each thread worker, an integer (int) of the number of elements this thread has processed. Valid integer is in the range [1,array_size]
The thread worker function can be executed by 1 or more threads simultaneously and you must ensure the correct result is calculated.
count_negative.h
// assume > number_of_threads extern uint64_t array_size; // assume already allocated with data extern int16_t *array; // assume > 0 extern int number_of_threads; // assume initially -1 extern uint64_t negative_count; // assume PTHREAD_MUTEX_INITIALIZER extern pthread_mutex_t negative_count_lock; extern void *count_negative_thread_worker(void *arg);
count_negative.c
void *count_negative_thread_worker(void *arg) { int thread_id = (int)arg; // write your code here } Threads all working equally
Your code must distribute the workload as equally as possible among the threads.
For example if there are 20 items and 2 threads.
Thread 1 must process approximately 10 items
Thread 2 must process approximately 10 items
For example if there are 19 items and 2 threads.
Thread 1 must process approximately 9 items
Thread 2 must process approximately 9 items
For example if there are 3 items and 2 threads.
Thread 1 must process approximately 1 item
Thread 2 must process approximately 1 item
For example if there are 100 items and 5 threads.
Thread 1 must process approximately 20 items
Thread 2 must process approximately 20 items
Thread 3 must process approximately 20 items
Thread 4 must process approximately 20 items
Thread 5 must process approximately 20 items
About the tests
4 tests for single threaded correctness
4 tests for two threads alternating (serialised) correctness.
12 tests for more data cases with multiple threads
Hidden test cases will apply after the PRAS using diffferent data
Not all tests will be presented in this practice. For example, the return value for each thread worker.
A sample of the input/output test has been provided and the staff count_negative_main.c file is also available for review. Note that you should not add, remove or adjust any code in the staff file count_negative_main.c. It will not influence a submission.
Compiler and flags used
clang -g -O0 -Wall -std=gnu11 -Wno-int-to-void-pointer-cast -lpthread count_negative.c count_negative_main.c -o count_negative
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
