LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2013, 08:42 PM   #1
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Rep: Reputation: 0
Sample program that uses SCHED_RR and SCHED_FIFO


Hi,

I'm trying to learn the differences of thread created using SCHED_FIFO and SCHED_RR. But I couldn't write a program that could demonstrate the difference of them. Can someone help?

Thanks
 
Old 03-06-2013, 08:47 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
You need to be root, or have a lax security policy to switch into sched_rr or sched_fifo - otherwise you'll be always running at sched_other.

As far as differences go, there is only one and you probably won't meaningfully have a way of demonstrating it. SCHED_FIFO can not be preempted for time reasons; sched_rr may be preempted by exhaustion of it's time quanta in the current scheduling epoch for a higher priority task. The only way I can think to effectively demonstrate this would make your system unusable once you've executed the program.

In general, if you need something to run without interruption then choose sched_fifo, with the understanding that you MUST YIELD TIME to the system. Otherwise, choose sched_rr, and at least you'll allow higher-priority tasks to preempt you. In either case, you need to have an explicit need for some kind of predictable task execution latency - so you're either writing something that is extremely time critical, or you're just playing around. Be careful - it is very easy to starve your entire system of resources in a tight-loop; if your task is highest priority in that case, you will no longer have a usable machine.
 
Old 03-10-2013, 08:23 PM   #3
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Original Poster
Rep: Reputation: 0
I wrote a program to examine the behavior, I compiled it to create two FIFO threads, and the other to create two RR threads. Both binaries run using root. From the output of the program, data written by the two FIFO threads (with same priority) still overlapped each other. Which didn't show any FIFO behavior.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

int loop_count = 10000;
volatile int index = 0;
char data[20000];

#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void *
thread_start1(void *arg) {
for (; index < loop_count; index++) {
data[index] = '1';
}
}
static void *
thread_start2(void *arg) {
for (; index < loop_count; index++) {
data[index] = '2';
}
}

int main(int argc, char *argv[]) {
pthread_t thr, thr2;
pthread_attr_t attr;
pthread_attr_t *attrp; /* NULL or &attr */
int s;

attrp = NULL;

int stack_size;
void *sp;

attrp = &attr;

s = pthread_attr_init(&attr);
if (s != 0)
handle_error_en(s, "pthread_attr_init");

s = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
if (s != 0)
handle_error_en(s, "pthread_attr_setschedpolicy");

s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (s != 0)
handle_error_en(s, "pthread_attr_setinheritsched");

struct sched_param schedParam;
schedParam.__sched_priority = sched_get_priority_min(SCHED_FIFO);
s = pthread_attr_setschedparam(&attr, &schedParam);
if (s != 0)
handle_error_en(s, "pthread_attr_setschedparam");

s = pthread_create(&thr, attrp, &thread_start1, (void*) "thr1 ");
if (s != 0)
handle_error_en(s, "pthread_create");

s = pthread_create(&thr2, attrp, &thread_start2, (void*) "thr2 ");
if (s != 0)
handle_error_en(s, "pthread_create");

if (attrp != NULL) {
s = pthread_attr_destroy(attrp);
if (s != 0)
handle_error_en(s, "pthread_attr_destroy");
}

pthread_join(thr, NULL);
pthread_join(thr2, NULL);

cout<<index<<endl;
index = 0;
for (; index < loop_count; index++) {
cout << data[index];
}
cout<<endl<<index<<endl;

return 0;
}
 
Old 03-11-2013, 09:13 AM   #4
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
1) Please enclose code in the proper tags.

2) You may have missed this:
"As a consequence, it may preempt the currently running process if it has the same priority."

SCHED_FIFO of the same priority _may_ interrupt each other on the same core, if they have the same priority - it isn't forbidden by the POSIX standard, and the kernel only needs to meet POSIX compliance.

3) Additionally, you don't say whether you are on single core machine, or multi-core. In any event your increment of the index loop is likely unsafe.

4) When you need high-prio scheduling, you generally should look to the rt kernel series. Additionally, you need to update your BIOS settings on intel to remove as many SMIs as you can, and on top of that you will likely need to switch off the HPET since it's counting mechanism uses both SMIs _and_ can miss interrupts

5) Finally, what about "... you probably won't meaningfully have a way of demonstrating it" was difficult to grasp? A naive counting program won't be the way to explain FIFO vs. RR. You're better off just trusting that the kernel devs have gotten this one correct.
 
  


Reply

Tags
realtime, scheduling, threading



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to compile libipq sample program jayasekar Programming 7 03-24-2010 08:31 AM
Run sample java program on jpcap in linux Nadishka Linux - Software 5 10-29-2009 01:37 AM
Compiling 32 bit sample program on 64 bit fedora using -m32 option pankajdev Linux - Newbie 2 10-07-2009 07:42 AM
need a ioctl sample program supernaturalyasir Programming 1 09-09-2009 08:12 AM
regarding a lex sample program... sam_cit Linux - Software 2 05-09-2006 12:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration