⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sjf_p.cpp

📁 This program sorts words in a line of text by length.
💻 CPP
字号:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int n,Bu[20];
float Twt,Awt,Awt1,Awt2,A[10],Wt[10],w;

//Getting the No of processes & burst time
void Getdata();
//Shortest job First Algorithm - Preemptive
void SjfP();
//Shortest job First Algorithm - Non-preemptive
void SjfNp();


void main()
{
	clrscr();
	int ch,cho;
	cout<<"\n\t\tShortestJobFirst - Preemptive";
	cout<<"\n\n\n\t\tGetting Burst Time";
	Getdata();
	cout<<"\n\n\n\t\tGetting Arrival Time";
	SjfP();
	cout<<"\n\n\t\tShortestJobFirst - Non-Preemptive";
	SjfNp();
	if(Awt1<Awt2)
		cout<<"\n\nIn the present case , preemptive algorithm is better.";
	else if(Awt1>Awt2)
		cout<<"\n\nIn the present case , non-preemptive algorithm is better.";
	else
		cout<<"\n\nIn the present case , both algorithms perform equally well.";
	getch();
}

//Getting no of processes and Burst time
void Getdata()
{
	int i;
	cout<<"\n\nEnter the no of processes:";
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cout<<"\nEnter The BurstTime for Process p"<<i<<"=";
		cin>>Bu[i];
	}
}

//Shortest job First Algorithm with Preemption
void SjfP()
{
	int i,j,m,Wt[10],k,B[10],A[10],Tt=0,Wtm[10],temp;
	char S[20],start[20];
	int max=0,Time=0,min;
	float Twt=0.0,Awt;
	for(i=1;i<=n;i++)
	{
		B[i]=Bu[i];
		cout<<"\nBurst time for process P"<<i<<"="<<B[i];
		if(B[i]>max)
			max=B[i];
		Wt[i]=0;
		S[i]='T';
		start[i]='F';
		Tt=Tt+B[i];
		cout<<"\nEnter the Arrival Time for "<<i<<"th process=";
		cin>>A[i];
		if(A[i]>Time)
			Time=A[i];
	}
	int w=0,flag=0,t=0;
	i=1;
	while(t<Time)
	{
		if(A[i]<=t && B[i]!=0)
		{
			if(flag==0)
			{
				Wt[i]=Wt[i]+w;
				cout<<"\nWt["<<i<<"]="<<Wt[i];
			}
			B[i]=B[i]-1;
			if(B[i]==0)
				S[i]='F';
			start[i]='T';
			t++;
			w=w+1;
			if(S[i]!='F')
			{
				j=1;flag=1;
				while(j<=n && flag!=0)
				{
					if(S[j]!='F' && B[i]>B[j] && A[j]<=t && i!=j )
					{
						flag=0;
						Wt[i]=Wt[i]-w;
						i=j;
					}
					else
					{
						flag=1;
					}
					j++;
				}
			}
			else
			{
				i++;
				j=1;
				while(A[j]<=t &&j<=n)
				{
					if(B[i]>B[j] && S[j]!='F')
					{
						flag=0;
						i=j;
					}
					j++;
				}
			}
		}
		else
			if(flag==0)
			i++;
	}


	cout<<"\nPrinting remaining burst time";
	for(i=1;i<=n;i++)
		cout<<"\nB["<<i<<"]="<<B[i];
	cout<<"";

	while(w<Tt)
	{
		min=max+1;
		i=1;
		while(i<=n)
		{
			if(min>B[i] && S[i]=='T')
			{
				min=B[i];
				j=i;
			}
			i++;
		}
		i=j;
		if(w==Time && start[i]=='T')
		{
			w=w+B[i];
			S[i]='F';
		}
		else
		{
			Wt[i]=Wt[i]+w;
			w=w+B[i];
			S[i]='F';
		}
	}

cout<<"\nWeight info";

	for(i=1;i<=n;i++)
		cout<<"\nWT["<<i<<"]="<<Wt[i];
cout<<"\nafter subtracting arrival time";
	for(i=1;i<=n;i++)
	{
		Wt[i]=Wt[i]-A[i];
		cout<<"\nWT["<<i<<"]="<<Wt[i];
	}
	//Calculating Average Weighting time
	for(i=1;i<=n;i++)
		Twt=Twt+Wt[i];
	Awt=Twt/n;
	cout<<"\nAverage Weighting Time="<<Awt;
	Awt1=Awt;
}

void SjfNp()
{
	int i,B[10],Tt=0,temp,j;
	char S[10];
	float A[10],temp1,t;
	Twt=0.0;
	w=0.0;
	for(i=1;i<=n;i++)
	{
		B[i]=Bu[i];
		cout<<"\nBurst time for process p"<<i<<"=";
		cout<<B[i];
		S[i]='T';
		Tt=Tt+B[i];
		cout<<"\nEnter the Arrival Time for "<<i<<"th process=";
		cin>>A[i];
	}
	for(i=n;i>=1;i--)
	{
		for(j=3;j<=n;j++)
		{
			if(B[j-1]>B[j])
			{
				temp=B[j-1];
				temp1=A[j-1];
				B[j-1]=B[j];
				A[j-1]=A[j];
				B[j]=temp;
				A[j]=temp1;
			}
		}
	}

	for(i=1;i<=n;i++)
	{
		cout<<"\np"<<i<<"	"<<B[i]<<"	"<<A[i];
	}

	//For the 1st process
		Wt[1]=0;
		w=w+B[1];
		t=w;
		S[1]='F';

		while(w<Tt)
		{
			i=2;
			while(i<=n)
			{
				if(S[i]=='T'&&A[i]<=t)
				{
					Wt[i]=w;
					cout<<"\nWT"<<i<<"="<<Wt[i];
					S[i]='F';
					w=w+B[i];
					t=w;
					i=2;
				}
				else
					i++;
			}
		}

		for(i=1;i<=n;i++)
			cout<<"\nWt"<<i<<"=="<<Wt[i];


		//calculating average weighting Time
		for(i=1;i<=n;i++)
			Twt=Twt+(Wt[i]-A[i]);
		Awt=Twt/n;
	cout<<"\nTotal   Weighting Time="<<Twt<<"";
	cout<<"\nAverage Weighting Time="<<Awt<<"";
	Awt2=Awt;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -