OS time scheduling program

I making a program for my subject, OS time scheduling program written in C language. I'm just starting it out but my problem is how to display the GANTT chart. 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...

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....

#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("nnEnter your choice: ");
scanf("%d", &choose);
switch (choose)
{
case 1:
    system("cls");
    fcfs();
    break;
case 7:
    break;
default:
    system("cls");
    printf("Please Enter Correct Value!nn");
    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: nn");

//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("ntProcesstBTtATtSTARTtWAITtFINISHtTA");
printf("nt-------t--t--t-----t----t------t--");
for(i=0; i<process; i++)
{
    printf("ntP%dt%dt%dt%dt%dt%dt%d", (i+1),bt[i],at[i],start[i],wait[i],fnsh[i],ta[i]);
}
printf("nnAverage Waiting Time: %f", awt);
printf("nAverage Turn Around Time: %fn",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: nn");

//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("ntProcesstBTtATtSTARTtWAITtFINISHtTA");
printf("nt-------t--t--t-----t----t------t--");
for(i=0; i<process; i++)
{
    printf("ntP%dt%dt%dt%dt%dt%dt%d", (i+1),bt[i],at[i],start[i],wait[i],fnsh[i],ta[i]);
}
printf("nnAverage Waiting Time: %f", awt);
printf("nAverage Turn Around Time: %fn",att);
backq();
}

And I don't know how to display the GANTT chart depending on the burst time like for example

______________________________
|     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.

Non preemptive SJF

Preemptive SJF

These Algorithms contain separate C function for drawing Gantt Chart and Calculating Process sequence.

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

    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("%2dnn",currentTime);
    averageWaitingTime=averageWaitingTime/numberOfProcesses;
    printf("Average waiting Time : %fn",averageWaitingTime);
}
链接地址: http://www.djcxy.com/p/84862.html

上一篇: 最短剩余时间第一查询

下一篇: OS时间安排程序