Question

Write a program called tenthreads that uses 10 threads to increment a shared variable. Each thread...

Write a program called tenthreads that uses 10 threads to increment a shared variable.

Each thread must loop 6 times, incrementing the shared variable by its Thread ID (tid) in every iteration of the loop.

Once a thread has finished looping, print the ID of the thread saying “Thread [ID] has finished.”

It is important to make use of mutexes so that only one thread is incrementing the shared variable at a time.

Output the value of the shared variable once all threads have finished incrementing it.

Before submission, make sure you clean up the directories so that no miscellaneous files are kept around in the submission. Include the source code and the Makefile in the submission.

(Please show what the Makefile looks like in answer).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please go through code and output.

CODE:

Makefile

CC= gcc

CFLAGS=-lpthread

thread: thread.o

[tab] $(CC) -o thread thread.c

thread.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int sharedVar = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/* thread handler */
void *threadHandler(void *vargp)
{
int i =0;
pthread_mutex_lock(&mutex);
for(i =0; i<6; i++)
sharedVar++;
printf("Thread [%ld] has finished\n",pthread_self());
pthread_mutex_unlock(&mutex);
}

int main()
{
pthread_t thread_id[10];
int i=0;
/* create thread array */
for(i=0; i<10; i++)
{
pthread_create(&thread_id[i], NULL, threadHandler, NULL);
}
/* wait for exit thread */
pthread_join(thread_id[0], NULL);
pthread_join(thread_id[1], NULL);
pthread_join(thread_id[2], NULL);
pthread_join(thread_id[3], NULL);
pthread_join(thread_id[4], NULL);
pthread_join(thread_id[5], NULL);
pthread_join(thread_id[6], NULL);
pthread_join(thread_id[7], NULL);
pthread_join(thread_id[8], NULL);
pthread_join(thread_id[9], NULL);
/* print shared variable */
printf("Shared Variable - %d\n",sharedVar);

}
OUTPUT:

Thread [140713293514496] has finished Thread [140713301907200] has finished Thread [140713194944256] has finished Thread [140713285121792] has finished Thread [140713276729088] has finished Thread [140713186551552] has finished Thread [140713178158848] has finished Thread [140713169766144] has finished Thread [140713161373440] has finished Thread [140713152980736] has finished Shared Variable - 60

Thread [140713293514496] has finished Thread [140713301907200] has finished Thread [140713194944256] has finished Thread [140713285121792] has finished Thread [140713276729088] has finished Thread [140713186551552] has finished Thread [140713178158848] has finished Thread [140713169766144] has finished Thread [140713161373440] has finished Thread [140713152980736] has finished Shared Variable - 60

Add a comment
Know the answer?
Add Answer to:
Write a program called tenthreads that uses 10 threads to increment a shared variable. Each thread...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT