您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 操作原理 设备的添加申请删除和归还
  所属分类: C
  开发工具:
  文件大小: 13kb
  下载次数: 0
  上传时间: 2009-05-25
  提 供 者: hanshan********
 详细说明: // device.cpp : Defines the entry point for the console application. 什么时候不能删除设备 // //#include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "iostream.h" //定义“空” #define null 0 //定义“进程”结构体 struct process { char process_name[10]; char apply_deviceid; struct process *next; }; //定义“系统设备表”结构体(项) struct SDT_item { char device_id; struct DCT *SDT_dct; //设备驱动程序入口 }SDT[20]; //定义“设备控制表”结构体 struct DCT { //char device_type; char device_id; int device_state; struct COCT *DCT_coct; int times; struct process *DCT_queue; }*device_K, *device_M, *device_T, *device_P; //定义“控制器控制表”结构体 struct COCT { char controller_id; int controller_state; struct CHCT *COCT_chct; struct process *COCT_queue; }*COCTS[10], *COCT1, *COCT2, *COCT3; //定义“通道控制表”结构体 struct CHCT { char channel_id; int channel_state; char use_device; struct process *CHCT_queue; }*CHCT1, *CHCT2; void init() { //初始化“设备控制表(DCT)” device_K = (struct DCT *)malloc(sizeof(struct DCT)); device_M = (struct DCT *)malloc(sizeof(struct DCT)); device_T = (struct DCT *)malloc(sizeof(struct DCT)); device_P = (struct DCT *)malloc(sizeof(struct DCT)); COCT1 = (struct COCT *)malloc(sizeof(struct COCT)); COCT2 = (struct COCT *)malloc(sizeof(struct COCT)); COCT3 = (struct COCT *)malloc(sizeof(struct COCT)); CHCT1 = (struct CHCT *)malloc(sizeof(struct CHCT)); CHCT2 = (struct CHCT *)malloc(sizeof(struct CHCT)); device_K->device_id = 'K'; device_K->device_state = 0; device_K->DCT_coct = COCT1; device_K->times = 0; device_K->DCT_queue = null; device_M->device_id = 'M'; device_M->device_state = 0; device_M->DCT_coct = COCT1; device_M->times = 0; device_M->DCT_queue = null; device_T->device_id = 'T'; device_T->device_state = 0; device_T->DCT_coct = COCT2; device_T->times = 0; device_T->DCT_queue = null; device_P->device_id = 'P'; device_P->device_state = 0; device_P->DCT_coct = COCT3; device_P->times = 0; device_P->DCT_queue = null; //初始化“系统设备表(SDT)” SDT[0].device_id = 'K'; SDT[0].SDT_dct = device_K; SDT[1].device_id = 'M'; SDT[1].SDT_dct = device_M; SDT[2].device_id = 'T'; SDT[2].SDT_dct = device_T; SDT[3].device_id = 'P'; SDT[3].SDT_dct = device_P; //初始化“控制器控制表(COCT)” COCT1->controller_id = '1'; COCT1->controller_state = 0; COCT1->COCT_chct = CHCT1; COCT1->COCT_queue = null; COCT2->controller_id = '2'; COCT2->controller_state = 0; COCT2->COCT_chct = CHCT2; COCT2->COCT_queue = null; COCT3->controller_id = '3'; COCT3->controller_state = 0; COCT3->COCT_chct = CHCT2; COCT3->COCT_queue = null; COCTS[0] = COCT1; COCTS[1] = COCT2; COCTS[2] = COCT3; //初始化“通道控制表(CHCT)” CHCT1->channel_id = '1'; CHCT1->channel_state = 0; CHCT1->use_device = '0'; CHCT1->CHCT_queue = null; CHCT2->channel_id = '2'; CHCT2->channel_state = 0; CHCT2->use_device = '0'; CHCT2->CHCT_queue = null; } void add_device() { struct DCT *dct_temp; struct COCT *coct_temp; char yes, coct_choice, chct_choice; dct_temp = (struct DCT *)malloc(sizeof(struct DCT)); printf("请输入设备的标识符:"); cin>>dct_temp->device_id; //判断设备重名 int m=0; for(m=0; m<20; m++){ if(SDT[m].device_id == dct_temp->device_id){ printf("系统中已有标识符为 %c 的设备,请重新操作!\n", dct_temp->device_id); break; } } if(m == 20){ dct_temp->device_state = 0; dct_temp->DCT_coct = null; dct_temp->times = 0; dct_temp->DCT_queue = null; //添加SDT[]表项 for(int k=0;k<20;k++){ if(SDT[k].SDT_dct == null){ SDT[k].device_id = dct_temp->device_id; SDT[k].SDT_dct = dct_temp; break; } } printf("是否利用原有的COCT?(Y/N)"); cin>>yes; if(yes == 'Y' || yes == 'y'){ printf("目前系统中存在的COCT有:\n"); for(int i=0; i<10; i++){ if(COCTS[i] != null){ printf("%c ", COCTS[i]->controller_id); } } printf("\n请选择你想使用的COCT(标识符):"); cin>>coct_choice; for(int l=0; l<10; l++){ if(coct_choice == COCTS[l]->controller_id){ dct_temp->DCT_coct = COCTS[l]; break; } } }else if(yes == 'N' || yes == 'n'){ printf("请输入COCT的标识符:"); coct_temp = (struct COCT *)malloc(sizeof(struct COCT)); cin>>coct_temp->controller_id; //初始化coct_temp coct_temp->controller_state = 0; coct_temp->COCT_chct = null; coct_temp->COCT_queue = null; for(int j=0; j<10; j++){ if(COCTS[j] == null){ COCTS[j] = coct_temp; break; } } printf("请选择你想使用的CHCT(标识符):(1/2)"); cin>>chct_choice; if(chct_choice == '1'){ coct_temp->COCT_chct = CHCT1; }else if(chct_choice == '2'){ coct_temp->COCT_chct = CHCT2; }else{ printf("通道输入选择错误"); } dct_temp->DCT_coct = coct_temp; }else{ printf("错误输入"); } } } void delete_device() { char device; printf("目前系统中有设备:\n"); for(int i=0; i<20; i++){ if(SDT[i].SDT_dct != null){ printf("%c ", SDT[i].SDT_dct->device_id); } } printf("\n请选择你要删除的设备:"); cin>>device; int flag = 0;//表示对应的COCT可以删 for(int j=0; j<20; j++){ if(SDT[j].SDT_dct != null && SDT[j].device_id == device){ for(int m=0; m<20; m++){ if((SDT[m].SDT_dct != null) && (m != j) && (SDT[m].SDT_dct->DCT_coct->controller_id == SDT[j].SDT_dct->DCT_coct->controller_id)){ flag = 1;//表示对应的COCT不可以删 break; } } if(flag == 0){ for(int k=0; k<10; k++){ if(COCTS[k]->controller_id == SDT[j].SDT_dct->DCT_coct->controller_id){ //删除对应的COCT COCTS[k] = null; break; } } SDT[j].SDT_dct->DCT_coct = null; } SDT[j].SDT_dct = null; SDT[j].device_id = '0'; } } } void apply_device() { char process[10], device; struct process *process_p, *process_apply; printf("请输入你要申请设备的进程名和设备标识符:"); scanf("%s %c", process, &device); //查找设备 for(int i=0; i<20; i++){ if((SDT[i].SDT_dct != null) && (SDT[i].device_id == device)){ //所申请的设备忙 if(SDT[i].SDT_dct->device_state == 1){ process_apply = (struct process *)malloc(sizeof(struct process)); strcpy(process_apply->process_name, process); process_apply->apply_deviceid = device; process_apply->next = null; if(SDT[i].SDT_dct->DCT_queue == null){ SDT[i].SDT_dct->DCT_queue = (struct process *)malloc(sizeof(struct process)); SDT[i].SDT_dct->DCT_queue = process_apply; }else{ process_p = SDT[i].SDT_dct->DCT_queue; while(process_p->next != null){ process_p = process_p->next; } process_p->next = process_apply; } printf("所申请的设备忙!,进程已经成功阻塞到设备 %c 下...\n", device); //所申请设备对应的控制器忙 }else if(SDT[i].SDT_dct->DCT_coct->controller_state == 1){ process_apply = (struct process *)malloc(sizeof(struct process)); strcpy(process_apply->process_name, process); process_apply->apply_deviceid = device; process_apply->next = null; if(SDT[i].SDT_dct->DCT_coct->COCT_queue == null){ SDT[i].SDT_dct->DCT_coct->COCT_queue = (struct process *)malloc(sizeof(struct process)); SDT[i].SDT_dct->DCT_coct->COCT_queue = process_apply; }else{ process_p = SDT[i].SDT_dct->DCT_coct->COCT_queue; while(process_p->next != null){ process_p = process_p->next; } process_p->next = process_apply; } //将对应的 设备状态置1 SDT[i].SDT_dct->device_state = 1; printf("所申请设备对应的控制器忙!,进程已经成功阻塞到控制器 %c 下...\n", SDT[i].SDT_dct->DCT_coct->controller_id); //所申请设备对应的通道忙 }else if(SDT[i].SDT_dct->DCT_coct->COCT_chct->channel_state == 1){ process_apply = (struct process *)malloc(sizeof(struct process)); strcpy(process_apply->process_name, process); process_apply->apply_deviceid = device; process_apply->next = null; if(SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue == null){ SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue = (struct process *)malloc(sizeof(struct process)); SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue = process_apply; }else{ process_p = SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue; while(process_p->next != null){ process_p = process_p->next; } process_p->next = process_apply; } //将对应的 控制器状态、设备状态均置1 SDT[i].SDT_dct->DCT_coct->controller_state = 1; SDT[i].SDT_dct->device_state = 1; printf("所申请设备对应的通道忙!,进程已经成功阻塞到通道 %c 下...\n", SDT[i].SDT_dct->DCT_coct->COCT_chct->channel_id); //设备申请成功! }else{ SDT[i].SDT_dct->device_state = 1; SDT[i].SDT_dct->DCT_coct->controller_state = 1; SDT[i].SDT_dct->DCT_coct->COCT_chct->channel_state = 1; SDT[i].SDT_dct->DCT_coct->COCT_chct->use_device = device; printf("进程%s 申请设备%c 成功...\n", process, device); } } } } void release_device(){ char device; struct process *process_p, *process_t; printf("请输入你要归还的设备标识符:"); cin>>device; //查找设备 for(int i=0; i<20; i++){ if((SDT[i].SDT_dct != null) && (SDT[i].device_id == device)){ //察看所对应的通道队列是否为空 if(SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue == null){ //察看所对应的控制器队列是否为空 if(SDT[i].SDT_dct->DCT_coct->COCT_queue == null){ int j=0; for(j=0; j<20; j++){ if((SDT[j].SDT_dct != null) && (SDT[j].SDT_dct->DCT_coct->controller_id == SDT[i].SDT_dct->DCT_coct->controller_id)){ if(SDT[j].SDT_dct->DCT_queue != null){ printf("进程 %s 已经被唤醒,并成功申请到设备 %c ...\n", SDT[j].SDT_dct->DCT_queue->process_name, SDT[j].SDT_dct->DCT_queue->apply_deviceid); SDT[j].SDT_dct->DCT_coct->COCT_chct->use_device = SDT[j].SDT_dct->DCT_queue->apply_deviceid; SDT[j].SDT_dct->DCT_queue = SDT[j].SDT_dct->DCT_queue->next; break; }else{ SDT[j].SDT_dct->device_state = 0; } } } if(j == 20){ SDT[i].SDT_dct->DCT_coct->COCT_chct->channel_state = 0; SDT[i].SDT_dct->DCT_coct->controller_state = 0; SDT[i].SDT_dct->device_state = 0; SDT[i].SDT_dct->DCT_coct->COCT_chct->use_device = '0'; printf("当前没有对应于通道 %c 且处于阻塞状态的进程...\n", SDT[i].SDT_dct->DCT_coct->COCT_chct->channel_id); } //察看所对应的设备队列是否为空 }else{ if(SDT[i].SDT_dct->DCT_queue != null){ SDT[i].SDT_dct->device_state = 1; }else{ SDT[i].SDT_dct->device_state = 0; } printf("进程 %s 已经被唤醒,并成功申请到设备 %c ...\n", SDT[i].SDT_dct->DCT_coct->COCT_queue->process_name, SDT[i].SDT_dct->DCT_coct->COCT_queue->apply_deviceid); SDT[i].SDT_dct->DCT_coct->COCT_chct->use_device = SDT[i].SDT_dct->DCT_coct->COCT_queue->apply_deviceid; SDT[i].SDT_dct->DCT_coct->COCT_queue = SDT[i].SDT_dct->DCT_coct->COCT_queue->next; } }else{ if(SDT[i].SDT_dct->DCT_coct->COCT_queue != null){ SDT[i].SDT_dct->DCT_coct->controller_state = 1; SDT[i].SDT_dct->device_state = 1; }else if(SDT[i].SDT_dct->DCT_queue != null){ SDT[i].SDT_dct->DCT_coct->controller_state = 1; SDT[i].SDT_dct->device_state = 1; }else{ SDT[i].SDT_dct->DCT_coct->controller_state = 0; SDT[i].SDT_dct->device_state = 0; } printf("进程 %s 已经被唤醒,并成功申请到设备 %c ...\n", SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue->process_name, SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue->apply_deviceid); SDT[i].SDT_dct->DCT_coct->COCT_chct->use_device = SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue->apply_deviceid; SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue = SDT[i].SDT_dct->DCT_coct->COCT_chct->CHCT_queue->next; } } } } void dispaly() { struct process *p; printf("系统中存在以下设备:\n"); printf("SDT[i] DCT COCT CHCT 正使用设备\n\n"); for(int i=0; i<20; i++){ if(SDT[i].SDT_dct != null){ printf("%c %c(%d) %c(%d) %c(%d) ", SDT[i].device_id, SDT[i].SDT_dct->device_id, SDT[i].SDT_dct->device_state, SDT[i].SDT_dct->DCT_coct->controller_id, SDT[i].SDT_dct->DCT_coct->controller_state, SDT[i].SDT_dct->DCT_coct->COCT_chct->channel_id, SDT[i].SDT_dct->DCT_coct->COCT_chct->channel_state); if(SDT[i].SDT_dct->DCT_coct->COCT_chct->use_device == SDT[i].device_id){ printf("<<---"); } printf("\n"); } } printf("\n"); if(CHCT1->CHCT_queue != null){ printf("通道 1 下阻塞的进程有:"); p = CHCT1->CHCT_queue; while(p != null){ printf("%s ", p->process_name); p = p->next; } printf("\n"); } if(CHCT2->CHCT_queue != null){ printf("通道 2 下阻塞的进程有:"); p = CHCT2->CHCT_queue; while(p != null){ printf("%s ", p->process_name); p = p->next; } printf("\n"); } } int main(int argc, char* argv[]) { char operate; //对系统设备进行初始化 init(); printf("\n ----------- 操作系统实验三 -----------\n\n"); dispaly(); while(1){ printf("请输入操作命令:"); cin>>operate; switch(operate){ case 'a': add_device(); dispaly(); break; case 'd': delete_device(); dispaly(); break; case 's': apply_device(); dispaly(); break; case 'h': release_device(); dispaly(); break; default: printf("输入错误!请重新输入...\n"); } } return 0; } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

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