Я наткнулся на проблему с приоритетами тредов. Возьмем такой код:
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sched.h>
unsigned int countA;
unsigned int countB;
const unsigned int MAX = 2000000000;
void *startA(void *a) {
sleep(1);
for(countA = 0; countA < MAX; countA++) {
if(countA % 1024 == 0)
printf("Thread A is working, countA = %d\n", countA);
}
printf("Thread A has ended: countA = %d, countB = %d\n", countA, countB);
exit(0);
}
void *startB(void *a) {
sleep(1);
for(countB = 0; countB < MAX; countB++) {
if(countB % 1024 == 0)
printf("Thread B is working, countB = %d\n", countB);
}
printf("Thread B has ended: countA = %d, countB = %d\n", countA, countB);
exit(0);
}
void check(int r) {
printf("res = %d\n", r);
if(r) {
printf("error = %s\n", strerror(r));
exit(0);
}
}
int main() {
struct sigaction act;
pthread_t tA, tB;
pthread_attr_t tA_attr, tB_attr;
struct sched_param sch;
int res;
int MY_SCHED_POLICY = SCHED_FIFO;
if(geteuid() != 0) {
fprintf(stderr, "root expected\n");
return -1;
}
MY_SCHED_POLICY = SCHED_FIFO;
/// tA
check(pthread_attr_init(&tA_attr));
check(pthread_attr_setschedpolicy(&tA_attr, MY_SCHED_POLICY));
/// tB
check(pthread_attr_init(&tB_attr));
check(pthread_attr_setschedpolicy(&tB_attr, MY_SCHED_POLICY));
/// tA
check(pthread_create(&tA, &tA_attr, &startA, NULL));
sch.sched_priority = 55;
check(pthread_setschedparam(tA, MY_SCHED_POLICY, &sch));
/// tB
check(pthread_create(&tB, &tB_attr, &startB, NULL));
sch.sched_priority = 54;
check(pthread_setschedparam(tB, MY_SCHED_POLICY, &sch));
//
sleep(10000000);
printf("Thread C is exiting: countA = %d, countB = %d\n", countA, countB);
return 0;
}
Тред с меньшим приоритетом вообще не увеличивает свой count. Почему?
Я читал man sched_setscheduler, там предостерегают использовать
приотритеты процессов, но подобная ситуация не описывается...
Подскажите, где я неправ.
Да, у меня Red Hat 8, kernel 2.6.20-19.8, LinuxThreads.
Заранее спасибо.
Ответ на:
комментарий
от AVI
Ответ на:
комментарий
от jek_
Ответ на:
комментарий
от AVI
Ответ на:
комментарий
от jek_
Ответ на:
комментарий
от AVI
Ответ на:
комментарий
от jek_
Ответ на:
комментарий
от AVI
Ответ на:
комментарий
от AVI
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.
Похожие темы
- Форум Завершение потока: остается занятая память. (2009)
- Форум Помогите разобраться с потоками (2003)
- Форум pthreads - ошибка линковки (2001)
- Форум Потоки (2017)
- Форум pthread (2008)
- Форум [Потоки] [pthread_create] Минимальный пример. Текут дескрипторы и память. (2011)
- Форум Странное поведение thread (2003)
- Форум Переход от LinuxThreads к NTPL (2004)
- Форум Горю (2020)
- Форум pthread_cancel неработает, или я (2003)