Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

SolKosLaba2

.doc
Скачиваний:
12
Добавлен:
15.03.2015
Размер:
6.45 Кб
Скачать
#include <stdio.h>
#include <dos.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>

#define stacksize (2048)
#define _stek unsigned int
#define intr 0x08

const int num_pr=3;
typedef
struct Tdescr{
    unsigned int ss,sp,si,di,bp,ds;
};
#ifdef __cplusplus
#define __CPPARGS...
#else
#define __CPPARGS
#endif

void huge t_add(Tdescr huge *proc,void huge* task);
void huge t_switch(Tdescr huge *procO,Tdescr huge *procN);

void huge set_disp(void);
void huge stop_disp(void);
void huge task1(void);
void interrupt (*old_han)(__CPPARGS);
void interrupt (*old_han61)(__CPPARGS);

void interrupt _handler(__CPPARGS);

void huge task2(void);
void huge task3(void);


Tdescr huge *proc1=(Tdescr * huge)malloc(sizeof(Tdescr));
Tdescr huge *proc2=(Tdescr * huge)malloc(sizeof(Tdescr));
Tdescr huge *proc3=(Tdescr * huge)malloc(sizeof(Tdescr));
Tdescr huge *Main=(Tdescr * huge)malloc(sizeof(Tdescr));

int _08_cs,_08_ip,_61_cs,_61_ip;
int num_proc;

unsigned int d_count1,d_count2,d_count3;
unsigned int count;

void main(void)
{
	clrscr();
	t_add(proc1,task1);
	t_add(proc2,task2);
	t_add(proc3,task3);
    
    clrscr();
    
    set_disp();
	
	free (proc1);
	free (proc2);
	free (proc3);
}

void huge t_add(Tdescr huge *proc,void huge *task)
{
	_stek huge *st=(_stek huge *)malloc(stacksize);
	proc->ss=FP_SEG(st);
	proc->sp=FP_OFF(st)+1000;
	*(int huge *)MK_FP(proc->ss,proc->sp)=FP_OFF(task);
	*(int huge *)MK_FP(proc->ss,proc->sp+2)=FP_SEG(task);
	proc->sp-=8;
}
void huge t_switch(Tdescr huge *procO,Tdescr huge *procN )
{
  /*  procO->ss=_SS;
    procO->sp=_SP;
    procO->bp=_BP;
    procO->ds=_DS;
    procO->si=_SI;
    procO->di=_DI;
    _AX=procN->ss;
    _SS=_AX;
    _SP=procN->sp;
    _BP=procN->bp;
    _DS=procN->ds;
    _SI=procN->si;
    _DI=procN->di; */

	 asm {
		les bx,procO
		mov es:[bx],ss
		mov es:[bx+2],sp
		mov es:[bx+4],bp
		mov es:[bx+6],ds
		mov es:[bx+8],si
		mov es:[bx+10],di
		les bx,procN
		mov ax,es:[bx]
		mov ss,ax
		mov sp,es:[bx+2]
		mov bp,es:[bx+4]
		mov ds,es:[bx+6]
		mov si,es:[bx+8]
		mov di,es:[bx+10]
	};
}
void huge task1(void){
    
    int i=0;
    int j=0;
    enable();
    while(j<1){
        num_proc=1;
        i++;
        cprintf(" 1Proc= %i \r\n ",i);
        delay(55);
    }
}


void huge task2(void){
    int i=0;
    int j=0;
    enable();
    
    while(j<1){
        num_proc=2;
        i++; i++;
        cprintf("                     2Proc=%i \r \n ",i);
        delay(20);
    }
}

void huge task3(void) {
    int i=0;
    int j=0;
    while(j<1){
        num_proc=3;
        i++;
        cprintf("                                          3Proc=%i \r \n",i);
        enable();
        delay(5);
    }
}

void huge set_disp(void)
{
              disable();
	      old_han=getvect(intr);
	      setvect(intr,_handler);
                int random_proc;
                srand(time(NULL));
                random_proc = 1 + rand() %3;

                    switch (random_proc)
                    {
                        case 1: t_switch(Main,proc1);
                            break;
                        case 2: t_switch(Main,proc2);
                            break;
                        case 3: t_switch(Main,proc3);
                            break;
                    }
        }


void huge stop_disp(void)
        {
            asm{
		cli
		push ds
		lds dx,old_han
		
		mov ax,2508h
		int 21h
		pop ds
	 sti
	};
            //setvect(intr,old_han);
            switch (num_proc)
            {
                case 1:t_switch(proc1,Main);;
                    break;
                case 2: t_switch(proc2,Main);;
                    break;
                case 3: t_switch(proc3,Main);;
                    break;
            }            
            //enable();
        }

void interrupt _handler(__CPPARGS)
{
    
            disable();
            old_han();
           
    int random_proc;
    srand(time(NULL));
    random_proc = 1 + rand() %3;
    
    if (num_proc==1)
            switch(random_proc){
                case 1:
                    t_switch(proc1,proc1);
                    if(kbhit()) {
                        getch();
                        stop_disp();
                    }
                    break;
                case 2:
                    t_switch(proc1,proc2);
                    if(kbhit()) {
                        getch();
                        stop_disp();
                    }
                    break;
                case 3:
                    t_switch(proc1,proc3);
                    if(kbhit()) {
                        getch();
                        stop_disp();
                    }
                    break;
            }
if (num_proc==2)
            switch(random_proc){
                case 1:
                    t_switch(proc2,proc1);
                    if(kbhit()) {
                        getch();
                        stop_disp();
                    }
                    break;
                case 2:
                    t_switch(proc2,proc2);
                    if(kbhit()) {
                        getch();
                        stop_disp();
                    }
                    break;
                case 3:
                    t_switch(proc2,proc3);
                    if(kbhit()) {
                        getch();
                        stop_disp();
                    }
                    break;

                    }
if (num_proc==3)
        switch(random_proc){
            case 1:
                t_switch(proc3,proc1);
                if(kbhit()) {
                    getch();
                    stop_disp();
                }
                break;
            case 2:
                t_switch(proc3,proc2);
                if(kbhit()) {
                    getch();
                    stop_disp();
                }
                break;
            case 3:
                t_switch(proc3,proc3);
                if(kbhit()) {
                    getch();
                    stop_disp();
                }
                break;
                
        }
            asm {
                mov al, 20h
                out 20h,al
            }
            enable();
}
    
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]