-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRR.cpp
95 lines (95 loc) · 2.56 KB
/
RR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#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;
pcb1.sta = pcb2.sta = pcb3.sta = pcb4.sta = pcb5.sta = 'w';
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)
{
head->rr++;
if ((head->rr == head->time) || (head->time == 0))
{
tail->sta = 'w'; // 将进程状态设置为等待态
head->sta = 'f'; // 将进程状态设置为执行态
printf2();
head = head->next;
tail->next = head;
flag++;
}
else
{
tail->sta = 'w'; // 将进程状态设置为等待态
head->sta = 'r'; // 将进程状态设置为就绪态
printf2();
tail = head;
head = head->next;
}
}
}
int main(void)
{
init();
printf2();
operation();
}