Skip to content

Commit

Permalink
Create program.c
Browse files Browse the repository at this point in the history
  • Loading branch information
khwaishchawla authored Nov 8, 2024
1 parent 3e8f8e9 commit 44805ad
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Miscellaneous Algorithms/Lottery Scheduling/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Structure to represent a process
struct Process {
int pid; // Process ID
int burst_time; // CPU burst time
int tickets; // Number of tickets (priority)
};

// Function to perform lottery scheduling
void lotteryScheduling(struct Process processes[], int n) {
int total_tickets = 0;
int time = 0;

// Calculate total number of tickets
for (int i = 0; i < n; i++) {
total_tickets += processes[i].tickets;
}

srand(time(NULL)); // Seed for random number generation

while (n > 0) {
// Draw a winning ticket
int winning_ticket = rand() % total_tickets + 1;
int ticket_counter = 0;
int chosen_index = -1;

// Find the process with the winning ticket
for (int i = 0; i < n; i++) {
ticket_counter += processes[i].tickets;
if (ticket_counter >= winning_ticket) {
chosen_index = i;
break;
}
}

// Execute the chosen process
printf("Process %d is selected with burst time %d\n", processes[chosen_index].pid, processes[chosen_index].burst_time);
time += processes[chosen_index].burst_time;
printf("Process %d completed at time %d\n", processes[chosen_index].pid, time);

// Update total tickets and remove the process from the list
total_tickets -= processes[chosen_index].tickets;

// Shift remaining processes
for (int i = chosen_index; i < n - 1; i++) {
processes[i] = processes[i + 1];
}
n--; // Reduce the number of processes
}
}

int main() {
struct Process processes[] = {
{1, 5, 10},
{2, 3, 20},
{3, 7, 5},
{4, 4, 15}
};
int n = sizeof(processes) / sizeof(processes[0]);

printf("Lottery Scheduling:\n");
lotteryScheduling(processes, n);

return 0;
}

0 comments on commit 44805ad

Please sign in to comment.