15 Process State and O.S. Scheduling

1 Process State and Scheduling

1.scheduler唯一的任务是决定哪个进程使用cpu

2 The O.S. Scheduler

2.1 O.S. Scheduling Ideas

  1. 一个好的scheduling algorithm必须满足以下三点
    1. CPU Utilization:cpu被充分利用
    2. Turnaround Time:进程的周转时间,一个进程运行多长时间
    3. Fairness:平等,所有的进程优先权相同

2.2 Preemptive Scheduling

  1. 所有进程交替执行
  2. 但是如果有等待IO的进程,就会浪费时间

2.3 Priority Queue Scheduling

  1. unix使用混合的 round-robin scheduling and priority scheduling,既 multilevel queue scheduling
  2. 所有的进程交替执行,进行被选择执行的条件时优先权

2.3.1 How nice a program is its priority

  1. priority优先权最低是-20,very mean,最高是19,very nice
  2. 所有的进程优先权是0
  3. 鼠标移动的进程优先权要高
  4. 守护进程的优先权低,不影响用户

2.3.2 Viewing Process State with ps

ps axo nice,comm,pid --sort nice

2.3.3 Programming Priority with getpriority() and nice()

/*getpriority.c*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/resource.h>

int main(){

  int prio;

  prio = getpriority(PRIO_PROCESS, 0); //get the priority of this process

  printf("Priority: %d\n",prio);
  return 0;

}

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/resource.h>


int main(){

  int prio;

  prio = getpriority(PRIO_PROCESS, 0); //get the priority of this process
  printf("Priority: %d\n",prio);


  if( nice(10) != 10){ //error checking is different
    perror("nice");
    return 1;
  }

  prio = getpriority(PRIO_PROCESS, 0); //get the priority of this process
  printf("Priority: %d\n",prio);

  return 0;
}


3 Process States

  1. OS需要综合考虑both the priority of a process and if it ready to run in order to maximize the utilization of the CPU

3.1 Running, Waiting, Blocked, and Undead

  1. 三种主要的状态
    1. running 正在cpu上运行
    2. ready and waiting 一旦被选中就会运行
    3. blocked and waiting 需要等待其他信息完成,才能运行,比如等待I/O,比如父进程等待子进程

3.2 Undead processes: Zombies

  1. 僵尸进程概念
    1. 父进程创建了子进程
    2. 子进程退出
    3. 父进程没有wait子进程,且没有退出
    4. 父进程和这些子进程都统称为僵尸进程
  2. ps -o pid,comm,state 查看僵尸进程
/*zombies.c*/
int main(){
  int i;
  for(i=0; i<10;i++){

   if(fork() == 0){ //create a child
      _exit(0); // and exit
   }
  }
  while(1); //run forever
}

3.3 Orphan Process, init, and daemons

  1. 孤儿进程
    1. 父进程死,子进程被init继承领养
    2. init进程调用wait等待这些子进程
  2. shell中用d.标识,既daemon,daemon进程在父进程死后,会被init领养,继续存活
    参考:
    https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/15/lec.html#coderef-bad_ref
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值