OS Process Task 安排
来源:互联网 发布:怎样查看淘宝价格曲线 编辑:程序博客网 时间:2024/05/18 01:37
- #include <iostream>
- #include<string.h>
- using namespace std;
- const int block_time=5; //定义时间片的长度为10秒
- const int maxPCB=100; //定义最大进程数
- //------------------------------定义进程结构体------------------------
- typedef struct node
- {
- char name[20];
- int status;
- /**
- 三个状态:1:运行态,2:就绪态,3:完成态。
- */
- int time;
- int privilege;
- int finished;//完成状态
- int wait_time;
- }pcb;
- pcb pcbs[maxPCB]; //结构体数组
- int quantity; //总进程数量
- //----------------------------初始化函数------------------------------
- void initpcbfoo()
- {
- /*
- * 初始化pcb信息
- */
- int i;
- for(i=0;i<maxPCB;i++)
- {
- strcpy(pcbs[i].name,"");
- pcbs[i].status=0;
- pcbs[i].time=0;
- pcbs[i].privilege=0;
- pcbs[i].finished=0;
- pcbs[i].wait_time=0;
- }
- quantity=0;//开始至进程数量为0;
- }
- //-------------------从文件中读数据函数 -------------
- int readdatafoo()
- {
- FILE *fp;
- char fname[20];
- int i;
- cout<<"请输入进程信息文件(提示:aaa.txt):";
- cin>>fname;
- if((fp=fopen(fname,"r"))==NULL)
- {
- cout<<"错误,文件打不开,请检查文件名"<<endl;
- }
- else
- {
- while(!feof(fp))
- {
- fscanf(fp,"%s %d %d %d",pcbs[quantity].name,&pcbs[quantity].status,
- &pcbs[quantity].time,&pcbs[quantity].privilege);
- quantity++;
- } //输出所读入的数据 次序为:进程名 进程状态 所需时间 优先数
- cout<<"所读入的 进程信息数据"<<endl;
- cout<<"进程名 进程状态 所需时间 优先数"<<endl;
- for(i=0;i<quantity;i++)
- {
- cout<<""<<pcbs[i].name<<" "<<pcbs[i].status<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
- }
- return(1);
- }
- return(0);
- }
- void init()
- { //重置数据,以供另一个算法使用
- int i;
- for(i=0;i<maxPCB;i++)
- {
- pcbs[i].finished=0; pcbs[i].wait_time=0;
- }
- }
- //先进先出算法
- void FIFOfoo()
- {
- int i,j;
- int total;
- //输出FIFO算法执行流
- cout<<endl<<"*****************************************************"<<endl;
- cout<<"FIFO算法:"<<endl;
- cout<<"进程名 等待时间"<<endl;
- for(i=0;i<quantity;i++)
- {
- cout<<""<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
- for(j=i+1;j<quantity;j++)
- {
- pcbs[j].wait_time+=pcbs[i].time;
- }
- }
- total=0;
- for(i=0;i<quantity;i++)
- {
- total+=pcbs[i].wait_time;
- }
- cout<<"总等待时间:"<<total<<" 平均等待时间:"<<total/quantity<<endl;
- }
- /*--------------------------间片轮转调度算法---------------------------------*/
- void RRtimefoo()
- {
- cout<<endl<<"*******************************************************"<<endl;
- cout<<"时间片轮转调度(时间片设为5):"<<endl;
- int i,j,number,flag=1;
- int passed_time=0;
- int max_time=0;
- int round=0;
- int queue[1000];
- int total=0;
- while(flag==1)
- {
- flag=0;
- number=0;
- for(i=0;i<quantity;i++)
- {
- if(pcbs[i].finished==0)
- {
- number++;
- j=i;
- }
- }
- if(number==1)
- {
- queue[total]=j;
- total++;
- pcbs[j].finished=1;
- }
- if(number>1)
- {
- for(i=0;i<quantity;i++)
- {
- if(pcbs[i].finished==0)
- { flag=1;
- queue[total]=i;
- total++;
- if(pcbs[i].time<=block_time*(round+1))
- {
- pcbs[i].finished=1;
- }
- }
- }
- }
- round++;
- }
- if(queue[total-1]==queue[total-2])
- {
- total--;
- }
- for(i=0;i<total;i++)
- {
- cout<<pcbs[queue[i]].name<<" ";
- cout<<endl;
- }
- cout<<"*******************************************************"<<endl;
- }
- //显示
- void help()
- { //cout<<" Define what you want do!/n";
- cout<<"*****Created by husiwen******/n";
- cout<<"--------------------------------/n";
- cout<<"** *1.时间片轮转调度 **/n";
- cout<<"** *2.先来先服务调度 **/n";
- cout<<"** *0.退出 **/n";
- cout<<"--------------------------------/n";
- }
- void seek()
- {
- int flag;
- cout<<"请选择你要进行的操作:";
- cin>>flag;
- while(0!=flag)
- {
- switch(flag)
- {
- case 1: //时间片轮转调度
- {
- cout<<" 你选择的是时间片轮转调度"<<endl;
- initpcbfoo();
- int openflag=readdatafoo();
- if(1 == openflag)
- {
- RRtimefoo();
- init();
- }
- }
- flag = 0;
- seek();
- break; //case 1: RRtimefoo;
- case 2: //先来先服务调度
- {
- cout<<" 你选择的是先来先服务调度"<<endl;
- initpcbfoo();
- int openflag=readdatafoo();
- if(1 == openflag)
- {
- //init();
- //initpcbfoo();
- FIFOfoo();
- init();
- }
- }
- flag = 0;
- seek();
- break; //case 2: FIFOfoo
- default:
- cout<<"erro/n";
- seek();// 输入错误 ,再次调用 seek 函数 提示用户输入选择操作;
- break;
- }
- }
- return ;
- }
- //主函数
- int main()
- {
- help();
- seek();
- return 0;
- }
- OS Process Task 安排
- [OS]Process
- Unable to find Mach task port for process-id 28860: (os/kern) failure (0x5).
- A Task Process DP
- task和process
- ActivityManager之Task、Process
- Execute Process Task
- <AFNI>Task based process
- MAC下GDB出现问题-Unable to find Mach task port for process-id 1262: (os/kern) failure (0x5).
- Unable to find Mach task port for process-id 47072: (os/kern) failure (0x5). (please check gdb is c
- hdu 3433 A Task Process
- A Task Process hdu 3433
- HDU 3433 A Task Process
- 【OS学习】之 Process API
- failed to get the task for process
- Android task process thread 进程与线程
- Kitkat Activity、Task、Application和Process
- hdu3433 A Task Process dp+二分
- FTP服务器的配置
- 呵呵
- 新的开始
- 农夫养牛问题 (将牛分为三种小牛、中牛、大牛)
- goto模拟循环
- OS Process Task 安排
- 怎么学好C++
- 学习内嵌sql语句
- 我只系想学下易语言
- 新人报道
- 一些JAVA练习题
- 1254
- matlab 学习
- JavaScript最常用的55个经典技巧