免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1521 | 回复: 3
打印 上一主题 下一主题

[进程管理] 如何创建实时进程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-18 10:07 |只看该作者 |倒序浏览
自己创建实时进程的步骤如下:(但是总是失败,求大神告知原因)、

#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main()
{
int rc,old_scheduler_policy;
struct sched_param my_params;
old_scheduler_policy=sched_getscheduler(getpid());       // 返回值为0,表示是普通进程,采用SCHED_OTHER调度策略

my_params.sched_priority=sched_get_priority_max(SCHED_RR);                            // 尽可能高的实时优先级

printf("press any key to change the current scheduler and priority to SCHED_RR\n");
getchar();
rc=sched_setscheduler(getpid(),SCHED_RR,&my_params);                    
if(rc<0)
{
   perror("sched_setscheduler to SCHED_RR error");                  
   exit(0);
}

return 0;
}


1. 创建实时进程失败了,为什么,应该如何来创建实时进程呢?
2. 执行main函数的进程调度策略是SCHED_OTHER,通过ps -elcf查看该进程的PRI,发现为19,普通进程的优先级不是在[100,139]范围么?

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
2 [报告]
发表于 2014-02-18 10:31 |只看该作者
我试了一下这段代码,是可以的呀
4 S root     28125 28001 RR  139 -   995 hrtime 10:25 pts/4    00:00:00 ./a.out

另外,这里看到PRI和内核中的理解是不一样的,反正就是经过了一定的转换。

论坛徽章:
0
3 [报告]
发表于 2014-02-18 10:35 |只看该作者
回复 1# zhangyuxiu
1. 创建实时线程必须使用root用户
2. ps显示的pri为用户态显示的进程普通优先级,计算方式为p->prio - 100,详见proc_tid_stat->do_task_stat->task_prio;实时进程的优先级为ps显示的rtprio
/**
* task_prio - return the priority value of a given task.
* @p: the task in question.
*
* This is the priority value as seen by users in /proc.
* RT tasks are offset by -200. Normal tasks are centered
* around 0, value goes from -16 to +15.
*/
int task_prio(const struct task_struct *p)
{
        return p->prio - MAX_RT_PRIO;
}


   

论坛徽章:
0
4 [报告]
发表于 2014-02-18 11:02 |只看该作者
回复 3# junnyg


    由于我无法登陆到root用户,我给那段程序代码编译链接后的可执行文件+s权限,以root的身份运行。可是还是失败了,不能这样么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP