0% found this document useful (0 votes)
5 views5 pages

Week 10 OS Task

The document explains the Shortest-Job-First Scheduling (SJFS) algorithm, which assigns the CPU to the process with the smallest next CPU burst, optimizing average waiting time. It includes two implementations of the algorithm: one using pointers and another using arrays, both demonstrating how to insert processes and calculate average waiting and turnaround times. An example output is provided, showing the execution of three processes with their respective burst times and calculated averages.

Uploaded by

rydhamgupta84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Week 10 OS Task

The document explains the Shortest-Job-First Scheduling (SJFS) algorithm, which assigns the CPU to the process with the smallest next CPU burst, optimizing average waiting time. It includes two implementations of the algorithm: one using pointers and another using arrays, both demonstrating how to insert processes and calculate average waiting and turnaround times. An example output is provided, showing the execution of three processes with their respective burst times and calculated averages.

Uploaded by

rydhamgupta84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

WEEK 10

SHORTEST-JOB-FIRST-SCHEDULING(SJFS)

A different approach to CPU scheduling is the shortest-job-first-


scheduling(SJFS) algorithm. This algorithm associates with each process the
length of the process’s next CPU burst. When the CPU is available, it is assigned
to the process that has the smallest next CPU burst. If the next bursts of two
processes are the same,FCFS is used to break the tie.

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

void insert() //FUNCTION DEFINITION


{
node *p,*temp;
int b;
p=(node*)malloc(sizeof(node)); //DYNAMIC MEMORY
ALLOCATION
printf("\n\tEnter the process name : ");
scanf("%s",p->prss);
printf("\tEnter Burst time : ");
scanf("%d",&b);
p->burst=b;
p->next=NULL;
if(front==NULL) //IF FIRST ELEMENT
{
front=p;
rear=p;
}
else if( p->burst < front->burst)
{
p->next=front;
front=p;
}
else if( p->burst > rear->burst)
{
rear->next=p;
rear=p;
}
else
{
temp=front;
while( p->burst > (temp->next)->burst )
temp=temp->next;
p->next=temp->next;
temp->next=p;
}
}

void display(int n) //FUNCTION DEFINITION


{
node *temp=front;
int c=0;
float turn=0.0,wttime=0.0;
if(front!=NULL)
{

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:

Enter number of processes : 3

Enter the process 1 name : P1


Enter Burst time : 24

Enter the process 2 name : P2


Enter Burst time : 2

Enter the process 3 name : P3


Enter Burst time : 3

Executing processes :

------------------------------------------------------------------------------------------
| P2 | P3 | P1 |
------------------------------------------------------------------------------------------
2 3 24
------------------------------------------------------------------------------------------
0 2 5 29

Average wt time = 2.333333


Turnaround time = 12.000000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy