22BCE1726 Baker Challenge

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Bakery Algorithm - Challenging Question [20

marks]

Instructions

Write a C program to implement the interactive Quiz


Application where there are 3 students playing at this time.
After all three students logging in the application, a question
will be displayed for all the three. A student who presses the
buzzer first will be given the chance to answer the question.
If he answers correctly he will get 10 marks for the correct
question. If he answers wrongly, 5 marks will be reduced. The
one who takes the next chance to answer that same question
by pressing the buzzer will be given 5 marks if his answer is
correct. Else again he also loses 5 marks. Implement this
application using bakery algorithm and give the final marks
for the students, for 5 questions in total. Display the winning
student's name. Implement this application using the Bakery
Algorithm to handle the critical section problem.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

#define NUM_STUDENTS 3
#define NUM_QUESTIONS 5

int scores[NUM_STUDENTS] = {0};


int currentQuestion = 0;
int currentStudent = -1;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


sem_t studentSemaphore;
int choosing[NUM_STUDENTS] = {0};
int number[NUM_STUDENTS] = {0};

void lock(int studentId) {


choosing[studentId] = 1;
int maxNumber = 0;
for (int i = 0; i < NUM_STUDENTS; i++) {
if (number[i] > maxNumber) {
maxNumber = number[i];
}
}
number[studentId] = maxNumber + 1;
choosing[studentId] = 0;
for (int i = 0; i < NUM_STUDENTS; i++) {
if (i != studentId) {
while (choosing[i]) {}
while (number[i] != 0 && (number[i] < number[studentId] || (number[i] == number[studentId] && i <
studentId))) {}
}
}
}

void unlock(int studentId) {


number[studentId] = 0;
}

void *studentFunction(void *studentId) {


int id = *(int *)studentId;
while (currentQuestion < NUM_QUESTIONS) {
lock(id);

if (currentStudent == -1) {
currentStudent = id;
printf("Student %d has pressed the buzzer for Question %d.\n", id, currentQuestion + 1);
} else {
printf("Student %d has pressed the buzzer for Question %d but lost the chance to Student %d.\n", id,
currentQuestion + 1, currentStudent);
scores[id] -= 5;
}
unlock(id);
int isCorrect = (rand() % 3);
if (isCorrect) {
printf("Student %d has answered correctly and earned 10 marks for Question %d.\n", id, currentQuestion + 1);
scores[id] += 10;
} else {
printf("Student %d has answered incorrectly and lost 5 marks for Question %d.\n", id, currentQuestion + 1);
scores[id] -= 5;
int newid=-1;
do{
newid=(rand()%2);
}
while(newid==id);
printf("Question passed to Student %d.\n",newid);
int isCorrect = (rand() % 3);
if (isCorrect)
{
printf("Student %d has answered correctly and earned 5 marks for Question %d.\n", newid, currentQuestion + 1);
scores[newid] += 5;
}
else
{
printf("Student %d has answered incorrectly and lost 5 marks for Question %d.\n", newid, currentQuestion + 1);
scores[newid] -= 5;

}
}

currentQuestion++;

pthread_mutex_lock(&mutex);
currentStudent = -1;
pthread_mutex_unlock(&mutex);
usleep(1000000);
}

pthread_exit(NULL);
}

int main() {
pthread_t students[NUM_STUDENTS];
int studentIds[NUM_STUDENTS];
srand(time(NULL));
for (int i = 0; i < NUM_STUDENTS; i++) {
studentIds[i] = i;
pthread_create(&students[i], NULL, studentFunction, &studentIds[i]);
}
for (int i = 0; i < NUM_STUDENTS; i++) {
pthread_join(students[i], NULL);
}
int winningStudent = 0;
int maxScore = scores[0];

for (int i = 1; i < NUM_STUDENTS; i++) {


if (scores[i] > maxScore) {
maxScore = scores[i];
winningStudent = i;
}
}

printf("Final Scores:\n");
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Student %d: %d marks\n", i, scores[i]);
}

printf("Student %d is the winner with %d marks!\n", winningStudent, maxScore);

return 0;
}

Output:

You might also like