Demo entry 6553544

opopopopop

   

Submitted by opop on Jun 02, 2017 at 09:49
Language: C++. Code size: 2.5 kB.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"

typedef struct node /*创建PCB*/
{
     char name;  /*进程名*/
     int createtime; /*进程创建时间*/
     int needtime; /*进程完成所需时间*/
     struct node *next; /*链指针*/

}PCB;


PCB* createpro()
{
     PCB* runpro;
     PCB* newpcb;
     PCB* currentpcb;
     int i;
     srand((unsigned)time(NULL));

      runpro=(PCB*)malloc(sizeof(PCB));
      runpro->name = 'A';
      runpro->createtime = rand() % 100;
      runpro->needtime = rand() % 100;
      runpro->next = NULL;

     currentpcb = runpro;

     for(i=1;i<=4;i++)
      {
	  newpcb=(PCB*)malloc(sizeof(PCB));
          newpcb->name = 'A'+i;
	  newpcb->createtime = rand() % 100;
	  newpcb->needtime = rand() % 100;
	  newpcb->next = NULL;

          currentpcb->next = newpcb;
          currentpcb = newpcb;
      }

   return runpro;
}

void fcfs( PCB* run)
{
  PCB* ok;
  PCB* current;

  char t1;
  int  t2;
  int  t3;
  ok = NULL;
  while( ok != run )
  {
     current = run;
     while( current->next != ok)
     {  if( current->createtime > current->next->createtime )
        {
              t1 = current->name;
		t2 = current->createtime;
              t3 = current->needtime;

              current->name = current->next->name;
              current->createtime = current->next->createtime;
              current->needtime = current->next->needtime;

              current->next->name = t1;
              current->next->createtime = t2;
              current->next->needtime = t3;

         }
        current = current->next;
      }
       ok = current;
  }

}

void runpro( PCB* run)
{  PCB* currentpcb;
   //clrscr();
   system("cls");
   printf("       DISPLAY OF THE PROGRESS:\n");
   printf("************************************************\n");

   currentpcb = run;
   while( currentpcb != NULL)
  	{
	   printf("pro name:%c", currentpcb->name);
	   printf("    pro create time:%d", currentpcb->createtime);
	   printf("    pro need time:%d\n", currentpcb->needtime);
	   currentpcb = currentpcb->next;
	}
   printf("************************************************\n");
}
void delpcb(PCB* run)
{
   PCB*  cur;
   while( run != NULL)
      {
           cur = run;
	   run = run->next;
	   free(cur);
       }
}
void main()
{
  PCB* run;
  run = createpro();
  fcfs(run);
  runpro(run);
  delpcb(run);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).