我想玩POSIX的優先權。我的目標是有以下程序。兩個線程正在運行Thread0,優先級爲10,Thread1的優先級爲50. Thread1在有限循環(如3秒)中阻塞,並且在此時間間隔內,Thread0嘗試執行自己。結果應該是Thread0將被阻塞,因爲具有更高優先級的線程正在執行。線程優先與posix
我的結果是,優先級不會改變線程的行爲...我使用以下命令編譯gcc -Wall -o scheduling scheduling3.c -pthread 並在ubuntu上使用sudo su命令。 結果:
Prio min = 1, Prio max = 99
SCHED_FIFO
Priority of the thread 0 : 10
SCHED_FIFO
Priority of the thread 1 : 50
Value of test_thread_0 1
Value of test_thread_1 1
結果,我想:
Prio min = 1, Prio max = 99
SCHED_FIFO
Priority of the thread 0 : 10
SCHED_FIFO
Priority of the thread 1 : 50
Value of test_thread_0 1
Value of test_thread_1 1
計劃:
// The thread with high priority wil block the thread with low priority
// Run with super user privilege (sudo su with ubuntu)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#define NUM_THREADS 2
int test_thread_0 = 0;
int test_thread_1 = 0;
void *BlockThread0(void *threadid) {
test_thread_0 = 1;
}
void *BlockThread1(void *threadid) {
struct timeval start, end;
long secs_used;
gettimeofday(&start, NULL);
test_thread_1 = 1;
while(1) {
gettimeofday(&end, NULL);
secs_used=(end.tv_sec - start.tv_sec); //avoid overflow by subtracting first
if(secs_used > 3)
break;
}
}
int main(int argc, char *argv[]) {
int i, policy;
pthread_t tid[NUM_THREADS];
pthread_attr_t attr[NUM_THREADS];
struct sched_param param[NUM_THREADS], test;
int prio_max, prio_min;
// Get the range of the policy
prio_max = sched_get_priority_max(SCHED_FIFO);
prio_min = sched_get_priority_min(SCHED_FIFO);
printf("Prio min = %d, Prio max = %d \n", prio_min, prio_max);
// Set the different priority
param[0].sched_priority = 10;
param[1].sched_priority = 50;
// Set all the attribute (policy + priority)
for(i = 0; i < NUM_THREADS; i++) {
pthread_attr_init(&attr[i]);
if(pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED) != 0)
fprintf(stderr, "Unable to set EXPLICIT SCHEDULER.\n");
pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_JOINABLE);
/* The attribute get the new policy */
if(pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO) != 0)
fprintf(stderr, "Unable to set policy.\n");
/* Test to change the priority of each task */
if(pthread_attr_setschedparam(&attr[i], ¶m[i]) != 0)
fprintf(stderr, "Unable to set priority.\n");
}
// Get all the attribute (policy + priority)
for(i = 0; i < NUM_THREADS; i++) {
// Get the policy
if(pthread_attr_getschedpolicy(&attr[i], &policy) != 0)
fprintf(stderr, "Unable to get policy.\n");
else{
if(policy == SCHED_OTHER)
printf("SCHED_OTHER\n");
else if(policy == SCHED_RR)
printf("SCHED_RR\n");
else if(policy == SCHED_FIFO)
printf("SCHED_FIFO\n");
}
/* Get the priority */
pthread_attr_getschedparam(&attr[i], &test);
printf("Priority of the thread %d : %d \n",i,test.sched_priority);
}
// Thread1 with the most important priority is executing
pthread_create(&tid[1], &attr[1], BlockThread1, (void *)1);
// To be sure that the thread1 is running
sleep(1);
//Thread2 with lower priority attempt to execute himself but he is blocked because thread1 is executing
pthread_create(&tid[0], &attr[0], BlockThread0, (void *)0);
/* now join on each thread */
for(i = 0; i < NUM_THREADS; i++)
pthread_join(tid[i], NULL);
printf("Value of test_thread_0 %d \n",test_thread_0);
printf("Value of test_thread_1 %d \n",test_thread_1);
}
什麼是操作系統?根據我在非實時操作系統上的經驗,設置諸如線程和/或進程優先級之類的東西幾乎沒有影響,它們具有的影響是非確定性的。如果您需要一個更頻繁運行的進程或按特定順序發生的事情,則需要顯式使用同步對象,例如互斥鎖,條件變量和信號量,並自行對其進行編碼。如果您需要確定性的優先級和/或調度,則您需要使用正確的工具來完成這項工作,而通用操作系統不是該工具。 –