实验题目:进程调度算法实验学号:20100030xxxx
日期:2012-5-2班级:五班姓名:
Email:
实验目的:加深对进程调度概念的理解,体验进程调度机制的功能,了解Linux 系统中进程调度策略的使用方法。练习进程调度算法的编程和调试技术。
硬件环境:IBM实验室计算机
软件环境:eclipse gcc编译器
Ubuntu-Linux操作系统
Gnome桌面
实验步骤:
1.认真阅读试验指导书所给出的相关知识与示例程序,在此基础上分析试验要求,然后着手开始编写程序。
2.用eclipse新建一个c project。
3.新建source folder
4.新建名为psched.c的C语言程序
5.再建立以下名为psched.h的C语言头文件
6.build项目,产生可运行的二进制文件。
7.对程序进行调试,排除bug。
8.进入终端,运行程序,结果如下图所示:
源代码:
#include"psched.h"
int main(int argc,char*argv[]){
int pid;//存放⼦进程号
struct sched_param p1;//设置⼦进程调度策略时使⽤的数据结构struct sched_param p2;//设置⽗进程调度策略时使⽤的数据结构
if((pid=fork())<0){
perror("process not create");
exit(EXIT_FAILURE);}
else if(pid==0){
signal(SIGTSTP,handler1);//注册⼀个处理ctrl+z的信号量,将优先级减⼀
signal(SIGINT,handler3);//注册⼀个处理ctrl+c的信号量,什么也不做
sleep(1);
while(1){
printf("Child PID=%d priority=%d policy is
%d\\n
sleep(3);
}
}else{
signal(SIGINT,handler2);//注册⼀个处理ctrl+c的信号量,将优先级加⼀
signal(SIGTSTP,handler4);//注册⼀个处理ctrl+z的信号量,什么也不做
sched_setscheduler(pid,SCHED_OTHER,&p1);
sched_setscheduler(getpid(),SCHED_OTHER,&p2);
setpriority(PRIO_PROCESS,pid,10);
setpriority(PRIO_PROCESS,getpid(),10);
sleep(1);
while(1){
printf("Parent PID=%d priority=%d policy is%d\\n
sleep(3);
}
}
return EXIT_SUCCESS;
}
#include #include #include #include #include #include #include //⼦进程处理信号SIGTSTP的⽅法,将优先级减⼀ void handler1(){ setpriority(PRIO_PROCESS,getpid(),getpriority(PRIO_PROCESS,0)-1); } //⽗进程处理信号SIGINT的⽅法,将优先级加⼀ void handler2(){setpriority(PRIO_PROCESS,getpid(),getpriority(PRIO_PROCESS,0)+1); } void handler3(){ } void handler4(){ } 结论分析与体会:通过编写进程调度试验,首先,我更加熟练了如何编写多进程程序,更加了解了型号量的注册和使用方法。加深了对进程调度概念的理解,体验进程调度机制的功能,明白了如何控制进程的优先级和调度算法。下载本文