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

📄 1.c

📁 在计算机系统中
💻 C
字号:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#define NULL 0
#define Max_Pri 100

struct Process
{
  int name;
  int prio;
  int needtime;
  int piecetime;
  int CPUtime;
  int runtime;
  int state;
  struct Process *next;
};
typedef struct Process process;

/**************************************/
process *CreateP()
{
  process *h,*t,*p;
  int i,a;
  int ct=0;
  for(i=0;i<5;i++)
  {
     t=(process *)malloc(sizeof(process));
     printf("\n Enter the name of process:");
     scanf("%d",&a);
     printf("\nEnter the CPUtime of process:");
     scanf("%d",&ct);
     t->name=a;
     t->CPUtime=ct;
     t->runtime=0;
     t->prio=100-t->CPUtime;
     t->needtime=t->CPUtime;
     t->piecetime=t->CPUtime/2+t->CPUtime%2;
     t->state=1;
     if(i==0)
      {
	h=t;
	p=t;
      }
      else
      {
	 p->next=t;
	 p=p->next;
      }
  }
  p->next=NULL;
  return h;
}
/**************************************/
void PrintP1(process*h)
{
   process*t;
   t=h;
   printf("      1:ready  2:execute  3:finish ");
   printf("\n  name  priority  needtime    runtime  state\n");
   {
    while(t)
    {
      printf("\n%4d%8d%10d%10d%10d",t->name,t->prio,t->needtime,t->runtime,
	     t->state);
     t=t->next;
    }
   }
   putchar('\n');
}
/**************************************/
void PrintP2(process *h)
{
  process *t;
  t=h;
  printf("      1:ready  2:execute  3:finish ");
  printf("\n  name  piecetime  needtime  runtime  CPUtime  state\n");
   while(t)
   {
     printf("\n%4d%8d%10d%10d%10d%8d",t->name,t->piecetime,t->needtime,t->runtime,t->CPUtime,t->state);
     t=t->next;
   }

   putchar('\n');
}

/**********************************************/
int Finish(process *h)
{
  int i=1;
  process *p;
  p=h;
  if (h!=NULL)
  {
    do
    {
      if (p->state!=3)
      {
        i=0;
        break;
      }
      else p=p->next;
    } while (p!=NULL);
  }
  return i;
}

/*********************************************/
process*Find(process *h)
{
  int max;
  process *p,*t;
  p=t=h;
  if (h!=NULL)
  {
    max=p->prio;
    while(p)
    {
      p=p->next;
      if (max<p->prio) max=p->prio;
    }
    while(t)
    {
      if (max==t->prio)
      {
	return t;

      }
      else t=t->next;
    }
  }
}

/***************************************/
process *ExecuteP_prio(process *h)
{
  process *t;
  while (Finish(h)==0)
  {
    if (h!=NULL)
    {
      clrscr();
      t=Find(h);
      if (t->needtime<=1)
      {
        t->needtime=0;
        t->state=3;
      }
      else
      {
        t->needtime-=1;
        t->state=2;
      }
      if (t->runtime<t->CPUtime) t->runtime+=1;
      t->prio-=3;
      PrintP1(h);
      if (t->state!=3)
      {
        sleep(1);
        t->state=1;
      }
    }
  }
  sleep(3);
}

/*************************************/
process *ExecuteP_time(process *h)
{
  process *t;
  while (Finish(h)==0)
  {
    t=h;
    if (h!=NULL)
    {
      while(t)
      {
        clrscr();
        if (t->needtime<=2)
        {
          t->state=3;
          t->needtime=0;
        }
        else
        {
          t->needtime-=2;
          t->state=2;
        }
        if (t->piecetime>0) t->piecetime-=1;
        if (t->runtime<t->CPUtime) t->runtime+=2;
	PrintP2(h);
        if (t->state!=3) sleep(1);
        if (t->state!=3) t->state=1;
        t=t->next;
      }
    }
  }
  sleep(3);
}

/************************************/

main()
{
   int num,count;
   char select;
   process *p=NULL;
   while (1)
  {
    clrscr();
    printf("\n\t------------------------------------------\n");
    printf("\t  a.Priority.                             \n");
    printf("\t  b.Piece of time.                        \n");
    printf("\t------------------------------------------\n");
    printf("Enter a letter a or b:");
    scanf("%s",&select);

    switch (select)
    {
      case 'a':
             {
		clrscr();
		while(1)
		{
		   printf("\n\t--------------------------\n");
		   printf("\t  1.Create a new process. \n");
		   printf("\t  2.Execute process.      \n");
		   printf("\t  3.Exit.                 \n");
		   printf("\t--------------------------\n");
		   printf("Enter a number between 1 to 3:");
		   scanf("%d",&num);
		   switch (num)
		   {
		      case 1:
		      {
			printf("This step is create a process.\n");
			p=CreateP();
			PrintP1(p);
			break;
		       }

		      case 2:
		      {
			printf("This step is execute a process by priority.\n");
			ExecuteP_prio(p);
			PrintP1(p);
			break;
		      }
		      case 3:exit(0);break;

		      default : printf("\nPlease input right select!\n"); break;
		   }
		}
             }
      case 'b':
	     {
		clrscr();
		while(1)
		{
		   printf("\n\t--------------------------\n");
		   printf("\t  1.Create a new process. \n");
		   printf("\t  2.Execute process.      \n");
		   printf("\t  3.Exit.                 \n");
		   printf("\t--------------------------\n");
		   printf("Enter a number between 1 to 3:");
		   scanf("%d",&num);
		   switch (num)
		   {
		      case 1:
		      {
			printf("This step is create a process.\n");
			p=CreateP();
			PrintP2(p);
			break;
		       }

		      case 2:
		      {
			printf("This step is execute a process by piece of time.\n");
		        ExecuteP_time(p);
			PrintP2(p);
			break;
		      }
		      case 3:exit(0);break;

		      default : printf("\n\tPlease input right select!\n"); break;
		   }
		}
	     }
      default : printf("\n\tPlease input right select!\n"); break;
    }
  }
}

⌨️ 快捷键说明

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