3,286
社区成员
发帖
与我相关
我的任务
分享
void FunThread1()
{
sleep(1); //thread1休眠1秒
int i,j;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER\n");
if(policy==SCHED_RR)
printf("SCHED_RR\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO\n");
//sched_yield();
for(i=1;i<10;i++)
{
for(j=1;j<50000000;j++)
{
}
printf("thread 1\n");
//sleep(1);
}
printf("Thread1 exit\n");
}
void FunThread2()
{
sleep(1); //thread2也休眠1秒
int i,j,m;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER\n");
if(policy==SCHED_RR)
printf("SCHED_RR\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO\n");
//sched_yield();
m = 0;
for(i=1;i<10;i++)
{
for(j=1;j<50000000;j++)
{
m++; //增加thread2内部处理时间
}
printf("thread 2\n");
}
printf("Thread2 exit\n");
}
void FunThread3()
{
//sleep(1); //thread3不进行休眠,直接进行运行
int i,j;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER\n");
if(policy==SCHED_RR)
printf("SCHED_RR\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO\n");
//sched_yield();
for(i=1;i<10;i++)
{
for(j=1;j<50000000;j++)
{
}
printf("thread 3\n");
}
printf("Thread3 exit\n");
}
以上运行的结果:
The current user is root
SCHED_OTHER
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
Thread3 exit
SCHED_RR
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
Thread1 exit
SCHED_RR
thread 2
thread 2
thread 2
thread 2
thread 2
thread 2
thread 2
thread 2
thread 2
Thread2 exit
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
void FunThread1()
{
sleep(3);
/*Thread 1休眠3秒,Thread3和thread2休眠2秒,这样可以保证三个进程都能顺利创建,
由于优先级低的实时线程thread2比优先级高的thread1优先运行,只要保证thread2的循环
执行时间超过1秒,就可以出现线程抢占的情况.
*/
int i,j;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER\n");
if(policy==SCHED_RR)
printf("SCHED_RR\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO\n");
sched_yield(); //就本进程而言这一句可有可无,不影响结果,原因上面的帖子已经说过了,这里不在重复
for(i=1;i<10;i++)
{
for(j=1;j<50000000;j++)
{
}
printf("thread 1\n");
}
printf("Thread1 exit\n");
}
void FunThread2()
{
sleep(2); //休眠便于thread1能够正常创建,否则直到该线程退出才会执行thread1创建的代码
int i,j;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER\n");
if(policy==SCHED_RR)
printf("SCHED_RR\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO\n");
sched_yield();//就本进程而言这一句可有可无,不影响结果,原因上面的帖子已经说过了,这里不在重复
//增大循环的次数以便增加该线程执行的时间,保证thread1被调度时thread2仍在运行
for(i=1;i<100;i++)
{
for(j=1;j<50000000;j++)
{
}
printf("thread 2\n");
}
printf("Thread2 exit\n");
}
void FunThread3()
{
sleep(3);
int i,j;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER\n");
if(policy==SCHED_RR)
printf("SCHED_RR\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO\n");
sched_yield();
for(i=1;i<10;i++)
{
for(j=1;j<50000000;j++)
{
}
printf("thread 3\n");
}
printf("Thread3 exit\n");
}
int main()
{
int i;
i=getuid();
if(i==0)
printf("The current user is root\n");
else
printf("The current user is not root\n");
pthread_t ppid1,ppid2,ppid3;
struct sched_param param;
param.sched_priority=51;
pthread_attr_t attr,attr2,attr1;
pthread_attr_init(&attr);
pthread_attr_init(&attr2);
pthread_attr_init(&attr1);
pthread_attr_setschedpolicy(&attr2,SCHED_RR);
pthread_attr_setschedparam(&attr2,¶m);
//do not inherit father's attr
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);
param.sched_priority=21;
pthread_attr_setschedpolicy(&attr1,SCHED_RR);
pthread_attr_setschedparam(&attr1,¶m);
pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
pthread_create(&ppid3,&attr,FunThread3,NULL);
pthread_create(&ppid2,&attr1,FunThread2,NULL);
pthread_create(&ppid1,&attr2,FunThread1,NULL);
pthread_join(ppid3,NULL);
pthread_join(ppid2,NULL);
pthread_join(ppid1,NULL);
pthread_attr_destroy(&attr);
pthread_attr_destroy(&attr2);
return 0;
}
pthread_create(&ppid1,&attr2,FunThread1,NULL);