preemptive scheduling algorithm using Priority

0 383

Write the simulation program for preemptive scheduling algorithm using Priority. The arrival time, first CPU bursts and process priority of different jobs should be input to the system. The output should give the Gantt chart and Turnaround Time for each process and average times.

#include<stdio.h>

#include<string.h>

 

struct process

{

char pname[20];

int bt;

int at;

int tempbt;

int wt;

int tat;

int ct;

int pri;

}p[10];

 

int N; //total no.of processes

int total_wt,total_tat;

 

void AcceptProcessInfo()

{

int i;

printf(“\n Enter the no. of process:”);

scanf(“%d”,&N);

for(i=0;i<N;i++)

{

printf(“\n Enter the proces name:”);

scanf(“%s”,p[i].pname);

printf(“\n Enter the AT:”);

scanf(“%d”,&p[i].at);

printf(“\n Enter the BT:”);

scanf(“%d”,&p[i].bt);

printf(“\n Enter the Priority:”);

scanf(“%d”,&p[i].pri);

p[i].tempbt=p[i].bt;

}

}

void SortProcess()

{

char tname[10];

int temp;

int i,j;

for(i=0;i<N;i++)

for(j=0;j<N;j++)

{

if(p[i].pri>p[j].pri)

{

//swap pname

strcpy(tname,p[i].pname);

strcpy(p[i].pname,p[j].pname);

strcpy(p[i].pname,tname);

 

//swap At

temp=p[i].at;

p[i].at=p[j].at;

p[j].at=temp;

 

//swap BT

temp=p[i].bt;

p[i].bt=p[j].bt;

p[j].bt=temp;

 

//swap tempbt

temp=p[i].tempbt;

p[i].tempbt=p[j].tempbt;

p[j].tempbt=temp;

 

//swap priority

temp=p[i].pri;

p[i].pri=p[j].pri;

p[j].pri=temp;

 

 

}

}

}

 

void DisplayProcessInfo()

{

int i;

printf(“\n————————————-\n”);

printf(“\nPname\tAt\tBT\tPriority\tTAT\tWT\n”);

printf(“\n————————————-\n”);

for(i=0;i<N;i++)

{

printf(“\n%s\t%d\t%d\t%d\t%d\t%d”,p[i].pname,p[i].at,p[i].bt,p[i].pri,p[i].tat,p[i].wt);

}

printf(“\n————————————-\n”);

printf(“\n Total TAT:%d”,total_tat);

printf(“\n Total WT:%d\n”,total_wt);

}

void sjf_np()

{

int i,j;

int time=0;

int finish=0;

int flag=0;

while(finish!=1)

{

for(i=0;i<N;i++)

{

if(p[i].tempbt>0 && p[i].at<=time)

{

flag=1;

printf(“|%d %s”,time,p[i].pname);

time=time+p[i].bt;

printf(” %d|”,time);

p[i].ct=time;

p[i].tempbt=0;

}

}

if(flag==0)

{

printf(“|%d##”,time);

time++;

printf(” %d|”,time);

}

for(j=0;j<N;j++)

{

if(p[j].tempbt==0)

continue;

else

break;

}

if(j==N)

finish=1;

 

}

}

void Calculate_tat_wt()

{

int i;

for(i=0;i<N;i++)

{

p[i].tat=p[i].ct-p[i].at;

total_tat=total_tat+p[i].tat;

p[i].wt=p[i].tat-p[i].bt;

total_wt=total_wt+p[i].wt;

}

}

 

int main()

{

AcceptProcessInfo();

SortProcess();

sjf_np();

Calculate_tat_wt();

DisplayProcessInfo();

return 0;

}

 

 

 

*********************  Output: *************************

 

 

Enter the no. of process:6

 

Enter the proces name:p1

 

Enter the AT:0

 

Enter the BT:5

 

Enter the Priority:3

 

Enter the proces name:p2

 

Enter the AT:0

 

Enter the BT:3

 

Enter the Priority:4

 

Enter the proces name:p3

 

Enter the AT:0

 

Enter the BT:9

 

Enter the Priority:5

 

Enter the proces name:p4

 

Enter the AT:0

 

Enter the BT:8

 

Enter the Priority:9

 

Enter the proces name:p5

 

Enter the AT:0

 

Enter the BT:2

 

Enter the Priority:8

 

Enter the proces name:p6

 

Enter the AT:0

 

Enter the BT:4

 

Enter the Priority:1

|0 p1 8||8 p2 10||10 p3 19||19 p4 22||22 p5 27||27 p6 31|

——————————————————–

 

Pname   At      BT      Priority        TAT     WT

 

——————————————————–

 

p1      0       8       9       8       0

p2      0       2       8       10      8

p3      0       9       5       19      10

p4      0       3       4       22      19

p5      0       5       3       27      22

p6      0       4       1       31      27

——————————————————–

 

Total TAT:117

Total WT:86

 

Write the simulation program for non-preemptive scheduling algorithm using Priority. The arrival time, first CPU bursts and process priority of different jobs should be input to the system. The output should give the Gantt chart and waiting Time for each process and average time.

#include<stdio.h>

#include<string.h>

 

struct process

{

char pname[20];

int bt;

int at;

int tempbt;

int wt;

int tat;

int ct;

int pri;

}p[10];

 

int N;//total no.of process

int total_wt,total_tat;

 

void AcceptProcessInfo()

{

int i;

printf(“\n Enter the no.of process:”);

scanf(“%d”,&N);

for(i=0;i<N;i++)

{

printf(“\n Enter the process name:”);

scanf(“%s”,p[i].pname);

printf(“\n Enter the AT:”);

scanf(“%d”,&p[i].at);

printf(“\n Enter the BT:”);

scanf(“%d”,&p[i].bt);

printf(“\n Enter the priority:”);

scanf(“%d”,&p[i].pri);

p[i].tempbt=p[i].bt;

}

}

void SortProcess()

{

char tname[10];

int temp;

int i,j;

for(i=0;i<N;i++)

for(j=0;j<N;j++)

{

if(p[i].pri>p[j].pri)

{

//swap pname

strcpy(tname,p[i].pname);

strcpy(p[i].pname,p[j].pname);

strcpy(p[j].pname,tname);

 

//swap At

temp=p[i].bt;

p[i].bt=p[j].bt;

p[j].bt=temp;

//swap at

temp=p[i].at;

p[i].at=p[j].at;

p[j].at=temp;

}

}

}

void DisplayProcessInfo()

{

int i;

printf(“\n————————\n”);

printf(“\n Pname\tAt\tBt\tPriority\tTat\tWt\n”);

printf(“\n—————————-\n”);

for(i=0;i<N;i++)

{

printf(“\n%s\t%d\t%d\t%d\t%d\t%d”,p[i].pname,

p[i].at,p[i].bt,p[i].pri,p[i].tat,p[i].wt);

}

printf(“\n——————————–\n”);

printf(“\n Total TAT:%d”,total_tat);

printf(“\n Total Wt:%d”,total_wt);

}

void Sjf_np()

{

int i,j;

int time=0;

int finish=0;

int flag=0;

while(finish!=1)

{

for(i=0;i<N;i++)

{

if(p[i].tempbt>0&&p[i].at<=time)

{

flag=1;

printf(“%d%s”,time,p[i].pname);

time=time+p[i].bt;

printf(“%d”,time);

p[i].ct=time;

p[i].tempbt=0;

}

}

if(flag==0)

{

printf(“%d##”,time);

time++;

printf(“%d”,time);

}

if(j==N)

finish=j;

}

}

void Calculate_tat_wt()

{

int i;

for(i=0;i<N;i++)

{

p[i].tat=p[i].ct+p[i].at;

total_tat=total_tat+p[i].tat;

p[i].wt=p[i].tat+p[i].bt;

total_wt=total_wt+p[i].wt;

}

}

void main()

{

AcceptProcessInfo();

SortProcess();

Sjf_np();

Calculate_tat_wt();

DisplayProcessInfo();

}

 

 

*********************  Output: *************************

 

 

Enter the no.of process: 3

 

Enter the process name: P1

 

Enter the AT: 1

 

Enter the BT: 3

 

Enter the priority: 1

 

Enter the process name: P2

 

Enter the AT: 2

 

Enter the BT: 2

 

Enter the priority: 2

 

Enter the process name: P3

 

Enter the AT: 0

 

Enter the BT: 5

 

Enter the priority: 3

0P355P277P110

Leave A Reply

Your email address will not be published.