0% found this document useful (0 votes)
31 views4 pages

381 CCS - MiniProject-Group3

Uploaded by

ahmedfadaeyl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

381 CCS - MiniProject-Group3

Uploaded by

ahmedfadaeyl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mini Project Group-3

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];

void *philosopher(void *num);


void take_chopsticks(int phil_id);
void put_chopsticks(int phil_id);
void test(int phil_id);

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;
}

void *philosopher(void *num) {


int phil_id = *(int *)num;
while (1) {
printf("Philosopher %d is THINKING.\n", phil_id);
sleep(1);
take_chopsticks(phil_id);
1
sleep(2);
put_chopsticks(phil_id);
}
return NULL;
}

void take_chopsticks(int phil_id) {


sem_wait(&mutex);
state[phil_id] = HUNGRY;
printf("Philosopher %d is HUNGRY.\n", phil_id);
test(phil_id);
sem_post(&mutex);
sem_wait(&chopstick[phil_id]);
}

void put_chopsticks(int phil_id) {


sem_wait(&mutex);
state[phil_id] = THINKING;
printf("Philosopher %d has finished EATING and is THINKING again.\n", phil_id);
test((phil_id + 4) % NUM_PHILOSOPHERS);
test((phil_id + 1) % NUM_PHILOSOPHERS);
sem_post(&mutex);
}

void test(int phil_id) {


if (state[phil_id] == HUNGRY &&
state[(phil_id + 4) % NUM_PHILOSOPHERS] != EATING &&
state[(phil_id + 1) % NUM_PHILOSOPHERS] != EATING) {
state[phil_id] = EATING;
printf("Philosopher %d is EATING.\n", phil_id);
sem_post(&chopstick[phil_id]);
}
}

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;
};

void sortProcessesByPriority(struct Process processes[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].priority > processes[j + 1].priority) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
}

void calculateWaitingTime(struct Process processes[], int n) {


processes[0].waitingTime = 0;
for (int i = 1; i < n; i++) {
processes[i].waitingTime = processes[i - 1].waitingTime + processes[i - 1].burstTime;
}
}

void calculateTurnaroundTime(struct Process processes[], int n) {


for (int i = 0; i < n; i++) {
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
}
}

void displayProcesses(struct Process processes[], int n) {


printf("\nProcess\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id, processes[i].burstTime,
processes[i].priority, processes[i].waitingTime, processes[i].turnaroundTime);
}
}

int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);

struct Process processes[n];


for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Enter burst time for process P%d: ", i + 1);
3
scanf("%d", &processes[i].burstTime);
printf("Enter priority for process P%d (lower value = higher priority): ", i + 1);
scanf("%d", &processes[i].priority);
}

sortProcessesByPriority(processes, n);
calculateWaitingTime(processes, n);
calculateTurnaroundTime(processes, n);
displayProcesses(processes, n);

float totalWaitingTime = 0, totalTurnaroundTime = 0;


for (int i = 0; i < n; i++) {
totalWaitingTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
}

printf("\nAverage Waiting Time: %.2f\n", totalWaitingTime / n);


printf("Average Turnaround Time: %.2f\n", totalTurnaroundTime / n);

return 0;
}

You might also like