操作系统非抢占式静态优先级调度算法(C语言)

主要数据结构

单链表(进程表,就绪进程队列),结构体(结构化进程信息)

大致步骤:

1.输入模块:接收进程信息,包括进程代号(简化了输入信息,只是int型),进程创建时间,进程运行时间,进程优先级,得到进程表
2.就绪进程模块:用Time记录系统当前运行的时间,得出就绪进程队列
3.对就绪进程队列按照优先序列排序
**4.**调度排序后就绪队列第一个执行

注意点:

state用来记录当前进程是否存在于就绪队列中(可以考虑把state放入结构体中这样更有逻辑性)

排序算法采用的是交换结点值的冒泡算法,而不是直接交换结点

就绪队列应该随着系统时间Time的变化实时更新,并且改变优先序列(即一个进程
完成调度,系统时间发生变化,就绪队列及其优先顺序也发生变化)

Time默认值为进程最小开始时间,可以改进更普适一点

拓展

其他调度算法只需要调整排序条件即可,如短进程优先把进程运行时间改为比较条件

C语言实现

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ProcessNum 5 //进程数
//进程结构体
typedef struct pcb
{
    int Pname; //进程代号
    int CreateTime; //创建时间
    int ProcessTime; //运行时间
    int Priority; //优先级
    struct pcb *next; //指向下一个进程
}PCB;
int Time = 0; //系统执行时间
int state[6] = {0,0,0,0,0,0}; //五个进程的到达状态
PCB *ready; // 就绪进程队列

//进程就绪队列
void Ready_Queue(PCB *head)
{
    PCB *h = head;
    PCB *current = NULL;
    while(h){
        if(h->CreateTime<=Time && state[h->Pname]==0){
            state[h->Pname] = 1;//对进程序列中的进程状态进行标记
            current = (PCB*)malloc(sizeof(PCB));
            current->next = NULL;
            current->Pname = h->Pname;
            current->CreateTime = h->CreateTime;
            current->ProcessTime = h->ProcessTime;
            current->Priority = h->Priority;
            if(!ready)
                ready = current;
            else{
                current->next = ready;
                ready = current;
            }
        }
        h = h->next;
    }
    printf("当前就绪进程:");
    h = ready;
    while(h){
        printf("p%d ",h->Pname);
        h = h->next;
    }
}
//进程优先级排序
void Priority_Sort()
{
    int Pname; //进程代号
    int CreateTime; //创建时间
    int ProcessTime; //运行时间
    int Priority; //优先级
    PCB *h = ready;
    PCB *current = NULL;
    while(h){
        current = h->next;
        while(current){
            if(current->Priority > h->Priority){//交换进程信息
                Pname = current->Pname;
                current->Pname = h->Pname;
                h->Pname = Pname;

                CreateTime = current->CreateTime;
                current->CreateTime = h->CreateTime;
                h->CreateTime = CreateTime;

                ProcessTime = current->ProcessTime;
                current->ProcessTime = h->ProcessTime;
                h->ProcessTime = ProcessTime;

                Priority = current->Priority;
                current->Priority = h->Priority;
                h->Priority = Priority;
            }
             current = current->next;
        }
        h = h->next;
    }
    printf("\n当前就绪进程优先级:");
    h = ready;
    while(h){
        printf("p%d ",h->Pname);
        h = h->next;
    }
}
//读取进程信息队列(尾插法)
PCB* Process_Info()
{
    int i;
    PCB *current,*head,*tail;
    tail = head = NULL;
    printf("请输入各进程状态(代号,进程创建时间,进程执行时间,优先级)\n");
    for(i = 0;i < ProcessNum; i++){
        current = (PCB*)malloc(sizeof(PCB));
        scanf("%d",&current->Pname);
        scanf("%d",&current->CreateTime);
        scanf("%d",&current->ProcessTime);
        scanf("%d",&current->Priority);
        if(!head)
            tail = head = current;
        else{
            tail->next = current;
            tail = current;
        }
    }
    tail->next = NULL;
    return head;
}
//打印进程信息
void Print_Info(PCB* head)
{
    PCB *h = head;
    printf("Pname    CreateTime     ProcessTime      Priority\n");
    while(h){
        printf("P%d\t\t",h->Pname);
        printf("%d\t\t",h->CreateTime);
        printf("%d\t\t",h->ProcessTime);
        printf("%d\n",h->Priority);
        h = h->next;
    }
}
void ProcessDispatch(PCB *head)
{
    int finish=0;
    PCB *del= NULL;
    while(1){
        Ready_Queue(head);
        Priority_Sort();
        Time += ready->ProcessTime;
        printf("\n时间Time=%d时,进程P%d完成\n\n",Time,ready->Pname);
        finish += 1;
        //虽然程序很短运行结束就会自动释放,但malloc过的空间记得释放是个好习惯
        del = ready;
        ready = ready->next;
        free(del);

        //判断所有进程是否完成
        if(finish == 5)
            break;
    }
}
int main()
{
    PCB *head=NULL;
    head = Process_Info();
    Print_Info(head);
    ProcessDispatch(head);
    printf("进程调度完毕!");
    return 0;
}

在这里插入图片描述
短进程优先:

#include <stdio.h>
#include <stdlib.h>
//进程结构体
typedef struct pcb
{
    int Pname; //进程代号
    int state; //进程就绪状态1就绪0未就绪
    int CreateTime; //创建时间
    int ProcessTime; //运行时间
    struct pcb *next; //指向下一个进程
}PCB;
int Time = 0; //系统执行时间
PCB *ready = NULL; // 就绪进程队列

//进程就绪队列
void Ready_Queue(PCB *head)
{
    PCB *h = head;
    PCB *current = NULL;
    while(h){
        if(h->CreateTime<=Time && h->state==0){
            h->state = 1;//对进程序列中的进程状态进行标记
            current = (PCB*)malloc(sizeof(PCB));
            current->next = NULL;
            current->Pname = h->Pname;
            current->CreateTime = h->CreateTime;
            current->ProcessTime = h->ProcessTime;
            if(!ready)
                ready = current;
            else{
                current->next = ready;
                ready = current;
            }
        }
        h = h->next;
    }
    printf("当前就绪进程:");
    h = ready;
    while(h){
        printf("p%d ",h->Pname);
        h = h->next;
    }
}
//进程优先级排序
void Priority_Sort()
{
    int Pname; //进程代号
    int CreateTime; //创建时间
    int ProcessTime; //运行时间
    PCB *h = ready;
    PCB *current = NULL;
    while(h){
        current = h->next;
        while(current){
            if(current->ProcessTime < h->ProcessTime){//交换进程信息
                Pname = current->Pname;
                current->Pname = h->Pname;
                h->Pname = Pname;

                CreateTime = current->CreateTime;
                current->CreateTime = h->CreateTime;
                h->CreateTime = CreateTime;

                ProcessTime = current->ProcessTime;
                current->ProcessTime = h->ProcessTime;
                h->ProcessTime = ProcessTime;
            }
             current = current->next;
        }
        h = h->next;
    }
    printf("\n当前就绪进程优先级:");
    h = ready;
    while(h){
        printf("p%d ",h->Pname);
        h = h->next;
    }
}
//打印进程信息
void Print_Info(PCB* head)
{
    PCB *h = head;
    printf("Pname    CreateTime     ProcessTime\n");
    while(h){
        printf("P%d\t\t",h->Pname);
        printf("%d\t\t",h->CreateTime);
        printf("%d\n",h->ProcessTime);
        h = h->next;
    }
}
void ProcessDispatch(PCB *head,int ProcessNum)
{
    int finish=0;
    PCB *del= NULL;
    while(1){
        Ready_Queue(head);
        Priority_Sort();
        Time += ready->ProcessTime;
        printf("\n时间Time=%d时,进程P%d完成\n\n",Time,ready->Pname);
        finish += 1;
        //释放已完成的就绪序列
        del = ready;
        ready = ready->next;
        free(del);

        //判断所有进程是否完成
        if(finish == ProcessNum)
            break;
    }
}
int main()
{
    int i,ProcessNum;
    PCB *head=NULL;
    PCB *current,*tail;
    tail = head = NULL;
    printf("请输入待调度进程数:\n");
    scanf("%d",&ProcessNum);
    printf("请输入各进程状态(代号,进程创建时间,进程执行时间)\n");
    for(i = 0;i < ProcessNum; i++){
        current = (PCB*)malloc(sizeof(PCB));
        scanf("%d",&current->Pname);
        scanf("%d",&current->CreateTime);
        scanf("%d",&current->ProcessTime);
        current->state = 0;
        if(!head)
            tail = head = current;
        else{
            tail->next = current;
            tail = current;
        }
    }
    tail->next = NULL;
    Print_Info(head);
    ProcessDispatch(head,ProcessNum);
    printf("进程调度完毕!");
    return 0;
}

  • 21
    点赞
  • 244
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
抢占优先级是指,当一个高优先级任务就绪时,它可以抢占当前正在执行的低优先级任务。抢占优先级是指,在一个任务执行期间,即使有更高优先级的任务就绪,也不会被抢占。 下面分别给出抢占优先级和抢占优先级的代码示例。 抢占优先级: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 4 void *thread_func(void *arg) { int thread_num = *((int*) arg); printf("Thread %d running\n", thread_num); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int thread_args[NUM_THREADS]; int rc, i; // 设置线程的属性 pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); // 创建线程 for (i = 0; i < NUM_THREADS; i++) { thread_args[i] = i; rc = pthread_create(&threads[i], &attr, thread_func, (void*) &thread_args[i]); if (rc) { printf("ERROR: return code from pthread_create() is %d\n", rc); exit(-1); } } // 等待线程完成 for (i = 0; i < NUM_THREADS; i++) { rc = pthread_join(threads[i], NULL); if (rc) { printf("ERROR: return code from pthread_join() is %d\n", rc); exit(-1); } } // 删除线程的属性 pthread_attr_destroy(&attr); // 结束程序 printf("Main thread exiting\n"); pthread_exit(NULL); } ``` 在这个示例中,我们创建了四个线程,每个线程都打印出自己的编号。由于我们没有显地设置线程的优先级,因此它们的优先级都相同。在这种情况下,如果一个线程正在执行,而另一个线程变为就绪状态,操作系统抢占当前正在运行的线程,以便执行更高优先级的线程。 抢占优先级: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 4 void *thread_func(void *arg) { int thread_num = *((int*) arg); printf("Thread %d running\n", thread_num); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int thread_args[NUM_THREADS]; int rc, i; // 创建线程 for (i = 0; i < NUM_THREADS; i++) { thread_args[i] = i; rc = pthread_create(&threads[i], NULL, thread_func, (void*) &thread_args[i]); if (rc) { printf("ERROR: return code from pthread_create() is %d\n", rc); exit(-1); } } // 等待线程完成 for (i = 0; i < NUM_THREADS; i++) { rc = pthread_join(threads[i], NULL); if (rc) { printf("ERROR: return code from pthread_join() is %d\n", rc); exit(-1); } } // 结束程序 printf("Main thread exiting\n"); pthread_exit(NULL); } ``` 在这个示例中,我们创建了四个线程,每个线程都打印出自己的编号。与抢占优先级的示例不同,我们没有显地设置线程的优先级。在这种情况下,如果一个线程正在执行,而另一个线程变为就绪状态,操作系统不会抢占当前正在运行的线程,而是等待当前线程执行完毕后再执行下一个线程。这就是抢占优先级的特点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值