C 模拟进程调度

题目;利用单链表模拟了进程调度的实现,主要功能有先来先服务算法,优先级算法,时间片轮转算法

#include<stdio.h>

#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
//时间延迟
#define DELAY 500
//时间片
#define SJP 3


unsigned short TIME = 0; //时间
unsigned short NUM = 0; //进程数量
unsigned short flag = 0;
char TYPE = '1'; //模拟类型
//PCB结构体定义
typedef struct PCB
{
char name[10];
char state; //[O]Operate,[R]Ready,[W]Wait,[F]Finish
unsigned short priority; //优先数越大,优先级越高,最小为1
unsigned short t_arrive; //到达时间
unsigned short t_start; //开始时间
unsigned short t_finish; //完成时间
unsigned short t_service; //服务时间
unsigned short t_run; //运行时间
unsigned short t_wait; //等待时间
struct PCB *next;
} pcb;
enum state
{
O, W, R, F
};
pcb *now = NULL, //现在运行的pcb
*head = NULL; //pcb链头部指针

void fcfs(); //先到先服务
void priority_algorithm(); //优先级调度
void round_robin(); //时间片轮转
void init(); //初始化,完成pcb录入
pcb *sort_time(pcb*); //对init()录入的pcb按到达时间排序
pcb *sort_priority(pcb*);//对pcb按优先级排序 
void timer(); //定时器,每一个延迟自我调用一次
void result(); //打印结果
void fcfs();//先到先服务算法
void fcfs()
{
if (now->t_arrive>TIME)
{
printf("[时间:%d]\t无进程运行\n", TIME);
return;
}
if (now->state == 'R')
{
now->state = 'O';
now->t_start = TIME;
printf("[时间:%d]\t进程:%s 首次运行\n", TIME, now->name);
}
else if (now->state == 'O')
{
(now->t_run)++;
if (now->t_run >= now->t_service)
{
now->state = 'F';
now->t_finish = TIME;
printf("[时间:%d]\t进程:%s 任务完成\n", TIME, now->name);
now = now->next;
if (now != NULL)
fcfs();
}
else
printf("[时间:%d]\t进程:%s 正在运行,已运行时间:%d\n", TIME, now->name, now->t_run);
}
}


//优先级调度算法
void priority_algorithm()
{
pcb *p = head, *p_max = NULL;
unsigned short t_max = 0;
//从现在时间以前并且未结束的进程中,选出优先级最大的进程
while (p != NULL && p->t_arrive <= TIME)
{
if (p->state == 'F')
{
p = p->next;
continue;
}
if (p->state == 'W')
{
p->t_wait++;
}
if (p->priority>t_max)
{
t_max = p->priority;
p_max = p;
}
p = p->next;
}
//如果为空,判断全部进程是否都已完成
if (p_max == NULL)
{
unsigned int k = 1;
p = head;
while (p != NULL)
{
if (p->state != 'F')
k = 0;
p = p->next;
}
if (k == 1)
now = NULL;
else
printf("[时间:%d]\t无进程运行\n", TIME);
return;
}
//如果选出的进程和之前的不同
if (p_max != now)
{
if (now->state == 'O')
{
now->state = 'W';
printf("[时间:%d]\t进程:%s 暂停运行\n", TIME, now->name);
}
}
if (p_max == NULL)
now = head;
else
now = p_max;
if (now->state == 'R')
{
now->state = 'O';
now->t_start = TIME;
printf("[时间:%d]\t进程:%s 首次运行\n", TIME, now->name);
}
else
{
if (now->state == 'W')
{
now->state = 'O';
now->t_wait = 0;
printf("[时间:%d]\t进程:%s 继续运行\n", TIME, now->name);
}
(now->t_run)++;
if (now->t_run >= now->t_service)
{
now->state = 'F';
now->t_finish = TIME;
printf("[时间:%d]\t进程:%s 任务完成\n", TIME, now->name);
}
else
printf("[时间:%d]\t进程:%s 正在运行,已运行时间:%d\n", TIME, now->name, now->t_run);
}
}
//时间片轮转算法
void round_robin()
{
pcb* p = head, *q = now;
//每个时间片结束时的处理
if (TIME%SJP == 0 && TIME / SJP>0)
{
int k = 1;
while (p != NULL)
{
if (p->state != 'F')
{
k = 0;
break;
}
p = p->next;
}
if (k == 1)
{
now = NULL;
return;
}
if (p != NULL)
{
while (1)
{
if (q->next != NULL)
{
if ((q->next)->state == 'F')
{
q = q->next;
continue;
}
else
{
now = q->next;
break;
}
}
else
{
q = head;
if (q->state == 'F')
continue;
else
{
now = head;
break;
}
}
}
}
else
{
p = head;
while (p->next != NULL) p = p->next;
now = p;
}
}
if (now->t_arrive>TIME)
{
printf("[时间:%d]\t无进程运行\n", TIME);
return;
}
if (now->state == 'R')
{
now->state = 'O';
now->t_start = TIME;
printf("[时间:%d]\t进程:%s 首次运行\n", TIME, now->name);
}
else if (now->state == 'O')
{
(now->t_run)++;
if (now->t_run >= now->t_service)
{
now->state = 'F';
now->t_finish = TIME;
printf("[时间:%d]\t进程:%s 任务完成\n", TIME, now->name);
if (now != NULL)
fcfs();
}
else
printf("[时间:%d]\t进程:%s 正在运行,已运行时间:%d\n", TIME, now->name, now->t_run);
}
else if (now->state == 'F')
printf("[时间:%d]\t无进程运行\n", TIME);
}
void result()
{
head = sort_priority(head);
pcb *p = head;
printf("\n========================运行结果==============\n\n");
printf("名称 优先级 到达时间 开始时间 完成时间 服务时间\n");
while (p != NULL)
{
printf(" %s\t%d\t %d\t %d\t %d\t %d\n", p->name, p->priority, p->t_arrive,
p->t_start, p->t_finish, p->t_service);
p = p->next;
}
}
void timer()
{
if (TYPE == '1')
{
fcfs();
}
else if (TYPE == '2')
{
priority_algorithm();
}
else if (TYPE == '3')
{
round_robin();
}
if (now == NULL)
return;
TIME++;
flag++;
Sleep(DELAY);
timer();
}
void init()
{
pcb *p, *q;
printf("\t\t1:先来先服务\n\t\t2:优先级\n\t\t3:时间片轮转\n输入模拟类型:");
scanf("%c", &TYPE);
//printf("输入进程数目:\n");
//scanf("%d", &NUM);
//system("cls");
//printf("请录入%d个进程信息:\n", NUM);
//for (i = 0; i<NUM; i++)
//{
p = (pcb *)malloc(sizeof(pcb));
//printf("[第%d个] 依次输入:\n名称 优先数 到达时间 服务时间 \n", i + 1);
//scanf("%s\t%d\t%d\t %d", &p->name, &p->priority, &p->t_arrive, &p->t_service);
if (head == NULL)
{
head = p;
q = p;
}
strcpy(p->name, "1");
p->priority = 5;
p->t_arrive = 3;
p->t_service = 4;
q->next = p;
p->t_start = 0;
p->t_finish = 0;
p->t_run = 0;
p->t_wait = 0;
p->next = NULL;
p->state = 'R';
q = p;
//}

p = (pcb *)malloc(sizeof(pcb));
if (head == NULL)
{
head = p;
q = p;
}
strcpy(p->name, "2");
p->priority = 8;
p->t_arrive = 5;
p->t_service = 5;
q->next = p;
p->t_start = 0;
p->t_finish = 0;
p->t_run = 0;
p->t_wait = 0;
p->next = NULL;
p->state = 'R';
q = p;

p = (pcb *)malloc(sizeof(pcb));
if (head == NULL)
{
head = p;
q = p;
}
strcpy(p->name, "3");
p->priority = 6;
p->t_arrive = 9;
p->t_service = 3;
q->next = p;
p->t_start = 0;
p->t_finish = 0;
p->t_run = 0;
p->t_wait = 0;
p->next = NULL;
p->state = 'R';
q = p;
}
//按到达时间冒泡排序
pcb* sort_time(pcb *h_head)
{
pcb *p, *p1, *p2, *p3;
pcb h, t;
if (h_head == NULL)
return NULL;
h.next = h_head;
p = &h;
while (p->next != NULL)
{
p = p->next;
}
p = p->next = &t;
while (p != h.next)
{
p3 = &h;
p1 = p3->next;
p2 = p1->next;
while (p2 != p)
{
if ((p1->t_arrive)>(p2->t_arrive))
{
p1->next = p2->next;
p2->next = p1;
p3->next = p2;
p3 = p2;
p2 = p1->next;
}
else
{
p3 = p1;
p1 = p2;
p2 = p2->next;
}
}
p = p1;
}
while (p->next != &t)
{
p = p->next;
}
p->next = NULL;
return h.next;
}
pcb* sort_priority(pcb *h_head)
{
pcb *p, *p1, *p2, *p3;
pcb h, t;
if (h_head == NULL)
return NULL;
h.next = h_head;
p = &h;
while (p->next != NULL)
{
p = p->next;
}
p = p->next = &t;
while (p != h.next)
{
p3 = &h;
p1 = p3->next;
p2 = p1->next;
while (p2 != p)
{
if ((p1->priority)<(p2->priority))
{
p1->next = p2->next;
p2->next = p1;
p3->next = p2;
p3 = p2;
p2 = p1->next;
}
else
{
p3 = p1;
p1 = p2;
p2 = p2->next;
}
}
p = p1;
}
while (p->next != &t)
{
p = p->next;
}
p->next = NULL;
return h.next;
}
int main()
{
init();
head = sort_time(head);
now = head;
system("CLS");
printf("进程正在模拟运行:\n");
timer();
result();
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值