c - 打印默认线程的优先级
问题描述
我想编写一个打印默认线程优先级的代码,但我不知道这是否可能。到目前为止,我创建了一个具有默认属性的线程,但我没有找到任何允许我存储和打印其默认优先级的语句。
// main.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include "task.h"
int main()
{
pthread_attr_t attr;
struct sched_param prio;
pthread_t tid;
int create = 1;
// default attributes
pthread_attr_init(&attr);
create = pthread_create(&tid, &attr, task, NULL);
if (create != 0) exit(EXIT_FAILURE);
pthread_join(tid, NULL);
return(0);
}
// task.h
#ifndef TASK_H
#define TASK_H
void *task();
#endif
// task.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "task.h"
void *task()
{
printf("I am a simple thread.\n");
pthread_exit(NULL);
}
解决方案
我没有找到任何允许我存储和打印其默认优先级的语句。
它是pthread_attr_getschedparam
并且sched_param
具有调度优先级(至少)。
struct sched_param prio;
pthread_attr_getschedparam(&attr, &prio);
printf("sched_priority = %d\n", prio.sched_priority);
推荐阅读
- python - ASCII 字母拓扑排序
- javascript - 从 NFC 阅读器直接读取到桌面 Web 应用程序?
- java - 如何在 Java 中获取 UTF8 字符集的完整列表
- javascript - 如何使用 Vanilla JavaScript 搜索和替换 HTML 文件中的所有标签?
- javascript - 使用 ReactJS 的启用-禁用按钮
- python-3.x - 无法运行命令 rasa train
- sql - 如何在 Apache Beam 中使用 Calcite SQL 中的 LEAST 函数
- celery - 如何在芹菜中使用flask babel gettext?
- python - 如何从“hh:mm”时间戳列表中获取最大值?
- api - 使用 Python 的区块链 Websocket API 收到错误请求 400