LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Fairness: Where can it be better handled? (http://www.linuxquestions.org/questions/programming-9/fairness-where-can-it-be-better-handled-807549/)

sinu_nayak2001 05-13-2010 04:30 AM

Fairness: Where can it be better handled?
 
Hi,

I would like to share one of my practical experience with multiprogramming here.

Yesterday I had written a multiprogram. Modifications to sharable resources were put under critical sections protected by P(mutex) and V(mutex) and those critical section code were put in a common library. The library will be used by concurrent applications (of my own).

I had three applications that will use the common code from library and do their stuff independently.

my library
---------
work_on_shared_resource
{
P(mutex)
get_shared_resource
work_with_it
V(mutex)
}

my application
-----------
application1
{
*[
work_on_shared_resource
do_something_else_non_ctitical
]
}


application2
{
*[
work_on_shared_resource
do_something_else_non_ctitical
]
}


application3
{
*[
work_on_shared_resource
]
}

*[...] denote a loop.
------------


I had to run the applications on Linux OS. I had a thought in my mind, hanging over years, that, OS shall schedule all the processes running under him with all fairness. In other words, it will give all the processes, their pie of resource-usage equally well.

When first two applications were put to work, they run perfectly well without deadlock. But when the third application started running, always the third one got the resources, but since it is not doing anything in its non-critical region, it gets the shared resource more often when other tasks are doing something else. So the other two applications were found almost totally halted. When the third application got terminated forcefully, the previous two applications resumed their work as before.

I think, this is a case of starvation, first two applications had to starve.

Now how can we ensure fairness?

Now I started believing that OS scheduler is innocent and blind. It depends upon who won the race; he got the largest pie of CPU and resource.

Shall we attempt to ensure fairness of resource users in the critical-section code in library?

Or shall we leave it up to the applications to ensure fairness by being liberal, not greedy?

To my knowledge, adding code to ensure fairness to the common library shall be an overwhelming task. On the other hand, believing on the applications will also never ensure 100% fairness. The application which does a very little task after working with shared resources shall win the race where as the application which does heavy processing after their work with shared resources shall always starve.

What is the best practice in this case? Where we ensure fairness and how?

Sincerely,
Srinivas Nayak

pixellany 05-13-2010 11:56 PM

Moved to programming....

Note: Just skimming, it is not obvious to me what you mean by "fairness". Maybe something is getting lost in translation?

Sergei Steshenko 05-14-2010 12:30 AM

Quote:

Originally Posted by sinu_nayak2001 (Post 3966767)
...
Now I started believing that OS scheduler is innocent and blind. It depends upon who won the race; he got the largest pie of CPU and resource.
...

Suppose you have an application with the following piece of code:

Code:

...
// silly delay loop, _never_ use in multi tasking OS !

unsigned ctr;

for(ctr = 1000000; ctr != 0; ctr--)
  {
  // do nothing
  }
...

From your point of view the application is doing nothing useful, from the scheduler point of view the application is getting its fair share of resources - the scheduler knows nothing that the loop is useless.

Since the above mentioned resources are given to an application doing from your point of view nothing, the application that from your point of view are doing something useful are short of the needed resources.

So, maybe you have a situation like this ?

posixculprit 05-14-2010 01:54 AM

I would not modify the library code. work_on_shared_resource should do exactly what its name indicates, and do so without forcing any "fairness" algorithm on applications which use it. I'd either force application #3 to sleep after the call to work_on_shared_resource has returned (the programmer solution) or give applications #1 and #2 higher priorities than application #3, via the nice utility or similar (the system administrator solution).


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