include <stdio.h>

include <graphics.h>

include <conio.h>

// Swap function to swap two values
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}

// Function to plot the Gantt chart
void plotGraph(int n, int p[], int at[], int bt[], int ct[]) {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\TC\BGI"); // Specify your path to BGI files correctly
setbkcolor(WHITE);
cleardevice();
setcolor(BLACK);
outtextxy(50, 20, "Process Scheduling Chart");

int x = 50, y = 100, width = 60, height = 50;  // Box size
int timeX = 50;  // Time axis starting point

// Loop to draw each process's Gantt chart bar
for (int i = 0; i < n; i++) {
    char processLabel[10];
    sprintf(processLabel, "P%d", p[i]);

    // Set color for each process (different color for each process)
    setcolor(BLUE + i);
    setfillstyle(SOLID_FILL, BLUE + i);

    // Draw rectangle for each process
    rectangle(timeX, y, timeX + width, y + height);
    floodfill(timeX + width / 2, y + height / 2, BLUE + i);

    // Label the process inside the rectangle
    outtextxy(timeX + 25, y + 15, processLabel);

    // Print Arrival Time (AT), Burst Time (BT), and Completion Time (CT) below each box
    char atLabel[20], btLabel[20], ctLabel[20];

    // Move the timeX for the next process
    timeX += width + 10;  // Spacing between bars
}

// Draw time axis labels
timeX = 50;  // Reset time axis starting point
for (int i = 0; i < n; i++) {
    char timeLabel[10];
    sprintf(timeLabel, "%d", ct[i]);
    outtextxy(timeX + 15, y + height + 40, timeLabel);  // Display CT values below bars
    timeX += width + 10;
}

getch();
closegraph();

}

// Main function to take input and run the scheduling
int main() {
printf("Enter the number of processes: ");
int n;
scanf("%d", &n);

int p[n], at[n], bt[n], ct[n];

printf("For every process, enter process identifier, AT, BT\n");
for (int i = 0; i < n; i++) {
    printf("For process %d\n", i + 1);
    scanf("%d", &p[i]);
    scanf("%d", &at[i]);
    scanf("%d", &bt[i]);
}

// Sort processes based on Arrival Time (AT)
for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
        if (at[i] > at[j]) {
            swap(&at[i], &at[j]);
            swap(&bt[i], &bt[j]);
            swap(&p[i], &p[j]);
        }
    }
}

// Calculate Completion Time (CT)
ct[0] = at[0] + bt[0];
for (int i = 1; i < n; i++) {
    if (ct[i - 1] >= at[i])
        ct[i] = ct[i - 1] + bt[i];
    else
        ct[i] = at[i] + bt[i];
}

// Print the scheduling table
printf("P\tAT\tBT\tCT\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
    printf("P%d\t%d\t%d\t%d\t%d\t%d\n", p[i], at[i], bt[i], ct[i], ct[i] - at[i], ct[i] - at[i] - bt[i]);
}

// Call the function to plot the Gantt chart
plotGraph(n, p, at, bt, ct);

return 0;

}

Edit

Pub: 03 Feb 2025 10:54 UTC

Views: 13