sched_setscheduler is for all threads or main thread?
我在主线程和主线程创建中询问了有关SCHED_RR的问题, 然后我尝试通过以下源代码测试它:
void *Thread1(void *arg)
{
pthread_detach(pthread_self());
pthread_attr_t attr;
pthread_attr_init (&attr);
int ix = -1 ;
int s = pthread_attr_getinheritsched (&attr, &ix );
printf("s=(%d)\n",s) ;
if( ix == PTHREAD_INHERIT_SCHED )
printf("1.............\n") ;
if( ix == PTHREAD_EXPLICIT_SCHED )
printf("2.............\n") ;
pid_t tid = syscall(SYS_gettid);
printf("tid=(%d) \n",tid);
int policy = -1;
struct sched_param sp ;
printf("before pthread_getschedparam \n");
s = pthread_getschedparam(tid,&policy,&sp) ;
printf("after pthread_getschedparam \n");
printf("s=(%d)\n",s) ;
printf("policy=(%d) , sp.sched_priority=(%d)\n",policy,sp.sched_priority) ;
}
int main()
{
const char *sched_policy[] = {
"SCHED_OTHER",
"SCHED_FIFO",
"SCHED_RR",
"SCHED_BATCH"
};
struct sched_param sp = {
.sched_priority = 90
};
pid_t pid = getpid();
printf("pid=(%d)\n",pid);
sched_setscheduler(pid, SCHED_RR, &sp);
printf("Scheduler Policy is %s.\n", sched_policy[sched_getscheduler(pid)]);
pthread_t tid ;
pthread_create(&tid , NULL, Thread1, (void*)(long)3);
while( 1 )
sleep ( 5 ) ;
}
我可以看到“在pthread_getschedparam之前”并且看不到“在pthread_getschedparam之后”,然后我尝试修改源代码如下:
修改来源,删除
s = pthread_getschedparam(tid,&policy,&sp) ;
然后将其更改为
s = pthread_getschedparam(pthread_self(),&policy,&sp) ;
然后它工作正常,我想知道为什么在pthread_getschedparam, pthread_self()work,syscall(SYS_gettid)不会。
答案 0 :(得分:3)
pthread_self()返回pthread_t(unsigned long int),而syscall(SYS_gettid)返回SPID(int)。它们不是同一个标识符。
因此,如果要调用pthread_ *函数,则需要提供使用pthread_self()检索的pthread id。
您可以从一个帖子中打印两个标识符,然后您就会看到它们有多么不同。