Question: C code help please! Use the following C code as the base source code and edit based on questions 1. Declare one global variable of
C code help please!
Use the following C code as the base source code and edit based on questions
1. Declare one global variable of type integer. For example, int globalVar.
2. You will find the function named void *executeThreadFunc(void *args) in the source code. In this method do the following:
a. When it starts the execution, print the ID of the current thread being executed. For example - Thread 1 Started Execution
b. Next, check the ID of Thread i. If the ID is an even number, then increment the globalVar by 1[1] ii. If the ID is an odd number, then decrement the globalVar by 1
c. When it ends the execution, print the ID of the current thread being ended. For example - Thread 1 Ended Execution
#include
#define NUM_Threads 10
void *executeThreadFunc(void *args) { long threadID = (long)args; int i,j;
for(i=0;i<10;i++) for(j=0;j<100;j++) pthread_exit(NULL); }
int main() { pthread_t nThreads[NUM_Threads]; long i, status; for(i=0;i { status=pthread_create(&nThreads[i], NULL, executeThreadFunc, (void *) i); if (status>0) { printf(" Error Creating Thread"); return 1; } }
for(i=0;i { pthread_join(nThreads[i], NULL); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
