タイムスライス回転アルゴリズムシミュレーション

2283 ワード

これは私たちのオペレーティングシステムのカリキュラムが設計されたときに書いたタイムスライスの回転をシミュレートするアルゴリズムです.比較的簡単です.ハハハハ
コードは次のとおりです.
/*       
 author GODLUO
 1.      
 2.           
 3.            
  3.1       
  3.2         ,       
 4.  ,               
             
 */
#include
#include
#include
int slice=1;//       
float totaltime=0;//        
float totalcycletime=0;//       
typedef struct pcb
{
	char name;
    float needtime;//         
	float spenttime;//        
	float cycletime;//      
    struct pcb *next;
}PCB;
void createlist(PCB *&,int );
void timeslice(PCB *&,int );
int main()
{
	//         
    system("mode con cols=50 lines=50");
    int a;//         
    PCB *L;//       
    printf("      
"); scanf("%d",&a); // createlist(L,a); timeslice(L,a); return 0; } // void createlist(PCB* &L,int n) { PCB *a,*b; L=(PCB*)malloc(sizeof(PCB)); L->next=NULL; a=L; int i; for(i=1;i<=n;i++) { b=(PCB*)malloc(sizeof(PCB)); int z=getchar();// printf(" %d
",i); scanf("%c",&b->name); printf(" %c
",b->name); scanf("%f",&b->needtime); b->spenttime=0;// b->cycletime=0;// a->next=b; a=b; } a->next=L->next;// } // void timeslice(PCB* &L,int n) { int p=n; PCB *a,*b; a=L->next; b=L;//b while(n!=0) { if(a->needtime>slice) { a->needtime-=slice; totaltime+=slice; a->spenttime+=slice; printf("%c
",a->name); Sleep(1000);// a=a->next; b=b->next; } else if(a->needtimeneedtime==slice) { a->spenttime+=a->needtime; totaltime+=a->needtime; a->cycletime=totaltime/a->spenttime; totalcycletime+=a->cycletime; printf(" %c , %f , %f , %f
",a->name,a->spenttime,totaltime,a->cycletime); Sleep((a->needtime)*1000); // b->next=a->next; free(a); a=b->next; n-=1; } } float z=totalcycletime/p; printf(" , %f , %f
",totaltime,z); }