6 #define THREADS 5 7

8 /*

9 * Structure describing each thread.

10 */

11 typedef struct thread_tag {

12  int index;

13  pthread_t id;

14 } thread_t;

15

16 thread_t threads[THREADS];

17 int rr_min_priority; 18

19 /*

20 * Thread start routine that willset its own priority.

21 */

22 void *thread_routine (void *arg)

23 {

24  thread_t *self = (thread_t*)arg;

25  int my_policy;

26  struct sched_param my_param;

27  int status;

28

29  my_param.sched_priority = rr_min_priority + self->index;

30  DPRINTF ((

31  'Thread %d will set SCHED_FIFO, priority %d ',

32  self->index, my_param.sched_priority));

33  status = pthread_setschedparam (

34  self->id, SCHED_RR, &my_param);

35  if (status != 0) 

36  err_abort (status, 'Set sched');

37  status = pthread_getschedparam (

38  self->id, &my_policy, &my_param);

39  if (status != 0)

40  err_abort (status, 'Get sched');

41  printf ('thread_routine %d running at %s/%d ',

42  self->index,

43  (my_policy == SCHED_FIFO ? 'FIFO'

44  : (my_policy == SCHED_RR ? 'RR'

45  : (my_policy == SCHED_OTHER ? 'OTHER' 46 : 'unknown'))),

47  my_param.sched_priority);

48  return NULL;

49 }

50

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

52 {

53  int count, status;

54

55  rr_min_priority = sched_get_priority_min (SCHED_RR);

56  if (rr_min_priority == -1) {

57 #ifdef sun

58  if (errno == ENOSYS) {

59  fprintf (stderr, 'SCHED_RR is not supported. ');

60  exit (0);

61  }

62 #endif

63  errno_abort ('Get SCHED_RR min priority');

64  }

65  for (count = 0; count < THREADS; count++) {

66  threads[count].index = count;

67  status = pthread_create (

68  &threads[count].id, NULL,

69  thread_routine, (void*)&threads[count]);

70  if (status != 0)

71  err_abort (status, 'Create thread');

72  }

73  for (count = 0; count < THREADS; count++) {

74  status = pthread_join (threads[count].id, NULL);

75  if (status != 0)

76  err_abort (status, 'Join thread');

77  }

78  printf ('Main exiting ');

79  return 0;

80 }

5.5.3 Contention scope and allocation domain

int pthread_attr_getscope (

const pthread_attr_t *attr, int *contentionscope);

int pthread_attr_setscope (

pthread_attr_t *attr, int contentionscope);

Besides scheduling policy and parameters, two other controls are important in realtime scheduling. Unless you are writing a realtime application, they probably don't matter. If you are writing a realtime application, you will need to find out which settings of these controls are supported by a system.

The first control is called contention scope. It is a description of how your threads compete for processor resources. System contention scope means that your thread competes for processor resources against threads outside your process. A high-priority system contention scope thread in your process can keep system contention scope threads in other processes from running (or vice versa). Process contention scope means that your threads compete only among themselves. Usually, process contention scope means that the operating system chooses a process to execute, possibly using only the traditional UNIX priority, and some additional scheduler within the process applies the POSIX scheduling rules to determine which thread to execute.

Pthreads provides the thread scope attribute so that you can specify whether each thread you create should have process or system contention scope. A Pthreads system may choose to support PTHREAD_SCOPE_PROCESS, PTHREAD_ SCOPE_SYSTEM, or both. If you try to create a thread with a scope that is not supported by the system, pthread_attr_setscope will return ENOTSUP.

The second control is allocation domain. An allocation domain is the set of processors within the system for which threads may compete. A system may have one or more allocation domains, each containing one or more processors. In a uniprocessor system, an allocation domain will contain only one

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату