Question: It's about C language and thread. #include #include #include #include #define NUM_Threads 10 int globalVar void *executeThreadFunc(void *args) { long threadID = (long)args; int i,j;

It's about C language and thread.

#include

#include

#include

#include

#define NUM_Threads 10

int globalVar

void *executeThreadFunc(void *args)

{

long threadID = (long)args;

int i,j;

for(i=0;i<10;i++){

for( j=0;j<100;j++)

{

thead.sleep(100);

}}

pthread_exit(NULL);

}

int main()

{

pthread_t nThreads[NUM_Threads];

long i, status;

int r;

r=thread.getid();

if(r%2==0)

{

globalVar++;

}

else

{

globalVar--;

}

for(i=0;i {

status=pthread_create(&nThreads[i], NULL, executeThreadFunc, (void *) i);

if(status>0)

{

print( Error Creating Thread);

return 1;

}

}

print(r);

Return 0;

}

1. When it starts the execution, print the ID of the current thread being executed. For example - Thread 1 Started Execution

2. When it ends the execution, print the ID of the current thread being ended. For example - Thread 1 Ended Execution

3. What is the expected order of thread execution? Provide the expected order with thread numbers. what is the expected value of globalVar after the last thread finishes execution? show calculation for globalVar.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!