Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. The code below is a thread program with synchronization via mutex. Answer the

ID: 3719854 • Letter: 8

Question

8. The code below is a thread program with synchronization via mutex. Answer the following questions:

1.      how many threads are created? Highlight and mark on the code.

2.      When thread start to execute, which function does it start to run?

3.      Describe the execution logic of this program and the expected output.

4.      Modify the program so the execution order of the children threads will be reversed.

int value[]={1,2,3,4,5,6,7,8,9,10};

int counter = 10;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char *argv[])

{

pthread_t tid[10];

pthread_attr_t attr[10];

for(int j=0; j<10; j++)

    {

      pthread_attr_init(&attr[j]);

      pthread_create(&tid[j],&attr[j],runner,&value[j]);

    }

   for(int k=0; k<10; k++)

    {

      pthread_join(tid[k],NULL);

    }

}

void *runner(void *param)

{

while(i>0)

{

           if(*(int *)param == counter)

           {

                       printf("param = %d ", *(int *)param);

                       pthread_mutex_lock(&mutex);

                       counter --;

                       pthread_mutex_unlock(&mutex);

                       sleep(random());

                       pthread_yield();

           }

           else {

                       pthread_yield();

           }

          

    }

Explanation / Answer

int value[]={1,2,3,4,5,6,7,8,9,10};

int counter = 10;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char *argv[])

{

pthread_t tid[10];

pthread_attr_t attr[10];

for(int j=0; j<10; j++)

    {

      pthread_attr_init(&attr[j]);

      pthread_create(&tid[j],&attr[j],runner,&value[j]);

    }

   for(int k=0; k<10; k++)

    {

      pthread_join(tid[k],NULL);

    }

}

void *runner(void *param)

{

while(i>0)

{

           if(*(int *)param == counter)

           {

                       printf("param = %d ", *(int *)param);

                       pthread_mutex_lock(&mutex);

                       counter --;

                       pthread_mutex_unlock(&mutex);

                       sleep(random());

                       pthread_yield();

           }

           else {

                       pthread_yield();

           }   

    }

PART 1: In the above code 10 threads are created. (Font set to bold and italics)

Threads are created in C language by means of pthread_create method(). In the above code as this function runs for 10 times in the first for loop hence 10 threads are created.

Explaination:-

Inside main() a variable of type pthread_t is created which is basically an integer used to identify the thread in the system.

After variable declaration a call to the function pthread_create() is made.

This function takes 4 arguments as parameters:-

1st  argument:- pointer to variable of type pthread_t which this function sets.

3rd argument:- name of the function to be executed for the thread created. In above it is runner()

4th argument:- passes arguments to the thread.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote