Increasing thread priority in Android native

2,992 views
Skip to first unread message

code vthme

unread,
Jun 13, 2011, 5:42:52 AM6/13/11
to andro...@googlegroups.com

Hi all,

 

I have tried to increase the thread priority in android native through “pthread_setschedparam ()” but it is returning error 22(EINVAL).  Please find below the code I have used

 

struct sched_param   param;

int policy;

CHECK_EQ(pthread_getschedparam(pthread_self(), &policy, &param),0);

 

param.sched_priority = -10;

CHECK_EQ(pthread_setschedparam(pthread_self(), policy, &param),0);//giving error

 

Can anyone please share the issue in above code or let me know the any other procedure to increase the thread priority?

 

 

 

 

Thanks and regards

Mahesh

Tim Mensch

unread,
Jun 21, 2011, 5:03:28 PM6/21/11
to andro...@googlegroups.com
On 6/13/2011 3:42 AM, code vthme wrote:
I have tried to increase the thread priority in android native through “pthread_setschedparam ()” but it is returning error 22(EINVAL).  Please find below the code I have used

If I remember correctly, it's possible under Linux to REDUCE your priority in a user account, but you need to be root to increase your priority.

Tim

fadden

unread,
Jun 21, 2011, 5:30:50 PM6/21/11
to android-ndk
On Jun 21, 2:03 pm, Tim Mensch <tim.men...@gmail.com> wrote:
> > I have tried to increase the thread priority in android native through
> > “*/pthread_setschedparam ()/*” but it is returning error 22(EINVAL).
>
> If I remember correctly, it's possible under Linux to REDUCE your
> priority in a user account, but you need to be root to increase your
> priority.

If that were the problem, the result should be EPERM. EINVAL
indicates an invalid parameter.

setpriority() takes a "nice" value, where zero is baseline and
negative numbers indicate higher priority. pthread_setschedparam
takes a scheduler priority value, which are positive integers where
higher numbers indicate higher priority. The min and max values can
be determined from sched_get_priority_max() and _min().

If you make that change, then you should start getting the expected
EPERM result, unless the thread has elevated privileges that allow it
to raise its priority.

Bill Gatliff

unread,
Jun 21, 2011, 6:38:00 PM6/21/11
to andro...@googlegroups.com
Guys:


On Tue, Jun 21, 2011 at 4:30 PM, fadden <fad...@android.com> wrote:
> If you make that change, then you should start getting the expected
> EPERM result, unless the thread has elevated privileges that allow it
> to raise its priority.

How would the thread ever get those elevated privileges under Android?

Assuming that the OP's thread is running as native code under an
Activity, I mean.


b.g.
--
Bill Gatliff
bg...@billgatliff.com

Zoran Angelov

unread,
Jun 22, 2011, 2:32:16 AM6/22/11
to andro...@googlegroups.com
Hi,
Have you tried with java.lang.Thread.setPriority()?
Note that you need to pre-cache the class and methodId of java.lang.Thread.setPriority() and attach/detach the native thread in order the call to setPriority to succeed.





--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


David Turner

unread,
Jun 22, 2011, 4:34:27 AM6/22/11
to andro...@googlegroups.com
On Wed, Jun 22, 2011 at 12:38 AM, Bill Gatliff <bg...@billgatliff.com> wrote:
Guys:


On Tue, Jun 21, 2011 at 4:30 PM, fadden <fad...@android.com> wrote:
> If you make that change, then you should start getting the expected
> EPERM result, unless the thread has elevated privileges that allow it
> to raise its priority.

How would the thread ever get those elevated privileges under Android?

It can't
 
Assuming that the OP's thread is running as native code under an
Activity, I mean.


b.g.
--
Bill Gatliff
bg...@billgatliff.com

Bill Gatliff

unread,
Jun 22, 2011, 10:04:52 AM6/22/11
to andro...@googlegroups.com
Guys:

On Wed, Jun 22, 2011 at 3:34 AM, David Turner <di...@android.com> wrote:
>> How would the thread ever get those elevated privileges under Android?
>>
> It can't

That was my understanding. Just making sure. :)

Reply all
Reply to author
Forward
0 new messages