当前位置: 首页 > wzjs >正文

深圳市建设监理协会网站上海关键词自动排名

深圳市建设监理协会网站,上海关键词自动排名,宣传片拍摄方案策划书,哈尔滨模板建站定制网站FreeRTOS 操作系统支持三种调度方式: 抢占式调度,时间片调度和合作式调度。 实际应用主要是抢占式调度和时间片调度,合作式调度用到的很少. 1,抢占式调度 每个任务都有不同的优先级, 任务会一直运行直到被高优先级任务抢占或者遇到…

FreeRTOS 操作系统支持三种调度方式: 抢占式调度,时间片调度和合作式调度。 实际应用主要是抢占式调度和时间片调度,合作式调度用到的很少.

1,抢占式调度
每个任务都有不同的优先级, 任务会一直运行直到被高优先级任务抢占或者遇到阻塞式的 API 函数,比如 vTaskDelay(延迟,事件标志等待,信号量等待)才会切换到其他任务。抢占式调度要掌握的最关键一点是: 每个任务都被分配了不同的优先级, 抢占式调度器会获得就绪列表中优先级最高的任务,并运行这个任务

FreeRTOS 操作系统是设置的数值越小任务优先级越低, 数值越大任务优先级越高,由于任务2的优先级高于任务1,因此任务2将首先运行。

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"#define TASK1_PRIORITY   (tskIDLE_PRIORITY + 1)
#define TASK2_PRIORITY   (tskIDLE_PRIORITY + 2)// 任务函数原型
void vTask1(void *pvParameters);
void vTask2(void *pvParameters);// 任务1函数
void vTask1(void *pvParameters) {const char *pcTaskName = (const char *)pvParameters;for (;;) {// 打印任务名printf("%s is running\n", pcTaskName);// 延时一段时间,模拟任务的工作负载vTaskDelay(pdMS_TO_TICKS(1000));}
}// 任务2函数
void vTask2(void *pvParameters) {const char *pcTaskName = (const char *)pvParameters;for (;;) {// 打印任务名printf("%s is running\n", pcTaskName);// 延时一段时间,模拟任务的工作负载vTaskDelay(pdMS_TO_TICKS(500));}
}int main(void) {// 创建任务xTaskCreate(vTask1, "Task 1", configMINIMAL_STACK_SIZE, "Task 1", TASK1_PRIORITY, NULL);xTaskCreate(vTask2, "Task 2", configMINIMAL_STACK_SIZE, "Task 2", TASK2_PRIORITY, NULL);// 启动调度器vTaskStartScheduler();// 如果调度器启动失败,将不会到达这里for (;;);
}

Task 2 is running
Task 1 is running
Task 2 is running
Task 1 is running
Task 2 is running
Task 1 is running
Task 2 is running
Task 1 is running
Task 2 is running
Task 1 is running
Task 2 is running

 抢占式调度算法实现。

#include <stdio.h>
#include <stdbool.h>#define NUM_TASKS 3typedef struct {int priority;bool isRunning;
} Task;void initTask(Task *task, int priority) {task->priority = priority;task->isRunning = false;
}void runTask(Task *task) {if (task->isRunning) {printf("Task with priority %d is running.\n", task->priority);}
}void scheduleTasks(Task *tasks, int numTasks) {int highestPriority = -1;int highestPriorityTaskIndex = -1;// Find the highest priority task that is ready to runfor (int i = 0; i < numTasks; ++i) {if (tasks[i].priority > highestPriority && !tasks[i].isRunning) {highestPriority = tasks[i].priority;highestPriorityTaskIndex = i;}}// Preempt lower priority tasksfor (int i = 0; i < numTasks; ++i) {tasks[i].isRunning = false;}// Run the highest priority taskif (highestPriorityTaskIndex != -1) {tasks[highestPriorityTaskIndex].isRunning = true;runTask(&tasks[highestPriorityTaskIndex]);}
}int main() {Task tasks[NUM_TASKS];initTask(&tasks[0], 1);initTask(&tasks[1], 2);initTask(&tasks[2], 3);// Initially, the highest priority task is runningtasks[2].isRunning = true;// Simulate schedulingprintf("Initial scheduling:\n");scheduleTasks(tasks, NUM_TASKS);// Now, a higher priority task becomes readyprintf("\nAfter a higher priority task becomes ready:\n");tasks[1].priority = 4; // Increase the priority of task 1scheduleTasks(tasks, NUM_TASKS);return 0;
}

 2, 时间片调度
每个任务都有相同的优先级, 任务会运行固定的时间片个数或者遇到阻塞式的 API 函数,比如
vTaskDelay, 才会执行同优先级任务之间的任务切换。

在 FreeRTOS 操作系统中只有同优先级任务才会使用时间片调度, 另外还需要用户在
FreeRTOSConfig.h 文件中使能宏定义:#define configUSE_TIME_SLICING 1

每个任务分配的时间片大小是 5 个系统时钟节拍。而在时间片轮询的过程中如果调用了阻塞式 API 函数, 调用函数时, 虽然 5 个系统时钟节拍的时间片大小还没有用完, 此时依然会通过时间片调度切换到下一个任务。

FreeRTOSConfig中:
configUSE_PREEMPTION=0关闭抢占,只有阻塞/挂起/运行的任务调用 taskYIELD()/ISR才会切换下文的任务

configUSE_TIME_SLICING=0关闭时间片,相同优先级的任务不会在tick间隔后切换。

#include "FreeRTOS.h"
#include "task.h"
#include<stdio.h>
#include "timers.h"void vApplicationMallocFailedHook() {while(1);
}void vApplicationStackOverflowHook(TaskHandle_t xTask, char * pcTaskName ) {while(1);
}#define TASK_PRIORITY 1
#define TASK_STACK_SIZE 256
#define TIME_SLICE_MS 1000// 任务句柄
TaskHandle_t taskHandles[2] = {NULL, NULL};// 时间片定时器回调函数
void vTimerCallback(TimerHandle_t xTimer) {// 交换两个任务的优先级,以实现时间片调度vTaskPrioritySet(taskHandles[0], TASK_PRIORITY + 1);vTaskPrioritySet(taskHandles[1], TASK_PRIORITY);
}// 任务函数
void vTaskFunction(void *pvParameters) {const char *pcTaskName = (const char *)pvParameters;UBaseType_t uxPriority;// 获取当前任务的优先级uxPriority = uxTaskPriorityGet(NULL);for (;;) {// 执行任务的工作printf("%s is running\n", pcTaskName);// 延时,模拟任务工作负载vTaskDelay(pdMS_TO_TICKS(TIME_SLICE_MS / 2));}
}int main(void) {// 创建两个任务xTaskCreate(vTaskFunction, "Task 1", TASK_STACK_SIZE, "Task 1", TASK_PRIORITY, &taskHandles[0]);xTaskCreate(vTaskFunction, "Task 2", TASK_STACK_SIZE, "Task 2", TASK_PRIORITY, &taskHandles[1]);// 创建一个定时器,用于周期性地切换任务优先级TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(TIME_SLICE_MS), pdTRUE, (void *)0, vTimerCallback);// 启动定时器xTimerStart(xTimer, 0);// 启动调度器vTaskStartScheduler();// 如果调度器启动失败,将不会到达这里for (;;);
}

Task 1 is running
Task 2 is running
Task 1 is running
Task 2 is running
Task 1 is running
Task 2 is running

时间片c语言实现算法:

#include <stdio.h>
#include <stdbool.h>#define NUM_TASKS 2
#define TIME_SLICE 5typedef struct {int priority;int timeSliceCount;bool isRunning;
} Task;void initTask(Task *task, int priority) {task->priority = priority;task->timeSliceCount = 0;task->isRunning = false;
}void runTask(Task *task) {if (task->isRunning) {printf("Task with priority %d is running for time slice %d.\n", task->priority, task->timeSliceCount);}
}void scheduleTasks(Task *tasks, int numTasks) {static int currentTaskIndex = 0;// Stop the current tasktasks[currentTaskIndex].isRunning = false;tasks[currentTaskIndex].timeSliceCount = 0;// Move to the next taskcurrentTaskIndex = (currentTaskIndex + 1) % numTasks;// Start the next tasktasks[currentTaskIndex].isRunning = true;tasks[currentTaskIndex].timeSliceCount++;// Run the next taskrunTask(&tasks[currentTaskIndex]);
}int main() {Task tasks[NUM_TASKS];initTask(&tasks[0], 1);initTask(&tasks[1], 2);// Simulate round-robin schedulingprintf("Round-robin scheduling:\n");for (int i = 0; i < TIME_SLICE * NUM_TASKS; ++i) {scheduleTasks(tasks, NUM_TASKS);}return 0;
}

 

http://www.dtcms.com/wzjs/450138.html

相关文章:

  • 武汉网站开发公司哪家好合肥seo网站建设
  • 网页设计作业网站网站友情链接代码
  • 企业网站开发哪家专业哔哩哔哩推广网站
  • 网站访问者qqseo外包公司排名
  • 哪个网站可以免费做H5抖音seo怎么做
  • 做销售平台哪个网站好广点通广告平台
  • 网站做的很差的案例seo公司重庆
  • 广州旅游网站建设设计seo推广服务
  • 帮别人做网站违法吗奉节县关键词seo排名优化
  • 可信网站认证办理厦门seo计费
  • 关键词整站排名优化抖音推广引流
  • php网站开发推荐书籍网页设计与制作期末作品
  • 阿里巴巴网站谁做的今日竞彩足球最新比赛结果查询
  • 南京做网站的公司企业管理
  • 做化妆品销售网站如何百度关键词排名爬虫
  • 网站开发应如何入账泉州百度首页优化
  • 商城网站 前置审批网络销售网站
  • 福安网站定制世界杯数据分析
  • 广告设计好学吗seo权威入门教程
  • 佛山外贸企业网站建设跨境电商seo什么意思
  • 高新建设网站电话靠谱的免费建站
  • 国家鼓励做网站的行业智能建站
  • 网站建设中页面模板安卓手机优化软件排名
  • 做网站好的网站建设公司哪家好核心关键词是什么意思
  • 一元夺宝网站建设2017足球比赛直播2021欧冠决赛
  • 商标局网站可以做变更吗南宁网络推广有限公司
  • 怀化网站建设企业潍坊做网站哪家好
  • 手机做任务赚钱的网站百度推广费用
  • 做投票链接网站中国站长工具
  • 做网站用什么颜色好搜索引擎营销有哪些方式