Question Create a thread worker function to count the number of negative element
ID: 3841501 • Letter: Q
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 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
count_negative.c
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
Explanation / Answer
// A recursive function to print all increasing sequences
// of first n natural numbers. Every sequence should be
// length k. The array arr[] is used to store current
// sequence.
void printSeqUtil(int n, int k, int &len, int arr[])
{
// If length of current increasing sequence becomes k,
// print it
if (len == k)
{
printArr(arr, k);
return;
}
// Decide the starting number to put at current position:
// If length is 0, then there are no previous elements
// in arr[]. So start putting new numbers with 1.
// If length is not 0, then start from value of
// previous element plus 1.
int i = (len == 0)? 1 : arr[len-1] + 1;
// Increase length
len++;
// Put all numbers (which are greater than the previous
// element) at new position.
while (i<=n)
{
arr[len-1] = i;
printSeqUtil(n, k, len, arr);
i++;
}
// This is important. The variable 'len' is shared among
// all function calls in recursion tree. Its value must be
// brought back before next iteration of while loop
len--;
}
// This function prints all increasing sequences of
// first n natural numbers. The length of every sequence
// must be k. This function mainly uses printSeqUtil()
void printSeq(int n, int k)
{
int arr[k]; // An array to store individual sequences
int len = 0; // Initial length of current sequence
printSeqUtil(n, k, len, arr);
}
// Driver program to test above functions
int main()
{
int k = 3, n = 7;
printSeq(n, k);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.