381 CCS - MiniProject-Group3
381 CCS - MiniProject-Group3
Name: S. No:
Academic No: Section:
Subject: 381CCS Marks: 5
1. Write a c program to implement the Dining Philosopher problem. Create a thread function
for each philosopher and synchronize the states-Think, Eat, and Hungry. print the state of
each philosopher in the output.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
int state[NUM_PHILOSOPHERS];
sem_t mutex;
sem_t chopstick[NUM_PHILOSOPHERS];
int main() {
pthread_t thread_id[NUM_PHILOSOPHERS];
int philosopher_ids[NUM_PHILOSOPHERS];
sem_init(&mutex, 0, 1);
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_init(&chopstick[i], 0, 0);
state[i] = THINKING;
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
philosopher_ids[i] = i;
pthread_create(&thread_id[i], NULL, philosopher, &philosopher_ids[i]);
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_join(thread_id[i], NULL);
}
return 0;
}
2
2. Write a C language program to implement the Priority-based Scheduling Algorithm.
#include <stdio.h>
struct Process {
int id;
int burstTime;
int priority;
int waitingTime;
int turnaroundTime;
};
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
sortProcessesByPriority(processes, n);
calculateWaitingTime(processes, n);
calculateTurnaroundTime(processes, n);
displayProcesses(processes, n);
return 0;
}