How to set a priority to a native thread

2,375 views
Skip to first unread message

一郎 鈴木

unread,
Jun 2, 2010, 11:28:39 AM6/2/10
to android-ndk
Please show me how to set a priority to a native thread.

I try to set a priority to a native thread which is created in a JNI
method by calling pthread_create.

As the default schedule policy is SCHED_OTHER or SCHED_NORMAL, I can
only set zero priority to the thread.

Although I change the schedule policy to SCHED_FIFO or SCHED_RR and
the priority to "99" by calling pthread_setschedparam,
pthread_setschedparam returns 1(EPERM).

The JNI method is called from a Activity.
I suppose that I can change priority the thread as the process does
not have a a root authority.

Is there any other way to set apriority to a native thread?

Test code is as follows;
I modify hello-jni.c.
--------------------------------------------------------------------
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env, jobject
thiz){

pthread_t tid;
pthread_attr_t tattr;
struct sched_param param;

pthread_attr_int(&tattr);

pthread_attr_getschedparam(&tattr, &param);
pthread_attr_setschedparam(&tattr, &param);

pthread_create(&tid, &tattr, threadrun, NULL);

param.schedule_priority = 99;
int ret1 = pthread_setschedparam(tid, SCHED_FIFO, &param);

param.schedule_priority = 1;
int ret2 = pthread_setschedparam(tid, SCHED_RR, &param);

param.schedule_priority = 0;
int ret3 = pthread_setschedparam(tid, SCHED_RR, &param);

--------------------------------------------------------------------
[Result]
ret1:1(EPERM)
ret2:1(EPERM)
ret3:0

David Turner

unread,
Jun 2, 2010, 11:59:27 PM6/2/10
to andro...@googlegroups.com
As far as I understand, you need to be root to change to SCHED_FIFO or SCHED_RR,
so you can't use these in an application.

2010/6/2 一郎 鈴木 <ichi...@gmail.com>

--
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.


一郎 鈴木

unread,
Jun 5, 2010, 4:53:11 AM6/5/10
to android-ndk
Thanks David,

I understand I can't change a priority of native thread.

Thank you for your cooperation.

On 6月3日, 午後12:59, David Turner <di...@android.com> wrote:
> As far as I understand, you need to be root to change to SCHED_FIFO or
> SCHED_RR,
> so you can't use these in an application.
>
> 2010/6/2 一郎 鈴木 <ichiro...@gmail.com>
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegr-oups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.- 引用テキストを表示しない -
>
> - 引用テキストを表示 -

Crossle Song

unread,
Apr 3, 2014, 12:49:06 PM4/3/14
to andro...@googlegroups.com
Why don't change ?  it's need root, How can i changed pthread priority?
Reply all
Reply to author
Forward
0 new messages