OPERATING SYSTEMS Operating system • System software that manages hardware/software of a device • Functions ○ Provides a user interface ○ Resource management - allocates CPU, memory and devices to ○ File management - organises files/folders/storage ○ Interrupt handling - responds to interrupts and events ○ Security - manages user accounts permissions and protection ○ Provides a platform for applications to run - loads programs into memory, gives access to hardware/software, etc. ○ Provide utilities - maintenance tools ○ Scheduling Memory management • The OS manages how RAM is allocated and used • Paging ○ Memory divided into fixed-size pages ○ As pages are the same size, it is easier to move them between RAM and secondary storage, making it good for virtual storage ○ Prevents external fragmentation but causes internal fragmentation (unused space within pages) • Segmentation ○ Divides memory into logical variable-sized segments ○ e.g. code, data, stack ○ Logical segments means logical segments can be easily shared, different permissions can be assigned to different segments and memory tracking and debugging is easier ○ External fragmentation (when memory no longer needed variable size empty gaps are left behind) but no internal fragmentation because segments fit the required sized • Virtual memory ○ When there are too many jobs loaded into memory ○ The operating system may swap pages of temporarily inactive jobs out to disk ○ Using secondary storage as an extension of memory to make room for the next job ○ This slows down performance as the operating system has to spend time swapping pages in and out of secondary storage Interrupts • An interrupt is a signal that causes the CPU to pause its current task and deal with something else requiring attention • e.g. input from keyboard/mouse, errors, timer events, completion of I/O operations • Allows CPU to respond quickly to important events, avoids constant checking • Interrupt service routine (ISR) - program executed when an interrupt is received ○ CPU checks for interrupts at the end of each FDE cycle ○ CPU receives an interrupt request ○ Pushes value of PC and other register values onto the system stack ○ Memory address of the ISR is loaded onto the PC ○ ISR is fetched, decoded and executed ○ Once the ISR is finished, old register values are popped from the stack and previous process resumes Scheduling • Multiple tasks need to be run with the same limited CPU time, so OS scheduling algorithms are used to decide which tasks run when • First come first served ○ Tasks run in order of arrival until finished ○ Simple and easy, minimal time switching tasks ○ But shorter tasks can get trapped behind longer ones • Round robin ○ Each task gets a fixed time slice, unfinished tasks go to back of queue ○ Fair, responsive, prevents short tasks trapped behind long ones ○ Too much switching can create overhead, no priorities • Shortest job first ○ Shortest total job time runs first - no pre-emption so once a long process starts, a new shorter task has to wait ○ Minimises average waiting time, fast response for short tasks ○ But long jobs may starve, no pre-emption, time taken for prediction of length which may be inaccurate • Shortest remaining time ○ Pre-emptive version of shortest job first - task with least remaining time runs ○ Minimises average waiting time even more than shortest job first, highly responsive for short tasks ○ Severe starvation of long tasks especially if there is a constant stream of shorter tasks, task switching overhead, time for prediction which may be inaccurate • Multi-level feedback queue ○ Uses multiple queues of different priorities, higher priority queues run first ○ Priority is based on interactivity, how long it is, how long it has been waiting, importance assigned by OS/user ○ Good responsiveness especially interactive tasks, prevents starvation of longer tasks, flexible, prioritises important tasks ○ Complex, time taken to move tasks between queues and switch tasks, lower priority tasks my starve if high priority tasks keep arriving Types of operating systems • Distributed ○ Spreads load across multiple computers ○ Higher performance, failure on one machine won't stop system ○ Complex management, depends on network communication ○ A single job is split into several tasks which are run by different computers, coordinated by the operating system ○ Used in cloud storage, large databases, multiplayer games • Embedded ○ Designed for specific devices, small and lightweight ○ Limited RAM so no complex memory management, low power usage, reliable ○ Limited functionality, hard to update/change ○ Used in cars, washing machines, cameras • Multi-tasking ○ Runs multiple tasks apparently simultaneously using scheduling ○ Better responsiveness can run multiple applications together ○ Too many tasks can reduce performance, scheduling overhead • Multi-user ○ Allows multiple users to use the computer system simultaneously ○ OS schedules CPU time between users, allocates memory/storage, manages permissions/security, keeps users processes separate ○ Allows many users to share one powerful system, storage, printers, network resources ○ Cheaper than separate systems for everyone ○ But more security management needed, heavy usage by one user can affect others ○ Used for school/business servers, cloud systems • Real time ○ Guarantees responses within a strict time limit, using very high priority scheduling ○ So there is predictable timing ○ Used for airbags, aircraft control, pacemakers BIOS (Basic input output system) • Firmware stored in ROM that starts a computer and checks hardware before OS loads • Process ○ Power On - electricity is supplied to the computer's components and CPU starts working ○ Tests hardware functionality with POST (Power on self test) ○ Searches for a boot device ○ If successful, loads operating system by running bootloader ○ Control of the computer is passed to operating system Device drivers • Software that allows the OS to communicate with specific hardware devices • Drivers translate between OS and hardware • Needed because different hardware uses different instructions Virtual machine • Software that emulates the behaviour of a real computer using a computer • Behaves like a separate computer with its own OS, memory and storage • Used for software testing, running old incompatible software, running multiple different operating systems • Isolation/safety - crashes/malware in virtual machine don't affect main system, efficient hardware use - only one computer is needed instead of multiple • But slower than real hardware, uses a lot of resources, complex APPLICATIONS GENERATION Applications • Programs designed to perform tasks for users • Word processors - create/edit documents • Database software - store/manage data • Web browser - access websites/internet • Graphics software - create/edit images • Spreadsheet - calculations/data analysis Utility software • Maintains, manages and protects a computer system • Disk defragmentation ○ Reorganises fragmented file parts into contiguous block on a disk ○ Reduces read/write head movement so access is faster • File management ○ Allows files to be created, renamed, moved, copied, deleted, organised in folders • Device drivers ○ Translates between OS commands and hardware specific commands • System cleanup ○ Removes temporary files, cache, logs and unnecessary data ○ Frees storage space and may improve performance • Security ○ Detects and prevents malware and unauthorised access • Backup software ○ Creates copies of files/data on separate storage • Compression software • Translators Open vs closed source software • Open source ○ Source code is available for anyone to use, modify and distribute ○ Benefits § Users can customise software § Community can make fixes and improvements, so faster development § Mostly free to use § Not dependent on a single supplier, if they go out of business, the community can maintain software ○ Drawbacks § Lack of dedicated support § Security risks - hackers can analyse source code and identify vulnerabilities • Closed source ○ Source code is private and controlled by the creator/company ○ Benefits § Professional support for users § Usually polished and easy to use § Better profit for companies ○ Drawbacks § Usually paid § Code cannot be modified § Company must handle development and support internally § Dependent on supplier - prices and whether they can continue supporting it Compilers vs interpreters • Translators are needed because CPUs only understand machine code • Compilers ○ Translates the entire program at once ○ Produces executable object code which can be saved and run whenever without a compiler ○ Advantages § Executable runs fast § Can be distributed and executed without need for compiler present § Code more secure as it cannot be read without a lot of reverse engineering § Useful for frequently run programs, distributed/sold software § Can optimise code ○ Disadvantages § Compilation takes longer than interpreter § Must recompile after changes § Only works on specific hardware as it translates to machine code • Interpreters ○ Translates and executes line by line ○ If it finds an error, it reports it and stops executing the program ○ Advantages § Good for debugging as it stops and reports errors the moment it occurs § Works on any hardware with the correct interpreter § Lower memory usage ○ Disadvantages § Slower execution time § Interpreter needed every time to run § Can't optimise code as it doesn't see the whole program Assemblers • Translates assembly code into machine code • Low-level, almost 1:1 translation • Assembly code used in embedded systems and real-time systems (fast and simple) Stages of compilation • Lexical analysis ○ Source code is taken as input ○ Unnecessary elements are removed, e.g. white space and comments ○ Turns code into tokens - meaningful pieces of code e.g. keywords (if, while), identifiers, operators and numbers ○ Creates a token table ○ Output is a stream of tokens passed to the syntax analysis stage • Syntax analysis ○ Checks that the stream of tokens follows the language grammar ○ If a syntax error is found, the compiler checks the whole program for errors and reports them all, then stops ○ At the same time, an abstract syntax tree is built to represent program structure in a form the compiler can easily process ○ The abstract syntax tree is fed to the code optimisation and generation stage • Code optimisation and generation ○ Object code is generated from the abstract syntax tree ○ Code optimisation reduces execution time, by spotting redundant code or simplifying code ○ Compiler checks tokens whether the program can actually be turned into valid object/machine code for the targeted hardware § e.g. variable references exist, data types compatible, enough registers/memory can be allocated, operations are valid for the hardware Libraries • A library is a collection of pre-written code that programmers can use to perform specific tasks • Benefits ○ Saves development time ○ Avoids rewriting common code ○ Usually well-tested and reliable • Drawbacks ○ May include unnecessary features and code, decreasing performance ○ Can't customise ○ Potential security vulnerabilities if libraries are poorly written ○ Dependent on libraries, if libraries are no longer supported, code will eventually break • During compilation, libraries can't be translated directly into machine code, so references to them are left instead • Linking after compilation combine object code with libraries into a single executable file ○ Static - library code copied into executable ○ Dynamic - library loaded separately at runtime • Loader loads the executable into RAM for execution SOFTWARE DEVELOPMENT METHODOLOGIES Waterfall • Gather all requirements up front • Design everything carefully • Focusing on delivering initial requirements • Then build it all • Advantages ○ Simple and easy to manage ○ Easy to track progress ○ Budget and timeline predictability ○ Client availability is not needed • Disadvantages ○ Difficult to change requirements later ○ Problems may be discovered late ○ Users see working software very late Agile/Extreme(XP) • Similar to spiral but focus on releasing often • Continuous releases of valuable software • Expects customer requirements to change • Iteratively design, build, evaluate and release • XP ○ Focus on high quality code released frequently ○ Regular automated tests ○ Pair programming and code reviews • Advantages ○ Flexible to changing requirements ○ Problems found early ○ Frequent working versions produced • Disadvantages ○ Harder to predict deadlines/cost ○ Requires constant communication ○ Documentation weaker as more focus on producing software Spiral • Gather some requirements up front • Iteratively design, build, evaluate prototypes/features • Focus on managing/reducing risk • Client feedback each iteration • Advantages ○ Risks identified early ○ Flexible to changing requirements ○ Good for large and complex systems • Disadvantages ○ Expensive and time consuming ○ Complex management ○ Requires constant client availability Rapid application development (RAD) • Focus on prototypes • Regular feedback from clients to refine objectives • Advantages ○ Very fast development ○ Users can see working software earlier ○ Easy to adapt to changing requirements ○ Continuous feedback improves usability • Disadvantages ○ May sacrifice code quality/design ○ Hard for very large and complex systems ○ Requires constant user involvement ○ Documentation may be weaker TYPES OF PROGRAMMING LANGUAGE Programming paradigms • Procedural ○ Programs are ordered into procedures and functions ○ Follow step by step instructions ○ Strengths § Simple for smaller programs § Easy to trace execution flow § Efficient execution ○ Weaknesses § Harder to manage very large systems § Poorer modularity as data and functions are split up • OOP • Low-level ○ Languages close to machine code e.g. assembly ○ Strengths § Fast and efficient § Direct hardware control ○ Weaknesses § Difficult to write and debug § Hardware dependent so less portable Assembly language • Purpose ○ Easier for humans to read/write than machine code ○ Very fast and efficient (direct access to hardware) ○ Can access registers and memory directly • ADD/SUB - adds/subtracts value in accumulator ○ LDA num1 ADD num2 OUT • STA - stores accumulator value into memory • LDA - loads data into accumulator • DAT - data location in memory ○ LDA num STA result num DAT 1 result DAT • BRA - branch/jump always • BRZ - branch if value in accumulator is zero • BRP - branch if value in accumulator is positive or zero • INP - input value from user • OUT - outputs accumulator value • HLT - ends program (put in at the end of every program) Types of addressing • Instructions consist of an opcode (the instruction to perform) and an operand (the data the instruction operates on) • Immediate ○ Operand contains the actual value ○ Very fast as no memory lookup is needed ○ Value is fixed so less flexible, if a value is reused it has to be typed out again • Direct ○ Operand contains the memory address of the data ○ Fast address calculation, value does not have to be rewritten all out • Indirect ○ Operand contains the register or memory address containing the address of the data ○ Useful for pointers and dynamic data structures, when the address changes ○ Slower due to extra memory address lookup time • Indexed ○ The operand is added to the index register to get the address of the data to add ○ Efficient for arrays/lists and looping through data ○ More complex address calculation, may require extra processing time Object-oriented programming • Classes • Objects • Methods • Attributes • Encapsulation • Inheritance • Polymorphism