Operating System: Assignment
Operating System: Assignment
COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING (Autonomous Institution Affiliated to VTU) Bangalore 560 059
Operating System
Assignment
By Abhishek R 1RV09CS004
Question: Write a C program to print the schedule for the following example using non-preemptive priority scheduling algorithm. Also compute the average waiting time.
Process
P1 P2 P3 P4
Arrival time
0 1 2 3
Burst time
8 4 9 5
Priority
3 4 1 2
Solution:
#include<stdio.h> #include<stdlib.h> struct job { char name[5]; int a,b,s,w,f,v,p; }x[20]; int t[20]; int n; float avgw; int exect=0; void scheduler(void); void waitingtime(void); void display(void); int main() { int i; printf("Program to schedule processes according to non-preemptive priority scheduling \n"); printf("Enter the number of processes: "); scanf("%d",&n); printf("\n"); for(i=0;i<n;i++) { printf("Enter process name, arrival time, burst time, priority for job %d:\n",i+1); scanf("%s %d %d %d",&x[i].name,&x[i].a,&x[i].b,&x[i].p); x[i].v=0; } scheduler(); waitingtime(); display(); return 0; } void waitingtime() { int i; avgw=0; for(i=0;i<n;i++) { x[i].w=x[i].s-x[i].a; avgw=avgw+x[i].w; } avgw=avgw/n; } void display() { int i,j; printf("\n\nSchedule for the processes: \n\n"); printf("Process Arrival time Burst time Priority
for(j=0;j<n;j++) { i=t[j]; printf("%s\t %d\t %d\t\t %d\t\t %d\t\t %d \n" ,x[i].name,x[i].a,x[i].b,x[i].p,x[i].s,x[i].f); } printf("\n\n Enter 1 to display the waiting time of processes, Enter 0 to exit: "); scanf("%d",&i); if(i==1) { printf("\n\tProcess Waiting time \n"); for(j=0;j<n;j++) { printf("\t%s\t\t %d \n " ,x[j].name,x[j].w); } printf("\n Average waiting time=%.2f \n",avgw); } } void scheduler() { int c=-1; int i,j; for(j=0;j<n;j++) { c=-10; for(i=0;i<n;i++) { if(x[i].v==1||x[i].a>exect) continue; else { if(c<0) { c=i; } else { if(x[i].p<x[c].p) { c=i; } else if(x[i].p==x[c].p) { if(x[i].a<x[c].a) { c=i; } } else; } } } if(c<0)
{ i=0; while(i<n) { if(x[i].v!=1) { if(c<0) c=i; else { if(x[i].a<x[c].a) c=i; else if(x[i].a==x[c].a) { if(x[i].p<x[c].p) c=i; } else; } } i++; } x[c].s=x[c].a; exect=x[c].s+x[c].b; x[c].f=exect; x[c].v=1; } else { x[c].s=exect; exect=exect+x[c].b; x[c].f=exect; x[c].v=1; } t[j]=c; } }
Output :