1

私は2つのスレッドを作成しましたが、作成後にそれらの優先順位を確認したいと思います:-このために、次のコードを記述しました:-

   #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セグメンテーション違反が発生していることに関連するコードを追加した場合にのみ、コードは正常に機能します。私はここで何か間違ったことをしていますか?私もarhumentsched1を交換してみましsched2NULL

4

2 に答える 2

2

sched1ポインタとsched2型を宣言しint*ましたが、それらのポインタを初期化したことはありません。int変数を割り当ててから、それらの変数へのポインターを渡す必要があります。

このような:

int sched1, sched2;
.....
if (pthread_getschedparam(t1, &sched1, ....
于 2012-05-22T13:36:20.067 に答える
2
int * sched1;
pthread_getschedparam(t1、sched1、&t1_param); // sched1は初期化されていないポインタです!

この変更を検討してください。

int sched1;
pthread_getschedparam(t1、&sched1、&t1_param);
于 2012-05-22T13:36:48.533 に答える