当前位置: 技术问答>linux和unix
你敢吗?抛砖引玉的问题,值得一看
来源: 互联网 发布时间:2016-12-13
本文导语: 路过的技术牛人,想请教一个问题。我想用fork产生3个子进程,分别执行不同的任务。并且给他们设定不同的优先级,让他们按照优先级执行。 我的代码在下面。进程1,child1的优先级是14.child2的优先级是10.child3的优...
路过的技术牛人,想请教一个问题。我想用fork产生3个子进程,分别执行不同的任务。并且给他们设定不同的优先级,让他们按照优先级执行。
我的代码在下面。进程1,child1的优先级是14.child2的优先级是10.child3的优先级是-3.按道理应该是子进程child3先执行。
#include
#include
#include
#include
#include
#include
void main()
{
pid_t father,child1,child2,child3;
int prio;
father=getpid();
setpriority(PRIO_PROCESS,0,20);
//nice(10);
printf("the father ID is : %dn",father);
prio=getpriority(PRIO_PROCESS,0);
printf("the father priority is %dn",prio);
child1=fork();
if(child1==0) {
setpriority(PRIO_PROCESS,0,14);
prio=getpriority(PRIO_PROCESS,0);
printf("the child one ID is : %dn",getpid());
printf("the child one priority is %dn",prio);
}
else if(child1>0){
child2=fork();
if(child2==0) {
setpriority(PRIO_PROCESS,0,10);
prio=getpriority(PRIO_PROCESS,0);
printf("the child two ID is : %dn",getpid());
printf("the child two priority is %dn",prio);
}
else if(child2>0){
child3=fork();
if(child3==0) {
setpriority(PRIO_PROCESS,0,-3);
prio=getpriority(PRIO_PROCESS,0);
printf("the child three ID is : %dn",getpid());
printf("the child three priority is %dn",prio);
}
else if(child3>0) {
printf("my ID is %dn",getpid());
printf("my prio is %dn",getpriority(PRIO_PROCESS,0));
}
}
}
}
执行结果是:
先执行1,再 2,再3
我的代码在下面。进程1,child1的优先级是14.child2的优先级是10.child3的优先级是-3.按道理应该是子进程child3先执行。
#include
#include
#include
#include
#include
#include
void main()
{
pid_t father,child1,child2,child3;
int prio;
father=getpid();
setpriority(PRIO_PROCESS,0,20);
//nice(10);
printf("the father ID is : %dn",father);
prio=getpriority(PRIO_PROCESS,0);
printf("the father priority is %dn",prio);
child1=fork();
if(child1==0) {
setpriority(PRIO_PROCESS,0,14);
prio=getpriority(PRIO_PROCESS,0);
printf("the child one ID is : %dn",getpid());
printf("the child one priority is %dn",prio);
}
else if(child1>0){
child2=fork();
if(child2==0) {
setpriority(PRIO_PROCESS,0,10);
prio=getpriority(PRIO_PROCESS,0);
printf("the child two ID is : %dn",getpid());
printf("the child two priority is %dn",prio);
}
else if(child2>0){
child3=fork();
if(child3==0) {
setpriority(PRIO_PROCESS,0,-3);
prio=getpriority(PRIO_PROCESS,0);
printf("the child three ID is : %dn",getpid());
printf("the child three priority is %dn",prio);
}
else if(child3>0) {
printf("my ID is %dn",getpid());
printf("my prio is %dn",getpriority(PRIO_PROCESS,0));
}
}
}
}
执行结果是:
先执行1,再 2,再3
|
#include
#include
#include
#include
#include
#include
#include
void main()
{
pid_t father,child1,child2,child3;
int prio;
int rt,curr_policy;
struct sched_param my_params;
my_params.sched_priority=sched_get_priority_max(SCHED_RR);
father=getpid();
curr_policy=sched_getscheduler(father);
printf("SCHED_OTHER = %d SCHED_FIFO =%d SCHED_RR=%d n",SCHED_OTHER,SCHED_FIFO,SCHED_RR);
printf("the current scheduler = %d n",curr_policy);
rt=sched_setscheduler(0,SCHED_RR,&my_params);
if(rt0){
child2=fork();
if(child2==0) {
curr_policy=sched_getscheduler(child2);
printf("child2 scheduler = %d n",curr_policy);
setpriority(PRIO_PROCESS,0,10);
prio=getpriority(PRIO_PROCESS,0);
printf("the child two ID is : %dn",getpid());
printf("the child two priority is %dn",prio);
}
else if(child2>0){
child3=fork();
if(child3==0) {
curr_policy=sched_getscheduler(child3);
printf("child2 scheduler = %d n",curr_policy);
setpriority(PRIO_PROCESS,0,-3);
prio=getpriority(PRIO_PROCESS,0);
printf("the child three ID is : %dn",getpid());
printf("the child three priority is %dn",prio);
}
else if(child3>0) {
printf("my ID is %dn",getpid());
printf("my prio is %dn",getpriority(PRIO_PROCESS,0));
}
}
}
}
试了下SCHED_RR实时调度策略,结果还是不行。
我也一直相信4楼大哥说的“你是在子进程起来之后,再修改的子进程的优先级,这样做可能晚了。因为你修改进程3的优先级时,进程2可能都已经执行完了。。。”
如果有什么特别需要只能靠signal了来同步了。