ОСКурс
.doc#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <iostream.h>
#include <stdio.h>
#define intr 0x08
#define stacksize 1024
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
#define _stek unsigned int
typedef struct Tdescr{
unsigned int ss,sp,bp,ds,si,di,cx,dx,bx,ax,es,fl;
Tdescr *Next;
int Num;
};
void huge t_add(Tdescr huge *proc, void huge *task);
void huge t_switch(Tdescr huge *proc0, Tdescr huge *procN);
void huge MakeQueDone(void);
void huge set_disp(void);
void huge stop_disp(void);
void interrupt (*old_han)(__CPPARGS);
void interrupt _handler(__CPPARGS);
void QueDoneNext(void);
void choise(void);
void huge task1(void);
void huge task2(void);
void huge task3(void);
void huge task4(void);
void huge task5(void);
void huge task6(void);
void huge task7(void);
Tdescr *proc1 = (Tdescr *)malloc(sizeof(Tdescr));
Tdescr *proc2 = (Tdescr *)malloc(sizeof(Tdescr));
Tdescr *proc3 = (Tdescr *)malloc(sizeof(Tdescr));
Tdescr *proc4 = (Tdescr *)malloc(sizeof(Tdescr));
Tdescr *proc5 = (Tdescr *)malloc(sizeof(Tdescr));
Tdescr *proc6 = (Tdescr *)malloc(sizeof(Tdescr));
Tdescr *proc7 = (Tdescr *)malloc(sizeof(Tdescr));
Tdescr huge *Main=(Tdescr * huge)malloc(sizeof(Tdescr));
Tdescr *FirstDone = proc1;
Tdescr *LastDone = proc7;
Tdescr *FirstKill = NULL;
Tdescr *LastKill = NULL;
unsigned int flag;
void main(void){
clrscr();
t_add(proc1, task1);
t_add(proc2, task2);
t_add(proc3, task3);
t_add(proc4, task4);
t_add(proc5, task5);
t_add(proc6, task6);
t_add(proc7, task7);
set_disp();
Tdescr *Current;
while(FirstKill != NULL){
Current = FirstKill;
FirstKill = FirstKill->Next;
printf("\nProcess %d is killed!", Current->Num);
free(Current);
}
getch();
}
void huge t_add(Tdescr huge *proc, void huge *task)
{
_stek huge *st=(_stek huge *)malloc(2*stacksize);
proc->ss=FP_SEG(st); //Bazaviy adress
proc->sp=FP_OFF(st)+1000; //adress cmesheniay
*(int huge *)MK_FP(proc->ss,proc->sp)=FP_OFF(task); // daet adress podprog.
*(int huge *)MK_FP(proc->ss, proc->sp+2)=FP_SEG(task); // too
proc->es=_ES;
proc->fl=_FLAGS;
proc->sp-=8;
}
void huge MakeQueDone(void)
{
proc1->Next=proc2;
proc2->Next=proc3;
proc3->Next=proc4;
proc4->Next=proc5;
proc5->Next=proc6;
proc6->Next=proc7;
proc7->Next=NULL;
proc1->Num = 1;
proc2->Num = 2;
proc3->Num = 3;
proc4->Num = 4;
proc5->Num = 5;
proc6->Num = 6;
proc7->Num = 7;
}
void huge t_switch(Tdescr huge *procF, Tdescr huge *procW){
asm {
les bx, procF;
mov es:[bx],ss
mov es:[bx+2],sp
mov es:[bx+4],si
mov es:[bx+6],di
mov es:[bx+8],cx
mov es:[bx+10],dx
mov es:[bx+12],bx
mov es:[bx+14],ax
pop ax
}
asm{
les bx, procW
mov ax,es:[bx]
mov ss,ax
mov sp,es:[bx+2]
mov si,es:[bx+4]
mov di,es:[bx+6]
mov cx,es:[bx+8]
mov dx,es:[bx+10]
mov bx,es:[bx+12]
mov ax,es:[bx+14]
}
}
void huge task1(void){
enable();
while(1){
flag = 1;
cprintf("1->");
delay(55);
choise();
}
}
void huge task2(void){
enable();
while(1){
flag = 2;
cprintf("2->");
delay(55);
choise();
}
}
void huge task3(void){
enable();
while(1){
flag = 3;
cprintf("3->");
delay(55);
choise();
}
}
void huge task4(void){
enable();
while(1){
flag = 4;
cprintf("4->");
delay(55);
choise();
}
}
void huge task5(void){
enable();
while(1){
flag = 5;
cprintf("5->");
delay(55);
choise();
}
}
void huge task6(void){
enable();
while(1){
flag = 6;
cprintf("6->");
delay(55);
choise();
}
}
void huge task7(void){
enable();
while(1){
flag = 7;
cprintf("7->");
delay(55);
choise();
}
}
void huge set_disp(void){
disable();
old_han=getvect(intr);
setvect(intr, _handler);
MakeQueDone();
t_switch(Main, FirstDone);
}
void huge stop_disp(void){
setvect(intr, old_han);
switch(flag){
case 1: t_switch(proc1, Main);
break;
case 2: t_switch(proc2, Main);
break;
case 3: t_switch(proc3, Main);
break;
case 4: t_switch(proc4, Main);
break;
case 5: t_switch(proc5, Main);
break;
case 6: t_switch(proc6, Main);
break;
case 7: t_switch(proc7, Main);
break;
}
}
void interrupt _handler(__CPPARGS){
asm cli;
old_han();
QueDoneNext();
asm{
mov al, 20h
out 20h, al
}
asm sti;
}
void QueDoneNext(void) {
if (FirstDone->Next != NULL){
Tdescr *Current;
Current = FirstDone->Next;
FirstDone->Next = NULL;
LastDone->Next = FirstDone;
FirstDone = Current;
LastDone = LastDone->Next;
t_switch(LastDone, FirstDone);
} else t_switch(FirstDone, FirstDone);
}
void choise(void) {
Tdescr *Current;
disable();
if (FirstDone != NULL){
if(kbhit()){
getch();
clrscr();
if (FirstKill == NULL) {
Current = FirstDone->Next;
FirstKill = FirstDone;
FirstDone = FirstDone->Next;
FirstKill->Next = NULL;
LastKill = FirstKill;
}
else {
Current = FirstDone;
FirstDone = FirstDone->Next;
Current->Next = NULL;
LastKill->Next = Current;
LastKill = Current;
}
enable();
if (FirstDone == NULL) stop_disp();
else t_switch(LastKill, FirstDone);
}
}
else stop_disp();
enable();
}