C language is the preferred choice of coding Objective: In this assignment, you
ID: 3602030 • Letter: C
Question
C language is the preferred choice of coding
Objective:
In this assignment, you will be writing a multithreaded program that shares data between threads. You will need to properly protect data that is shared between threads. You will be using mutex locks around critical sections to ensure your program works correctly and does not experience race conditions.
Details:
The previous program could possibly use too many threads and we need to fix that problem. You will write the program to only use three threads; the main thread, a producer, and a consumer. The three threads will accomplish the following tasks:
Main Thread:
Create the producer and consumer threads
Send each number from the command line to the producer thread via a shared buffer.
This buffer only needs to be shared between the main thread and the producer.
Wait for each thread to complete before terminating the program
Producer Thread:
Wait for numbers to be added to the buffer shared with the main thread
Factor each number
Save numbers to a buffer shared between the producer and consumer threads
Consumer Thread:
Wait for factors to be added to the buffer shared with the producer thread
Display the factors
This program will use two sets of shared data, in the producer-consumer style. Both sets of data need to be protected using pthread_mutex_t variables. The only thread producing any output to the console should be the consumer thread. All other threads should perform their actions silently.
Hints:
The buffer between the main thread and the producer thread can be a simple integer array
The buffer between the producer thread and the consumer thread might be an array of integer pointers.
Each element in the array of integer pointers will point to an array of integers that contains the original number and the factors of that number.
You could also make the producer/consumer buffer an array of pointers to structures. The possibilities are endless.
Output:
Here are several examples of what is expected when you run your program:
# ./assn3
Usage:./assn3 <number to factor>...
# ./assn3 12
12: 2 2 3
# ./assn3 7 12 25
7: 7
12: 2 2 3
25: 5 5
# ./assn3 {32..48}
32: 2 2 2 2 2
33: 3 11
34: 2 17
35: 5 7
36: 2 2 3 3
37: 37
38: 2 19
39: 3 13
40: 2 2 2 5
41: 41
42: 2 3 7
43: 43
44: 2 2 11
45: 3 3 5
46: 2 23
47: 47
48: 2 2 2 2 3
Explanation / Answer
#define _REENTRANT #include #include /* Function prototypes for thread routines */ void *sub_a(void *); void *sub_b(void *); void *sub_c(void *); void *sub_d(void *); void *sub_e(void *); void *sub_f(void *); thread_t thr_a, thr_b, thr_c; void main() { thread_t main_thr; main_thr = thr_self(); printf("Main thread = %d ", main_thr); if (thr_create(NULL, 0, sub_b, NULL, THR_SUSPENDED|THR_NEW_LWP, &thr_b)) fprintf(stderr,"Can't create thr_b "), exit(1); if (thr_create(NULL, 0, sub_a, (void *)thr_b, THR_NEW_LWP, &thr_a)) fprintf(stderr,"Can't create thr_a "), exit(1); if (thr_create(NULL, 0, sub_c, (void *)main_thr, THR_NEW_LWP, &thr_c)) fprintf(stderr,"Can't create thr_c "), exit(1); printf("Main Created threads A:%d B:%d C:%d ", thr_a, thr_b, thr_c); printf("Main Thread exiting... "); thr_exit((void *)main_thr); } void *sub_a(void *arg) { thread_t thr_b = (thread_t) arg; thread_t thr_d; int i; printf("A: In thread A... "); if (thr_create(NULL, 0, sub_d, (void *)thr_b, THR_NEW_LWP, &thr_d)) fprintf(stderr, "Can't create thr_d "), exit(1); printf("A: Created thread D:%d ", thr_d); /* process */ for (i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.