当前位置: 技术问答>linux和unix
进程调度,关于状态机的问题
来源: 互联网 发布时间:2016-12-13
本文导语: 我想用fork产生3个子进程执行不同的任务,各个子进程分别按照他们自己的优先级做事情,这个咋弄啊。我原本是通过setpriority函数来设定他们的优先级,可以步行。进程1,child1的优先级是14.child2的优先级是10.child3的...
我想用fork产生3个子进程执行不同的任务,各个子进程分别按照他们自己的优先级做事情,这个咋弄啊。我原本是通过setpriority函数来设定他们的优先级,可以步行。进程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
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
int main()
{
pid_t father,child1,child2,child3;
int prio;
int *flag;
flag = (int *)mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
(*flag)=3;
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) {
while(1 != (*flag)){
//printf("child1 waitn");
sleep(1);
}
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) {
while(2 != (*flag)){
//printf("child2 waitn");
sleep(1);
}
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);
(*flag) = 1;
}
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);
(*flag) = 2;
}
else if(child3>0) {
printf("my ID is %dn",getpid());
printf("my prio is %dn",getpriority(PRIO_PROCESS,0));
}
}
}
return 0;
}
楼主好执着啊,纠着这个问题不放了
不知道你想要干嘛,你自己把sleep改为其它高效一点的函数,看看是否符合你的要求