os实验--进程的调度 - webdancer's Blog
os实验--进程的调度
Linux系统中,进程的调度策略其实是一个比较困难的问题,实验指导书上的只是很不全面,而且模棱两可。现在只能大致了解一下: 通用Linux系统支持实时和非实时两种进程,实时进程相对于普通进程具有绝对的优先级。 通用Linux系统支持实时和非实时两种进程,实时进程相对于普通进程具有绝对的优先级。对应地,实时进程采用SCHED_FIFO或者SCHED_RR调度策略,普通的进程采用SCHED_OTHER调度策略。
优先级:
非实时进程有两种优先级,一种是静态优先级,另一种是动态优先级。实时进程又增加了第三种优先级,实时优先级。优先级是一些简单的整数,它代表了为决定应该允许哪一个进程使用 CPU 的资源时判断方便而赋予进程的权值——优先级越高,它得到 CPU 时间的机会也就越大:
1 .静态优先级——被称为“静态”是因为它不随时间而改变,只能由用户进行修改。它指明了在被迫和其它进程竞争 CPU 之前该进程所应该被允许的时间片的最大(但是也可能由于其它原因,在该时间片耗尽之前进程就被迫交出了 CPU。)值。
2.动态优先级——只要进程拥有 CPU,它就随着时间不断减小;当它小于 0 时,标记进程重新调度。它指明了在这个时间片中所剩余的时间量。
3.实时优先级——指明这个进程自动把 CPU 交给哪一个其它进程:较高权值的进程总是优先于较低权值的进程。因为如果一个进程不是实时进程,其优先级就是 0,所以实时进程总是优先于非实时进程的。
(这并不完全正确;如同后面论述的一样,实时进程也会明确地交出 CPU,而在等待 I/O 时也会被迫交出 CPU。前面的描述
仅限于能够交付 CPU 运行的进程).
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<signal.h>
#include<sched.h>
#include<sys/times.h>
#include<sys/resource.h>
typedef void (*sighandler_t)(int);
void sigcon(){
pid_t pi=getpid();
int pj=getpriority(PRIO_PROCESS,pi);
setpriority(PRIO_PROCESS,pi,pj+1);
printf("the pid is : %d ,the priority is %d,after SIGINT the priority is : %d\n",pi,pj,pj+1);
}
void sigcon1(){
pid_t pi=getpid();
int pj=getpriority(PRIO_PROCESS,pi);
setpriority(PRIO_PROCESS,pi,pj-1);
printf("the pid is : %d ,the priority is %d,after SIGTSTP the priority is : %d\n",pi,pj,pj-1);
}
int main(){
pid_t p0;
signal(SIGINT,(sighandler_t)sigcon);
signal(SIGTSTP,(sighandler_t)sigcon1);
int index;
if((p0=fork())<0){
perror("error!");
exit(0);
}
else if(p0==0){
for(index=0;index<4;index++){
pid_t cp=getpid();
int chpriority=getpriority(PRIO_PROCESS,cp);
int schednum=sched_getscheduler(cp);
printf("the child pid is : %d , the priority is : %d , the sched is : %d \n",cp,chpriority,schednum);
kill(cp,SIGINT);
// kill(cp,SIGTSTP);
}
}
else{
int in;
for(in=0;in<4;in++){
pid_t pp=getpid();
int ppriority=getpriority(PRIO_PROCESS,pp);
int pschednum=sched_getscheduler(pp);
printf("the parent pid is : %d , the priority is : %d , the sched is : %d \n",pp,ppriority,pschednum);
kill(pp,SIGINT);
// kill(pp,SIGTSTP);
}
}
return 0;
}
评论 (0)