Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should C program do in idle time when running on Linux?

I've written many C programs for microcontrollers but never one that runs on an OS like linux. How does linux decide how much processing time to give my application? Is there something I need to do when I have idle time to tell the OS to go do something else and come back to me later so that other processes can get time to run as well? Or does the OS just do that automatically?

Edit: Adding More Detail My c program has a task scheduler. Some tasks run every 100ms, some run every 50 ms and so on. In my main program loop i call ProcessTasks which checks if any tasks are ready to run, if none are ready it calls an idle function. The idle function does nothing but it's there so that I could toggle a GPIO pin and monitor idle time with an O'scope... or something if I so desired. So maybe I should call sched_yield() in this idle function???

like image 992
PICyourBrain Avatar asked Aug 16 '12 16:08

PICyourBrain


1 Answers

How does linux decide how much processing time to give my application

Each scheduler makes up its own mind. Some reward you for not using up your share, some roll dices trying to predict what you'll do etc. In my opinion you can just consider it magic. After we enter the loop, the scheduler magically decides our time is up etc.

Is there something I need to do when I have idle time to tell the OS to go do something else

You might call sched_yield. I've never called it, nor do I know of any reasons why one would want to. The manual does say it could improve performance though.

Or does the OS just do that automatically

It most certainly does. That's why they call it "preemptive" multitasking.

like image 63
cnicutar Avatar answered Sep 30 '22 05:09

cnicutar