尝试显示线程优先级时出现分段错误
segmentation fault while trying to display priority of my thread
>我已经创建了两个线程,我想在创建它们后查看它们的优先级:-为此,我编写了以下代码:-
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include <errno.h>
int main()
{ //something
struct sched_param main_param, t1_param, t2_param;
pthread_t t1, t2;
int *sched1, *sched2;
if (pthread_create(&t1, NULL, fn1, NULL) != 0) // inside fn1 thread sleeps in loop
{
std::cout << "couldn't create t1" << std::endl;
return -1;
}
if (pthread_create(&t2, NULL, fn2, NULL) != 0) //inside fn2 thread sleeps in loop
{
std::cout << "couldn't create t2" << std::endl;
return -1;
}
if (pthread_getschedparam(t1, sched1, &t1_param) != 0)
{
std::cout << "error setting priority for T1: (" << errno << "), " <<
strerror(errno) << std::endl;
}
std::cout << "t1 thread will have a prio of " << t1_param.sched_priority << std::endl;
if (pthread_getschedparam(t1, sched2, &t1_param) != 0)
{
std::cout << "error setting priority for T1: (" << errno << "), " <<
strerror(errno) << std::endl;
}
std::cout << "t2 thread will have a prio of " << t2_param.sched_priority <<std::endl;
// something
}
只有当我添加与pthread_getschedparam
相关的代码时,我才得到分段错误,否则代码工作正常.我在这里做错了什么吗?我什至尝试用NULL
替换sched1
和sched2
您声明了指针sched1
和sched2
类型为 int*
,但从未初始化过这些指针。您需要分配int
变量,然后将指针传递给这些变量。
喜欢这个:
int sched1, sched2;
.....
if (pthread_getschedparam(t1, &sched1, ....
int *sched1;pthread_getschedparam(T1、Sched1 和 t1_param(;sched1 是未初始化的指针!
请考虑此更改:
国际调度1;pthread_getschedparam(T1, 和 Sched1, &t1_param(;
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?