打印默认线程的优先级
Printing the default thread's priority
我想编写一个代码来打印默认线程的优先级,但我不知道这是否可行。到目前为止,我创建了一个具有默认属性的线程,但我没有找到任何允许我存储和打印其默认优先级的语句。
// 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);
}
I didn't find any statement which allows me to store and print its default priority.
它是 pthread_attr_getschedparam
并且 sched_param
具有调度优先级(至少)。
struct sched_param prio;
pthread_attr_getschedparam(&attr, &prio);
printf("sched_priority = %d\n", prio.sched_priority);
我想编写一个代码来打印默认线程的优先级,但我不知道这是否可行。到目前为止,我创建了一个具有默认属性的线程,但我没有找到任何允许我存储和打印其默认优先级的语句。
// 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);
}
I didn't find any statement which allows me to store and print its default priority.
它是 pthread_attr_getschedparam
并且 sched_param
具有调度优先级(至少)。
struct sched_param prio;
pthread_attr_getschedparam(&attr, &prio);
printf("sched_priority = %d\n", prio.sched_priority);