void init_pcb(PCB* pcb, int pid, int priority, int time_required) { pcb->pid = pid; pcb->priority = priority; pcb->time_required = time_required; pcb->dynamic_priority = priority; pcb->next = NULL; }
时间: 2024-02-26 20:53:22 浏览: 116
这段代码是一个函数定义,函数名为 init_pcb,参数列表包括一个指向 PCB 结构体的指针 pcb,一个 pid 整型变量,一个 priority 整型变量,一个 time_required 整型变量。
函数内部给 pcb 结构体的各个成员变量赋值,包括 pid,priority,time_required 和 dynamic_priority。其中,pid 表示进程 ID,priority 表示进程优先级,time_required 表示进程需要的执行时间,dynamic_priority 表示动态优先级,初始值等于静态优先级。
函数最后将 PCB 的 next 指针设置为 NULL,表示该 PCB 不属于任何链表。
需要注意的是,该函数中传入的指针必须是已经分配空间的指针,否则会出现错误。
相关问题
简析代码:void FCFS(PCB pro[], int num) { int time,done_time; int i,count,tt,pronum; float sum_T_time,sum_QT_time; PCB *curpro,*temp_PCB; printf("\n\t\t\t\t\t先来先服务算法进程调度模拟\n\n"); printf("\t————————————————————————————————————————————————\n"); count=0; PCB pro2[100]; sortWithEnterTime(pro, num); PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue)); Queueinit(queue); EnterQueue(queue, &pro[0]); time = pro[0].arrivetime; pronum = 1; sum_T_time = 0, sum_QT_time = 0; while (queue->size > 0) { curpro = poll(queue); if (time < curpro->arrivetime){ time = curpro->arrivetime; } done_time = time + curpro->running_time; curpro->start_time=time; curpro->done_time=done_time; curpro->zztime = done_time - curpro->arrivetime; curpro->dqzztime = curpro->zztime / curpro->running_time; sum_T_time += curpro->zztime; sum_QT_time += curpro->dqzztime; for (tt = time; tt <= done_time && pronum < num; tt++) { if (tt >= pro[pronum].arrivetime) { EnterQueue(queue, &pro[pronum]); pronum++; } } CopyProgram(&pro2[count],curpro); PrintRunningprogram(&pro2[count]); count++; if(queue->size!=0) { printf("\t就绪队列:\n"); printf("\t————————————————————————————————————————————————\n"); printf("\t进程 到达时间 服务时间 优先级\n"); temp_PCB=queue->firstProg->next; for(i=queue->size; i>0; i--) { printf("\t%s\t%d\t%d\t%d\n",temp_PCB->name,temp_PCB->arrivetime,temp_PCB->running_time,temp_PCB->priority); temp_PCB=temp_PCB->next; } printf("\t————————————————————————————————————————————————\n"); printf("\n\n\n"); } else { printf("\t无进程处于就绪状态!\n"); printf("\t————————————————————————————————————————————————\n\n\n"); } time += curpro->running_time; if (queue->siz
该函数是一个先来先服务(FCFS)的进程调度算法模拟。其输入参数包括一个进程控制块(PCB)数组和进程数量。该函数输出模拟的结果。
该函数的主要实现流程如下:
1. 按照进程的到达时间对进程进行排序。
2. 创建一个PCB队列,将第一个进程加入队列,并将当前时间设置为第一个进程的到达时间。
3. 对于队列中的每个进程,计算该进程的完成时间、开始时间、周转时间和带权周转时间,并将其加入一个新的PCB数组中(用于后续输出)。
4. 在计算当前进程完成后,将到达时间在当前时间之前的所有进程加入队列。
5. 输出当前正在运行的进程和就绪队列中的所有进程信息。
6. 重复步骤3-5直到队列为空。
其中,PCB结构体包括进程的名称、到达时间、服务时间、优先级、开始时间、完成时间、周转时间和带权周转时间等信息。Queueinit()函数用于初始化PCB队列,EnterQueue()函数用于将进程加入队列,poll()函数用于从队列中取出一个进程。
#define MAX_PROCESS_NUM 10 typedef enum{ READY, RUNNING, BLOCKED } ProcessState; typedef struct{ int pid; ProcessState state; int priority; int remain_time_slice; } PCB; PCB processes[MAX_PROCESS_NUM]; int process_num = 0; #define TIME_SLICE 3 void time_slice_scheduling(){ int i; do{ for(i = 0; i < process_num; i++){ if(processes[i].state == RUNNING){ processes[i].remain_time_slice--; if(processes[i].remain_time_slice == 0){ processes[i].state = READY; } } if(processes[i].state == READY){ processes[i].state = RUNNING; processes[i].remain_time_slice = TIME_SLICE; } } }while(1); } void priority_scheduling(){ int i, j, max_priority, max_index; do{ max_priority = -1; max_index = -1; for(i = 0; i < process_num; i++){ if(processes[i].state == READY && processes[i].priority > max_priority){ max_priority = processes[i].priority; max_index = i; } } if(max_index != -1){ processes[max_index].state = RUNNING; for(j = 0; j < process_num; j++){ if(j != max_index && processes[j].state != BLOCKED){ processes[j].state = READY; } } } }while(1); } #include <stdio.h> int main(){ // 创建进程并初始化 processes[0].pid = 0; processes[0].state = RUNNING; processes[0].priority = 2; processes[0].remain_time_slice = TIME_SLICE; processes[1].pid = 1; processes[1].state = READY; processes[1].priority = 1; processes[1].remain_time_slice = 0; processes[2].pid = 2; processes[2].state = READY; processes[2].priority = 3; processes[2].remain_time_slice = 0; process_num = 3; // 调用时间片轮转调度算法 time_slice_scheduling(); // 调用静态优先级调度算法 priority_scheduling(); return 0; }代码运行错误,请修改
这段代码存在两个死循环,分别在 time_slice_scheduling() 和 priority_scheduling() 函数中。这会导致程序无法正常退出,需要在循环中添加退出条件。另外,在 priority_scheduling() 函数中,当没有就绪进程时,应该等待新的进程到来,而不是一直循环。修改后的代码如下:
```
#include <stdio.h>
#define MAX_PROCESS_NUM 10
#define TIME_SLICE 3
typedef enum { READY, RUNNING, BLOCKED } ProcessState;
typedef struct {
int pid;
ProcessState state;
int priority;
int remain_time_slice;
} PCB;
PCB processes[MAX_PROCESS_NUM];
int process_num = 0;
void time_slice_scheduling() {
int i;
do {
int all_blocked = 1;
for (i = 0; i < process_num; i++) {
if (processes[i].state == RUNNING) {
processes[i].remain_time_slice--;
if (processes[i].remain_time_slice == 0) {
processes[i].state = READY;
} else {
all_blocked = 0;
}
}
if (processes[i].state == READY) {
processes[i].state = RUNNING;
processes[i].remain_time_slice = TIME_SLICE;
all_blocked = 0;
}
}
if (all_blocked) {
break;
}
} while (1);
}
void priority_scheduling() {
int i, j, max_priority, max_index;
do {
int all_blocked = 1;
max_priority = -1;
max_index = -1;
for (i = 0; i < process_num; i++) {
if (processes[i].state == READY && processes[i].priority > max_priority) {
max_priority = processes[i].priority;
max_index = i;
all_blocked = 0;
}
}
if (!all_blocked) {
processes[max_index].state = RUNNING;
for (j = 0; j < process_num; j++) {
if (j != max_index && processes[j].state != BLOCKED) {
processes[j].state = READY;
}
}
} else {
break;
}
} while (1);
}
int main() {
// 创建进程并初始化
processes[0].pid = 0;
processes[0].state = RUNNING;
processes[0].priority = 2;
processes[0].remain_time_slice = TIME_SLICE;
processes[1].pid = 1;
processes[1].state = READY;
processes[1].priority = 1;
processes[1].remain_time_slice = 0;
processes[2].pid = 2;
processes[2].state = READY;
processes[2].priority = 3;
processes[2].remain_time_slice = 0;
process_num = 3;
// 调用时间片轮转调度算法
time_slice_scheduling();
// 调用静态优先级调度算法
priority_scheduling();
return 0;
}
```
阅读全文