ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.