linux - SCHED_FIFO 线程在 Linux 中被 SCHED_OTHER 线程抢占

标签 linux pthreads scheduler thread-priority preemption

我已经编写了一个测试程序来测试 SCHED_FIFO。我了解到 SCHED_FIFO 不能被 SCHED_OTHER 线程抢占。但是我无法解释多次运行同一个程序时获得的结果。

/* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <sched.h>

/* prototype for thread routine */
void print_message_function ( void *ptr );
void print_message_function1 ( void *ptr );

/* struct to hold data to be passed to a thread
 * this shows how multiple data items can be passed to a thread
 */
typedef struct str_thdata
{
    int thread_no;
    int thread_value;
    char message[100];
} thdata;

int main()
{
    pthread_t thread1, thread2;  /* thread variables */
    thdata data1, data2;         /* structs to be passed to threads */

    /* initialize data to pass to thread 1 */
    data1.thread_no = 1;
    data1.thread_value = 0;
    strcpy(data1.message, "Hello!");

    /* initialize data to pass to thread 2 */
    data2.thread_no = 2;
    data2.thread_value = 10000;
    strcpy(data2.message, "Hi!");

    /* create threads 1 and 2 */    
    pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1);
    pthread_create (&thread2, NULL, (void *) &print_message_function1, (void *) &data2);

    /* Main block now waits for both threads to terminate, before it exits
     * If main block exits, both threads exit, even if the threads 
     * have not finished their work 
     */ 
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
          
    /* exit */  
    exit(0);
} /* main() */

/** 
* print_message_function is used as the start routine for the threads used
* it accepts a void pointer 
**/
void print_message_function ( void *ptr )
{
    thdata *data;            
    data = (thdata *) ptr;  /* type cast to a pointer to thdata */

    struct sched_param param;
    //int priority=10;
    /* sched_priority will be the priority of the thread */
    //param.sched_priority = priority;
    /* only supported policy, others will result in ENOTSUP */

    int policy = SCHED_OTHER;
    /* scheduling parameters of target thread */
    pthread_setschedparam(pthread_self(), policy, &param);
    printf("Thread %d says sched policy  %d \n", data->thread_no, SCHED_OTHER);
    pthread_getschedparam(pthread_self(),&policy,&param);

    printf("Thread %d says %s  %d \n", data->thread_no, data->message,policy);

    int i=0;
    /* do the work */
    printf("Thread %d says %s %d \n", data->thread_no, data->message,(int)pthread_self());
    for(i=0;i<100;i++) 
        printf("Thread %d says %d \n", data->thread_no,data->thread_value++);
    pthread_exit(0); /* exit */
} /* print_message_function ( void *ptr ) */

void print_message_function1 ( void *ptr )
{
    thdata *data;            
    data = (thdata *) ptr;  /* type cast to a pointer to thdata */

    struct sched_param param;
    int priority=10;
    /* sched_priority will be the priority of the thread */
    param.sched_priority = priority;
    /* only supported policy, others will result in ENOTSUP */

    int policy = SCHED_FIFO;
    /* scheduling parameters of target thread */
    pthread_setschedparam(pthread_self(), policy, &param);
    printf("Thread %d says sched policy %d \n", data->thread_no, SCHED_FIFO);

    pthread_getschedparam(pthread_self(),&policy,&param);

    printf("Thread %d says %s  %d \n", data->thread_no, data->message,policy);

    int i=0;
    /* do the work */
    printf("Thread %d says %s  %d \n", data->thread_no, data->message,(int)pthread_self());
    for(i=0;i<100;i++)
        printf("Thread %d says %d \n", data->thread_no,data->thread_value++);
    pthread_exit(0); /* exit */
} /* print_message_function ( void *ptr ) */

我在多次运行中得到了意想不到的结果,我看到 SCHED_FIFOSCHED_OTHER 线程抢占,即根据程序,线程 2 在 FIFO 模式,而线程 1 是 SCHED_OTHER 模式。我多次看到 thread2 被 thread1 抢占。

有人可以帮我找出问题吗?

最佳答案

您可能使这些 sysctl 设置生效,它们是默认值:

kernel.sched_rt_period_us = 1000000
kernel.sched_rt_runtime_us = 950000

这意味着实时线程只能占用每 1 秒周期的 95%。

另见:Can't provoke Priority Inversion in C++

关于linux - SCHED_FIFO 线程在 Linux 中被 SCHED_OTHER 线程抢占,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10287561/

相关文章:

python - 定期数据获取应使用调度程序阻止其他线程

windows - 任务计划程序如何结束正在运行的作业?

linux - WordPress 网站丢失,CSS 效果不起作用

c - 这些不同的 gcc 链接选项如何改变最终的可执行文件?

c - 修复线程以在某些内核上执行

c - POSIX 线程和公平性(信号量)

linux - 有谁知道 fifo.c linux fifo 调度程序的文件位置?

java - 使用Java程序设置/创建环境变量,该变量在Linux中执行程序后仍然存在

php - 尝试安装 Horde 时出错

c - 在 C 中一次运行恒定数量的线程