psched.c文件
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <signal.h>
typedef void (*sighandler_t)(int);
void sigcat(){
//实现优先数+1
setpriority(PRIO_PROCESS,getpid(), getpriority(PRIO_PROCESS,0)+1);
}
void sigzat(){
//实现优先数-1
setpriority(PRIO_PROCESS,getpid(), getpriority(PRIO_PROCESS,0)-1);
}
void nothing() {}
int main(int argc, char *argv[])
{
int i,j,status;
int pid; //存放进程号
struct sched_param p[2]; //设置调度策略时使用的数据结构
//SIGINT对应sigcat,SIGTSTP对应sigzat
if((pid=fork()) <0){
perror("process not create");
exit(EXIT_FAILURE);
}
else if(pid>0){
signal(SIGINT, (sighandler_t)sigcat);
signal(SIGTSTP, (sighandler_t)nothing);
sched_setscheduler(getpid(), SCHED_OTHER, &p[0]);
setpriority(PRIO_PROCESS, getpid(), 0);
sleep(1); //每隔1妙报告一次进程号和优先级
for(i=0; i<10; i++){
printf("Parent PID = %d priority = %d\n",getpid(),getpriority(PRIO_PROCESS,0));
printf("按Ctrl+c可为父进程的优先数加1\n");
pause();
sleep(1);
}
exit( EXIT_SUCCESS);
}
//各子进程循环报告其优先数和调度策略
else{
sleep(1); //每隔1妙报告一次进程号和优先级
signal(SIGTSTP, (sighandler_t)sigzat);
signal(SIGINT, (sighandler_t)nothing);
sched_setscheduler(getpid(), SCHED_OTHER, &p[1]);
setpriority(PRIO_PROCESS, getpid(), 0);
sleep(1); //每隔1妙报告一次进程号和优先级
for(i=0; i<10; i++){
printf("Child PID = %d priority = %d\n",getpid(),getpriority(PRIO_PROCESS,0));
printf("按Ctrl+z可为父进程的优先数加1\n");
pause();
sleep(1);
}
exit( EXIT_SUCCESS);
}
return EXIT_SUCCESS;
}
MakeFile文件:
srcs = psched.c
objs = psched.o
opts = -g -c
all: psched
psched: $(objs)
gcc $(objs) -o psched
psched.o: $(srcs)
gcc $(opts) $(srcs)
clean:
rm psched *.o