Управление процессами и нитями, страница 3

# gcc getp.c -o getp

# ./getp

Priority of PID 2662453 is equal to 10. Enter new priority:

 23

Now priority of PID 2662453 is equal to 23

Priority of PID 2662453 is equal to 23. Enter new priority:

12

 Now priority of PID 2662453 is equal to 12

Priority of PID 2662453 is equal to 12. Enter new priority:

255

 Now priority of PID 2662453 is equal to 255

Priority of PID 2662453 is equal to 255. Enter new priority:

256

 Now priority of PID 2662453 is equal to 255

Priority of PID 2662453 is equal to 255. Enter new priority:

700

 Now priority of PID 2662453 is equal to 255

Priority of PID 2662453 is equal to 255. Enter new priority:

-23

 Now priority of PID 2662453 is equal to 255

Priority of PID 2662453 is equal to 255. Enter new priority:

0

 Now priority of PID 2662453 is equal to 255

Priority of PID 2662453 is equal to 255. Enter new priority:

1

 Now priority of PID 2662453 is equal to 1

Priority of PID 2662453 is equal to 1. Enter new priority:

Как видно, приоритеты имеют диапазон 1-255. Изначальный приоритет равен 10. При попытке присвоить значение приоритета за пределами диапазона, он остаётся прежним.

Аналогичную программу напишем с использованием функций QNX sched_getparam() и sched_setparam(), оформленных в структуру sched_param:

getp2.c

#include <stdio.h>

#include <sched.h>

#include <stdio.h>

main(){

      int i;

      int pid = getpid();

      struct sched_param str;

      while(1) {

      sched_getparam(pid, &str);  

      printf("Priority of PID %i is equal to %i\nEnter new priority:\n ",pid,str.sched_priority);

      scanf("%i",&i);

      str.sched_priority=i;

      sched_setparam(pid,&str);

      sched_getparam(pid,&str);

      printf("Now priority of PID %i is equal to %i\n",pid,str.sched_priority);

      }

}

После компиляции программы имеем аналогичную картину:

# gcc getp2.c -o getp2

# ./getp2

Priority of PID 491564 is equal to 10

Enter new priority:

 11

Now priority of PID 491564 is equal to 11

# gcc getp2.c -o getp2

# ./getp2

Priority of PID 540716 is equal to 10

Enter new priority:

 23

Now priority of PID 540716 is equal to 23

Priority of PID 540716 is equal to 23

Enter new priority:

4

 Now priority of PID 540716 is equal to 4

Priority of PID 540716 is equal to 4

Enter new priority:

5

 Now priority of PID 540716 is equal to 5

Priority of PID 540716 is equal to 5

Enter new priority:

6

 Now priority of PID 540716 is equal to 6

Priority of PID 540716 is equal to 6

Enter new priority:

67

 Now priority of PID 540716 is equal to 67

Priority of PID 540716 is equal to 67

Enter new priority:

333

 Now priority of PID 540716 is equal to 67

Priority of PID 540716 is equal to 67

Определить минимальное и максимальное значение приоритетов при различных политиках планирования можно  при помощи двух функций: sched_get_priority_min(), sched_get_priority_max(). Приведём пример.

getp3.c

#include <sched.h>

#include <stdio.h>

main(){

      printf("Policies     MinPrio     MaxPrio\n");

      printf("FIFO    %12i%12i\n",sched_get_priority_min(SCHED_FIFO),sched_get_priority_max(SCHED_FIFO));

      printf("RR      %12i%12i\n",sched_get_priority_min(SCHED_RR),sched_get_priority_max(SCHED_RR));

      printf("OTHER   %12i%12i\n",sched_get_priority_min(SCHED_OTHER),sched_get_priority_max(SCHED_OTHER));

      printf("SPORADIC%12i%12i\n",sched_get_priority_min(SCHED_SPORADIC),sched_get_priority_max(SCHED_SPORADIC));

}