Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s agenda Lab3 assigned (Due: Oct 28) Midterm Review

Similar presentations


Presentation on theme: "Today’s agenda Lab3 assigned (Due: Oct 28) Midterm Review"— Presentation transcript:

1 Today’s agenda Lab3 assigned (Due: Oct 28) Midterm Review
Synchronization: deadlock (recap) CS354-Fall2018

2 Midterm: Thursday October 11, 6:30 PM-7:30PM, Lilly 1105
Bring your ID (will be checked) Closed book/note/phone/laptop/other electronic Chapter supplementary materials + labs + xinu’s source codes CS354-Fall2018

3 Midterm Questions T/F will use bubblesheet Short-answer questions
Scheduling Synchronization CS354-Fall2018

4 A Big Picture OS overview Process: concurrent execution
Process scheduling Process synchronization + XINU implementation + Supplementary materials for scheduling policies and other synchronization forms CS354-Fall2018

5 OS Overview OS services OS abstractions between System call
Provides abstract computing environment Supplies computational services Manages resources Hides low-level hardware details OS abstractions between API to user space (above) Hardware (below) System call OS service: allocate resources CS354-Fall2018

6 Sample Questions Quiz 1 [True] Kprintf() is a system call provided by XINU. What else is a system call? Quiz 1 [True] Some instructions cannot be executed in user mode. user mode and privileged mode CS354-Fall2018

7 XINU Process – Unique process identifier – Owner (e.g., a user)
– Scheduling priority – Location of code and all data (including the stack) – Status of the computation – Current program counter – Current values of registers CS354-Fall2018

8 Process (Process.h) /* Definition of the process table (multiple of 32 bits) */ struct procent { /* Entry in the process table */ uint16 prstate; /* Process state: PR_CURR, etc. */ pri16 prprio; /* Process priority */ char *prstkptr; /* Saved stack pointer */ char *prstkbase; /* Base of run time stack */ uint32 prstklen; /* Stack length in bytes */ char prname[PNMLEN]; /* Process name */ uint32 prsem; /* Semaphore on which process waits */ pid32 prparent; /* ID of the creating process */ umsg32 prmsg; /* Message sent to this process */ bool8 prhasmsg; /* Nonzero iff msg is valid */ int16 prdesc[NDESC]; /* Device descriptors for process */ }; prstate The current status of the process (e.g., whether the process is currently executing or waiting) prprio The scheduling priority of the process prstkptr The saved value of the process’s stack pointer when the process is not executing prstkbase The address of the base of the process’s stack CS354-Fall2018

9 Process States States: current, ready, suspended, waiting …
#define PR_FREE 0 /* Process table entry is unused */ #define PR_CURR 1 /* Process is currently running */ #define PR_READY 2 /* Process is on ready queue */ #define PR_RECV 3 /* Process waiting for message */ #define PR_SLEEP 4 /* Process is sleeping */ #define PR_SUSP 5 /* Process is suspended */ #define PR_WAIT 6 /* Process is on semaphore queue */ #define PR_RECTIM 7 /* Process is receiving with timeout */ Q: What is a legible process? Or what processes will be considered by resched()? Quiz1:  [False] The Xinu scheduler always chooses to run a process that has highest priority. It chooses from eligible processes which are current running or ready to run. CS354-Fall2018

10 Process: concurrent execution, scheduling & synchronization
CS354-Fall2018

11 Something you must know in XINU (1)
create() resume() suspend() ready() resched() wait() signal() kill() ctxsw() nulluser() semcreate() semdelete() CS354-Fall2018

12 What call resched() or ready()?
ready() calls resched() right away. suspend()? resume()? create()? signal()? [True] [True] [False] why? Suspended [True] CS354-Fall2018

13 When to call resched() Four cases:
Current process is no longer eligible Chprio A process becomes eligible Clock interrupt handler Note: choose the highest-priority one among eligible processes (current or ready) CS354-Fall2018

14 Which process can call suspend()?
Similar questions: which process can call create(), resume(), or kill()… ? suspend(): by itself and other process kill(): by itself and other processes resume(): by other process create(): by other process CS354-Fall2018

15 How to kill() a process? kill.c … PR_CURR: // suicide
prptr->prstate = PR_FREE; resched() PR_WAIT: // increments semaphore count semtab[prptr->prsem].scount++; PR_READY: // remove from queue getitem(pid); CS354-Fall2018

16 Scheduling Three steps Scheduling policy context switch
Examine processes that are eligible for execution Select a process to run Switch the processor to the selected process Scheduling policy XINU: At any time, the processor must be executing a highest priority eligible process. Among processes with equal priority, scheduling is round robin. context switch CS354-Fall2018

17 Scheduling Policy Preemptive vs non-preemptive
FCFS? RR? SJF? SRTF? How do they compare with each other? Fairness? Completion time? Waiting time? … Starvation? CPU burst length and quantum How to set quantum? Multi-level Feedback Queue scheduling CS354-Fall2018

18 Sample Questions Quiz1 [False] The default XINU scheduler is starvation-free [False] RR is always better than FCFS. When RR = FCFS? (quantum = infinity) When FCFS is better than RR? (exec time is equal for each process) [True] CPU-intensive processes typically have longer burst length than for those non-CPU-intensive processes CS354-Fall2018

19 Sample Questions (con’d)
Process Burst Time p1 24 ms p2 3 ms p3 P1 P2 P3 P1 P1 10 3 3 10 4 RR (quantum =10): Average completion time = ( )/3=19.6 ms How about FCFS, SRTF? CS354-Fall2018

20 Context Switch A context switch is triggered by
Low-level Usually written in assembly language Called by scheduler A context switch is triggered by a hardware interrupt resched() Steps: Given a new process N, and old process O Save copy of all information pertinent to O on process O’s stack or the process table entry for process O Load saved information for N Resume execution of N CS354-Fall2018

21 Sample Questions Quiz1 [True] The value of the stack pointer is updated during the context switching operation. [False] No context switch can occur during the execution of the function when interrupts are disabled at the beginning of a system call The current process may give it up. Which process executes the context switch code? Both O and N (first O and then N) At any time, one process must be executing. All user processes may be blocked (e.g., in a situation when all applications are waiting for input). Which process executes? NULLPROC CS354-Fall2018

22 Interrupt Template For System Calls
disable() at the start restore() at the end Quiz 2 [False] OS can handle interrupt in many ways. It can examine current interrupt mask. It can also explicitly disable and enable interrupts in system calls. OS can clear interrupt mask to allow interrupts but it can't explicitly enable interrupts in system calls. CS354-Fall2018

23 Process Coordination Two approaches Hardware: busy waiting
Handling mutex exclusion with spinlock also called test-and-set Pros and cons? Pros: It worked in multicore Cons: it doesn’t work in single core; a spin lock is wasteful as a process merely blocks in a loop until access is granted OS: Mutual exclusion CS354-Fall2018

24 High-Level Mutual Exclusion
Counting semaphore semcreate(), wait(), signal(), semdelete() semtab struct sentry semtab[NSEM]; /* Semaphore table */ sentry: scount and queue (waiting process) CS354-Fall2018

25 Mutual Exclusion With Semaphores
Initialize: create a mutex semaphore sid = semcreate(1); Use: bracket critical sections of code with calls to wait and signal wait(sid); ...critical section (use shared resource)... signal(sid); CS354-Fall2018

26 Producer-Consumer Synchronization With Semaphores
Two semaphores suffice (why?) Initialize: create producer and consumer semaphores psem = semcreate(buffer-size); csem = semcreate(0); Producer: ? wait(?); CS; signal (?); Consumer? Producer: repeat forever { wait(psem); fill_next_buffer_slot; signal(csem); } wait(csem); extract_from_buffer_slot; signal(psem); CS354-Fall2018

27 What values will be scount?
semaphore count (scount) A nonnegative semaphore count means that the set of processes is empty. A count of negative N means that the set contains N waiting processes. max? min? CS354-Fall2018

28 Semaphore Queuing Policy
Determines which process to select among those waiting Needed when signal called FCFS (XINU) See signal() if ((semptr->scount++) < 0) { /* Release a waiting process */ ready(dequeue(semptr->squeue)); } CS354-Fall2018

29 Wait() …. if (--(semptr->scount) < 0) { /* If caller must block */ prptr = &proctab[currpid]; prptr->prstate = PR_WAIT; prptr->prsem = sem; /* Record semaphore ID */ enqueue(currpid,semptr->squeue); resched(); /* and reschedule */ } CS354-Fall2018

30 semcreate() and semdelete()
semcreate(): dynamic (xinu) Semaphores are created at runtime – More flexible semtab (global table) semdelete() XINU: make process ready when one or more processes may be waiting when semaphore is deleted How? CS354-Fall2018

31 semdelete() … resched_cntl(DEFER_START);
while (semptr->scount++ < 0) { /* Free all waiting processes */ ready(getfirst(semptr->squeue)); } resched_cntl(DEFER_STOP); CS354-Fall2018

32 Reader-Writer problem
How different from producer-consumer problem? Implementation in the lecture Writer: starvation Implementation variations (see Quiz2) Unbounded Priority Inversion Why it occurs? How to fix it? CS354-Fall2018

33 Deadlock Dining-Philosophers Problem Bridge Crossing Example
Resource allocation graph no cycles  no deadlock a cycle  deadlock CS354-Fall2018

34 Deadlock (more) Supplementary lecture CS354-Fall2018

35 Something you must know in XINU (2)
currpid NULLPROC (always eligible) proctab semtab readylist: sorted, double-linked readylist = newqueue(); The current process does not appear on the ready list queuetab: struct qentry queuetab[NQENT]; /* Table of process queues */ readylist sleeplist insert(pid32 pid, qid16 q, int32 key) firstkey(), last getitem() // remove from the queue In the XINU queue table (queuetab), the current process will be linked into one of the lists enqueue() dequeue() CS354-Fall2018

36 Final word: Go through all the materials
Slides Quizs Labs XINU Source codes Good luck! CS354-Fall2018


Download ppt "Today’s agenda Lab3 assigned (Due: Oct 28) Midterm Review"

Similar presentations


Ads by Google