?? dd.c
字號:
//////////////////////////////////////////////////////////////////////////////////
//數據:進程,隊列結構
//處理流程:
//1 初始化--進程隊列結構(包括:就緒隊列,等待隊列,運行隊列)等必要的數據結構 init();
//2 進入無限循環,反復調度隊列
/////////////////////////////////////////////////////////////////////////
#define MAX 5
#include<stdio.h>
#include<stdlib.h>
int total_time=20;
int time_slice=3;
typedef struct process { // 進程控制塊
char pname[10];
int WaitTime;
int BurstTime;
int priority; // 數字越小優先級越高
struct process *next;
}PROCESS;
//typedef struct process PROCESS;
PROCESS * in_queue(PROCESS *head,PROCESS *p); //聲明
PROCESS *init() //進程初始化
{
int i=0;
char a;
PROCESS *head_new; //隊列的隊頭
head_new=(struct process*)malloc(sizeof(struct process));
if(!head_new) exit(1);
head_new=NULL;
do
{
struct process *s;
printf("initialize the process:\n");
s=(struct process *)malloc(sizeof(struct process));
if(!s) exit(1);
printf("please input the pname:WaitTime: BurstTime: priority:\n");
scanf("%c",&(s->pname));
scanf("%d",&(s->WaitTime));
scanf("%d",&(s->BurstTime));
scanf("%d",&(s->priority));
s->next=NULL;
in_queue(head_new,s);
i++;
printf("do u want to insert process more ?? 'Y'or'N'\n");
printf("----------------------------------------'\n");
scanf("%c",&a);
scanf("%c",&a);
// if(a=='Y'||a=='y') continue;
// else if(a=='N'||a=='n') break;
}while((i<MAX) &&(a=='Y'||a=='y'));
return head_new;
}
///////////////////////////////////////////////////////////
PROCESS *in_queue(PROCESS *head,PROCESS *p) //入隊函數
{
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
}
// printf("the process insert into the mothball queue :\n");
return head;
}
/////////////////////////////////////////////////////////////
/*
void new_queue() //后備隊列 先來先服務方式進入就緒
{
return *head_new;
}
*/
PROCESS *FCFS_process()
{
PROCESS *p,*q,*a; //a用來記錄選中結點的前一個結點
q=p=init(); //這里是不是有個問題??
while(p->next!=NULL)
{
a=p;
if(p->WaitTime>=q->WaitTime)
{
q=p;
p=p->next;
}
}
q->WaitTime--;
if(q->WaitTime==0) //如果等待時間為0則把該進程從后備隊列中移除
{
a->next=p->next;
free(p);
}
return q; //選擇等待時間最久的)
}
//////////////////////就緒隊列,入口函數為就緒隊列的頭指針/////////////
int count=0;
PROCESS *ready_queue(PROCESS *head) //就緒隊列 優先級進入運行 4道
{
PROCESS *p;
while(count<4)
{
p=FCFS_process();
p->next=head->next;
head=p;
count++;
printf("the process has inserted into the ready queue :\n");
}
return head;
}
//insert_ready() //
PROCESS *high_priority(PROCESS *P) //選擇優先級最高的進程
{
PROCESS *q,*p; //問題,入口形參中
q=p;
while(p->next!=NULL)
{
if(q->priority>p->priority)
q=p;
p=p->next;
}
return q;
}
PROCESS *pick_ready(PROCESS *a) //從就緒隊列中選擇進程運行
{
PROCESS *p=ready_queue(a);
PROCESS *b=high_priority(p);
return b;
}
void run(PROCESS *a) //運行一個時間片
{
while((time_slice>0)&&(a->BurstTime>0)) //指針用-> 變量用.
{
printf("the process %c is runing",a->pname);
printf("the process BurstTime time is %d ",a->WaitTime);
printf("the process BurstTime time is %d ",a->BurstTime);
printf("the process priority is %d ",a->priority);
a->BurstTime--;
time_slice--;
}
a->priority--;
total_time--;
/*
if(a->runtime>0)
return a;
else return NULL;*/
}
void main()
{
PROCESS *p;
PROCESS *head=init();
while(total_time!=0)
{
ready_queue(head);
p=pick_ready(head);
if(p->BurstTime==0)
{
p=p->next;
free(p);
count--;
}
time_slice=3;
}
}
////////////////////////////////////////////////////////////////////////
//注意的點:
//(1)pedef struct process process; c++ 可以直接用process 定義,但是c 不可以
//(2)返回值一致,PROCESS *MM(){PROCESS *P;return p}
//指針用. 鏈表用->
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -