Write a C program to simulate the following IPC problem. Make sure to use semaph
ID: 3775504 • Letter: W
Question
Write a C program to simulate the following IPC problem. Make sure to use semaphores to synchronize the processes/threads.
Jurassic Park consists of a dinosaur museum and a park for safari tiding. There are n passengers and m single-passenger cars. Passengers wander around the museum for a while, then line up to take a ride in a safari car. When a car is available, it loads the one passenger it can hold and rides around the park for a random amount of time. If the m cars are all out riding passenger around, the passenger who wants to ride waits; if a car is ready to load but there are no waiting passengers, then the car waits. Use semaphores to synchronize the n passenger threads/processes and the m car threads/processes.
Explanation / Answer
#define _REENTRANT
#include <stdio.h>
#include <thread.h>
/* 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;i<1000000*(int)thr_self();i++);
printf("A: Thread exiting... ");
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);
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;
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++);
printf("E: Thread exiting... ");
thr_exit((void *)44);
}
void *sub_f(void *arg)
{
int i;
printf("F: In thread F... ");
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.