您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 基于DOS的多任务系统的实现
  所属分类: C/C++
  开发工具:
  文件大小: 7kb
  下载次数: 0
  上传时间: 2011-11-20
  提 供 者: span****
 详细说明: 基于DOS的多任务系统的实现 #include #include #include #include /*定义TCB状态*/ #define FINISHED 0 #define RUNNING 1 #define READY 2 #define BLOCKED 3 #define NTCB 5 /*定义最大空闲TCB个数*/ #define TC 2 /*定义时间片长度*/ #define NBUF 6 /*定义缓冲区大小*/ #define GET_INDOS 0x34 #define GET_CRIT_ERR 0x5d06 struct TCB{ unsigned char *stack; /*线程堆栈起始地址*/ unsigned ss; /*堆栈段址*/ unsigned sp; /*堆栈指针*/ char state; /*线程状态*/ char name[15]; /*线程外部标识符*/ struct TCB *next; }tcb[NTCB]; /*初始化堆栈时使用*/ struct int_r egs{ unsigned bp, di, si, ds, es, dx, cx, bx, ax, ip, cs, flags, off, seg; }; char fpub[3]; int current= -1; /*定义一个当前TCB下标的全局变量*/ int timecount = 0; /*定义时间片*/ char far *indos_ptr = 0; /*该指针变量存放INDOS表示的地址*/ char far *crit_err_ptr = 0; /*该指针变量存放严重错误标志的地址*/ void interrupt (*old_int8)(void);/*旧的时钟中断处理程序*/ typedef int (far *codeptr)(void); /*定义了一个函数指针类型*/ int buffer[NBUF] = {-1,-1,-1,-1,-1,-1}; /*生产者消费者中的缓冲区*/ int in = 0; int out = 0; typedef struct{ int value; struct TCB *wq; }semaphore; semaphore mutex={1,NULL},empty={NBUF,NULL},full={0,NULL},Mutex2={1,NULL}; /*生产者消费者中的信号量*/ void InitDos(void) { union REGS regs; struct SREGS segregs; regs.h.ah = GET_INDOS; intdosx(®s,®s,&segregs); indos_ptr = MK_FP(segregs.es,regs.x.bx); if(_osmajor<3) crit_err_ptr = indos_ptr+1; else if(_osmajor==3 && _osminor==0) crit_err_ptr = indos_ptr-1; else{ regs.x.ax = GET_CRIT_ERR; intdosx(®s,®s,&segregs); crit_err_ptr = MK_FP(segregs.ds,regs.x.si); } } int DosBusy(void) { if(indos_ptr && crit_err_ptr) return (*indos_ptr || *crit_err_ptr); else return -1; } int finished(void) { int i; for(i=1; i= TC) if(!DosBusy()) { /*调用my_swtch进行重新调度*/ my_swtch(); } } void Destroy(int i) { if(tcb[i].state==RUNNING) { disable(); tcb[i].state=FINISHED; free(tcb[i].stack); enable(); } return; } void over(void) { Destroy(current); printf("%s is finished!\n", tcb[current].name); my_swtch(); } void tcb_state() { int i; printf("These are the information of all threads: \n"); for(i=0;ids = _DS; temp->es = _ES; temp->flags = 0x200; temp->ip = FP_OFF(code); temp->cs = FP_SEG(code); temp->off = FP_OFF(over); /*将over函数压入堆栈,线程结束自动执行*/ temp->seg = FP_SEG(over); tcb[i].ss = FP_SEG(temp); tcb[i].sp = FP_OFF(temp); return i; } void block(semaphore *sem) { struct TCB *p; disable(); tcb[current].state = BLOCKED; p = sem->wq; if(p == NULL){ sem->wq = &tcb[current]; } else{ while(p->next != NULL) p = p->next ; p->next = &tcb[current]; } tcb[current].next = NULL; my_swtch(); enable(); } void wakeup(semaphore *sem) { struct TCB *p; disable(); p = sem->wq; if(p != NULL){ p->state = READY; sem->wq = sem->wq->next; } enable(); } void wait(semaphore *sem) { disable(); sem->value = sem->value - 1; if(sem->value < 0) { printf("\nthe thread of %s is blocked\n",tcb[current].name); block(sem); } enable(); } void signal(semaphore *sem) { disable(); sem->value = sem->value + 1; if(sem->value <= 0) wakeup(sem); enable(); } void proceducer() { int i, j, k; for(i = 1; i <=13;i++){ wait(&empty); wait(&mutex); buffer[in] = i*i; printf("%s puts a number of %d in the buffer \n",tcb[current].name, buffer[in]); /*for(j=0; j<10000; j++) for(k=0; k<10000; k++);*/ in = (in +1) % NBUF; signal(&mutex); signal(&full); } } void consumer() { int i, j, k; for(i = 1; i<=7;i++){ wait(&full); wait(&mutex); printf("consumer gets the number of %d from the buffer\n", buffer[out]); buffer[out] = -1; /*for(j=0; j<1000; j++) for(k=0; k<10000; k++);*/ out = (out+1) % NBUF; signal(&mutex); signal(&empty); } } void f1() { int i,j,k; wait(&Mutex2); strcpy(fpub,"f1"); signal(&Mutex2); for(i=1;i<=22;i++) { wait(&Mutex2); printf("%s is running !\n",fpub); signal(&Mutex2); for(j=0;j<10000;j++) for(k=0;k<10000;k++); } } void f2() { int i,j,k; wait(&Mutex2); /*做放入数的操作*/ strcpy(fpub,"f2"); signal(&Mutex2); for(i=1;i<=33;i++) { wait(&Mutex2); printf("%s is running !\n",fpub); signal(&Mutex2); for(j=0;j<10000;j++) for(k=0;k<10000;k++); } } void InitTcb()/*TCB的初始化*/ { int i; for(i=0;i
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: