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

📄 sch_demo.c

📁 操作系统实验教程核心技术与编程实例书中地例子代码
💻 C
字号:
#include<\tc\include\stdio.h>
#include<\tc\include\dos.h>
#include"g\sch_demo.h"
#define INTERRUPT 0x08
#include"user.c"
int far pp1(),far pp2(),far pp3();
int far gsch();
void far pcba_r(struct pcb far *it);
void far r_pcba(struct pcb far *pr);
void interrupt gint8();
void save8(int n);
void (interrupt * oldhandler)();
void ggret();
#include"g\g_func1.c"
void process_kill();
main()
  {
    int i,j,l,k;
    clrscr();
    ginit();
    printf("\n   Exit from genix");
    ggret();
  }
pcbcreat(char *gname,int gprio,int g_sys,int g_off,int g_seg)
  {
   int i=2;
   char nn;
   struct pcb far *rpt,far *rp00;
   rpt=rp00=(struct pcb far *)malloc(sizeof(struct pcb));
   while(i<=9&&pcba[i].status!='D')
      {i=i+1;}
   strcpy(pcba[i].name,gname);
   pcba[i].priority=gprio;
   pcba[i].id=i;
   if(g_sys==0)
     {pcba[i].status='S';}
   else
     {pcba[i].status='B';} /* create_state */
   if(g_sys==0)
   {
   }
   else
      {if(ready_head==NULL)
         {ready_head=&pcba[i];
          pcba[i].next=NULL; }
      else
         { rp00=ready_head;
           if( rp00->next==NULL)
              { if(rp00->priority<gprio)
                  {ready_head->next=&pcba[i];
                   pcba[i].next=NULL; }
                else
                { pcba[i].next=rp00;
                  ready_head=&pcba[i];}
              }
           else
              { if (rp00->priority>=gprio)
                   {ready_head=&pcba[i];
                    pcba[i].next=rp00;}
                else
                   {rpt=rp00->next;
                    while(rpt->next!=NULL&&rpt->priority<gprio)
                      {rp00=rpt;
                      rpt=rpt->next;}
                    if(rpt->priority<gprio)
                      {rpt->next=&pcba[i];
                       pcba[i].next=NULL; }
                    else
                     { pcba[i].next=rpt;
                      rp00->next=&pcba[i];}
                   }
              }
         }

   }

     r_flag=0x0200;
     r_ax=0;
     r_bx=0;
     r_cx=0;
     r_dx=0;
     r_si=0;
     r_di=0;
     r_bp=0;
     nn=gname[2];
     switch(nn)
     {  
        case '1':
             r_ip = FP_OFF(&pp1);
             r_cs = FP_SEG(&pp1);
             goto gs;
        case '2':
             r_ip = FP_OFF(&pp2);
             r_cs = FP_SEG(&pp2);
             goto gs;
        case '3':
             r_ip = FP_OFF(&pp3);
             r_cs = FP_SEG(&pp3);
     };
     gs:
     r_sp = g_off;
     r_ss = g_seg ;
     asm      mov r_es,es
     asm      mov r_ds,ds
     printf("creat->:%s",gname);
     printf("-sp:%d",r_sp);
     printf("-ss:%d",r_ss);
     printf("-ip:%d",r_ip);
     printf("-cs:%d",r_cs);
     rpt=&pcba[i];
     r_pcba(rpt);
     /*     dispreg(i);*/
  }
dispreadyq()
  {
   struct pcb far *prtp;
   printf("\n");
   printf("\n   THE READY QUEUE:");
   printf("\n   * * * * * * * * * * * * * * * * * * * * * * * * * * *");
   printf("\n          id        name    priority      status");
   prtp=ready_head;
   while(prtp!=NULL)
   {
      printf("%12s",prtp->name);
      printf("%12d",prtp->priority);
      printf("%12c",prtp->status);
      prtp=prtp->next;
   }
   printf("\n   * * * * * * * * * * * * * * * * * * * * * * * * * * *");
  }
gscreen_int()
{
  clrscr();
  gotoxy(1,22);
  printf("\--------------------------------------------------------------------------------");
  printf("\Input Command:");
}
void far pcb_reg(struct pcb far *it)
  {
   if (it->status=='B') 
      { it->status='R';
        process_kill_id=it->id;
      }
   else{ process_kill_id=-1; };
   pcba_r(it);
   time_count=3;
   time_sch=1;
   asm mov ax,r_ax
   asm mov bx,r_bx
   asm mov cx,r_cx
   asm mov dx,r_dx
   asm mov si,r_si
   asm mov di,r_di
   asm mov bp,r_bp
   asm mov es,r_es
   asm mov ds,r_ds

   asm mov sp,bp
   asm add sp,2

   asm mov ss,r_ss
   asm mov sp,r_sp
   asm cmp word ptr process_kill_id,0
   asm jl pcb_reg1
   asm push word ptr process_kill_id
   asm push word ptr process_kill_seg
   asm push word ptr process_kill_off
pcb_reg1: ;
   asm mov ax,r_ax
   asm push r_flag
   asm popf
   asm push r_cs
   asm push r_ip
   asm sti
   asm ret
   }
void far pcba_r(struct pcb far *it)
 {
   r_flag=it->rega.flag;
   r_ax=it->rega.ax;
   r_bx=it->rega.bx;
   r_cx=it->rega.cx;
   r_dx=it->rega.dx;
   r_si=it->rega.si;
   r_di=it->rega.di;
   r_bp=it->rega.bp;
   r_ip=it->rega.ip;
   r_sp=it->rega.sp;
   r_es=it->rega.es;
   r_ds=it->rega.ds;
   r_ss=it->rega.ss;
   r_cs=it->rega.cs;
  }
void far r_pcba(struct pcb far *pr)
  {
     pr->rega.flag=r_flag;
     pr->rega.ax=r_ax;
     pr->rega.bx=r_bx;
     pr->rega.cx=r_cx;
     pr->rega.dx=r_dx;
     pr->rega.si=r_si;
     pr->rega.di=r_di;
     pr->rega.bp=r_bp;
     pr->rega.ip=r_ip;
     pr->rega.sp=r_sp;
     pr->rega.es=r_es;
     pr->rega.ds=r_ds;
     pr->rega.ss=r_ss;
     pr->rega.cs=r_cs;
   }
void interrupt gint8()
 {  
    oldhandler();
    asm cli
    inregs.h.ah=0x34;
    intdosx(&inregs,&outregs,&segs);
    indos=(char far *)(((long)(segs.es)<<16)+outregs.x.bx);
    if (*indos) goto g_ret;
    asm mov al,0x0B
    asm out 0x20,al
    asm nop
    asm in al,0x20
    asm and al,0x0fe
    asm jnz g_ret
    time_count =time_count - 1;
    if ( time_sch == 0 ) goto g_ret ;
    if (time_count>=1) goto g_ret;
    time_sch=0;
        asm      pop word ptr r_bp
        asm      pop word ptr r_di
        asm      pop word ptr r_si
        asm      pop word ptr r_ds
        asm      pop word ptr r_es
        asm      pop word ptr r_dx
        asm      pop word ptr r_cx
        asm      pop word ptr r_bx
        asm      pop word ptr r_ax
        asm      pop word ptr r_ip
        asm      pop word ptr r_cs
        asm      pop word ptr r_flag
    asm push word ptr gsch_seg
    asm push word ptr gsch_off
    asm ret;
 g_ret:
    asm sti;
 }
int ginit()
  {
   int i,j,k,g_seg,g_off;
   run=ready_head=(struct pcb far *)malloc(sizeof(struct pcb));
/*   gscreen_int();*/

   for (i=0;i<=9;i++)
   {
      pcba[i].status='D';
   }
   ready_head=NULL;
   run=NULL;
   g_off=FP_OFF(&ss1)+4095;
   g_seg=FP_SEG(&ss1);
   pcbcreat(name1,2,1,g_off,g_seg);
   g_off=FP_OFF(&ss2)+4095;
   pcbcreat(name2,1,1,g_off,g_seg);
   g_off=FP_OFF(&ss3)+4095;
   pcbcreat(name3,3,1,g_off,g_seg);
   /*dispreadyq();*/
   time_sch=1;
   time_count=2;
   gsch_off=FP_OFF(&gsch);
   gsch_seg=FP_SEG(&gsch);
   sys_sp=FP_OFF(&ss0)+4095;
   sys_ss=FP_SEG(&ss0);
   process_kill_off=FP_OFF(&process_kill);
   process_kill_seg=FP_SEG(&process_kill);
   to_gsch=-1;
   run=NULL;
   sys=1;
   g_off=FP_OFF(&ss2)+4095;
   g_seg=FP_SEG(&ss2);
   oldhandler=getvect(INTERRUPT);

   asm cli
   asm mov sp,word ptr g_off
   asm mov ss,word ptr g_seg
   /*save8(1);*/
   /*svect();*/
   setvect(INTERRUPT,gint8);
   gsch();
   asm sti
   asm push word ptr gsch_seg
   asm push word ptr gsch_off
   pp2();
   }
int far gsch()
 {  struct pcb far *i,far *g_current;
    /*register -->r area */
    asm cli
    asm      mov ax,bp
    asm      add ax,2
    asm      mov r_sp,ax
    asm      mov r_ss,ss
    asm mov ss,sys_ss
    asm mov sp,sys_sp
    sys=0;
    i=(struct pcb far *)malloc(sizeof(struct pcb));
    if (ready_head==NULL){ggret();};
    if (run!=NULL) {if(to_gsch!=1){r_pcba(run);};}
    else {run=ready_head;goto gsch_begin;}
    if (run->next==NULL) {i=NULL;}
    else
       {                     
       i=run->next;
       while (i!=NULL) 
          {if (i->status=='x') {i=i->next;} else break;}
       };
    if (i!=NULL) { run=i;}
    else
       {
        i=ready_head;
        while (i!=NULL) 
          {
           if (i->status=='x') {i=i->next;} else break;}
           run=i;
           printf("--------enter to gsch ");
          };
gsch_begin:
    if (run!=NULL)
    {
     printf("\n gsch:ip:%d",r_ip);
     printf("-cs:%d",r_cs);
     printf("-flag:%d",r_flag);
     printf("--->%s",run->name);

     if (to_gsch==1)
     { if(run->next!=NULL) 
       {
         i=run->next;
         printf("\naaaa--->%s",i->name);
         i=i->next;
         if(i!=NULL){printf("bbbb--->%s",i->name);};
       };
      };
     to_gsch=-1;
     pcb_reg(run);
    }
    else ggret();
 }
void process_kill()
{   
    int pt1;
    struct pcb far *i;
    struct pcb far *j;
    asm cli
    i=(struct pcb far *)malloc(sizeof(struct pcb));
    j=(struct pcb far *)malloc(sizeof(struct pcb));
    asm push ax
    asm mov ax,[bp+2]
    asm mov pt1,ax
    asm pop ax
    pcba[pt1].status='D';
    printf("\n------------------------------kill:%s",pcba[pt1].name);
    if (ready_head->id==pt1)
    {  ready_head=ready_head->next;
       run=NULL;
       goto process_kill1;
    }
    i=ready_head;
    while (i->next!=NULL&&i->next->id!=pt1) 
    {i=i->next;};
    if (i->next!=NULL&&i->next->id==pt1)
    {
       run=i;
       j=i->next;
       i->next=j->next;
    }
process_kill1:;
    to_gsch=1;
    asm pop   ds
    asm pop   si
    asm mov   sp,bp
    asm pop   bp
    asm push word ptr gsch_seg
    asm push word ptr gsch_off
    asm ret
}

⌨️ 快捷键说明

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