Читаем Programming with POSIX® Threads полностью

31  my_param.sched_priority);

32 #else

33  printf ("thread_routine running\n");

34 #endif

35  return NULL;

36 }

37

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

39 {

40  pthread_t thread_id;

41  pthread_attr_t thread_attr;

42  int thread_policy;

43  struct sched_param thread_param;

44  int status, rr_min_priority, rr_max_priority;

45

46  status = pthread_attr_init (&thread_attr);

47  if (status != 0)

4 8  err_abort (status, "Init attr");

49

50 /*

51 * If the priority scheduling option is defined, set various

52 * scheduling parameters. Note that it is particularly important

53 * that you remember to set the inheritsched attribute to

54 * PTHREAD_EXPLICIT_SCHED, or the policy and priority that you've

55 * set will be ignored! The default behavior is to inherit

56 * scheduling information from the creating thread.

57 */

58 #if defined (_POSIX_THREAD_PRIORITY_SCHEDULING) && !defined (sun)

59  status = pthread_attr_getschedpolicy (

60  &thread_attr, &thread_policy);

61  if (status 1= 0)

62  err_abort (status, "Get policy");

63  status = pthread_attr_getschedparam (

64  &thread_attr, &thread_param);

65  if (status != 0)

66  err_abort (status, "Get sched param");

67  printf (

68  "Default policy is %s, priority is %d\n",

69  (thread_policy == SCHED_FIFO ? "FIFO"

70  : (thread_policy == SCHED_RR ? "RR"

71  : (thread_policy == SCHED_OTHER ? "OTHER" 72 : "unknown"))),

73  thread_param.sched_priority);

74

75  status = pthread_attr_setschedpolicy (

76  &thread_attr, SCHED_RR);

77  if (status != 0)

78  printf ("Unable to set SCHED_RR policy.\n");

79  else {

80 /*

81 * Just for the sake of the exercise, we'll use the

82 * middle of the priority range allowed for

83 * SCHED_RR. This should ensure that the thread will be

84 * run, without blocking everything else. Because any

85 * assumptions about how a thread's priority interacts

86 * with other threads (even in other processes) are

87 * nonportable, especially on an implementation that

88 * defaults to System contention scope, you may have to

89 * adjust this code before it will work on some systems.

90 */

91  rr_min_priority = sched_get_priority_min (SCHED_RR);

92  if (rr_min_priority == -1)

93  errno_abort ("Get SCHED_RR min priority");

94  rr_max_priority = sched_get_priority_max (SCHED_RR);

95  if (rr_max_priority == -1)

96  errno_abort ("Get SCHED_RR max priority");

97  thread_param.sched_priority =

98  (rr_min_priority + rr_max_priority)/2;

99  printf (

100  "SCHED_RR priority range is %d to %d: using %d\n",

101  rr_min_priority,

102  rr_max_priority,

Перейти на страницу:

Похожие книги

C++: базовый курс
C++: базовый курс

В этой книге описаны все основные средства языка С++ - от элементарных понятий до супервозможностей. После рассмотрения основ программирования на C++ (переменных, операторов, инструкций управления, функций, классов и объектов) читатель освоит такие более сложные средства языка, как механизм обработки исключительных ситуаций (исключений), шаблоны, пространства имен, динамическая идентификация типов, стандартная библиотека шаблонов (STL), а также познакомится с расширенным набором ключевых слов, используемым в .NET-программировании. Автор справочника - общепризнанный авторитет в области программирования на языках C и C++, Java и C# - включил в текст своей книги и советы программистам, которые позволят повысить эффективность их работы. Книга рассчитана на широкий круг читателей, желающих изучить язык программирования С++.

Герберт Шилдт

Программирование, программы, базы данных