weixin_54195891 2022-01-05 12:04 采纳率: 100%
浏览 66
已结题

进程的三种基本状态在代码里如何表示?

进程的三种基本状态在代码里用c语言如何表示?

  • 写回答

2条回答 默认 最新

  • 神仙别闹 2022-01-05 12:40
    关注

    参考下进程的三种状态转换

    #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条)

报告相同问题?

问题事件

  • 系统已结题 1月13日
  • 已采纳回答 1月5日
  • 创建了问题 1月5日

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂