c语言实现时间片轮转调度算法

#include <stdio.h>//引入输入输出流文件
#include <stdlib.h>
struct PCB
{
int pid;//进程标识符
int rr;//已运行时间
int time;//进程要求运行时间
char sta;//进程的状态
struct PCB *next;//链接指针
};
struct PCB pcb1,pcb2,pcb3,pcb4,pcb5,*tail,*head,*rp;
void init()//初始化各个进程的运行时间
{
int time;
pcb1.pid = 1;//用结构体进行赋初值
pcb2.pid = 2;
pcb3.pid = 3;
pcb4.pid = 4;
pcb5.pid = 5;
pcb1.rr =pcb2.rr =pcb3.rr =pcb4.rr =pcb5.rr = 0;//各个间片赋值是0
pcb1.sta = pcb2.sta = pcb3.sta = pcb4.sta = pcb5.sta = ‘w’;//用w字符表示wait等待
printf(“请输入进程p1需要运行的时间:”);
scanf("%d",&time);
pcb1.time =time;//把时间元素赋值给结构体元素
printf(“请输入进程p2需要运行的时间:”);
scanf("%d",&time);
pcb2.time=time; //把时间元素赋值给结构体元素
printf(“请输入进程p3需要运行的时间:”);
scanf("%d",&time);
pcb3.time=time; //把时间元素赋值给结构体元素
printf(“请输入进程p4需要运行的时间:”);
scanf("%d",&time);
pcb4.time =time; //把时间元素赋值给结构体元素
printf(“请输入进程p5需要运行的时间:”);
scanf("%d",&time);
pcb5.time=time; //把时间元素赋值给结构体元素
pcb1.next=&pcb2;//指向下一个地址
pcb2.next=&pcb3; //指向下一个地址
pcb3.next=&pcb4; //指向下一个地址
pcb4.next=&pcb5; /指向下一个地址
pcb5.next=&pcb1; //指向下一个地址
head=&pcb1;
tail=&pcb5;
}
void printf1()//显示表头
{
printf(“±--------------|---------------|---------------|---------------+\n”);
printf("|\tpid\t|\trr\t|\ttime\t|\tSTA\t|\n");//利用制表符号打印各表的头部
printf("|---------------|---------------|---------------|---------------|\n");
}
void printf2()//显示各个进程的初始状态
{
printf(“processes p%d running\n”,head->pid);//显示打印到第几个进程的状态
printf1();
printf("|\t%d\t|\t%d\t|\t%d\t|\t%c\t|\n",head->pid,head->rr,head->time,head->sta);
printf("|---------------|---------------|---------------|---------------|\n");
rp=head;
while(rp!=tail)
{
rp=rp->next;
printf("|\t%d\t|\t%d\t|\t%d\t|\t%c\t|\n",rp->pid,rp->rr,rp->time,rp->sta);
printf("|---------------|---------------|---------------|---------------|\n");
}
}
void operation()//运行
{
int flag=1;//定义标志位
while(flag<=5)//定义while循环
{
head->rr++; //头指针进行下一位位移
if((head->rrhead->time)||(head->time0))//if语句判断如果头部位等于执行时间
{
tail->sta=‘w’;//将进程状态设置为等待态
head->sta=‘f’;//将进程状态设置为执行态
printf2();//在operation()函数内部调用print()
head=head->next;
tail->next=head;
flag++;//标志位下移
}
else
{
tail->sta=‘w’;//将进程状态设置为等待态
head->sta=‘r’;//将进程状态设置为就绪态
printf2();
tail=head;
head=head->next;
}
}
}
void main()
{
init();
printf2();
operation();
}

输出结果:
请输入进程p1需要运行的时间:5
请输入进程p2需要运行的时间:10
请输入进程p3需要运行的时间:15
请输入进程p4需要运行的时间:20
请输入进程p5需要运行的时间:25
processes p1 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
105w
------------------------------------------------------------
2010w
------------------------------------------------------------
3015w
------------------------------------------------------------
4020w
------------------------------------------------------------
5025w
------------------------------------------------------------

processes p1 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
115r
------------------------------------------------------------
2010w
------------------------------------------------------------
3015w
------------------------------------------------------------
4020w
------------------------------------------------------------
5025w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2110r
------------------------------------------------------------
3015w
------------------------------------------------------------
4020w
------------------------------------------------------------
5025w
------------------------------------------------------------
115w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3115r
------------------------------------------------------------
4020w
------------------------------------------------------------
5025w
------------------------------------------------------------
115w
------------------------------------------------------------
2110w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4120r
------------------------------------------------------------
5025w
------------------------------------------------------------
115w
------------------------------------------------------------
2110w
------------------------------------------------------------
3115w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5125r
------------------------------------------------------------
115w
------------------------------------------------------------
2110w
------------------------------------------------------------
3115w
------------------------------------------------------------
4120w
------------------------------------------------------------

processes p1 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
125r
------------------------------------------------------------
2110w
------------------------------------------------------------
3115w
------------------------------------------------------------
4120w
------------------------------------------------------------
5125w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2210r
------------------------------------------------------------
3115w
------------------------------------------------------------
4120w
------------------------------------------------------------
5125w
------------------------------------------------------------
125w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3215r
------------------------------------------------------------
4120w
------------------------------------------------------------
5125w
------------------------------------------------------------
125w
------------------------------------------------------------
2210w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4220r
------------------------------------------------------------
5125w
------------------------------------------------------------
125w
------------------------------------------------------------
2210w
------------------------------------------------------------
3215w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5225r
------------------------------------------------------------
125w
------------------------------------------------------------
2210w
------------------------------------------------------------
3215w
------------------------------------------------------------
4220w
------------------------------------------------------------

processes p1 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
135r
------------------------------------------------------------
2210w
------------------------------------------------------------
3215w
------------------------------------------------------------
4220w
------------------------------------------------------------
5225w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2310r
------------------------------------------------------------
3215w
------------------------------------------------------------
4220w
------------------------------------------------------------
5225w
------------------------------------------------------------
135w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3315r
------------------------------------------------------------
4220w
------------------------------------------------------------
5225w
------------------------------------------------------------
135w
------------------------------------------------------------
2310w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4320r
------------------------------------------------------------
5225w
------------------------------------------------------------
135w
------------------------------------------------------------
2310w
------------------------------------------------------------
3315w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5325r
------------------------------------------------------------
135w
------------------------------------------------------------
2310w
------------------------------------------------------------
3315w
------------------------------------------------------------
4320w
------------------------------------------------------------

processes p1 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
145r
------------------------------------------------------------
2310w
------------------------------------------------------------
3315w
------------------------------------------------------------
4320w
------------------------------------------------------------
5325w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2410r
------------------------------------------------------------
3315w
------------------------------------------------------------
4320w
------------------------------------------------------------
5325w
------------------------------------------------------------
145w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3415r
------------------------------------------------------------
4320w
------------------------------------------------------------
5325w
------------------------------------------------------------
145w
------------------------------------------------------------
2410w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4420r
------------------------------------------------------------
5325w
------------------------------------------------------------
145w
------------------------------------------------------------
2410w
------------------------------------------------------------
3415w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5425r
------------------------------------------------------------
145w
------------------------------------------------------------
2410w
------------------------------------------------------------
3415w
------------------------------------------------------------
4420w
------------------------------------------------------------

processes p1 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
155f
------------------------------------------------------------
2410w
------------------------------------------------------------
3415w
------------------------------------------------------------
4420w
------------------------------------------------------------
5425w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2510r
------------------------------------------------------------
3415w
------------------------------------------------------------
4420w
------------------------------------------------------------
5425w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3515r
------------------------------------------------------------
4420w
------------------------------------------------------------
5425w
------------------------------------------------------------
2510w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4520r
------------------------------------------------------------
5425w
------------------------------------------------------------
2510w
------------------------------------------------------------
3515w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5525r
------------------------------------------------------------
2510w
------------------------------------------------------------
3515w
------------------------------------------------------------
4520w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2610r
------------------------------------------------------------
3515w
------------------------------------------------------------
4520w
------------------------------------------------------------
5525w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3615r
------------------------------------------------------------
4520w
------------------------------------------------------------
5525w
------------------------------------------------------------
2610w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4620r
------------------------------------------------------------
5525w
------------------------------------------------------------
2610w
------------------------------------------------------------
3615w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5625r
------------------------------------------------------------
2610w
------------------------------------------------------------
3615w
------------------------------------------------------------
4620w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2710r
------------------------------------------------------------
3615w
------------------------------------------------------------
4620w
------------------------------------------------------------
5625w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3715r
------------------------------------------------------------
4620w
------------------------------------------------------------
5625w
------------------------------------------------------------
2710w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4720r
------------------------------------------------------------
5625w
------------------------------------------------------------
2710w
------------------------------------------------------------
3715w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5725r
------------------------------------------------------------
2710w
------------------------------------------------------------
3715w
------------------------------------------------------------
4720w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2810r
------------------------------------------------------------
3715w
------------------------------------------------------------
4720w
------------------------------------------------------------
5725w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3815r
------------------------------------------------------------
4720w
------------------------------------------------------------
5725w
------------------------------------------------------------
2810w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4820r
------------------------------------------------------------
5725w
------------------------------------------------------------
2810w
------------------------------------------------------------
3815w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5825r
------------------------------------------------------------
2810w
------------------------------------------------------------
3815w
------------------------------------------------------------
4820w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
2910r
------------------------------------------------------------
3815w
------------------------------------------------------------
4820w
------------------------------------------------------------
5825w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
3915r
------------------------------------------------------------
4820w
------------------------------------------------------------
5825w
------------------------------------------------------------
2910w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
4920r
------------------------------------------------------------
5825w
------------------------------------------------------------
2910w
------------------------------------------------------------
3915w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
5925r
------------------------------------------------------------
2910w
------------------------------------------------------------
3915w
------------------------------------------------------------
4920w
------------------------------------------------------------

processes p2 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
21010f
------------------------------------------------------------
3915w
------------------------------------------------------------
4920w
------------------------------------------------------------
5925w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
31015r
------------------------------------------------------------
4920w
------------------------------------------------------------
5925w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41020r
------------------------------------------------------------
5925w
------------------------------------------------------------
31015w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51025r
------------------------------------------------------------
31015w
------------------------------------------------------------
41020w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
31115r
------------------------------------------------------------
41020w
------------------------------------------------------------
51025w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41120r
------------------------------------------------------------
51025w
------------------------------------------------------------
31115w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51125r
------------------------------------------------------------
31115w
------------------------------------------------------------
41120w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
31215r
------------------------------------------------------------
41120w
------------------------------------------------------------
51125w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41220r
------------------------------------------------------------
51125w
------------------------------------------------------------
31215w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51225r
------------------------------------------------------------
31215w
------------------------------------------------------------
41220w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
31315r
------------------------------------------------------------
41220w
------------------------------------------------------------
51225w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41320r
------------------------------------------------------------
51225w
------------------------------------------------------------
31315w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51325r
------------------------------------------------------------
31315w
------------------------------------------------------------
41320w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
31415r
------------------------------------------------------------
41320w
------------------------------------------------------------
51325w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41420r
------------------------------------------------------------
51325w
------------------------------------------------------------
31415w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51425r
------------------------------------------------------------
31415w
------------------------------------------------------------
41420w
------------------------------------------------------------

processes p3 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
31515f
------------------------------------------------------------
41420w
------------------------------------------------------------
51425w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41520r
------------------------------------------------------------
51425w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51525r
------------------------------------------------------------
41520w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41620r
------------------------------------------------------------
51525w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51625r
------------------------------------------------------------
41620w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41720r
------------------------------------------------------------
51625w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51725r
------------------------------------------------------------
41720w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41820r
------------------------------------------------------------
51725w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51825r
------------------------------------------------------------
41820w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
41920r
------------------------------------------------------------
51825w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
51925r
------------------------------------------------------------
41920w
------------------------------------------------------------

processes p4 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
42020f
------------------------------------------------------------
51925w
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
52025r
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
52125r
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
52225r
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
52325r
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
52425r
------------------------------------------------------------

processes p5 running
±--------------|---------------|---------------|---------------+

pidrrtimeSTA
52525f
------------------------------------------------------------

Program ended with exit code: 0

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,下面是一份简单的时间片轮转调度算法的 C 语言实现代码。在这个示例中,我们使用了一个结构体来代表进程,并且每个进程都有一个唯一的 ID 和一个剩余的执行时间量(用于模拟进程的执行)。 ``` #include <stdio.h> #include <stdlib.h> #define TIME_QUANTUM 2 // 时间片长度 // 进程的结构体定义 typedef struct process { int id; // 进程 ID int exec_time; // 剩余执行时间 } process_t; int main() { int num_processes; // 进程数 printf("请输入进程数:"); scanf("%d", &num_processes); // 动态分配存储空间 process_t *processes = (process_t *)malloc(num_processes * sizeof(process_t)); // 初始化进程 for (int i = 0; i < num_processes; i++) { processes[i].id = i; printf("请输入进程 %d 的执行时间:", i); scanf("%d", &processes[i].exec_time); } // 时间片轮转调度算法 int time = 0; // 当前时间 int curr_proc = 0; // 当前执行的进程 while (1) { // 执行当前进程 processes[curr_proc].exec_time -= TIME_QUANTUM; time += TIME_QUANTUM; // 判断当前进程是否执行完毕 if (processes[curr_proc].exec_time <= 0) { printf("进程 %d 执行完毕,用时 %d 秒\n", curr_proc, time); num_processes--; // 如果所有进程都执行完毕,则退出循环 if (num_processes == 0) { break; } // 删除当前进程,并选择下一个进程 for (int i = curr_proc; i < num_processes; i++) { processes[i] = processes[i + 1]; } curr_proc = curr_proc % num_processes; } // 选择下一个进程 curr_proc = (curr_proc + 1) % num_processes; } // 释放存储空间 free(processes); return 0; } ``` 这个示例程序可以通过用户输入进程数和每个进程的执行时间来模拟时间片轮转调度算法的执行过程,并输出每个进程的执行时间。需要注意的是,在实际的操作系统中,可能会使用更加复杂的调度算法来确保系统的性能和响应速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GOD FOR JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值