Question
Multithreading c code. Can someone give me a push in the right direction? #define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error_en(en,
Multithreading c code. Can someone give me a push in the right direction?
#define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
// there are 10 kiosks represented as named pipe kiosk[0-9]. // opening named pipes. int open_kiosk(int n, int* fd_client, int* fd_server) { int fd_c, fd_s; char name[16]; if (n < 0 || n > 9) handle_error_en(ERANGE, "kiosk number");
sprintf(name, "kiosk-client%d", n); fd_c = open(name, O_RDONLY); // this will block until other end opens if (fd_c == -1) handle_error("open (read) failed"); sprintf(name, "kiosk-server%d", n); fd_s = open(name, O_WRONLY); if (fd_s == -1) handle_error("open (write) failed");
*fd_client = fd_c; *fd_server = fd_s;
return 0; }
struct thread_info { int id; int fd_client; int fd_server; };
#define TOTAL_FLOOR (100) #define TOTAL_MEZZANINE (80) #define TOTAL_BALCONY (120) #define TOTAL_BOX (30)
#define FLO (0) #define MEZ (1) #define BAL (2) #define BOX (3) #define NUM_AREAS (4)
// our shared object struct _tickets { int total[NUM_AREAS]; int price[NUM_AREAS]; /**************************************** * TODO: Add additional members as needed ****************************************/ } tickets;
/**************************************** * TODO: Add mutexes and cond vars as needed: // pthread_mutex_t lock; // pthread_cond_t cond[NUM_AREAS]; ****************************************/
void tickets_init(void) { memset(&tickets, 0, sizeof(tickets)); tickets.total[FLO] = TOTAL_FLOOR; tickets.total[MEZ] = TOTAL_MEZZANINE; tickets.total[BAL] = TOTAL_BALCONY; tickets.total[BOX] = TOTAL_BOX;
tickets.price[FLO] = 150; tickets.price[MEZ] = 130; tickets.price[BAL] = 90; tickets.price[BOX] = 110;
/* * TODO: additional initialization as needed */ // pthread_mutex_init(&lock, NULL); //pthread_cond_init(&cond[FLO], NULL); //pthread_cond_init(&cond[MEZ], NULL); //pthread_cond_init(&cond[BAL], NULL); //pthread_cond_init(&cond[BOX], NULL); } /* * read into buf a 4-byte message from pipe */ ssize_t read_msg(struct thread_info* ti, int32_t* buf) { ssize_t n; n = read(ti->fd_client, buf, 4); if (n == 0) { return n; // pipe closed } if (n != 4) handle_error("message (read) failed"); return n; }
/* * write to pipe 4-byte message val */ ssize_t write_msg(struct thread_info* ti, int32_t val) { ssize_t n; n = write(ti->fd_server, &val, 4); if (n != 4) handle_error("message (write) failed"); return n; }
/* * main loop exit condition check * returns 1 if all tickets are sold out and all pipe closed. */ int check_all_sold_out(void) { /* * TODO: your implementation here */ return 1; }
void* kiosk_main(void* arg) { struct thread_info* ti = (struct thread_info*)arg; int res; int msg;
res = open_kiosk(ti->id, &ti->fd_client, &ti->fd_server); if (res) handle_error("open_kiosk failed"); while (1) { msg = 0; if (read_msg(ti, &msg) == 0) { if (!check_all_sold_out()) { printf("(%d) operation failure. ", ti->id); } break; } switch (msg) { case QUOTE_ME_FLOOR: case QUOTE_ME_MEZZANINE: case QUOTE_ME_BALCONY: case QUOTE_ME_BOX: {
/********************************* * TODO: YOUR IMPLEMENTATION HERE ********************************/ } break; default: fprintf(stderr, "(%d) incorrect message seq. ", ti->id); break; } }
return ti; }
#define NTHREADS (10) int main(int argc, char** argv) { int s, i; void* res;
pthread_t thread[NTHREADS];
tickets_init(); for (i = 0; i < NTHREADS; i++) { struct thread_info* ti = malloc(sizeof(*ti)); if (!ti) handle_error_en(ENOMEM, "malloc failed"); ti->id = i; s = pthread_create(&thread[i], NULL, &kiosk_main, ti); if (s) handle_error_en(s, "pthread_create: "); }
for (i = 0; i < NTHREADS; i++) { s = pthread_join(thread[i], &res); if (s) perror("pthread_join:"); printf("thread %d joined ", i); } printf("main done "); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started