日期:2014-05-16  浏览次数:20786 次

高分求解释一道操作系统的实验题,想的我蛋疼啊,求解释
说明:这是我们操作系统的一道实验题,是关于进程调度算法的,以下是老师给出的一道参考程序:
C/C++ code

#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
int main(int argc,char *argv[]) {

    int i;
    int pid[3];
    struct sched_param p[3];

    for(i=0;i<3;i++) {

        if((pid[i] = fork()) > 0) {
            p[i].sched_priority = (argv[i+1] != NULL)?atoi(argv[i+1]):10;

            //调试代码
            if(argv[i+4] != NULL) {
                printf("here!---%d\n",atoi(argv[i+4]));
            }


            sched_setscheduler(pid[i],(argv[i+4] != NULL) ? atoi(argv[i+4]):SCHED_OTHER,&p[i]);
            setpriority(PRIO_PROCESS,pid[i],(argv[i+1] != NULL) ? atoi(argv[i+1]):10);

            //测试代码
            if(argv[i+1] != NULL) {
                printf("priority is %d\n",atoi(argv[i+1]));
            }


        }else {
            sleep(1);
            for(i=0;i<10;i++) {
                printf("CHild PID= %d priotity = %d\n",getpid(),getpriority(PRIO_PROCESS,0));
                sleep(1);
            }

            exit(EXIT_SUCCESS);
        }
    }

    printf("My child %d policy is %d\n",pid[0],sched_getscheduler(pid[0]));
    printf("My child %d policy is %d\n",pid[1],sched_getscheduler(pid[1]));
    printf("My child %d policy is %d\n",pid[2],sched_getscheduler(pid[2]));
    return(0);

}


这个程序实现的结果是父进程创建三个子进程,然后通过传递六个参数分别指定每个进程的进程调度策略和优先级,我传递的参数为5 18 -10 0 0 1,运行结果如下:
here!---0
priority is 5
here!---0
priority is 18
here!---1
priority is -10
My child 5181 policy is 0
My child 5182 policy is 0
My child 5183 policy is 0
CHild PID= 5181 priotity = 5
CHild PID= 5182 priotity = 18
CHild PID= 5183 priotity = 0
CHild PID= 5182 priotity = 18
CHild PID= 5181 priotity = 5
CHild PID= 5183 priotity = 0
CHild PID= 5181 priotity = 5
CHild PID= 5182 priotity = 18
CHild PID= 5183 priotity = 0
CHild PID= 5181 priotity = 5
CHild PID= 5182 priotity = 18
CHild PID= 5183 priotity = 0
CHild PID= 5181 priotity = 5
CHild PID= 5182 priotity = 18
CHild PID= 5183 priotity = 0
CHild PID= 5181 priotity = 5
CHild PID= 5183 priotity = 0
CHild PID= 5182 priotity = 18
CHild PID= 5181 priotity = 5
CHild PID= 5182 priotity = 18
CHild PID= 5183 priotity = 0
CHild PID= 5181 priotity = 5
CHild PID= 5183 priotity = 0
CHild PID= 5182 priotity = 18
CHild PID= 5183 priotity = 0
CHild PID= 5182 priotity = 18
CHild PID= 5181 priotity = 5
CHild PID= 5183 priotity = 0
CHild PID= 5182 priotity = 18
CHild PID= 5181 priotity = 5


我不明白的问题有两个:
1.明明优先级我设了个-10,但是在执行的时候却没有-10的,变成了0!
2.进程调度策略我设置了 0 0 1,前两个表示默认的,第三个表示FIFO,但是在最开始的时候,显示三个child的策略都是默认的啊,中么回事啊,亲?
备注:我调试的时候把参数打印出来,优先级是有-10的啊,是不是setpriority方法哪里有错啊!!
求帮忙啊。

------解决方案--------------------
if((pid[i] = fork()) > 0) {

你设置的是父进程的, 从来没设置过子进程的。
------解决方案--------------------
有两份PID数据,你设置的是父进程中数据,没有设置子进程
------解决方案--------------------
fork()父进程返回子进程pid,子进程返回0
------解决方案--------------------
子进程里面只打印,没设置,看看else分支里面的代码
------解决方案--------------------
1楼正解
!!!!!