Linux创建线程,并设置线程的调度策略

74 阅读2分钟

在下面的代码中,我们创建线程,并设置线程的调度策略

// two.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void TestThread1Func() 
{
  sleep(1);
  int i, j;
  int iPolicy;
  struct sched_param sParam;

  pthread_getschedparam(pthread_self(), &iPolicy, &sParam);

  if (iPolicy == SCHED_OTHER)
    printf("SCHED_OTHER\n");
  if (iPolicy == SCHED_FIFO)
    printf("SCHED_FIFO\n");
  if (iPolicy == SCHED_RR)
    printf("SCHED_RR\n");

  for (i = 1; i <= 5; i++) {
    for (j = 1; j < 5000000; j++) {}
    printf("Exec thread func 1");
  }

  printf("Pthread 1 Exit\n");
}

void TestThread2Func() 
{
  sleep(2);
  int i, j;
  int iPolicy;
  struct sched_param sParam;

  pthread_getschedparam(pthread_self(), &iPolicy, &sParam);

  if (iPolicy == SCHED_OTHER)
    printf("SCHED_OTHER\n");
  if (iPolicy == SCHED_FIFO)
    printf("SCHED_FIFO\n");
  if (iPolicy == SCHED_RR)
    printf("SCHED_RR\n");

  for (i = 1; i <= 6; i++) {
    for (j = 1; j < 6000000; j++) {}
    printf("Exec thread func 2");
    
  }

  printf("Pthread 2 Exit\n");
}

void TestThread3Func() 
{
  sleep(3);
  int i, j;
  int iPolicy;
  struct sched_param sParam;

  pthread_getschedparam(pthread_self(), &iPolicy, &sParam);

  if (iPolicy == SCHED_OTHER)
    printf("SCHED_OTHER\n");
  if (iPolicy == SCHED_FIFO)
    printf("SCHED_FIFO\n");
  if (iPolicy == SCHED_RR)
    printf("SCHED_RR\n");
    

  for (i = 1; i <= 7; i++) {
    for (j = 1; j < 7000000; j++) {}
    printf("Exec thread func 3");
  }

  printf("Pthread 3 Exit\n");
}

int main(int argc, char* argv[])
{

  int i = 0;
  i = getuid();  // 获取当前用户id, 如果是0说明是超级管理员
  if (0 == i)
  {
    printf("current user is root\n");
  } else 
  {
    printf("current user is not root\n");
  }


  pthread_t ppid1, ppid2, ppid3;
  struct sched_param sParam;

  pthread_attr_t pAttr1,pAttr2,pAttr3;
  pthread_attr_init(&pAttr1);
  pthread_attr_init(&pAttr2);
  pthread_attr_init(&pAttr3);

  sParam.sched_priority = 31;

  // 修改线程的策略
  pthread_attr_setschedpolicy(&pAttr2, SCHED_RR);
  pthread_attr_setschedparam(&pAttr2, &sParam);
  // 默认情况下线程会继承创建它的线程的调度策略,使用该函数后可以自己指定新线程的调度策略
  pthread_attr_setinheritsched(&pAttr2, PTHREAD_EXPLICIT_SCHED);

  sParam.sched_priority = 11;

  // 修改线程的策略
  pthread_attr_setschedpolicy(&pAttr1, SCHED_RR);
  pthread_attr_setschedparam(&pAttr1, &sParam);
  // 默认情况下线程会继承创建它的线程的调度策略,使用该函数后可以自己指定新线程的调度策略
  pthread_attr_setinheritsched(&pAttr1, PTHREAD_EXPLICIT_SCHED);

  pthread_create(&ppid3,&pAttr3, (void *)TestThread3Func, NULL);
  pthread_create(&ppid2,&pAttr2, (void *)TestThread2Func, NULL);
  pthread_create(&ppid1,&pAttr1, (void *)TestThread1Func, NULL);

  pthread_join(ppid3, NULL);
  pthread_join(ppid2, NULL);
  pthread_join(ppid1, NULL);


  return 0;
}

测试

编译

gcc two.c -o two -lpthread

普通用户执行结果

./two                     
current user is not root
SCHED_OTHER
Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Pthread 3 Exit

超级管理员执行结果

 ./two 
current user is root
SCHED_RR
Exec thread func 1Exec thread func 1Exec thread func 1Exec thread func 1Exec thread func 1Pthread 1 Exit
SCHED_RR
Exec thread func 2Exec thread func 2Exec thread func 2Exec thread func 2Exec thread func 2Exec thread func 2Pthread 2 Exit
SCHED_OTHER
Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Exec thread func 3Pthread 3 Exit