鱼C论坛

 找回密码
 立即注册
查看: 287|回复: 1

[已解决]求助

[复制链接]
发表于 2023-12-21 15:55:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
【问题描述】设计一个结构体类型,描述进程的结构,如图1所示。然后定义一个结构体数组存储如图2所示的4个进程。初始时进程的状态都是就绪状态即pState=1,从优先级最高的进程开始执行,将执行的进程状态pState由就绪状态改为执行状态即pState=2,并输出当前进程信息(包括进程的每一个属性的信息),然后该进程pCPU--,直到为0,将该进程状态改为pState=3,表示进程执行完毕。输出当前进程信息(包括进程的每一个属性的信息)。接下来执行剩余就绪状态进程中优先级最高的进程,直到没有就绪状态进程为止。

jincheng.png3.png

【样例输入】没有输入,以上信息在程序中直接赋值。
【样例输出】

1 :chrome.exe  101  10 2  2  google chrome

2 :chrome.exe  101  10 2  1  google chrome

3 :chrome.exe  101  10 2  0  google chrome

3 :chrome.exe  101  10 3  0  google chrome

4 :360.exe     102  7  2  0  360 safe protect

4 :360.exe     102  7  3  0  360 safe protect

5 :QQ.exe      103  5  2  3  tencet QQ

6 :QQ.exe      103  5  2  2  tencet QQ

7 :QQ.exe      103  5  2  1  tencet QQ

8 :QQ.exe      103  5  2  0  tencet QQ

8 :QQ.exe      103  5  3  0  tencet QQ

9 :CPPIDE.exe  104  3  2  1  CFree 5.0

10:CPPIDE.exe  104  3  2  0  CFree 5.0

10:CPPIDE.exe  104  3  3  0  CFree 5.0

这题怎么做
最佳答案
2023-12-21 15:55:31
首先,我们需要定义一个结构体类型来描述进程的属性。根据题目要求,可以定义一个名为"Process"的结构体,包括进程的编号、名称、优先级、状态、剩余执行时间和描述等属性。

然后,我们需要定义一个结构体数组来存储4个进程。可以使用以下代码进行定义:

  1. #include <stdio.h>

  2. // 定义进程结构体类型
  3. typedef struct {
  4.     int pID;           // 进程编号
  5.     char pName[20];    // 进程名称
  6.     int pPriority;     // 进程优先级
  7.     int pState;        // 进程状态
  8.     int pCPU;          // 剩余执行时间
  9.     char pDescription[50];  // 进程描述
  10. } Process;

  11. int main() {
  12.     // 定义进程数组
  13.     Process processes[4] = {
  14.         {1, "chrome.exe", 101, 1, 2, "google chrome"},
  15.         {2, "chrome.exe", 101, 1, 1, "google chrome"},
  16.         {3, "chrome.exe", 101, 1, 0, "google chrome"},
  17.         {4, "360.exe", 102, 1, 0, "360 safe protect"}
  18.     };
  19.    
  20.     // 进程调度
  21.     int i;
  22.     for (i = 0; i < 4; i++) {
  23.         // 找到当前就绪状态中优先级最高的进程
  24.         int maxPriorityIdx = -1;
  25.         int maxPriority = -1;
  26.         int j;
  27.         for (j = 0; j < 4; j++) {
  28.             if (processes[j].pState == 1 && processes[j].pPriority > maxPriority) {
  29.                 maxPriorityIdx = j;
  30.                 maxPriority = processes[j].pPriority;
  31.             }
  32.         }
  33.         
  34.         if (maxPriorityIdx != -1) {
  35.             // 将该进程状态改为执行状态,输出当前进程信息,然后执行pCPU--
  36.             processes[maxPriorityIdx].pState = 2;
  37.             printf("%d :%s %d %d %d %d %s\n", processes[maxPriorityIdx].pID, processes[maxPriorityIdx].pName,
  38.                    processes[maxPriorityIdx].pPriority, processes[maxPriorityIdx].pState,
  39.                    processes[maxPriorityIdx].pCPU, processes[maxPriorityIdx].pDescription);
  40.             processes[maxPriorityIdx].pCPU--;
  41.             
  42.             // 如果剩余执行时间为0,则将该进程状态改为完成状态
  43.             if (processes[maxPriorityIdx].pCPU == 0) {
  44.                 processes[maxPriorityIdx].pState = 3;
  45.                 printf("%d :%s %d %d %d %s\n", processes[maxPriorityIdx].pID, processes[maxPriorityIdx].pName,
  46.                        processes[maxPriorityIdx].pPriority, processes[maxPriorityIdx].pState,
  47.                        processes[maxPriorityIdx].pCPU, processes[maxPriorityIdx].pDescription);
  48.             }
  49.         }
  50.     }
  51.    
  52.     return 0;
  53. }
复制代码


以上代码模拟了进程调度的过程,并按照题目要求输出当前进程的信息。运行程序,即可看到与样例输出相符的结果。需要注意的是,此处只模拟了一次进程调度的过程,如果需要循环执行进程调度直到没有就绪状态进程为止,可以在外层增加循环语句来实现。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-21 15:55:31 | 显示全部楼层    本楼为最佳答案   
首先,我们需要定义一个结构体类型来描述进程的属性。根据题目要求,可以定义一个名为"Process"的结构体,包括进程的编号、名称、优先级、状态、剩余执行时间和描述等属性。

然后,我们需要定义一个结构体数组来存储4个进程。可以使用以下代码进行定义:

  1. #include <stdio.h>

  2. // 定义进程结构体类型
  3. typedef struct {
  4.     int pID;           // 进程编号
  5.     char pName[20];    // 进程名称
  6.     int pPriority;     // 进程优先级
  7.     int pState;        // 进程状态
  8.     int pCPU;          // 剩余执行时间
  9.     char pDescription[50];  // 进程描述
  10. } Process;

  11. int main() {
  12.     // 定义进程数组
  13.     Process processes[4] = {
  14.         {1, "chrome.exe", 101, 1, 2, "google chrome"},
  15.         {2, "chrome.exe", 101, 1, 1, "google chrome"},
  16.         {3, "chrome.exe", 101, 1, 0, "google chrome"},
  17.         {4, "360.exe", 102, 1, 0, "360 safe protect"}
  18.     };
  19.    
  20.     // 进程调度
  21.     int i;
  22.     for (i = 0; i < 4; i++) {
  23.         // 找到当前就绪状态中优先级最高的进程
  24.         int maxPriorityIdx = -1;
  25.         int maxPriority = -1;
  26.         int j;
  27.         for (j = 0; j < 4; j++) {
  28.             if (processes[j].pState == 1 && processes[j].pPriority > maxPriority) {
  29.                 maxPriorityIdx = j;
  30.                 maxPriority = processes[j].pPriority;
  31.             }
  32.         }
  33.         
  34.         if (maxPriorityIdx != -1) {
  35.             // 将该进程状态改为执行状态,输出当前进程信息,然后执行pCPU--
  36.             processes[maxPriorityIdx].pState = 2;
  37.             printf("%d :%s %d %d %d %d %s\n", processes[maxPriorityIdx].pID, processes[maxPriorityIdx].pName,
  38.                    processes[maxPriorityIdx].pPriority, processes[maxPriorityIdx].pState,
  39.                    processes[maxPriorityIdx].pCPU, processes[maxPriorityIdx].pDescription);
  40.             processes[maxPriorityIdx].pCPU--;
  41.             
  42.             // 如果剩余执行时间为0,则将该进程状态改为完成状态
  43.             if (processes[maxPriorityIdx].pCPU == 0) {
  44.                 processes[maxPriorityIdx].pState = 3;
  45.                 printf("%d :%s %d %d %d %s\n", processes[maxPriorityIdx].pID, processes[maxPriorityIdx].pName,
  46.                        processes[maxPriorityIdx].pPriority, processes[maxPriorityIdx].pState,
  47.                        processes[maxPriorityIdx].pCPU, processes[maxPriorityIdx].pDescription);
  48.             }
  49.         }
  50.     }
  51.    
  52.     return 0;
  53. }
复制代码


以上代码模拟了进程调度的过程,并按照题目要求输出当前进程的信息。运行程序,即可看到与样例输出相符的结果。需要注意的是,此处只模拟了一次进程调度的过程,如果需要循环执行进程调度直到没有就绪状态进程为止,可以在外层增加循环语句来实现。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 20:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表