Skip to content

Instantly share code, notes, and snippets.

@h-j-13
Last active October 25, 2017 12:38
Show Gist options
  • Select an option

  • Save h-j-13/c943394326aa722a62edf2e206b6c7ca to your computer and use it in GitHub Desktop.

Select an option

Save h-j-13/c943394326aa722a62edf2e206b6c7ca to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#define getpch(type) (type*)malloc(sizeof(type))
#ifndef NULL
#define NULL 0
#endif
#ifndef HITwh_2017_os_1
/*
2017年 操作系统实验 (实验一)
time : 2017.10
author : @`13
source : 基于以前学长的代码进行修改
*/
#define HITwh_2017_os_1
using namespace std;
// ===============先来先去算法==========================
struct Process_PCB {
/** 进程PCB */
int id; //进程ID
char *position; //内存指针
int others; //其他资源
volatile int state; //进程状态码
int *sta; //状态队列链表指针
struct Process_PCB *pfather; //父进程指针
struct Process_PCB *pson; //子进程指针
const int prority; //优先级
};
struct Source_PCB {
/** 资源PCB */
int rid; //资源ID
string status; //资源状态 空闲/已分配
struct Process_PCB *queue; //等待队列,被本资源阻塞的进程链表
};
//先来先服务算法 保存进程信息结构体
struct fcfs{
char name[10];
float a_time;
float servicetime;
int prority;
float s_time;
float f_time;
};
fcfs a[100];
void input(fcfs *p,int N)
{/** 创建N个进程进程 */
int i;
cout<<endl;
printf("请您输入进程的 名字 | 到达时间 | 服务时间 | 优先级: \n");
for(i=0;i<=N-1;i++)
{
printf("请输入第%d个进程的相关数据: ",i+1);
scanf("\t\t\t%s%f%f%d",&p[i].name,&p[i].a_time,&p[i].servicetime,&p[i].prority);
printf("\n");
}
}
void Print(fcfs *p,float a_time,float servicetime,float s_time,float f_time,int prority,int N)
{/** 展示进程信息 */
int k;
printf("\n\n调用先来先服务算法以后进程运行的顺序是: ");
printf("%s",p[0].name);
for(k=1;k<N;k++)
{
printf("-->%s",p[k].name);
}
cout<<endl;
printf("\n具体进程调度信息:\n");
printf("\t进程名 | 到达时间 | 服务时间 | 开始时间 | 结束时间 | 优先级 \n");
for(k=0;k<=N-1;k++)
{
printf("\t%s\t%-.2f\t %-.2f\t %-.2f\t %-.2f\t %d\t ",p[k].name,p[k].a_time,
p[k].servicetime,p[k].s_time,p[k].f_time,p[k].prority);
printf("\n");
}
}
void sort(fcfs *p,int N)
{/** 基于到达时间排序 */
for(int i=0;i<=N-1;i++)
for(int j=0;j<=i;j++)
if(p[i].a_time<p[j].a_time)
{
fcfs temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
void deal(fcfs *p, float a_time,float servicetime,float s_time,float f_time,int prority,int N)
{/** 运行阶段 */
int k;
for(k=0;k<=N-1;k++)
{
if(k==0)
{
p[k].s_time=p[k].a_time;
p[k].f_time=p[k].a_time+p[k].servicetime;}
else
{
p[k].s_time=p[k-1].f_time;
p[k].f_time=p[k-1].f_time+p[k].servicetime;}
}
}
// 先来先去算法
void FCFS(fcfs *p,int N)
{
float a_time=0,servicetime=0,s_time=0,f_time=0,prority=0;
sort(p,N);
deal(p,a_time,servicetime,s_time,f_time,prority,N);
Print(p,a_time,servicetime,s_time,f_time,prority,N);
getchar();
}
// ===============最高优先级算法==========================
struct pcb {
// pcb
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
//@overload
void sort()
{/** 基于优先级排序 */
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)>(ready->super)))
{
p->link=ready;
ready=p;
}
else
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super))
{
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else
{
first=first->link;
second=second->link;
}
}
if(insert==0)
first->link=p;
}
}
void input()
{/** 输入进程 */
int i,num;
//clrscr();
printf("\n请输入进程数");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n进程号%d:\n",i);
p=getpch(PCB);
printf("输入进程名:");
scanf("%s",p->name);
printf("输入进程优先数:");
scanf("%d",&p->super);
printf("输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='w';
p->link=NULL;
sort();
}
}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB * pr)
{/** 展示进程 */
printf("\n进程名\t状态\t优先级\t需要时间\t运行时间\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
void check()
{/** 检查状态 */
PCB* pr;
printf("\n当前正在运行的进程是:%s",p->name);
disp(p);
pr=ready;
printf("\n当前就绪队列状态为:\n");
while(pr!=NULL){
disp(pr);
pr=pr->link;
}
}
void destroy()
{/** 销毁进程 */
printf("\n进程[%s]已完成.\n",p->name);
free(p);
}
void running()
{/** 运行进程 */
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();
else
{
(p->super)--;
p->state='w';
sort();
}
}
char menu()//用来输出相关信息的函数
{
char cse1;
while(1)
{
fflush(stdin);
cout<<"1.先来先服务调度算法"<<endl;
cout<<"2.优先级调度算法"<<endl;
cout<<endl;
cout<<"选择要使用的调度算法[1/2]:";
cse1=getchar();
if(cse1<'1'||cse1>'2')
cout<<"你的输入有错!"<<endl;
else
return cse1;
}
}
int main(int argc, char *argv[])
{
while(1)
{
switch(menu())
{
case '1':
int N;
printf("FCFS\n");
cout<<endl;
printf("输入进程数目:");
scanf("%d",&N);
input(a,N);
FCFS(a,N);
case '2':
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL)){
ch=getchar();
h++;
printf("\n执行次数:%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n按任意键继续...");
ch=getchar();
}
printf("\n进程已经完成\n");
ch=getchar();
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment