System Software
Operating System
- show understanding of how an OS can maximise the use of resources
- describe the ways in which the user interface hides the complexities of the hardware from the user
- show understanding of processor management: multitasking
- show understanding of paging for memory management
Objectives
While watching, put down answers on worksheet
Computer startup sequence
BIOS -> Operating System -> User Application
Why Operating System?
- A computer usually has more than one task running at the same time
- Three fundamental resources in a computer:
- CPU
- Memory
- I/O devices
- Operating system provides means to share the resources among different tasks
I/O System
- Covers all data supplied and collected from CPU
- In the OS point of view, I/O not only covers input and output devices, but also storage or even network
- Compare to CPU or RAM, I/O operations are very slow
- e.g.
- Writing to (fast) SD card is about 100Mb/s
- while keypress is about 10 Byte/s
- vs CPU speed is measured in GHz
- OS will prevent CPU idle by switching tasks while waiting for the I/O operation complete
Structure of Linux OS
Outer layers required delegated access to inner layers.
OS hide the complexities of low level devices from user or programmer
Modes in Operating System
Kernel Mode
- Kernel means "Core"
- Runs all the time
- Controls the interface of the hardware
- Privilege access to certain part of memory and system functions
- Errors are not recoverable - need restart
User Mode
- Only runs when needed
- e.g. running an user application
- No ability to direct access hardware or memory
- Errors are likely recoverable
Modes of operation in OS
Time sharing
- One computer serves many user
Multitasking
- Multi process or task in one computer
Process Scheduling
- OS allocate CPU time across multiple tasks (note: the task here can be time-sharing or multitasking)
Process
- When a program is loaded into memory, it became a process
- Once the whole program is loaded, it became ready state
- Scheduler will determine when will the process can start to run, and it move to running state
- The process will keep running, until terminated, except:
- Need to wait for I/O
- Interrupt
- Scheduler determines to switch the process
State diagram for a process
Threads
- A process (application) can break into parts to run and it is called Threads
- Thread is similar to process, except that:
- Thread is lighter weight than process, i.e. easier for CPU to switch thread than process
- Usually each application will start as a process, within an application may spawn multiple threads
- For desktop computer with many cores, OS will spread the workload across CPUs
Types of scheduler
- High-level Scheduler (long-term scheduler)
- Decide which program should be loaded in memory or;
- Move to the secondary storage
- Low-level Scheduler (short-term)
- Switching of state of the process
Low-level scheduling algorithm
-
First come first served (FCFS) - process that is "ready" first will execute, until it became suspended.
- NO multi-tasking
- Simple but inefficient
- Usually only employed as part of a complex algorithm
-
Round-robin - All process will be assigned a time-slice, meaning that a fixed amount of time is given, when time is up, the next process will take the turn
- Can be described as a special case of FCFS or First-in-First-Out (FIFO)
- If jobs are more or less equally important, it is efficient
- No good if some jobs need urgent attention
- Maybe part of a complex algorithm in modern OS
Priority based
- Priority based - The processor time will be allocated to different process according to the priority of the process. The priority is computed by:
- User assigned priority (e.g. very long computation process may set as low priority)
- Estimated run-time remaining
- Length of time in ready queue
- Resource of the process required and etc.
Memory management
- Allows sharing computer memory across process
- Each process will have their own memory space
- Process need not to aware the physical memory location they are using
- Preventing process to access memory allocated to other process
- Protecting memory space from OS
Paged memory management
- Programs are divided into fixed-sized units called "page"
- Main memory is also divided into "pages" of the same size, called "page frames"
- OS will manage the mapping between logical address to physical address using a page table
- Logical address is the address used in process
Virtual memory
- Virtual memory is a paging mechanism allows processes to use more memory than the physical memory (RAM, for instance)
- Allows some pages of the process to store in secondary storage
- OS will move the page from secondary storage to RAM and swap out some other pages if needed
- Since secondary storage are very slow compare to RAM, there is quite a significant overhead when swapping
Virtual Memory (CONT'D)
- Segmentation: Large process (program) is divided into segments when loading into main memory, segments can be different size
- Paging: Large process is divided into pages, page are same size
- Disk Thrashing: When RAM is fully utilized and swapping between virtual to physical memory very often, thus the backup storage (e.g. HDD) worked excessively
- Computer became not responsive
VIRTUAL Machine
- show understanding of the concept of a virtual machine
- give examples of the role of virtual machines
- show understanding of the benefits and limitations of virtual machines
Objectives
Virtual Machine
- Two types of virtual machine
- System virtual machine - emulates a complete computer system, each VM will have their own OS
- Process virtual machine - single application process under the host OS, the program will be running under VM and platform independent. Examples:
- Java VM (running Java applications)
- Microsoft CLR (running .Net applications)
System Virtual Machine
System Virtual Machine
- Host system: the system that startup the virtual machines
- Guest systems: the systems running on the virtual machines
- Each VM will "think" they have a full-function computer, with their dedicated CPU, RAM, HDD and even I/Os
- The guest systems can have different OS running on it. e.g. A Macintosh host can have guests in Win7, win10, linux all at the same time
- Modern CPU have virtualisation mode, application running on virtual machines are almost identical to running on host machines in terms of performance
Java VM
How JAVA different from compiler
C
JAVA
- Compiler
- Starts from Source code
- Ends with executable machine instructions
- The resultant code is machine dependent
- Java Compile
- Starts from source code
- Ends with Java Byte Code
- Imagine the byte code is a machine instruction for a non-existing machine..... which is Java VM
- When the host machine tries to run the java byte code, it may:
- Interpret by Java VM
- Just-in-time (JIT) compile to TRUE machine code of that particular device
How JAVA different from compiler
Translation Software
Objectives
- show understanding of how an interpreter can execute programs without producing a translated version
- show understanding of the various stages in the compilation of a program: lexical analysis, syntax analysis, code generation and optimisation
- show understanding of how the grammar of a language can be expressed using syntax diagrams or Backus-Naur Form (BNF) notation
- show understanding of how Reverse Polish Notation (RPN) can be used to carry out the
evaluation of expressions
Stages in Translation software
Lexical Analysis
Front end
Backend
Machine code
Optimisation
(Intermediate) Code Generation
Syntax Analysis
Lexical analysis
- Converting sequence of characters to sequence of tokens
- E.g.
h = aside**2 + bside**2
will form 9 tokens:
h, =, aside, **, 2, +, bside, **, 2 - Also check for simple syntax error, e.g. if a variable name starts with a number, it can't pass the tokenizer
-
Symbol table will be built to keep variables in programs:
- Name
- Data type
- Memory allocation
Tokenizer, RPN and shunting yard simulation:
https://replit.com/@andytsuiacm/ExpressionEvaluation#main.py
Syntax analysis
- Analyze the program constructs
- Record the results in syntax tree
Code generation
- Intermediate code is generated
Optimisation
- Code will be edited aiming to improve efficiency
Shunting Yard
- If Token is Operand (identifier/number), directly move to RPN
- If Token is operator
- If Top of stack is higher precedence, pop and push the token to stack
- If empty, push to stack
- If Open Bracket "(", push to stack
- If Close Bracket ")", pop until "(", discard "(" and ")" while popping
Evaluating RPN
- If Token is Number / Identifier, push to stack
- If token is operator, pop the first two from stack and calculate, push the result to stack
- If no more token, the end result is top of stack.
Interpreter
- Code is analyzed and generated for each line
- Each line is executed after intermediate code is generated
- i.e. usually no / less optimisation can be performed
- Compare to compiled code, interpreter language are more flexible, but also making syntax more difficult to check
Representing Grammar / Syntax
- Syntax Diagram
- Backus Naur Form (BNF) - A method to describe grammar rule in a programming language
BNF exercises
Given the following definitions:
digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
sign ::= + | -
Define:
- Unsigned Integer
- Signed Integer
- Decimal Number
[CSAL] System Software
By Andy tsui
[CSAL] System Software
- 313