进程的三种基本状态在代码里用c语言如何表示?
参考下进程的三种状态转换
#include <stdio.h>
#include <stdlib.h>
#define Ready 0
#define Runnig 1
#define Block 2
typedef struct ProcessNode {
int pid; //进程标识符
int status; //进程状态
struct ProcessNode *next;
} ProcessNode;
typedef struct ListQueue {
ProcessNode head;
int process_num;
}ListQueue;
//创建一个进程节点
ProcessNode *create_ProcessNode(int pid, int status) {
ProcessNode *node = (ProcessNode *)malloc(sizeof(ProcessNode));
node->pid = pid;
node->status = status;
node->next = NULL;
return node;
}
//初始化一个队列
ListQueue *init_queue() {
ListQueue *que = (ListQueue *)malloc(sizeof(ListQueue));
que->head.next = NULL;
que->process_num = 0;
return que;
}
//判空
int empty(ListQueue *que) {
if(que->process_num == 0) return 1;
else return 0;
}
//入队
int push(ListQueue *que, int pid, int status) {
if(que == NULL) return 0;
ProcessNode *node = create_ProcessNode(pid, status);
ProcessNode *p = &(que->head);
while(p->next) {
p = p->next;
}
node->next = p->next;
p->next = node;
que->process_num++;
return 1;
}
//出队
int pop(ListQueue *que) {
if(empty(que))return -1;
ProcessNode *p = &(que->head), *q;
int pid;
q = p->next;
p->next = q->next;
pid = q->pid;
free(q);
que->process_num--;
return pid;
}
//输出
void output(ListQueue *que) {
ProcessNode *p = que->head.next;
while(p) {
printf("%d ", p->pid);
p = p->next;
}
printf("\n");
return ;
}
//清空
void clear_ListQueue(ListQueue *que) {
if(que == NULL) return ;
ProcessNode *p, *q;
p = que->head.next;
while(p) {
q = p->next;
free(p);
p = q;
}
free(que);
return ;
}
int main() {
ListQueue *ready_queue = init_queue();
ListQueue *runnig_queue = init_queue();
ListQueue *block_queue = init_queue();
int n;
printf("------------------------------进程初始创建------------------------------\n");
printf("请输入进程数目:");
scanf("%d", &n);
printf("输入%d个进程的标识符:\n", n);
int pid;
for(int i = 1; i <= n; i++) {
scanf("%d", &pid);
getchar();
int status = Ready;
push(ready_queue, pid, status);
}
printf("进程已创建!\n");
printf("[就绪队列]:");
output(ready_queue);
printf("[执行队列]:");
output(runnig_queue);
printf("[阻塞队列]:");
output(block_queue);
printf("------------------------------进程状态转换------------------------------\n");
printf("说明:按P-->进程调度 按I-->I/O或其他事件发生 按O-->I/O或其他事件完成 按E-->时间片用完 \n");
while(1) {
printf("\n输入操作: ");
char c;
scanf("%c", &c);
getchar();
int pid;
if(c == 'P') {
pid = pop(ready_queue);
if(pid == -1) {
printf("就绪队列无进程!\n");
continue;
}
printf("进程 %d 由就绪态进入执行态!\n", pid);
push(runnig_queue, pid, Runnig);
} else if(c == 'I') {
pid = pop(runnig_queue);
if(pid == -1) {
printf("执行队列无进程!");
continue;
}
printf("进程 %d 由执行态进入阻塞态!\n", pid);
push(block_queue, pid, Block);
} else if(c == 'O'){
pid = pop(block_queue);
if(pid == -1) {
printf("阻塞队列无进程!\n");
continue;
}
printf("进程 %d 由阻塞态进入就绪态!\n", pid);
push(ready_queue, pid, Block);
} else if(c == 'E'){
pid = pop(runnig_queue);
if(pid == -1) {
printf("执行队列无进程!\n");
continue;
}
printf("进程 %d 由执行态进入就绪态!\n", pid);
push(ready_queue, pid, Block);
} else {
printf("操作错误,请看说明!");
}
printf("[就绪队列]:");
output(ready_queue);
printf("[执行队列]:");
output(runnig_queue);
printf("[阻塞队列]:");
output(block_queue);
}
clear_ListQueue(ready_queue);
clear_ListQueue(runnig_queue);
clear_ListQueue(block_queue);
return 0;
}
可以规定1,2,3分别表示进程的三种不同状态。