Comparing the real-time scheduling policies of the Linux kernel and an RTOS
FIFO scheduling policy of Linux, Thread1 priority is lower Thread2 priority.Because thread1 starts first, it runs for a short time before preemption by thread2 which has higher priority. When thread2 runs, thread1 has no chance to run again.
/*
===================================
Name : Thread_FIFO_2.c
Author : Thang Le
Copyright : www.letrungthang.blogspot.com
Description : FIFO scheduling policy of Linux, Thread1 priority is lower Thread2 priority
==================================
*/
#define _GNU_SOURCE
#include
#include
#include
#include
void *print_message_function(void *ptr);
intcount1 = 0;
intcount2 = 0;
inttimes = 100000; // need set enough big to see effecting
intmain(void) {
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
//Setting CPU affinity
int num_CPUs = 0; // only run on 1 core
cpu_set_t mask;
/* CPU_ZERO initializes all the bits in the mask to zero. */
CPU_ZERO(&mask);
/* CPU_SET sets only the bit corresponding to cpu. */
CPU_SET(num_CPUs, &mask);
/* sched_setaffinity returns 0 in success */
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
{
printf("Could not set CPU Affinity \n");
}
//=======================================
//set attributes
pthread_attr_t attr1, attr2;
struct sched_param parm1, parm2;
pthread_attr_init(&attr1);
pthread_attr_init(&attr2);
//=====================================
/* Create independent threads each of which will execute function */
pthread_attr_getschedparam(&attr1, &parm1);
parm1.sched_priority = sched_get_priority_min(SCHED_FIFO);
pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);
pthread_attr_setschedparam(&attr1, &parm1);
iret1 = pthread_create(&thread1, &attr1, (void*) print_message_function,
(void*) message1);
pthread_setschedparam(thread1, SCHED_FIFO, &parm1);
//====================================
pthread_attr_getschedparam(&attr2, &parm2);
parm2.sched_priority = sched_get_priority_max(SCHED_FIFO);
pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);
pthread_attr_setschedparam(&attr2, &parm2);
iret2 = pthread_create(&thread2, &attr2, (void*) print_message_function,
(void*) message2);
pthread_setschedparam(thread2, SCHED_FIFO, &parm2);
//==================================
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("count1 = %d, count2 = %d\n", count1, count2);
printf("Thread 1 returns: %d\n", iret1);
printf("Thread 2 returns: %d\n", iret2);
exit(0);
//return EXIT_SUCCESS;
}
//======================================
void *print_message_function(void *ptr) {
char *message;
message = (char *) ptr;
while (times > 0) {
int i = 0;
for (i = 0; i < 20000; i++)
i++; // only for delay
if (strcmp(message, "Thread 1") == 0) {
count1 += 1; // count times Thread1 run
} else {
count2 += 1; // count times Thread2 run
}
times--;
}
return (void*) NULL;
}
<code>
<pre>
Result:
count1 = 5444, count2 = 94557
Thread 1 returns: 0
Thread 2 returns: 0
<code>
<pre>


Loading comments... Write a comment