首页 > 作文

多任务处理

更新时间:2023-03-26 14:01:50 阅读: 评论:0

吹小号的天鹅读后感-80年代经典歌曲

多任务处理
2023年3月26日发(作者:月经期减肥)#include

#include

#include

#include



#define GET_INDOS 0x34

#define GET_CRIT_ERR 0x5d06

#define FINISHED 0

#define RUNNING 1

#define READY 2

#define BLOCKED 3

#define NTCB 5

#define NTEXT 20





char far * indos_ptr = 0;

char far * crit_err_ptr = 0;

int current = 0;

int timecount = 0;

int TL = 1;

int sum;

int buf[5];

int in, out;





typedef struct

{

int value;

struct TCB * wq;

} maphore;





struct TCB

{

unsigned char * stack;

unsigned ss;

unsigned sp;

char state;

char name[10];

struct TCB * next;

int pri;

maphore mutex;

maphore sm;

} tcb[NTCB];





struct int_regs

{

unsigned BP, DI, SI, DS, ES, DX, CX, BX, AX, IP, CS, Flags, off, g;

};









maphore mutexfb = {1, NULL};

maphore mutex = 带兔字的成语 {1, NULL};

maphore empty = {5, NULL};

maphore full = {0, NULL};



typedef int (far * codeptr)();

void interrupt(*old_int8)();



void initDos();

void initTcb();

int DosBusy();

int create(char * name, codeptr code, int stackLen);

void destroy(int id);

void over();

void interrupt new_int8();

void interrupt my_swtch();

void block(struct TCB ** qp);

void wakeup_first(struct TCB ** qp);

void P(maphore * m);

void V(maphore * m);



int find();

void tcb_state();

int finished();





void initDos()

{

union REGS regs;

struct SREGS gregs;



= GET_INDOS;

intdosx(®s, ®s, &gregs);

indos_ptr = MK_FP(, );





if (_osmajor < 3)

{

crit_err_ptr = indos_ptr + 1;

}

el

if (_osmajor == 3 && _osminor == 0)

{

crit_err_ptr = indos_ptr - 1;

}

el

{

= GET_CRIT_ERR;

intdosx(®s, ®s, &gregs);

crit_err_ptr = MK_FP(, );

}

}





void initTcb()

{

int id;



for (id = 0; id < NTCB; id++)

{

tcb[id].stack = NULL;

tcb[id].state = FINISHED;

tcb[id].name[0] = '0';

tcb[id].next = NULL;

tcb[id]. = 1;

tcb[id]. = NULL;

tcb[id].pri = id*2;

}

}





int DosBusy()

{

if (indos_ptr && crit_err_ptr)

{

return (*indos_ptr || *crit_err_ptr);

}

el

{

return -1;

}

}

int create(char * name, codeptr code, int stackLen)

{

int id;

struct int_regs far * regs;

disable();



for (id = 1; id < NTCB; id++)

{

if (tcb[id].state == FINISHED)

{

break;

}

}



if (id == NTCB)

{

printf("
**** thread %s build faied ****
", name);

return -1;

}





tcb[id].stack = (unsigned char *)malloc(stackLen);

regs = (struct int_regs far *)(tcb[id].stack + stackLen);

regs--;

tcb[id].ss = FP_SEG(regs);

tcb[id].sp = FP_OFF(regs);

tcb[id].state = READY;



strcpy(tcb[id

].name, name);



regs->DS = _DS;

regs->ES = _ES;

regs->IP = FP_OFF(code);

regs->CS = FP_SEG(code);

regs->Flags = 0x200;

regs->off = FP_OFF(over);

regs->g = FP_SEG(over);

printf("
**** thread%d %s have been builded ****
", id, tcb[id].name);

enable();

return id;

}





void destroy(int id)

{

disable();



free(tcb[id].stack);

tcb[id].stack = NULL;

t985大学排名 cb[id].state = FINISHED;

tcb[id].name[0] = '0';

tcb[id].next = NULL;

tcb[id]. = 1;

tcb[id]. = NULL;

tcb[id].pri = id*2;

printf("
**** thread %d %s have been destroyed ****
", id, tcb[id].name);

enable();

}





void over()

{

destroy(current);

my_swtch();

}





void interrupt new_int8()

{

(*old_int8)();

timecount++;

if(timecount == TL)

{

if(!DosBusy())

{

my_swtch();

return;

}

el

{

printf("dos is busy!
");

return;

}

}

el

return;

}





void interrupt my_swtch()

{

int id;

int i;

disable();



tcb[current].ss = _SS;

tcb[current].sp = _SP;

for(i = 1; i < NTCB; i++)

{

if(tcb[i].state ==RUNNING)

{



tcb[i].pri++;



}



}

if (tcb[current].state == RUNNING)

{

tcb[current].state = READY;

}

if(tcb[1].state == RUNNING||tcb[2].state == RUNNING)

{printf("%d ",tcb[1].pri);

printf("%d ",tcb[2].pri);}

id = find();



if (id < 0)

{

id = 0;

}





_SS = tcb[id].ss;

_SP = tcb[id].sp;

tcb[id].state = RUNNING;

current = id;

timecount = 0;

enable();

}





void block(struct TCB ** qp)

{

struct TCB * tcbp;

tcb[current].state = BLOCKED;



if ((*qp) == NULL)

{

(*qp) = &tcb[current];

}

el

{

tcbp = (*qp);



while (tcbp->next != NULL)

{

tcbp = tcbp->next;

}



tcbp->next = &tcb[current];

}



tcb[尊贵的反义词 current].next = NULL;

my_swtch();

}





void wakeup_first(struct TCB ** qp)

{

struct TCB * tcbp;



if ((*qp) == NULL)

{

return;

}



tcbp = (*qp);

(*qp) = (*qp)->next;

tcbp->state = READY;

tcbp->next = NULL;

}





void P(maphore * m)

{

struct TCB ** qp;

disable();

m->value--;



if (m->value < 0)

{

qp = &(m->wq);

block(qp);

}



enable();

}





void V(maphore * m)

{

struct TCB ** qp;

disable();

m->value++;



if (m->value <= 0)

{

qp = &(m->wq);

wakeup_first(qp);

}



enable();

}



int find()

{

int id = -1;

int i;

int min = 100;

for(i = 1; i < NTCB; i++)

{

if(tcb[i].state == READY && tcb[i].pri <= min)

{

id = i;

min = tcb[id].pri;

}

}

if(id == -1)

return 0;



return id;

}





voi

d tcb_state()

{

int id;

printf("
**** Process status ****
");



for (id = 0; id < NTCB; id++)

{

printf("Process:%d %9s status' ", id, tcb[id].name);



switch (tcb[id].state)

{

ca FINISHED:

puts("FINISHED");

break;



ca RUNNING:

puts("RUNNING");

break;



ca READY:

puts("READY");

break;



ca BLOCKED:

puts("BLOCKED");

break;

}

}

}





int finished()

{

int id;



for (id = 1; id 博卡萨 < NTCB; id++)

{

if (tcb[id].state != FINISHED)

{

return 0;

}

}



return 1;

}





void f1()

{

int i, j, k;



for (i = 0; i < 50; i++)

{

putchar('a');

delay(55*TL);







}

}





void f2()

{

long i, j, k;



for (i = 0; i < 30; i++)

{

putchar('b');

delay(55*TL);



}

}





void f3()

{

int i;

int tmp;



for (i = 0; i < 50; i++)

{

P(&mutex);

tmp = sum;

delay(100);

tmp++;

sum = tmp;

printf("%d",sum);

V(&mutex);

}

}





void f4()

{

int i;

int tmp;



for (i = 0; i < 50; i++)

{

P(&mutex);

tmp = sum;

delay(100);

tmp--;

sum = tmp;

printf("%d",sum);

V(&mutex);

}

}





void producer()

{

int i;

int tmp;



for (i = 1; i <= 50; i++)

{

tmp = i * i;

P(&empty);

P(&mutex);

buf[in] = tmp;

in = (in + 1) % 5;

delay(100);

V(&mutex);

V(&full);

}

}





void consumer()

{

int i;

int tmp;



for (i = 1; i <= 50; i++)

{

P(&full);

P(&mutex);

tmp = buf[out];

out = (out + 1) % 5;

delay(100);

V(&mutex);

V(&empty);

printf("%d Buf is %d %d %d %d %d
", tmp,buf[0],buf[1],buf[2],buf[3],buf[4]);

}

}













int main()

{

int lect = -1;

initDos();

initTcb();

old_int8 = getvect(8);



strcpy(tcb[0].name, "main");

tcb[0].state = RUNNING;

current = 0;



while (lect)

{

clrscr();

printf("


ttt Welcome
");

printf("
t******************************************************************
");

printf("t* Concurrent execution, can t TL *
");

printf("t* ive acces干部考察方案 s to the same resource implementation process *
");

printf("t* er and consumer *
");

printf("t* *
");

printf("t**********************************************************跳棋游戏 ********

");

printf("t plea input

0~4:");

scanf("%d", &lect);

clrscr();



switch (lect)

{

ca 0:

bre德育原则有哪些 ak;



ca 1:

printf("
ttthread Concurrent execution
");

printf("ttthreadf1 print a,5times
");

printf("ttthreadf2 print b,3times
");

printf("ttplea t :
");

scanf("%d", &TL);

printf("t简笔画全家福 tPress ant key. . . ");

getch();

create("f1", (codeptr)f1, 1024);

create("f2", (codeptr)f2, 1024);

tcb_state();

tvect(8, new_int8);

my_swtch();



while (!finished());



break;



ca 2:

printf("
tt Exclusive access to the same resource implementation process
");

printf("ttthread f3 and f4 Shared variables sum
");

printf("ttthread f3 add sum by 1,50times
");

printf("ttthread f4 reduce sum by 1,50times
");

printf("ttPress ant key. . . ");

getch();

sum = 0;

create("f3", (codeptr)f3, 1024);

create("f4", (codeptr)f4, 1024);

tcb_state();

TL = 1;

tvect(8, new_int8);

my_swtch();



while (!finished());



printf("sum = %d
", sum);

break;



ca 3:

printf("
ttProducers and consumers
");

printf("ttthread producer and consumer shared buf
");



printf("ttPress ant key. . . ");

getch();

in = out = 0;

create("producer", (codeptr)producer, 1024);

create("consumer", (codeptr)consumer, 1024);

tcb_state();

TL = 1;

tvect(8, new_int8);

my_swtch();



while (!finished());



break;







default:

printf("
tt Sorry, your input is wrong, plea input again!
");

printf("ttPress ant key. . . ");

getch();

break;

}



tcb_state();



if (lect > 0 && lect < 4)

{

printf("
all thread have ,Press ant key. . . ");

getch();

}



tvect(8, old_int8);

}



tcb[0].state = FINISHED;

tcb[0].name[0] = '0';

printf("


ttt thanks
");

return 0;

}



本文发布于:2023-03-26 14:01:47,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/e037d9b16506373c519efcbd3af66d5c.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:多任务处理.doc

本文 PDF 下载地址:多任务处理.pdf

标签:多任务处理
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图