Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Session 2, Processes and Scheduling

Similar presentations


Presentation on theme: "Practical Session 2, Processes and Scheduling"— Presentation transcript:

1 Practical Session 2, Processes and Scheduling
Operating Systems Practical Session 2, Processes and Scheduling

2 Five-state Process Model
New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait

3 Two types of scheduling
Preemptive scheduling A task may be rescheduled to operate at a later time (for example, it may be rescheduled by the scheduler upon the arrival of a “more important” task). Non Preemptive scheduling (cooperative) Task switching can only be performed with explicitly defined system services (for example: the termination task, explicit call to yield() , I/O operation which changes the process state to blocking, etc’…).

4 Scheduling algorithms
FCFS (First – Come, First – Served) Non preemptive Convoy effect SJF (Shortest Job First) Provably minimal with respect to the minimal average turn around time No way of knowing the length of the next CPU burst Can approximate according to: Tn+1=αtn+(1- α)Tn Preemptive (Shortest Remaining Time - SRT) or non preemptive Round Robin When using large time slices it imitates FCFS When using time slices which are closer to context switch time, more CPU time is wasted on switches Guaranteed scheduling Constantly calculates the ratio between how much time the process has had since its creation and how much CPU time it is entitled to CPU time entitled to = Time since process creation Total number of processses Guarantees 1/n of CPU time per process / user

5 Priority Scheduling algorithms
Priority scheduling (naïve) A generalization of SJF Multi Level Queue scheduling Partition the ready queue Each partition employs its own scheduling scheme A process from a lower priority group may run only if there is no higher priority process May cause starvation! Regarding Guaranteed scheduling: Tanenbaum second edition p. 170

6 Priority Scheduling algorithms (cont.)
Dynamic Multi Level scheduling Promote (starving) low priority processes Can be implemented by counting the time from last execution of the process Demote processes running longer Priority may become balanced eventually

7 Two Level scheduling First level: Choose process to run
Second level: Choose the process to swap out CPU Memory disk Memory scheduler CPU scheduler

8 The Completely Fair Scheduler
CFS models an “ideal, precise multitasking CPU” Can be thought of as a combination of guaranteed scheduling and dynamic priority scheduling It measure how much runtime each task has had and try and ensure that everyone gets their fair share of time The process’s runtime held in a vruntime variable A lower vruntime indicates that the process has had less time to compute, and therefore has more need of the processor

9 CFS Priorities CFS doesn't use priorities directly but instead uses them as a decay factor for the time a process is permitted to execute Lower-priority processes have higher factors of decay, while higher-priority processes have lower factors of decay Decay factor is inverse to the priority A higher priority process will accumulate vruntime more slowly Likewise, a low-priority process will have its vruntime increase more quickly

10 Quality criteria measures
Throughput – The number of completed processes per time unit. Turnaround time – The time interval between the process submission and its completion. Waiting time – The sum of all time intervals in which the process was in the ready queue. Response time – The time taken between submitting a command and the generation of first output. CPU utilization – Percentage of time in which the CPU is not idle.

11 XV6

12 Five-state Process Model
New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait proc.h enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; // Per-process state struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enum procstate state; // Process state int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) };

13 Creation of a Process (fork)
New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait np->sz = curproc->sz; np->parent = curproc; *np->tf = *curproc->tf; // Clear %eax so that fork returns 0 in the child. np->tf->eax = 0; for(i = 0; i < NOFILE; i++) if(curproc->ofile[i]) np->ofile[i] = filedup(curproc->ofile[i]); np->cwd = idup(curproc->cwd); safestrcpy(np->name, curproc->name, sizeof(curproc->name)); pid = np->pid; acquire(&ptable.lock); np->state = RUNNABLE; release(&ptable.lock); return pid; } proc.c int fork(void) { int i, pid; struct proc *np; struct proc *curproc = myproc(); // Allocate process. if((np = allocproc()) == 0){ return -1; // Copy process state from p. if((np->pgdir = copyuvm(curproc->pgdir, curproc->sz)) == 0){ kfree(np->kstack); np->kstack = 0; np->state = UNUSED;

14 Transition to a Running state
New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait // Switch to chosen process. It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. c->proc = p; switchuvm(p); p->state = RUNNING; swtch(&(c->scheduler), p->context); switchkvm(); // Process is done running for now. // It should have changed its p->state before coming back. c->proc = 0; } release(&ptable.lock); proc.c void scheduler(void) { struct proc *p; struct cpu *c = mycpu(); for(;;){ // Enable interrupts on this processor. sti(); // Loop over process table looking for process to run. acquire(&ptable.lock); for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ if(p->state != RUNNABLE) continue;

15 Transition to a Running state
New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait # Switch stacks movl %esp, (%eax) // esp points to a context like data // store it into old movl %edx, %esp // esp gets edx = switch kernel stack # Load new callee-save registers popl %edi popl %esi popl %ebx popl %ebp ret swtch.S # Context switch # void swtch(struct context **old, struct context *new); # Save current register context in old # and then load register context from new. .globl swtch swtch: movl 4(%esp), %eax // eax gets old movl 8(%esp), %edx // edx gets new # Save old callee-save registers pushl %ebp pushl %ebx pushl %esi pushl %edi struct context { uint edi; uint esi; uint ebx; uint ebp; uint eip; };

16 Transition to a Ready state
New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait void sched(void) { int intena; struct proc *p = myproc(); if(!holding(&ptable.lock)) panic("sched ptable.lock"); if(mycpu()->ncli != 1) panic("sched locks"); if(p->state == RUNNING) panic("sched running"); if(readeflags()&FL_IF) panic("sched interruptible"); intena = mycpu()->intena; swtch(&p->context, mycpu()->scheduler); mycpu()->intena = intena; } proc.c yield(void) acquire(&ptable.lock); //DOC: yieldlock myproc()->state = RUNNABLE; sched(); release(&ptable.lock);

17 Transition to a Blocked state
proc.c void sleep(void *chan, struct spinlock *lk) { struct proc *p = myproc(); if(p == 0) panic("sleep"); if(lk == 0) panic("sleep without lk"); if(lk != &ptable.lock){ //DOC: sleeplock0 acquire(&ptable.lock); //DOC: sleeplock1 release(lk); } // Go to sleep. p->chan = chan; p->state = SLEEPING; sched(); // Tidy up. p->chan = 0; // Reacquire original lock. if(lk != &ptable.lock){ //DOC: sleeplock2 release(&ptable.lock); acquire(lk); New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait

18 Transition to a Ready state
static void wakeup1(void *chan) { struct proc *p; for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) if(p->state == SLEEPING && p->chan == chan) p->state = RUNNABLE; } proc.c void wakeup(void *chan) acquire(&ptable.lock); wakeup1(chan); release(&ptable.lock); New Ready Running Exit Blocked Admit Event Occurs Dispatch Release Time-out Wait

19 Warm up Why bother with multiprogramming?
Assume processes in a given system wait for I/O 60% of the time. What is the approximate CPU utilization with one process running? What is the approximate CPU utilization with three processes running?

20 Warm up If a process is blocking on I/O 60% of the time, than there is only 40% of CPU utilization. At a given moment, the probability that all three processes are blocking on I/O is (0.6)3. That means that the CPU utilization is (1-(0.6)3)=0.784, or roughly 78%.

21 Preemptive dynamic priorities scheduling
(Taken from Silberschatz, 5-9) Consider the following preemptive priority scheduling algorithm with dynamically changing priorities: When a process is waiting for the CPU (in the ready queue, but not running), its priority changes at rate α; when it is running, its priority changes at rate β. All processes are given a priority of 0 when they enter the ready queue. The parameters alpha and beta can be set. Higher priority processes take higher values. What is the algorithm that results from β> α > 0? What is the algorithm that results from α < β< 0? Is there a starvation problem in 1? in 2? explain.

22 Preemptive dynamic priorities scheduling
β>α>0. To get a better feeling of the problem, we will create an example: C, P1, P2, P3 arrive one after the other and last for 3 TU, α=1 and β=2(bold marks the running process): The resulting schedule is FCFS. Slightly more formal: If a process is running it must have the highest priority value. While it is running, it’s priority value increases at a rate greater than any other waiting process. As a result, it will continue it’s run until it completes (or waits on I/O, for example). All processes in the waiting queue, increase their priority at the same rate, hence the one which arrived earliest will have the highest priority once the CPU is available. Time 1 2 3 4 5 6 7 8 9 P1 P2 P3

23 Preemptive dynamic priorities scheduling
α <β<0. We will use (almost) the same example problem as before, but this time α=- 2, β=-1: The resulting schedule is LIFO. More formally: If a process is running it must have the highest priority value. While it is running, it’s priority value decreases at a much lower rate than any other waiting process. As a result, it will continue it’s run until it completes (or waits on I/O, for example), or a new process with priority 0 is introduced. As before, all processes in the waiting queue, decrease their priority at the same rate, hence the one which arrived later will have the highest priority once the CPU is available. Time 1 2 3 4 5 6 7 8 9 P1 -1 -3 -5 -7 -9 -11 -13 -14 P2 -8 P3 -2

24 Preemptive dynamic priorities scheduling
In the first case it is easy to see that there is no starvation problem. When the kth process is introduced it will wait for at most (k-1)⋅max{timei} Time Units. This number might be large but it is still finite. This is not true for the second case - consider the following scenario: P1 is introduced and receives CPU time. While still working a 2nd process, P2, is initiated. According to the scheduling algorithm in the second case, P2 will receive the CPU time and P1 will have to wait. As long as new processes will keep coming before P1 gets a chance to complete its run, P1 will never complete its task.


Download ppt "Practical Session 2, Processes and Scheduling"

Similar presentations


Ads by Google