Week 10 OS Task
Week 10 OS Task
SHORTEST-JOB-FIRST-SCHEDULING(SJFS)
The SJF scheduling algorithm is provably optimal, in that it gives the minimum
average waiting time for a given set of processes. Moving a short process before
a long one decreases the waiting time of the long process. Consequently, the
average waiting time decreases.
PROGRAM:
USING POINTERS:
#include<stdio.h> //HEADER FILES
#include<malloc.h>
#include<string.h>
typedefstruct node //STRUCTURE
{
char prss[3];
int burst;
struct node *next;
}node;
node *front=NULL; //GLOBAL VARIABLES
node *rear=NULL;
void insert(); //FUNCTION DECLARATION
void display(int);
void main() //MAIN FUNCTION
{
inti,n;
printf("\nEnter number of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++) //LOOP
insert(); //FUNCTION CALL
printf("\n\nExecuting processes : \n");
display(n); //FUNCTION CALL
printf("\n");
} //END OF MAIN
printf("\n---------------------------------------------------\n\t");
while(temp!=NULL)
{
printf("|\t%s\t",temp->prss);
temp=temp->next;
}
printf("\n---------------------------------------------------------------
--\n\t");
temp=front;
while(temp!=NULL)
{
printf(" \t%d\t ",temp->burst);
temp=temp->next;
}
printf("\n---------------------------------------------------------------
-\n\t");
temp=front;
printf("0\t");
while(temp!=NULL)
{
wttime+=c;
turn+=c+temp->burst;
c=c+temp->burst;
printf(" \t%d\t ",c);
temp=temp->next;
}
printf("\n---------------------------------------------------------------
------\n");
printf("\n\nAveragewt time = %f ",wttime/n);
printf("\nTurn around time = %f\n",turn/n);
}
}
PROGRAM:
USINGARRAYS:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
char p[10][5],temp[5]; int
c=0,pt[10],i,j,n,temp1; float
bst=0.0,turn=0.0; clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
printf("\n.....................................................\n");
for(i=0;i<n;i++)
{
printf("|\t %s\t",p[i]);
}
printf("|\n.....................................................\n");
for(i=0;i<n;i++)
{
printf("\t\t%d",pt[i]);
}
printf("\n.....................................................\n");
printf("0");
for(i=0;i<n;i++)
{
bst+=c;
turn+=c+pt[i];
c=c+pt[i];
printf("\t\t%d",c);
}
printf("\nAverage time is %f: ",bst/n); printf("\nTurn around time is %f",
turn/n); getch();
OUTPUT:
Executing processes :
------------------------------------------------------------------------------------------
| P2 | P3 | P1 |
------------------------------------------------------------------------------------------
2 3 24
------------------------------------------------------------------------------------------
0 2 5 29