Introduction to Lab 2 EDA092 – Operating Systems 2008-02-18 Wolfgang John Slides by Magnus Almgren Overview of OSP • An Environment for Operating System Projects • Simulates an operating system (surprise!) • Consists of modules that executes different operating system functions, such as – Scheduling of CPU – Virtual Memory Management – File Access (lab 2.1) (lab 2.2) (lab 2.3) Slides by Magnus Almgren OSP Modules: Overview CPU MEMORY SIMCORE DIALOG FILES INTER DEVINT PAGEINT TIMEINT DEVICES Slides by Magnus Almgren OSP Modules: Overview Lab 2.1 CPU Lab 2.2 MEMORY SIMCORE DIALOG FILES INTER DEVINT PAGEINT TIMEINT DEVICES Slides by Magnus Almgren OSP Modules: Overview CPU MEMORY CPU MEMORY SIMCORE SIMCORE INTER DEVINT PAGEINT TIMEINT DIALOG DIALOG FILES DEVICES FILES INTER DEVINT PAGEINT TIMEINT DEVICES Slides by Magnus Almgren OSP Modules: Overview CPU MEMORY SIMCORE INTER DEVINT PAGEINT TIMEINT DIALOG FILES DEVICES CPU MEMORY SIMCORE INTER DEVINT PAGEINT TIMEINT DIALOG FILES DEVICES Slides by Magnus Almgren OSP Modules: Misc • • • • RESOURCES – resource management DEVICES – disk access SOCKETS – process communication PROTOCOLS – protocols used for the SOCKET module CPU MEMORY SIMCORE INTER DEVINT PAGEINT TIMEINT DIALOG FILES DEVICES Slides by Magnus Almgren OSP Modules: SimCore • SimCore – Main Part of System – Generates events • • • • • Start and termination of processes Virtual memory references Timer interrupts Read/write to external devices Interrupts from external devices – Parameters of the simulation controls the simulation – Collects statistics about resource allocation CPU MEMORY SIMCORE INTER DEVINT PAGEINT TIMEINT DIALOG FILES DEVICES Slides by Magnus Almgren OSP Modules: SimCore • Hardware of OSP CPU – – – – – Interval Timer Clock Interrupt vector Base register for page tables Disk access • Memory – Virtual memory with paging: PAGE_TBL CPU MEMORY SIMCORE INTER DEVINT PAGEINT TIMEINT DIALOG FILES DEVICES Slides by Magnus Almgren OSP Modules: CPU • Lab 2.1: CPU Scheduling • Three Process States blocked (+done:zombie) sleep wakeup ready timer dispatch interrupt running • Each process has a Process Control Block CPU MEMORY SIMCORE INTER DEVINT PAGEINT TIMEINT DIALOG FILES DEVICES Slides by Magnus Almgren struct pcb_node { /* from cpu.c */ int pcb_id; /* PCB id */ int size; /* process size in bytes; assigned by SIMCORE */ int creation_time; /* assigned by SIMCORE */ int last_dispatch; /* last time the process was dispatched */ int last_cpuburst; /* length of the previous CPU burst */ int accumulated_cpu; /* accumulated CPU time */ PAGE_TBL *page_tbl; /* page table associated with the PCB */ STATUS status; /* status of process: running, ready, waiting, done */ EVENT *event; /* event upon which process may be suspended */ int priority; /* user-defined priority; used for scheduling */ PCB *next; /* next PCB in whatever queue */ PCB *prev; /* previous PCB in whatever queue */ int *hook; /* can hook up anything here */ }; Slides by Magnus Almgren Lab 2.1: Round Robin Scheduling • Round Robin = Signing petitions w/out showing who signed first • Circular queue of processes ready to execute • No priority (regular RR) • Each process executes until it – Terminates – Waits (for a resource) – Runs out of time quanta (‘timer interrupt’) Slides by Magnus Almgren CPU Ready Queue 1 2 3 4 Disk Queue Slides by Magnus Almgren CPU Out of time Ready Queue 1 2 3 4 Disk Queue Slides by Magnus Almgren CPU Ready Queue 2 3 4 1 Disk Queue Slides by Magnus Almgren CPU Out of time Ready Queue 2 3 4 1 Disk Queue Slides by Magnus Almgren CPU Ready Queue 3 4 1 2 Disk Queue Slides by Magnus Almgren CPU Wait for disk access Ready Queue 3 Disk Queue 3 4 1 2 Slides by Magnus Almgren CPU Wait for disk access Ready Queue 4 1 Disk Queue 3 4 2 Slides by Magnus Almgren CPU Interrupt: Disk access Ready Queue 1 2 Disk Queue 3 4 Slides by Magnus Almgren CPU Interrupt: Disk access Ready Queue 2 3 Disk Queue 3 4 Slides by Magnus Almgren CPU Interrupt: Process Termination Disk access Ready Queue 2 Disk Queue 4 3 Slides by Magnus Almgren Lab 2.1 Requirements? Round Robin Scheduling in OSP: 1. Implement a ready queue, and move processes from running and ready queue. Key Information: OSP 1.6 (impossible to do lab without it!!!) 2. Main Working File: cpu.c 3. Need: Linked List for Round Robin … Slides by Magnus Almgren /*************************************************************/ /* Module CPU */ /* External Declarations */ /*************************************************************/ /* OSP constant */ #define MAX_PAGE 16 /* max size of page tables */ /* OSP enumeration constants */ typedef enum { false, true } BOOL; /* the boolean data type */ typedef enum { running,ready,waiting,done } STATUS; /* types of status */ Slides by Magnus Almgren /* external type definitions */ typedef typedef typedef typedef struct struct struct struct page_entry_node PAGE_ENTRY; page_tbl_node PAGE_TBL; event_node EVENT; pcb_node PCB; /* external data structures */ struct page_entry_node { int frame_id; /* frame id holding this page BOOL valid; /* page in main memory : valid = true; not : false BOOL ref; /* set to true every time page is referenced AD int *hook; /* can hook up anything here }; struct page_tbl_node { PCB *pcb; /* PCB of the process in question PAGE_ENTRY page_entry[MAX_PAGE]; int *hook; /* can hook up anything here }; */ */ */ */ */ */ Slides by Magnus Almgren struct pcb_node { int pcb_id; /* PCB id */ int size; /* process size in bytes; assigned by SIMCORE */ int creation_time; /* assigned by SIMCORE */ int last_dispatch; /* last time the process was dispatched */ int last_cpuburst; /* length of the previous CPU burst */ int accumulated_cpu; /* accumulated CPU time */ PAGE_TBL *page_tbl; /* page table associated with the PCB */ STATUS status; /* status of process */ EVENT *event; /* event upon which process may be suspended */ int priority; /* user-defined priority; used for scheduling */ PCB *next; /* next PCB in whatever queue */ PCB *prev; /* previous PCB in whatever queue */ int *hook; /* can hook up anything here */ }; Slides by Magnus Almgren /* external variables */ extern PAGE_TBL *PTBR; /* page table base register */ extern int Quantum; /* global time quantum; contains the value entered at the beginning or changed at snapshot. Has no effect on timer interrupts, unless passed to set_timer() */ /* external routines */ extern extern extern extern prepage(/* pcb */); int start_cost(/* pcb */); set_timer(/* time_quantum */); int get_clock(); /* PCB *pcb; */ /* int time_quantum; */ Slides by Magnus Almgren /*******************************************************/ /* Module CPU */ /* Internal Routines */ /*******************************************************/ void cpu_init() { lots of smart code Here is mostly where you add your code for lab 2.1! } void dispatch() { ready to run? Important: Check the Intro to OSP for all necessary steps. } void insert_ready(pcb) PCB *pcb; { round robin } /* end of module */ Slides by Magnus Almgren Building a Linked List • Assume that a list of positions (x,y) shall be implemented in the C language using linked lists. Determine a suitable type declaration and write functions to do the following: – Create an empty list – Add an element (x,y) to a list – Search for an element (x,y) in a list and remove it if present in the list Head: Previous: Previous: Previous: Previous: Next: Next: Next: Next: x: x: x: x: Y: Y: Y: Y: Slides by Magnus Almgren Introduction to LL • Double-linked, circular list • Useful to have synonym for the last element too Head: Previous: Previous: Previous: Previous: Next: Next: Next: Next: x: x: x: x: Y: Y: Y: Y: tail: head->previous Slides by Magnus Almgren Lab 2.2 Memory Management • OSP Modules – MEMORY – PAGEINT • Carefully read OSP 1.4.3 and 1.5 !!! • Experiment with parameters to reduce the page faults CPU SIMCORE INTER DEVINT PAGEINT TIMEINT MEMORY DIALOG FILES DEVICES Slides by Magnus Almgren Memory Management • Algorithms to replace pages – FIFO – Optimal Algorithm (Imaginary) – LRU (Least Recently Used) • Good but difficult to implement • Second-Chance Algorithm, w/ reference bit Slides by Magnus Almgren 2nd Chance Clock Algorithm Page 337 in Operating System Concepts reference pages bits next victim reference pages bits 0 0 0 0 1 0 1 0 0 0 1 1 1 1 Slides by Magnus Almgren