2

我为我的主题制作了一个程序,用 C 语言编写的 OS 时间调度程序。我刚刚开始,但我的问题是如何显示甘特图。而且我对先到先得的先发制人有点困惑。我了解它是如何工作的,但是如果到达时间是……

PROCESS  BURST TIME   ARRIVAL TIME  START   WAITING   FINISH   TURN AROUND TIME
P1         12            2            ?????
P2         6             1            ?????
P3         9             0            ?????

但是如果到达时间从0开始,我知道如何回答它......

PROCESS  BURST TIME   ARRIVAL TIME  START   WAITING   FINISH   TURN AROUND TIME
P1         12            0            0      0         12           12
P2         6             1            12     11        18           17
P3         9             4            18     14        27           23

average waiting time = 8.33
average turn around time= 17.33

这是我的代码,我刚刚将一些代码放到了互联网上。它还没有只完成我拥有的 FCFS 代码....

#include <stdio.h>
#include <stdlib.h> 

//FCFS
void npefcfs();
void pefcfs();
void fcfs();
//MENU
void menu();
//y/n statement
void backq();


int main()
{
menu();
system("pause");
return 0;
}

void menu()
{
int choose;
printf("    MENU OS Time Scheduling Discipline");
printf("\n  ---------------------------------------------------");
printf("\n  [1] (FCFS)First Come, First Serve");
printf("\n  [2] (SJF)Shortest Job First");
printf("\n  [3] (SRT)Shortest Remaining Time");
printf("\n  [4] (RR)Round-robin Scheduling");
printf("\n  [5] (MLQ)Multilevel queue scheduling");
printf("\n  [6] Priority");
printf("\n  [7] EXIT");
printf("\n\nEnter your choice: ");
scanf("%d", &choose);
switch (choose)
{
case 1:
    system("cls");
    fcfs();
    break;
case 7:
    break;
default:
    system("cls");
    printf("Please Enter Correct Value!\n\n");
    menu();
    break;
}

}

//y/n statement
void backq()
{
char choice2;
printf("Do you like another try? y/n, Y/N: ");
scanf("%s", &choice2);
switch (choice2)
{
case 'y':
    system("cls");
    menu();
    break;
case 'Y':
    system("cls");
    menu();
    break;
case 'n':
    system("exit");
    break;
case 'N':
    system("exit");
    break;
default:
    system("cls");
    printf("\nPlease Enter a coorect value!");
    backq();
    break;
}
}

//fcfs menu
void fcfs()
{
int choice;
printf("FIRST COME, FIRST SERVE");
printf("\n-----------------------------");
printf("\n  [1] Non Pre-emptive");
printf("\n  [2] Pre-emptive");
printf("\n  [3] BACK");
printf("\n  [4] Exit");
printf("\nPlease your choice: ");
scanf("%d",&choice);
switch (choice)
{
case 1:
    system("cls");
    npefcfs();
    break;
case 2:
    system("cls");
    pefcfs();
    break;
case 3:
    system("cls");
    menu();
    break;
case 4:
    break;
default:
    break;
}
}
//function for non pre-emptive fcfs
void npefcfs()
{
//declaration
int process;
int bt[10], at[10], start[10], wait[10], fnsh[10], ta[10];
int i, a, sum = 0;
float awt=0.0, att=0.0;
start[0] = 0;
//how many process
printf("How many process do you like to use: ");
scanf("%d",&process);
printf("\n-----------------------------------------");
printf("\nPlease Enter Burst time: \n\n");

//for declaring zero to at[] array
for(i=0; i<process; i++)
{
    at[i]=0;
}
//enter burst time depending on how many process
for (i = 0, a=1; i < process,a<=process; i++,a++)
{
    printf("P%d =",a);
    scanf("%d", &bt[i]);
}
//for calculation of fcfs
for(i=0; i<process; i++)
{
    sum=0;
    for(a=0; a<i; a++)
    {
        sum=sum+bt[a];
        start[i]=sum;
    }
}
//for calculation of start,wait,finish,turn around time, average waiting time and average turn around time
for(i=0; i<process; i++)
{
    wait[i] = start[i];
    fnsh[i] = bt[i]+start[i];
    ta[i] = fnsh[i];
    awt+=wait[i];
    att+=ta[i];
}
//computation
awt/=process;
att/=process;

//output in table like form
printf("\n\tProcess\tBT\tAT\tSTART\tWAIT\tFINISH\tTA");
printf("\n\t-------\t--\t--\t-----\t----\t------\t--");
for(i=0; i<process; i++)
{
    printf("\n\tP%d\t%d\t%d\t%d\t%d\t%d\t%d", (i+1),bt[i],at[i],start[i],wait[i],fnsh[i],ta[i]);
}
printf("\n\nAverage Waiting Time: %f", awt);
printf("\nAverage Turn Around Time: %f\n",att);
backq();
}
//function for pre-emptive fcfs
void pefcfs()
{
//declaration
int process;
int bt[10], at[10], start[10], wait[10], fnsh[10], ta[10];
int i, a, sum = 0;
float awt=0.0, att=0.0;
start[0] = 0;
//how many process
printf("How many process do you like to use: ");
scanf("%d",&process);
printf("\n-----------------------------------------");
printf("\nPlease Enter Burst Time: \n\n");

//enter burst time depending on how many process
for (i = 0, a=1; i < process,a<=process; i++,a++)
{
    printf("P%d =",a);
    scanf("%d", &bt[i]);
}
printf("\nPlease Enter Arrival Time: \n");

//enter arrival time
for(i=0; i<process; i++)
{
    printf("AT%d =", (i+1));
    scanf("%d", &at[i]);
}

//for calculation of fcfs
for(i=0; i<process; i++)
{
    sum=0;
    for(a=0; a<i; a++)
    {
        sum=sum+bt[a];
        start[i]=sum;
    }
}
//for calculation of start,wait,finish,turn around time, average waiting time and average turn around time
for(i=0; i<process; i++)
{
    wait[i] = start[i]-at[i];
    fnsh[i] = bt[i]+start[i];
    ta[i] = fnsh[i]-at[i];
    awt+=wait[i];
    att+=ta[i];
}
//computation
awt/=process;
att/=process;

//output in table like form
printf("\n\tProcess\tBT\tAT\tSTART\tWAIT\tFINISH\tTA");
printf("\n\t-------\t--\t--\t-----\t----\t------\t--");
for(i=0; i<process; i++)
{
    printf("\n\tP%d\t%d\t%d\t%d\t%d\t%d\t%d", (i+1),bt[i],at[i],start[i],wait[i],fnsh[i],ta[i]);
}
printf("\n\nAverage Waiting Time: %f", awt);
printf("\nAverage Turn Around Time: %f\n",att);
backq();
}

而且我不知道如何根据突发时间显示甘特图,例如

______________________________
|     p1      |    p2   | p3 |
|_____________|_________|____|
0             12        18    27
4

1 回答 1

1

这些是我用 C 实现的一些抢占式和非抢占式调度算法,它们肯定对你有用。

非抢占式 SJF

抢先 SJF

这些算法包含用于绘制甘特图和计算过程序列的单独 C 函数。

void drawGanttChart()
{
    const int maxWidth=100;
    int scalingFactor,i,counter,tempi,currentTime;
    printf("The gantt chart for the given processes is : \n\n");

    scalingFactor=maxWidth/totalCPUBurstTime;
    for(i=0;i<scalingFactor*totalCPUBurstTime+2+numberOfProcesses;i++)
    {
        printf("-");
    }
    printf("\n|");
    counter=0,tempi=0;
    for(i=0;i<scalingFactor*totalCPUBurstTime;i++)
    {
        if(i==CPUBurstTime[counter]*scalingFactor+tempi)
        {
            counter++;
            tempi=i;
            printf("|");
        }
        else if(i==(CPUBurstTime[counter]*scalingFactor)/2+tempi)
        {
            printf("P%d",processNumber[counter]);
        }
        else
        {
            printf(" ");
        }

    }
    printf("|");
    printf("\n");
    for(i=0;i<scalingFactor*totalCPUBurstTime+2+numberOfProcesses;i++)
    {
        printf("-");
    }
    printf("\n");

    /* printing the time labels of the gantt chart */ 
    counter=0;
    tempi=0;
    currentTime=minimumArrivalTime;
    printf("%d",currentTime);
    for(i=0;i<scalingFactor*totalCPUBurstTime;i++)
    {
        if(i==CPUBurstTime[counter]*scalingFactor+tempi)
        {

            tempi=i;
            currentTime+=CPUBurstTime[counter];
            averageWaitingTime+=currentTime;
            counter++;
            printf("%2d",currentTime);
        }
        else
        {
            printf(" ");
        }
    }
    currentTime+=CPUBurstTime[counter];

    printf("%2d\n\n",currentTime);
    averageWaitingTime=averageWaitingTime/numberOfProcesses;
    printf("Average waiting Time : %f\n",averageWaitingTime);
}
于 2014-08-17T12:21:00.770 回答