Linux多線程學習(八)pthread_setschedparam

 pthread_setschedparam 設置線程的權限

int pthread_setschedparam(pthread_t target_thread, int policy, const struct sched_param *param)

參數 1. target_thread是使用pthread_create所獲得的線程ID。
  2.線程的調度有三種策略:SCHED_OTHERSCHED_RRSCHED_FIFO。Policy用於指明使用哪種策略。下面我們簡單的說明一下這三種調度策略。
  SCHED_OTHER
  它是默認的線程分時調度策略,所有的線程的優先級別都是0,線程的調度是通過分時來完成的。簡單地說,如果系統使用這種調度策略,程序將無法設置線程的優先級。請注意,這種調度策略也是搶佔式的,當高優先級的線程準備運行的時候,當前線程將被搶佔並進入等待隊列。這種調度策略僅僅決定線程在可運行線程隊列中的具有相同優先級的線程的運行次序。
  SCHED_FIFO
  它是一種實時的先進先出調用策略,且只能在超級用戶下運行。這種調用策略僅僅被使用於優先級大於0的線程。它意味着,使用SCHED_FIFO的可運行線程將一直搶佔使用SCHED_OTHER的運行線程J。此外SCHED_FIFO是一個非分時的簡單調度策略,當一個線程變成可運行狀態,它將被追加到對應優先級隊列的尾部((POSIX 1003.1)。當所有高優先級的線程終止或者阻塞時,它將被運行。對於相同優先級別的線程,按照簡單的先進先運行的規則運行。我們考慮一種很壞的情況,如果有若干相同優先級的線程等待執行,然而最早執行的線程無終止或者阻塞動作,那麼其他線程是無法執行的,除非當前線程調用如pthread_yield之類的函數,所以在使用SCHED_FIFO的時候要小心處理相同級別線程的動作。
  SCHED_RR
  鑑於SCHED_FIFO調度策略的一些缺點,SCHED_RRSCHED_FIFO做出了一些增強功能。從實質上看,它還是SCHED_FIFO調用策略。它使用最大運行時間來限制當前進程的運行,當運行時間大於等於最大運行時間的時候,當前線程將被切換並放置於相同優先級隊列的最後。這樣做的好處是其他具有相同級別的線程能在“自私“線程下執行。
返回值  0表示設置成功 其他表示設置不成功

#define _MULTI_THREADED
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include "check.h"

#define  BUMP_PRIO     1
int      thePriority = 0;

int showSchedParam(pthread_t thread)
{
  struct sched_param   param;
  int                  policy;
  int                  rc;

  printf("Get scheduling parameters\n");
  rc = pthread_getschedparam(thread, &policy, &param);
  checkResults("pthread_getschedparam()\n", rc);

  printf("The thread scheduling parameters indicate:\n"
         "priority = %d\n", param.sched_priority);
  return param.sched_priority;
}

void *threadfunc(void *parm)
{
  int           rc;

  printf("Inside secondary thread\n");
  thePriority = showSchedParam(pthread_self());
  sleep(5);  /* Sleep is not a very robust way to serialize threads */
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  struct sched_param    param;
  int                   policy = SCHED_OTHER;
  int                   theChangedPriority=0;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create thread using default attributes\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  sleep(2);  /* Sleep is not a very robust way to serialize threads */

  memset(&param, 0, sizeof(param));
  /* Bump the priority of the thread a small amount */
  if (thePriority - BUMP_PRIO >= PRIORITY_MIN_NP) {
      param.sched_priority = thePriority - BUMP_PRIO;
  }

  printf("Set scheduling parameters, prio=%d\n",
         param.sched_priority);
  rc = pthread_setschedparam(thread, policy, &param);
  checkResults("pthread_setschedparam()\n", rc);

  /* Let the thread fill in its own last priority */
  theChangedPriority = showSchedParam(thread);
 
  if (thePriority == theChangedPriority ||
      param.sched_priority != theChangedPriority) {
      printf("The thread did not get priority set correctly, "
             "first=%d last=%d expected=%d\n",
             thePriority, theChangedPriority, param.sched_priority);
      exit(1);
  }
  
  sleep(5);  /* Sleep is not a very robust way to serialize threads */
  printf("Main completed\n");
  return 0;
}

check.h  

#ifndef _CHECK_H
      2 #define _CHECK_H
      3 /* headers used by a majority of the example program */
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <unistd.h>
      7 #include <errno.h>
      8
      9 /* Simple function to check the return code and exit the program
     10    if the function call failed
     11    */
     12 static void checkResults(char *string, int rc) {
     13   if (rc) {
     14     printf("Error on : %s, rc=%d\n",
     15            string, rc);
     16     exit(EXIT_FAILURE);
     17   }
     18   return;
     19 }
     20
     21 #endif


編譯  gcc -o pthread_setschedparam -lpthread pthread_setschedparam.c

運行結果  

Enter Testcase - ./pthread_setschedparam
Create thread using default attributes
Inside secondary thread
Get scheduling parameters
The thread scheduling parameters indicate:
priority = 0
Set scheduling parameters, prio=0
Get scheduling parameters
The thread scheduling parameters indicate:
priority = 0
The thread did not get priority set correctly, first=0 last=0 expected=0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章