2017-04-11 118 views
1

我想玩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], &param[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); 

} 
+2

什么是操作系统?根据我在非实时操作系统上的经验,设置诸如线程和/或进程优先级之类的东西几乎没有影响,它们具有的影响是非确定性的。如果您需要一个更频繁运行的进程或按特定顺序发生的事情,则需要显式使用同步对象,例如互斥锁,条件变量和信号量,并自行对其进行编码。如果您需要确定性的优先级和/或调度,则您需要使用正确的工具来完成这项工作,而通用操作系统不是该工具。 –

回答

1

这个循环被称为 “繁忙的循环”,它并没有给操作系统”调度器方式来安排其他线程。你应该使用sleep()的一些变体。

0

不,不是今天几乎所有的硬件常用“的结果应该在Thread0会因为更高优先级的线程执行被阻塞”。

线程只会保持就绪,等待执行,如果有更多的就绪/运行线程比可用于运行它们的核心多。

有了两个线程,你应该注意到你在一个只有一个内核的处理器上期待的东西。

现在它们非常罕见。