SCHED_FIFO线程被Linux中的SCHED_OTHER线程抢占

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

/* Includes */
#include stem Data Types */ 
#include dio.h>      /* Input/Output */
#include cpy(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,(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->message,policy);

int i=0;
/* do the work */
printf("Thread %d says %s %d \n",(int)pthread_self());
for(i=0;i<100;i++)

printf("Thread %d says %d \n",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(),&param);
printf("Thread %d says sched policy %d \n",SCHED_FIFO);

pthread_getschedparam(pthread_self(),policy);

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

我在多次运行中得到了意想不到的结果,我看到SCHED_FIFO被SCHED_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++

相关文章

/etc/sysctl.conf这个目录主要是配置一些系统信息,/etc/sys...
1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起...
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅...
不管是我们在安装软件还是监测软件的使用性能,我们都要随时...
装好Tomcat7后,发现除了本机能访问外界访问不了,岂有此理。...
修改防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,...