- 论坛徽章:
- 0
|
本帖最后由 chen0610 于 2012-08-17 15:13 编辑
体现线程2的优先级比线程1高最好是用条件变量,线程2和线程1同时等待一个条件变量,另外再创建一个线程3释放条件变量。
哪一个线程先获得条件变量就说明哪一个的线程的优先级高。
在你的代码上修改的:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sched.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/resource.h>
#include<string.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *FunThread1(void *arg)
{
int i;
int policy;
int priority;
struct sched_param param;
printf("Thread 1 start\n");
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
{
printf("thread 1 SCHED_OTHER\n");
priority = param.sched_priority;
printf("thread 1 SCHED_OTHER=%d \n",priority);
}
if(policy==SCHED_RR)
{
printf("thread 1 SCHED_RR\n");
priority = param.sched_priority;
printf("thread 1 SCHED_RR=%d \n",priority);
}
if(policy==SCHED_FIFO)
{
printf("thread 1 SCHED_FIFO\n");
priority = param.sched_priority;
printf("thread 1 SCHED_FIFO=%d \n",priority);
}
for(i=11;i<=20;i++)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
printf("thread 1: i = %d.\n", i);
pthread_mutex_unlock(&mutex);
}
printf("Thread 1 exit\n");
}
void *FunThread2(void *arg)
{
int j;
int policy;
int priority;
struct sched_param param;
printf("Thread 2 start\n");
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
{
printf("thread 2 SCHED_OTHER\n");
priority = param.sched_priority;
printf("thread 2 SCHED_OTHER=%d \n",priority);
}
if(policy==SCHED_RR)
{
printf("thread 2 SCHED_RR\n");
priority = param.sched_priority;
printf("thread 2 SCHED_RR=%d \n",priority);
}
if(policy==SCHED_FIFO)
{
printf("thread 2 SCHED_FIFO\n");
priority = param.sched_priority;
printf("thread 2 SCHED_FIFO=%d \n",priority);
}
for(j=1;j<=10;j++)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
printf("thread 2: j = %d.\n", j);
pthread_mutex_unlock(&mutex);
}
printf("Thread 2 exit\n");
}
void *FunThread3(void *arg)
{
int j;
int policy;
int priority;
struct sched_param param;
printf("Thread 3 start\n");
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy==SCHED_OTHER)
{
printf("thread 3 SCHED_OTHER\n");
priority = param.sched_priority;
printf("thread 3 SCHED_OTHER=%d \n",priority);
}
if(policy==SCHED_RR)
{
printf("thread 3 SCHED_RR\n");
priority = param.sched_priority;
printf("thread 3 SCHED_RR=%d \n",priority);
}
if(policy==SCHED_FIFO)
{
printf("thread 3 SCHED_FIFO\n");
priority = param.sched_priority;
printf("thread 3 SCHED_FIFO=%d \n",priority);
}
for(;;)
{
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sleep(3);
}
printf("Thread 3 exit\n");
}
int main(int argc, char *argv[])
{
int i;
pthread_t tid1,tid2,tid3;
pthread_attr_t attr1,attr2,attr3;
struct sched_param param1, param2, param3;
i=getuid();
if(i==0)
{
printf("The current user is root\n");
}
else
{
printf("The current user is not root\n");
}
param1.sched_priority=20;
param2.sched_priority=30;
param3.sched_priority=10;
pthread_attr_init(&attr1);
pthread_attr_init(&attr2);
pthread_attr_init(&attr3);
pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
pthread_attr_setschedparam(&attr1,¶m1);
pthread_attr_setschedpolicy(&attr2,SCHED_FIFO);
pthread_attr_setschedparam(&attr2,¶m2);
pthread_attr_setschedpolicy(&attr3,SCHED_FIFO);
pthread_attr_setschedparam(&attr3,¶m3);
sleep(1);
pthread_create(&tid1,&attr1,FunThread1,NULL);
sleep(1);
pthread_create(&tid2,&attr2,FunThread2,NULL);
sleep(1);
pthread_create(&tid3,&attr3,FunThread3,NULL);
pthread_join(tid3,NULL);
pthread_attr_destroy(&attr1);
pthread_attr_destroy(&attr2);
return 0;
}
结果如下:
[root@localhost test]# gcc -o test test.c -lpthread
[root@localhost test]# ./test
The current user is root
Thread 1 start
thread 1 SCHED_FIFO
thread 1 SCHED_FIFO=20
Thread 2 start
thread 2 SCHED_FIFO
thread 2 SCHED_FIFO=30
Thread 3 start
thread 3 SCHED_FIFO
thread 3 SCHED_FIFO=10
thread 2: j = 1.
thread 2: j = 2.
thread 2: j = 3.
thread 2: j = 4.
thread 2: j = 5.
thread 2: j = 6.
thread 2: j = 7.
thread 2: j = 8.
thread 2: j = 9.
thread 2: j = 10.
Thread 2 exit
thread 1: i = 11.
thread 1: i = 12.
thread 1: i = 13.
thread 1: i = 14.
thread 1: i = 15.
thread 1: i = 16.
thread 1: i = 17.
thread 1: i = 18.
thread 1: i = 19.
thread 1: i = 20.
Thread 1 exit |
|