#include <stdio.h>
#include <string.h>
#define MAX_PROC 10 //最多模拟10个进程,可按需改大
//进程状态枚举
typedef enum {
READY,
RUNNING,
FINISHED
} ProcStatus;
//进程控制块PCB结构体
typedef struct {
int pid;
char name[20];
int run_time;
ProcStatus status;
} Process;
Process procList[MAX_PROC];
int procCount = 0; //当前存在的进程数目
int globalCounter = 0; //临界共享计数器
int nextPid = 1;
//状态转中文字符串
const char* getStatusStr(ProcStatus s)
{
switch(s)
{
case READY: return "就绪";
case RUNNING: return "运行";
case FINISHED: return "结束";
default: return "未知";
}
}
//1. 添加进程
void addProcess()
{
if(procCount >= MAX_PROC)
{
printf("❌ 进程数量已达上限,无法新增\n");
return;
}
Process p;
p.pid = nextPid++;
printf("请输入进程名称:");
scanf("%s", p.name);
printf("请输入模拟运行时长(整数):");
scanf("%d", &p.run_time);
p.status = READY;
procList[procCount++] = p;
printf("✅ 进程添加成功,PID=%d\n", p.pid);
}
//2. 打印全部进程列表
void printAllProcess()
{
int i;
printf("\n==================== 进程列表 ====================\n");
printf("%-8s%-12s%-12s%-10s\n", "PID", "进程名", "运行时长", "状态");
printf("---------------------------------------------------\n");
for(i = 0; i < procCount; i++)
{
printf("%-8d%-12s%-12d%-10s\n",
procList[i].pid,
procList[i].name,
procList[i].run_time,
getStatusStr(procList[i].status));
}
printf("===================================================\n\n");
}
//3. 删除指定PID进程
void delProcess()
{
int pid, i, j;
printf("输入要删除的进程PID:");
scanf("%d", &pid);
for(i = 0; i < procCount; i++)
{
if(procList[i].pid == pid)
{
//后面元素前移覆盖,完成删除
for(j = i; j < procCount - 1; j++)
{
procList[j] = procList[j+1];
}
procCount--;
printf("✅ PID=%d 已删除\n", pid);
return;
}
}
printf("❌ 找不到该PID进程\n");
}
//4. FCFS先来先服务调度 + 临界资源模拟
void fcfsSchedule()
{
int i;
printf("\n---------- 开始 FCFS 先来先服务调度 ----------\n");
for(i = 0; i < procCount; i++)
{
if(procList[i].status != READY)
continue;
procList[i].status = RUNNING;
printf("[调度日志] PID:%d(%s) 进入运行状态\n", procList[i].pid, procList[i].name);
printf("[调度日志] PID:%d 模拟运行,运行时长=%d\n", procList[i].pid, procList[i].run_time);
//临界区
printf("[临界区] PID:%d 进入临界区,准备修改共享计数器\n", procList[i].pid);
globalCounter += procList[i].run_time;
printf("[临界区] PID:%d 修改共享计数器,计数器当前值 = %d\n", procList[i].pid, globalCounter);
printf("[临界区] PID:%d 离开临界区\n", procList[i].pid);
procList[i].status = FINISHED;
printf("[调度日志] PID:%d 运行完成,状态变为【结束】\n\n", procList[i].pid);
}
printf("---------- FCFS调度全部就绪进程执行完毕 ----------\n");
printf("调度结束后共享计数器最终值:%d\n\n", globalCounter);
}
//5. 拓展加分:打印就绪队列
void printReadyQueue()
{
int i;
printf("\n-------- 就绪队列(加分功能) --------\n");
int hasReady = 0;
for(i = 0; i < procCount; i++)
{
if(procList[i].status == READY)
{
printf("PID:%d %s\n", procList[i].pid, procList[i].name);
hasReady = 1;
}
}
if(!hasReady)
printf("就绪队列为空\n");
printf("------------------------------------\n");
}
//菜单
void showMenu()
{
printf("======== 简易进程管理模拟器(C语言) ========\n");
printf("1. 添加模拟进程\n");
printf("2. 打印全部进程列表\n");
printf("3. 删除指定PID进程\n");
printf("4. FCFS先来先服务调度模拟(演示临界资源)\n");
printf("5. 打印就绪队列【拓展加分】\n");
printf("0. 退出程序\n");
printf("==========================================\n");
printf("请输入操作序号:");
}
int main()
{
int op;
while(1)
{
showMenu();
scanf("%d", &op);
switch(op)
{
case 1: addProcess(); break;
case 2: printAllProcess(); break;
case 3: delProcess(); break;
case 4: fcfsSchedule(); break;
case 5: printReadyQueue(); break;
case 0:
printf("程序退出。\n");
return 0;
default:
printf("输入无效,请重新选择!\n");
}
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: