LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1280|回复: 1

问一个sched_yield的问题

[复制链接]
发表于 2005-11-9 21:25:55 | 显示全部楼层 |阅读模式
sched_yield说的是让运行的进程让出CPU。那在一个单进程多线程的程序中调用sched_yield会不会让当前的线程让出CPU呢?
 楼主| 发表于 2005-11-10 14:18:04 | 显示全部楼层
再问一下。在RH9.0下。为什么在主线程中没办法设置发起线程的调度策略呢。代码如下:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>

void * thread1(void *arg)
{
   int   i;
   short j;
  
   struct sched_param temp;

    pthread_getschedparam(pthread_self(),&i,&temp);
  if(i == SCHED_FIFO)
      printf("the policy is fifo in 1\n");
   printf("the prio is %d\n",temp.sched_priority);
     printf("this is thread1\n");
   return NULL;
}

int main(void)
{
  pthread_t thread_1;
  pthread_t thread_2;
  pthread_attr_t thread_attr;
  int thread_policy;
  int i,j;
  struct sched_param param;
  void * result;
  int status;
   
  #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
    status = pthread_attr_init(&thread_attr);

    status = pthread_attr_setschedpolicy(&thread_attr,SCHED_RR);  
    if(status != 0)
       printf("can't set policy fifo\n");
    i = sched_get_priority_min(SCHED_RR);
    j = sched_get_priority_max(SCHED_RR);
    param.sched_priority = (i+j)/2;
    status =pthread_attr_setschedparam(&thread_attr,&param);
    status = pthread_attr_setinheritsched(&thread_attr,PTHREAD_EXPLICIT_SCHED);
     if(status != 0)
       printf("can't set inheritsched\n");

  #else
     printf("can't set priority\n");
  #endif
  status =  pthread_create(&thread_1,NULL,&thread1,NULL);
  if(status != 0)
    perror("create thread 1 err");
   while(1);
  pthread_exit(NULL);
  return (1);
}

运行发现创建的线程的priority老是0,调度策略也不是FIFO。这是为什么,哪里错了???
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表