简体   繁体   English

操作系统时间计划程序

[英]OS time scheduling program

I making a program for my subject, OS time scheduling program written in C language. 我为我的主题编写了一个程序,它是用C语言编写的OS时间计划程序。 I'm just starting it out but my problem is how to display the GANTT chart. 我只是开始,但是我的问题是如何显示GANTT图表。 And I'm little bit confuse about the Preemptive First Come, First Serve. 我对先发制人的先到先得服务感到困惑。 I understand how it works but when it comes if the ARRIVAL time is... 我知道它是如何工作的,但是如果到达时间是...

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

but if the arrival time start in 0, I know how to answer it... 但是如果到达时间从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

here my code, I just got some of my code to the internet. 在这里我的代码,我刚刚将一些代码上传到了互联网。 Its not yet done only the FCFS code I have.... 它还没有完成我拥有的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();
}

And I don't know how to display the GANTT chart depending on the burst time like for example 而且我不知道如何根据突发时间显示GANTT图表,例如

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

These are some of the Preemptive and non-preemptive scheduling algorithms that i implemented in C, they will definitely be useful to you. 这些是我在C语言中实现的一些抢先式和非抢先式调度算法,它们绝对对您有用。

Non preemptive SJF 非抢先SJF

Preemptive SJF 抢先SJF

These Algorithms contain separate C function for drawing Gantt Chart and Calculating Process sequence. 这些算法包含用于绘制甘特图和计算过程顺序的单独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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM