You are designing a web server that has one server thread reading requests from
ID: 2247466 • Letter: Y
Question
You are designing a web server that has one server thread reading requests from n-number of users that stream in at a given rate. The requests are put in a buffer of constant size that are services by a thread pool of m-threads.
a) Break this problem into 1 or more producer-consumer problems, describing the producer and the consumer at each sub-problem.
Additionally, you decide to forgo the use of lock primitives such as pthread_lock and pthread_unlock to reduce latency on running threads.
b) Describe how compare-and-swap operations can be used to ensure mutual exclusion during produce and consume operations on the buffer.
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.