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

📄 进程调度实验.txt

📁 该源代码模拟操作系统中进程的调度
💻 TXT
字号:
#include <stdio.h>   
#include <stdlib.h>  
#include <string.h>  
typedef struct pcb/*定义结构*/   
{           char name[5];   
            struct pcb *next;   
            int   needtime;   
            int    priority;   
            char state[5];   
}NODE;   

NODE     *create_process(int   n)/*创建队列*/   
{         NODE     *head,*s,*t;   
          int   time,i,j;   
          char     pname[5];   
          head=(NODE   *)malloc(sizeof(NODE));   
          printf("please   input   process   name:");   
          scanf("%s",pname);   
          
		  strcpy(head->name,pname);   
          
		  printf("please   input   need   time:");   
          scanf("%d",&time);   
          
		  head->needtime=time;   
          
		  printf("please   input   priority:");   
          scanf("%d",&j);   
          
		  head->priority=j;   
          
		  strcpy(head->state,"ready");   
          head->next=NULL;   
          t=head;   
          for(i=1;i<n;i++)   
              {   s=(NODE   *)malloc(sizeof(NODE));   
                  printf("please   input   process   name:");   
                  getchar();   
                  gets(pname);   
                  strcpy(s->name,pname);   
                  printf("please   input   need   time:");   
                  scanf("%d",&time);   
                  s->needtime=time;   
                  printf("please   input   priority:");   
                  scanf("%d",&j);   
                  s->priority=j;   
                  strcpy(s->state,"ready");   
                  s->next=NULL;   
                  t->next=s;   
                  t=s;   
              }   
          return       head;   
}  
    
void pri_process(NODE     *p)/*输出进程队列*/   
{
     NODE   *q;   
     q=p->next;   
	 printf("\n  name   \t   needtime   \t   priority   \t     state\n");   
     while(q!=NULL)   
		{    printf("   %5s\t      %2d   \t       %2d   \t       %5s   \n",   
             q->name,q->needtime,q->priority,q->state);   /* \t为水平制表*/
             q=q->next;   
        }  
}   
    
    
NODE   *order(NODE  *head_sort)/*对进程的优先级进行排序*/   
 {    
	  NODE   *p,*q,*head,*r,*t;   
      int     m,pr;   
      char    name[5];   
      head=head_sort;   
      p=head->next;   
      r=p;   
      t=p;   
      q=p->next;   
      while(r!=NULL)   
	  {     
		  while(q!=NULL)   
		  { 	  
		    if(p->priority<q->priority)   
			{          
				m=p->priority;   
                p->priority=q->priority;   
                q->priority=m;   
                strcpy(name,p->name);   
                strcpy(p->name,q->name);   
                strcpy(q->name,name);   
                pr=p->needtime;   
                p->needtime=q->needtime;   
                q->needtime=pr;   
			}   
                p=q;   
                q=q->next;   
		  }   
            r=r->next;   
            p=t;   
            q=p->next;   
        }   
      return(head_sort);   
  }   
 void main()/*主程序*/   
  {     NODE   *p,*head,*m;   
        int   x;      
        printf("please   input   process   number:");   
        scanf("%d",&x);   
        p=create_process(x);   
        head=(NODE   *)malloc(sizeof(NODE));       
		head->next=p;   
        pri_process(head);   
  
        while(x>0)   
		{     
			   order(head);   
               m=head->next;   
               strcpy(m->state,"run");     
               m->priority--;   
               m->needtime--;   
             if(head->next!=NULL)   
                pri_process(head);   
             if(m->needtime==0)   
			 { 
				 head->next=m->next;   
                 printf("%s   has   finished\n",m->name);   
                 free(m);   
                 x--;   
			 }   
		}  
          printf("over! \n");   
  }   

⌨️ 快捷键说明

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