You are to implement the program below in C. This project simulates a buffered v
ID: 3606815 • Letter: Y
Question
You are to implement the program below in C.
This project simulates a buffered video streaming service. The main program (process) sets up resource that are shared by all threads of the process . The shared resources include semaphores, a data buffer, and a file pointer.
The main program takes in an input parameter ,which is the name of the video file to be streamed (a text file) . The data buffer, which is an array of characters (six in total), is used to store data chunks (one data chunk is represented by one character). The data buffer can store up to six data characters . The main program spawns one producer and one consumer thread for each character buffer (total of 12 threads) . The producer reads in a character in the order of the file and adds the character to the buffer. A consumer thread reads character from the buffer in first - in - first - out (FIFO) order . The consumer thread prints to the standard output (stdout) right after it consumed the character from the buffer. When the file reaches it’s end, all threads terminate. After all threads terminated, the main program finishes .
In writing your program, you need to use some thread library functions such as pthread_create, pthread_join, sem_init, sem_wait, and sem_post . You should use the “ - lpthread” or “ - pthread” option when compiling and linking all program modules .
Explanation / Answer
In these casei used thread library
#define _REENTRANT
#include <stdio.h>
#include <thread.h>
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 is = %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;i<1000000*(int)thr_self();i++);
printf("A: Thread will be exiting here... ");
thr_exit((void *)77);
}
void * sub_b(void *arg)
{
int i;
printf("B: In thread B... ");
/* process
*/
for (i=0;i<1000000*(int)thr_self();i++);
printf("B: Thread exiting... ");
thr_exit((void *)66);
}
void * sub_c(void *arg)
{
void *status;
int i;
thread_t main_thr, ret_thr;
main_thr = (thread_t)arg;
printf("C: In thread C... ");
if (thr_create(NULL, 0, sub_f, (void *)0, THR_BOUND|THR_DAEMON, NULL))
fprintf(stderr, "Can't create thr_f "), exit(1);
/*
join thread here excutred
*/
printf("C: Join main thread ");
if (thr_join(main_thr,(thread_t *)&ret_thr, &status))
fprintf(stderr, "thr_join Error "), exit(1);
printf("C: Main thread (%d) returned thread (%d) w/status %d ", main_thr, ret_thr, (int) status);
/* process
*/
for (i=0;i<1000000*(int)thr_self();i++);
printf("C: Thread exiting... ");
thr_exit((void *)88);
}
void * sub_d(void *arg)
{
thread_t thr_b = (thread_t) arg;
int i;
thread_t thr_e, ret_thr;
void *status;
/* here D thread will be excuted*/
printf("D: In thread D... ");
if (thr_create(NULL, 0, sub_e, NULL, THR_NEW_LWP, &thr_e))
fprintf(stderr,"Can't create thr_e "), exit(1);
printf("D: Created thread E:%d ", thr_e);
printf("D: Continue B thread = %d ", thr_b);
thr_continue(thr_b);
printf("D: Join E thread ");
if(thr_join(thr_e,(thread_t *)&ret_thr, &status))
fprintf(stderr,"thr_join Error "), exit(1);
printf("D: E thread (%d) returned thread (%d) w/status %d ", thr_e,
ret_thr, (int) status);
/* process
*/
for (i=0;i<1000000*(int)thr_self();i++);
printf("D: Thread exiting... ");
thr_exit((void *)55);
}
void * sub_e(void *arg)
{
int i;
thread_t ret_thr;
void *status;
printf("E: In thread E... ");
printf("E: Join A thread ");
if(thr_join(thr_a,(thread_t *)&ret_thr, &status))
fprintf(stderr,"thr_join Error "), exit(1);
printf("E: A thread (%d) returned thread (%d) w/status %d ", ret_thr, ret_thr, (int) status);
printf("E: Join B thread ");
if(thr_join(thr_b,(thread_t *)&ret_thr, &status))
fprintf(stderr,"thr_join Error "), exit(1);
printf("E: B thread (%d) returned thread (%d) w/status %d ", thr_b, ret_thr, (int) status);
printf("E: Join C thread ");
if(thr_join(thr_c,(thread_t *)&ret_thr, &status))
fprintf(stderr,"thr_join Error "), exit(1);
printf("E: C thread (%d) returned thread (%d) w/status %d ", thr_c, ret_thr, (int) status);
for (i=0;i<1000000*(int)thr_self();i++);
/* here E thread will be excuted*/
printf("E: Thread exiting... ");
thr_exit((void *)44);
}
void *sub_f(void *arg)
{
int i;
printf("F: In thread F... ");
/* here F thread will be excuted*/
while (1) {
for (i=0;i<10000000;i++);
printf("F: Thread F is still running... ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.