操作系统五大经典算法代码

最佳置换OPI算法

#include<stdio.h>
struct OPT	
{
   int Data[100];
   int make;
}bel[10];
int y[100];
int minlong;
int take=0; //从后往前查找的序列数
int k=0;
int main()
{
	int i,j,z;
	int p=1;
	int n1,n2;
	printf("分配物理块数量:");
	scanf("%d",&n1);
	printf("页面号引用串数量:");
	scanf("%d",&n2);
    for(i=1;i<=n2;i++)       scanf("%d",&y[i]);
	    for(j=1;j<=n1;j++)       bel[j].make=0;  
		for(j=1;j<=n1;j++)  {  bel[p].Data[j]=y[j]; printf("%2d",bel[p].Data[j]);	}  //先把前三个页面号插入物理块中
		printf("\n");  
        for(j=n1+1;j<=n2;j++)
		{    
		    for(i=1;i<=3;i++)   if(y[j]!=bel[p].Data[i])  k++; 
		    if(k==3) 
		    {	
	    for(i=n2;i>j;i--)    for(z=1;z<=n1;z++)   if(bel[p].Data[z]==y[i]) {  take++;   bel[z].make=take; }   
        minlong=bel[1].make;  
		for(i=2;i<=n1;i++)    if(bel[i].make<minlong)       minlong=bel[i].make;
        for(i=1;i<=n1;i++)    if(bel[i].make==minlong)   {  bel[p].Data[i]=y[j];   break;	} 
		for(i=1;i<=n1;i++)    printf("%2d",bel[p].Data[i]);   printf("\n");	 
		for(i=1;i<=n1;i++)       bel[i].make=0;  
		take=0;
			}
	     k=0;
		}
		return 0;
}

银行家算法

#include<stdio.h>
struct Dijkstra
{
    int Max[10]; 
	int Allocation[10];
	int Available[10];
    int Work[10];
}Dij[10];
int a[10];
int f[10];
int main()
{   
	int k=1,w=1;
	int count=0;
	int i,j,g,q,p,z;
	int n1,n2;
	printf("输入系统中的进程数目:");
    scanf("%d",&n1);
    printf("输入资源数目:");
    scanf("%d",&n2);
//    
	for(i=1;i<=n1;i++)
	{	
       printf("第%d个进程需要这%d类资源的最大数目",i,n2);
		for(j=1;j<=n2;j++)
		{
			scanf("%d",&Dij[i].Max[j]);
		}
	}
    for(i=1;i<=n1;i++)
	{	
       printf("第%d个进程已分得这%d类资源的数目",i,n2);
		for(j=1;j<=n2;j++)
		{
			scanf("%d",&Dij[i].Allocation[j]);
		}
	} 
	printf("分得这%d种资源数量分别为:",n2);
      for(i=1;i<=n2;i++)	  scanf("%d",&Dij[0].Available[i]);
	for(i=1;i<=n2;i++)  
	{	
		for(j=1;j<=n1;j++) 
		Dij[0].Available[i]=Dij[0].Available[i]-Dij[j].Allocation[i];
	}
   for(i=1;i<=n2;i++)
   	for(j=1;j<=n1;j++) 
   	{
   		Dij[i].Work[j]=Dij[0].Available[j];
	}
	for(p=1;p<=n1;p++)
{
	if(k==(n1+1))
	{ for(q=1;q<=n1-1;q++) { printf("%d->",a[q]);	  } printf("%d",a[n1]);  break;  } 
	else
	{ 
	for(i=1;i<=n1;i++)
	{	
	   for(j=1;j<=n2;j++)  if(Dij[i].Allocation[j]+Dij[w].Work[j]>=Dij[i].Max[j])     count++;
	if(count==n2&&f[i]==0)
	{  a[k]=i;
	   k++;
	   f[i]=1;
	    for(z=1;z<=n2;z++)
    Dij[i].Work[z]=Dij[w].Work[z]+Dij[i].Allocation[z];
    w=i;
	}
    }
    }
}  
	return 0;
}

先进先出FIFO算法

#include<stdio.h>
struct FIFO
{
   int Data[100];
}bel[10];
int y[100];
int take=0; 
int k=0;
int main()
{
	int i,j;
	int p=1,count;
	int n1,n2;
	printf("分配物理块数量:");
	scanf("%d",&n1);
	printf("页面号引用串数量:");
	scanf("%d",&n2);
    for(i=1;i<=n2;i++)       scanf("%d",&y[i]); 
		for(j=1;j<=n1;j++)  {  bel[p].Data[j]=y[j]; printf("%2d",bel[p].Data[j]);	}		printf("\n");  
        for(j=n1+1;j<=n2;j++)
		{    
		    for(i=1;i<=3;i++)   if(y[j]!=bel[p].Data[i])  k++; 
		    if(k==3) 
		    {	
				take++;
				count=take%3;
				if(count!=0)
				{ bel[p].Data[count]=y[j]; for(i=1;i<=n1;i++)    printf("%2d",bel[p].Data[i]); printf("\n");	  }
                else
				{		bel[p].Data[3]=y[j];  for(i=1;i<=n1;i++)    printf("%2d",bel[p].Data[i]); printf("\n");	   }
			
			}
	     k=0;
		}
		return 0;
}

先到先服务算法


#include<stdio.h>
struct FCFS
{
	int arrive;
	int serve;
	int start;
	int finish;
	int turn; 
}a[10],b[10];
int n;
int main()
{
	int i,k,j;
	int tmp;
	int c[11]={0,1,2,3,4,5,6,7,8,9,10};
	printf("请输入进程数:");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{	printf("\n");
		printf("第%d个进程\n",i);
	    printf("到达时间:");
		scanf("%d",&a[i].arrive);
        printf("服务时间:");
		scanf("%d",&a[i].serve);
	}
	for(i=1;i<=n;i++)
	{
	  k=i;
      for(j=i+1 ;j<=n; j++) { 
      if(a[k].arrive>=a[j].arrive) k = j;  
    }  
      b[i].arrive=a[k].arrive;
      b[i].serve=a[k].serve;
      tmp = a[i].arrive;  a[i].arrive = a[k].arrive; a[k].arrive = tmp;
    }
    printf("程序  到达时间  运行时间  开始时间  完成时间  周转时间\n");
	 b[1].start=b[1].arrive;
	 b[1].finish=b[1].serve+b[1].arrive;
	 b[1].turn=b[1].finish-b[1].arrive;
	  printf("%2.d%10.d%10d%10.d%10.d%10.d\n",c[1],b[1].arrive,b[1].serve,b[1].start,b[1].finish,b[1].turn);
     for(i=2;i<=n;i++)
	 {	 
	
         b[i].start=b[i-1].finish;
         if(b[i].start<b[i].arrive)
         {
         	b[i].finish=b[i].arrive+b[i].serve;
         	b[i].turn=b[i].finish-b[i].arrive;
         	printf("%2.d%10.d%10d%10.d%10.d%10.d\n",c[i],b[i].arrive,b[i].serve,b[i].arrive,b[i].finish,b[i].turn);
		 }
		 else
	     { 
		 b[i].finish=b[i].start+b[i].serve;
	     b[i].turn=b[i].finish-b[i].arrive;
		 printf("%2.d%10.d%10d%10.d%10.d%10.d\n",c[i],b[i].arrive,b[i].serve,b[i].start,b[i].finish,b[i].turn);
	     }
	 }
	return 0;
}

短作业优先算法


#include<stdio.h>
struct SJF
{
	int arrive;
	int serve;
	int start;
	int finish;
	int turn; 
}a[10],b[10];
int n;
int main()
{
	int i,k,j,f;
	int tmp,tep;
	int c[11]={0,1,2,3,4,5,6,7,8,9,10};
	printf("请输入进程数:");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{	printf("\n");
		printf("第%d个进程\n",i);
	    printf("到达时间:");
		scanf("%d",&a[i].arrive);
        printf("服务时间:");
		scanf("%d",&a[i].serve);
	}
	for(i=1;i<=n;i++)
	{
	  k=i;
      for(j=i+1 ;j<=n; j++) { 
      if(a[k].serve>=a[j].serve) k = j;  
    }  
      b[i].arrive=a[k].arrive;
      b[i].serve=a[k].serve;
      tmp = a[i].serve;  a[i].serve= a[k].serve; a[k].serve = tmp;
    }
    for(f=1;f<=n;f++)
    {
    	k=f;
    	 for(j=f+1 ;j<=n; j++) { 
      if(b[k].arrive>b[j].serve+b[j].arrive) k = j;  
    } 
	  tep = b[f].arrive;  b[f].arrive= a[k].arrive; a[k].arrive = tep;
      tmp = b[f].serve;  b[f].serve= b[k].serve; b[k].serve = tmp;
	}
    printf("程序  到达时间  运行时间  开始时间  完成时间  周转时间\n");
	 b[1].start=b[1].arrive;
	 b[1].finish=b[1].serve+b[1].arrive;
	 b[1].turn=b[1].finish-b[1].arrive;
	  printf("%2.d%10.d%10d%10.d%10.d%10.d\n",c[1],b[1].arrive,b[1].serve,b[1].start,b[1].finish,b[1].turn);
     for(i=2;i<=n;i++)
	 {	 
	
         b[i].start=b[i-1].finish;
         if(b[i].start<b[i].arrive)
         {
         	b[i].finish=b[i].arrive+b[i].serve;
         	b[i].turn=b[i].finish-b[i].arrive;
         	printf("%2.d%10.d%10d%10.d%10.d%10.d\n",c[i],b[i].arrive,b[i].serve,b[i].arrive,b[i].finish,b[i].turn);
		 }
		 else
	     { 
		 b[i].finish=b[i].start+b[i].serve;
	     b[i].turn=b[i].finish-b[i].arrive;
		 printf("%2.d%10.d%10d%10.d%10.d%10.d\n",c[i],b[i].arrive,b[i].serve,b[i].start,b[i].finish,b[i].turn);
	     }
	 }
	return 0;
}

上一篇:vue项目启动时,npm run serve 和 npm run dev 的区别


下一篇:【前端52_Vue】工程化