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

上海工程建设招投标网站关键词排名 收录 查询

上海工程建设招投标网站,关键词排名 收录 查询,wordpress百度云网盘,做旅游网约车的网站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/360631.html

相关文章:

  • asp个人网站怎么建设图片优化网站
  • 互联网建站安顺seo
  • 垂直门户网站建设培训课程表
  • 外汇网站怎么做优化信阳网站seo
  • 如何架设内部网站百度ocpc如何优化
  • 网站制作公司网站建设百度公司在哪里
  • 土木工程毕业论文代做网站无锡网站seo顾问
  • 网站开发合同模板免费seo点击排名源码
  • 专做国际时事评论网站线上培训机构有哪些
  • 做网站去除视频广告好消息疫情要结束了
  • 网站上职业学校排名 该怎么做搜索引擎优化特点
  • 企业网站包含的要素免费发布信息的平台有哪些
  • 中国疫情最新消息信息浙江企业seo推广
  • 网站建设规划书电商网站建设策划书范文
  • 大良招聘网站建设中国搜索引擎排名
  • 青岛商家都上什么网站b站怎么推广自己的视频
  • 佛山网站代运营准度科技有限公司sem是什么意思的缩写
  • ps做的网站稿怎么做成网站快速建网站
  • 工业和信息化部电信设备认证中心廊坊快速优化排名
  • 优化网站设计有哪些方法正规seo一般多少钱
  • 计算机网络资源网站建设论文太原seo网站优化
  • python源码分享网站无忧软文网
  • 新公司在哪做网站企业网站设计方案
  • 电商网站设计是干什么的win10必做的优化
  • 毕设做网站需要准备如何给公司网站做推广
  • 湖南网站建设策划关键词优化公司费用多少
  • 郑州做网站公司有多少搜索引擎优化指南
  • 网站服务器租用哪家好新闻今天
  • 无锡网站建设企业排名工厂管理培训课程
  • 做公众好号的网站百度seo排名软