CM10195 Computer Systems Architecture 2 Revision Notes

22
CM10195 Systems Architecture 2 Introduction to Operating Systems Lecture 1 An operating system is often called the kernel it is a bridge between hardware (cpu, memory & devices) and software applications. The OS will manage the computer’s resources (i.e the hardware & software) to make sure (as they have limited usage) they are shared usefully, as well as giving the programmer a usable interface to access them. Relating to the Von Neumann architecture, the programmer does not want to have to know a machines hardware specifics when writing a program they use a common OS which then interacts with the hardware so it can be used on any machine with that operating system. This also provides a common standard interface, making programming applications easier, and the operating system can make full use of the hardware than if a coder were to handle this individually. Warning; The end user GUI that we see onscreen is not part of the operating system it is just a program that uses the OS! When GUI’s were tied to the OS, a bug in a program running would cause the whole os to crash wiping out the machine entirely, and allowed an easy breach of security as this method circumvents the protection the OS provides. As modern computers are fairly compact (mobile phones, netbooks, even laptops), they have minimal resources and so there are strict limitations on usage such as memory and cpu, as well as energy consumption for those machines running on battery power. The OS will manage these resources to limit their usage. These resources are also protected by the operating system, in situations such as security one program corrupting another, authorisation ensuring only the resources allowed for a program are used, authentication ensuring the program is authorised to use the resources it claims to need, and protection from your own mistakes. Resources also need to meet certain criteria, such as responsiveness processing computations as quickly as possible, real time generate an (almost) immediate response to an input, and security preventing unauthorised access/modification to them. The layers of a computer; 1

description

Course notes for the CM10195 Systems Architecture 2 course at the University of Bath.

Transcript of CM10195 Computer Systems Architecture 2 Revision Notes

Page 1: CM10195 Computer Systems Architecture 2 Revision Notes

CM10195 ­ Systems Architecture 2Introduction to Operating Systems

Lecture 1

An operating system is often called the kernel ­ it is a bridge between hardware (cpu, memory &devices) and software applications. The OS will manage the computer’s resources (i.e thehardware & software) to make sure (as they have limited usage) they are shared usefully, aswell as giving the programmer a usable interface to access them. Relating to the Von Neumannarchitecture, the programmer does not want to have to know a machines hardware specificswhen writing a program ­ they use a common OS which then interacts with the hardware so itcan be used on any machine with that operating system. This also provides a common standardinterface, making programming applications easier, and the operating system can make full useof the hardware than if a coder were to handle this individually.

Warning; The end user GUI that we see onscreen is not part of the operating system ­ it is just aprogram that uses the OS! When GUI’s were tied to the OS, a bug in a program running wouldcause the whole os to crash wiping out the machine entirely, and allowed an easy breach ofsecurity as this method circumvents the protection the OS provides.

As modern computers are fairly compact (mobile phones, netbooks, even laptops), they haveminimal resources and so there are strict limitations on usage ­ such as memory and cpu, aswell as energy consumption for those machines running on battery power. The OS will managethese resources to limit their usage.

These resources are also protected by the operating system, in situations such as security ­ oneprogram corrupting another, authorisation ­ ensuring only the resources allowed for a programare used, authentication ­ ensuring the program is authorised to use the resources it claims toneed, and protection from your own mistakes.

Resources also need to meet certain criteria, such as responsiveness ­ processingcomputations as quickly as possible, real time ­ generate an (almost) immediate response to aninput, and security ­ preventing unauthorised access/modification to them.

The layers of a computer;

1

Page 2: CM10195 Computer Systems Architecture 2 Revision Notes

The OS should be lightweight and efficient as to not detract from the cpu usage needed by theprograms that are being run by the user. It should also be flexible as to not get in the way of theprogrammer.

Lecture 2

As operating systems are almost as old as computers, people tend to get confused betweenthem. As an OS is just a program, an OS can run on any hardware, and vice versa ­ a piece ofhardware can run any OS. Current Mac hardware is the same as PC hardware, so you can runWindows, MacOS, Linux and many other OS’s.

Well designed operating systems are hardware­independent apart from the elements that needto actually physically access the hardware. This makes them fairly portable from onearchitecture to another. This historically did not happen, as OS’s were tied to hardware sointricately that portability was difficult.

Phone operating systems are marketed for their GUI rather than their operating system, andphone vendors tend to make it hard for users to access the actual operating systems on thephones themselves. Operating system owners in general (Microsoft, Apple) tend to market theiroperating systems as their GUI’s, but they are not the same.

The term PC refers to Personal Computer, and refers to the hardware, rather than the operatingsystem being run on it ­ Macs are PC’s too.

An operating system should be chosen with several factors in mind; Ease of use, efficiency,security, stability, suitability for tasks, etc. There are many operating systems in existence, andembedded operating systems far outnumber PC operating systems.

Lecture 3

Historically, computers did not have operating systems, as each programmer would writeprograms for the individual computer they were using ­ so there was not portability. This alsomeant there was a lot of repeated code between programs ­ programs were created on a punchcard along with the data (program and data collectively called a job), which were then given to anoperator with scheduling turnaround possibly days, and the programmer could only then fix anybugs.

Useful programs were created ­ common functions and tools ­ to minimise repeated code,making program management a lot easier. However the large computer was always needed tobe kept busy, as when it was idle it was wasting time and money. To counteract this, operatorsput the programs on to a fast medium (magnetic tape) and the computer loaded and ran themas fast as possible. This is the first instance of spooling, a queue (or buffer) of jobs to be done,and was the first addressal of the difference between human and computer speeds. Spoolingwas also used on output for printing.

Spooling would later become automated ­ a monitor will load and run programs and store results

2

Page 3: CM10195 Computer Systems Architecture 2 Revision Notes

somewhere suitable. This would be directed by a job control language. The IBM job controllanguage was called JCL allowed different classes of job, scheduling of jobs and automaticcharging for use of the machine from these jobs. It also allowed batch processing ­ severalprograms to be collected and loaded at the same time for efficiency ­ reducing loading andunloading overheads.

When the monitor loaded more than one program at once, if program 1 was writing to tape, thenprogram 2 could use the CPU in the meantime. However if something goes wrong with oneprogram, not only would the monitor become corrupted, so would the other program. Thereforesome kind of protection was needed, as well as means of stopping infinitely running programswas needed.

Lecture 4

The monitor code loads the program, then just jumps to the program code so the machine nowruns the program. The flow of execution goes as follows; Monitor starts program, program 1runs until tape is needed, monitor sets up tape, monitor decided to run another program... etc.There is a single stream of control jumping between the monitor and several programs ­ so themonitor is not running when a program is running ­ this is called multitasking, even though thereis only ever one thing running. Multitasking improves efficiency, as the monitor can run anotherprogram when a program is waiting ­e.g. for a slow peripheral.

To decide which programs should be run when, the monitor uses scheduling. Which programrun when depends on many criteria; how long a program has been running, a programs priority,whether a program is likely to need to use of the CPU soon or later, how much the owner haspaid, etc. Scheduling algorithms have evolved from just running each program from start tofinish, to a more sophisticated model that factors in the criteria. However the more time themonitor spends deciding which program to run, the less time the programs can actually run, andso there is a trade off between fast and fair scheduling.

The program should also be well written, as if it goes into an infinite loop the stream of control isnever passed back to the monitor and the whole computer is jammed. To stop runawayprograms, a hardware clock/timer is used to regularly send interrupts. When the interrupt istaken, the flow of control returns to the monitor so it can decide what to do next which caninclude resuming the program that was interrupted, kill the program, or switch to anotherprogram. Setting a clock to regularly send interrupts is called preemptive scheduling, whichenables timesharing. Timesharing is the sharing of the CPU time with several differentprograms, so it seems they are running simultaneously, all mediated by the monitor.

Interrupts allowed the user to interact with the computer directly by the use of terminals, onlyrunning a program when a command from the terminal came in. This means that no CPUresources are used until they are needed. Frequent interrupts mean other programs can get aslice of CPU time more often, so more programs can appear to be running simultaneously, andan interactive program can appear to be dedicated to the user whilst the CPU can run otherthings. However, frequent interrupts also means the stream of control keeps going back to the

3

Page 4: CM10195 Computer Systems Architecture 2 Revision Notes

monitor so it can decide what to do with the interrupt, meaning it is using up CPU time theprograms could be using. This is therefore a tradeoff between frequent interrupts meaning goodinteractive behaviour, and rare interrupts meaning good computing behaviour. Interactiveprograms usually get high priority but small CPU time slices, and compute­intensive programsget low priority but long time slices.

Thrashing is when an operating system spends more time on deciding what to do than actuallydoing useful work, and many early OS’s had a problem with thrashing. To protect the programsand monitor from each other, hardware mechanisms were used, which had to be fast andunobtrusive. For example, operations like accessing tape or a printer should only be used by themonitor, and not be accessible by a program. To solve this, the CPU instructions were split intotwo categories; unprivileged ­ any program can access these (e.g addition, jumps), andprivileged ­ only certain privileged programs such as the monitor can access these (e.gperipheral access). The processor can then run in either privileged (kernel) mode for systemoperations, or unprivileged (user) mode for normal computation. Some processor architectureshave more than two privilege levels ­ also called privilege rings. Unprivileged programs that try toexecute a privileged operation causes an interrupt ­ or a system trap ­ and sets the processorinto privileged mode, and then returns the stream of control back to the monitor which thendecided what to do ­ like disallowing the operation or killing the program.

How the modes work; system starts in kernel (privileged) mode, monitor decided which programto schedule, uses a special jump & drop privilege instruction to run the program, the programruns unprivileged, the program finishes or decided it needs a system resource, the programexecutes a syscall instruction (calls the monitor) which enables privileged mode so the monitorregains control, and then the monitor decides what to do next. If the program does not execute asyscall, the program will be interrupted at some point anyway. The program can’t enter privilegedmode as every transition to privileged mode is tied by the hardware to a jump to the monitor.However jumping between kernel and user mode is a time­consuming operation due tooverheads, which is why we don’t want timer interrupts too often.

Forcing access to the hardware via the monitor increases security of other system resourceslike files or network, as the OS can enforce policy on access and ensure fair distribution of

4

Page 5: CM10195 Computer Systems Architecture 2 Revision Notes

shared resources.

Lecture 5

Memory protection stops a program from reading/writing the memory used by another programor by the monitor/operating system. The memory management unit (MMU) is a piece ofhardware that stores a series of flags which say if the program currently running can read orwrite (2 separate flags) a given area of memory. The setting of these flags (which are effectivelyjust one bit ­ a 1 or a 0) is a privileged operation, and if an unprivileged program tries to read/writeto an area of memory it is not allowed to, it is the MMU which raises an interrupt, and the monitortakes control. The area of memory that can be flagged is called a page ­ 4096 bytes. There isalso an executable flag ­ can the program running execute code from this memory address. Thistable of flags is part of the programs state, and must be saved and restored when the programstops and is rescheduled. As every read/write must be checked against the flags, the checkingprocess must be fast.

Some of the requirements of a monitor (operating system) are therefore; resource management­ scheduling CPU time, disk, network, etc. , Protection ­ memory, files, network data etc,Efficiency ­ time, size, energy.

Lecture 6

A process is an executable object, it’s data, and the associated information the operating systemneeds to run it. A process is a program in execution. A single program may have more than oneprocess, and is structured by process. The operating system has to keep a lot of informationabout a process, for example where in memory it’s stored, how much time it is allocated/used,it’s flags from the MMU, etc.

A process can be in several states, the five main states being:

1. New ­ a newly created process2. Running ­ currently being executed on the CPU3. Ready ­ ready to run but another process is currently using the CPU4. Blocked ­ waiting for an event or resources to become available5. Exit ­ a process that has finished

The operating system has a list of these processes and their states, so the scheduling themonitor has to do can be thought of just decided which of the processes to move from one stateto another.

Processes in Unix are arranged in tree, allowing a large bunch of processes to be controlled asa group. Each group within a tree has a session leader, which if killed would kill all the processesin the group.

The process states goes from new, to ready, which can cycle between running, block and ready,before becoming in the exit state. Here is a finite state machine describing the states of a

5

Page 6: CM10195 Computer Systems Architecture 2 Revision Notes

process;

Typical transition; monitor schedules a process on the ready list, process is dispatched, processcan relinquish/be interrupted and goes in to the ready state, or sleeps/is blocked and goes intothe blocked state, or is released into the access state. Early os’s relied on a running programwritten to relinquish control every once in awhile ­ called co­operative multitasking. The new andexit states only happen once per process.

Some other information a process must collect are; user ID’s, a priority, statistics like CPU &memory used, it’s state, and a list of resources used ­ particularly the memory for the code andit’s data. This collection of data is called the process control block ­ PCB.

To pause and restart a process requires the saving and restoring of the process state, stored inthe PCB. PCB’s of the various processes are stored as a linked list to allow for flexibility innumbering and ordering.

In the new state, we have to do several things before the process can move on to the readystate; allocate & create PCB structure, find a free PID, determine & allocate necessaryresources, determine initial priority & then insert the PCB into the relevant list. Although a newprocess can be spawned/forked from another process, it must ask the operating system toactually create the process using a syscall with specifications of the new process, and theoriginal calling process will become the parent to this new process.

On switch­on of the computer, the processor jumps to a location in the non­volatile memorywhere there is an ancestor process called init, with PID 1, which serves to create all otherprocesses ­ this is bootstrapping. It is complicated as it has to detect and determine hardware toinitialize it, set up all the data structures the operating system needs and start serviceprocessing running, all before looking at what the user wants.

Lecture 7

6

Page 7: CM10195 Computer Systems Architecture 2 Revision Notes

Scheduling is still a very difficult problem for the operating system and still has not been solved ­they have to do many things such as give each process a fair slice of CPU, interactiveprocesses react to humans in good time, give as much time possible to compute­heavyprocesses etc.. and all very quickly.

Scheduling is usually subdivided into three classes; short term ­ use of the cpu, medium term ­which processes to keep/load into memory, long term ­ deciding which processes to load intothe scheduling system. So, long­term scheduler determines processes for medium­term, whichdetermines processes for short­term which determines the processes to be run.

In short­term scheduling, we need to quantify which process to run, using CPU cycles used,memory/disk/network used etc. as well as throughput (jobs finished in a given time), turnaround,real­time, money paid etc.

scheduling algorithms;

Run until completion ­ First In First Out, good for large amounts of computation & nooverheads from multitasking, poor interaction with other hardware and no interactivity.not suitable for modern machines

Shortest job first ­ no multi tasking, good throughput, long jobs suffer, difficult toestimate time completion

Run until completion + cooperative multitasking ­ non­preemptive, weakmultitasking, poor utilisation of hardware, poor interactivity, easy for a process tostarve other processes

Preemptive Round Robin ­ each process gets a fixed time slice, multitasking,utilisation of software, no starvation, interactive process given same time ascompute­heavy processes, not good for interactivity or real­time.

Round robin ­ suited to systems with equal importance processes (e.g networkrouters splitting network capacity). Time slices awarded in circular order, nonstarvatio

Shortest remaining time ­ next process chosen by shortest remaining estimatedtime, preemptive, hard to estimate, good for short jobs, starvation.

Least completed next ­ process consumed the least CPU time goes next, allprocesses make equal progress, interactive processes get good attention due tosmall usage of CPU, starvation of long jobs

We can determine if a process is interactive by high I/O per compute, if low then the process iscompute­heavy. Priorities can also change throughout processing, they can be; static ­unchanging which is simple but resistant to change, dynamic ­ changes in response to the load,or purchased ­ more money = higher priority.

7

Page 8: CM10195 Computer Systems Architecture 2 Revision Notes

Lecture 8

Highest response ratio next ­ variant of shortest remaining time, takes time processhas been waiting since it’s last time slice into account ­ priority = time so far / CPU sofar. time slices repeated until priority drops below another process, avoids starvation,new jobs get immediate attention, critical shorter jobs can be starved. Requires lots ofreevaluation of priorities, so lots of scheduling overhead.

Multilevel feedback queueing ­ multiple FIFO queues with different priorities fromlow to high, new process starts at the end of the highest queue, a process moves tothe end of the next queue level down when it’s finished it’s time slice, or the the end ofthe same queue if it relinquishes voluntarily, or at the end of a higher queue if it blocksfor I/O. Processes in highest level priority queue executed first. New short processesget priority over older compute­heavy processes ­ starvation

Traditional Unx scheduling ­ based on a timer interrupting every 1/60th of a second,wth priority computer from the CPU use of each process ­ base priority + (cpu timeused/2). CPU time halved every second so only applies to recent useage, prioritiesre­computed every second. Smallest priority was chosen next, with same prioritytreated as round robin.

Fair share scheduling ­ each user of the machine rather than each process gets afair share of CPU time.

When we want to schedule multiple resources (not just CPU) the process calls the kernel and ismarked as blocked until the requested resource has arrived ­ sometimes the delay betweenrequest and reply from process to kernel and back is infinite.

Say process one wants access to memory disk 1 to copy data to disk 2, and process two wantsaccess to memory disk 2 to copy data to disk 1. The os grants p1 exclusive access to disk 1 &p2 exclusive access to d2. Then when either p1 or p2 asks the os for access to the other disk,they are both blocked due to the other process having exclusive access. This is called deadlock,& can happen on any kind of shared resource requiring exclusive access with more than 2processes.

A formal definition of deadlock ­ a set of processes D is deadlock if each process Pi in D isblocked on some event ei and event ei can only be caused by some process in D. Deadlock isonly possible if there is; mutual exclusion ­ one process can use a resource at one time,hold­and­wait ­ process holds a resource while waiting for other resources, no preemption ­no resource can be forcibly removed from a process holding it. A deadlock is possible if thereare these three factors, but will only actually happen if there is a circular wait ­ circular chain ofprocess waiting on each other to release a resource that is needed by the next in the circle.

8

Page 9: CM10195 Computer Systems Architecture 2 Revision Notes

Indefinite postponement happens when a process never manages to get all of the resources itneeds ­ could happen of hold­and­wait were to be dropped to prevent deadlock.

We can either prevent deadlock by preventing one of the conditions, or detect and breakdeadlock by destroying one of the conditions when a deadlock occurs. Prevention is furtherrefined as constraining resource allocation to prevent hold­and­wait.

Lecture 9

We can prevent deadlock by;

Breaking mutual exclusion ­ although hard to break, we can make sure resourcesare not held on to for longer than needed.

Breaking hold­and­wait ­ can require a process not to hold any resources if it evergets blocked on another resource. Could also require a process to request allresources necessary before going ahead. This prevents the process doing usefulwork while one resource is needed but the other is needed yet, and process may noteven know resources needed from the start.

Breaking no preemption ­ possible only for resources whose state can be savedand restored easily.

Breaking circular waits ­ putting an ordering on resources, so a process thatrequests R in the order can then only request a resource after R in the order. This isinefficient and causes unnecessary holding of resources, and is not always possiblefor resource to know what to request ­ will have to drop resource later in order andre­request from earlier in the order.

Deadlock avoidance does not stop a specific condition but doesn’t do anything that couldpotentially lead to deadlock, but this is difficult as with each request it has to be checked that itcouldn’t cause deadlock in the future, which can be hard to predict.

The banker’s algorithm makes two tests; feasibility ­ see if a request is possible i.e. the totalallocated resource doesn’t exceed the actual resource, and safety ­ see if a request cannot leadto deadlock i.e. there is at least one sequence of resource allocations and releases by which allprocesses can complete their computation. An unsafe request will not be granted by the OS, noran unfeasible request. Therefore there must exist at least on path to completion for all processesfor a request to be granted. However, there are several problems with this algorithm; there mustbe a fixed number of resources to allocate, fixed number of processes, processes must knowtheir maximum needs in advance etc.

Deadlock detection is different again, allowing deadlock but relying on noticing & breaking them,spotting when the circular wait happens. One method of this uses resource request andallocation graphs (RRAG), by simply finding circuits in these graphs using graph reduction ­remove all available resource request links then remove all links from allocated units of resource

9

Page 10: CM10195 Computer Systems Architecture 2 Revision Notes

to the process. If we can reduces a RRAG by all process links, there is no deadlock, else wehave isolated the graph to just the deadlocking processes.

Lecture 10

Then we just have to break the deadlock by either; killing one or more of the processes, preemptthe blocking resources (better), add resources. Deadlock avoidance is rarely used as preventionis easier and cheaper. The more general solution is virtualization, where the operating systempretends each process has exclusive access to a resource ­ e.g spooling; instead of writing to aprinter, the process writes to a tape which is then later written to the printer. This means there isno possibility of deadlock.

Suppose a low priority process holds a resource that a higher priority process requests ­ thehigher priority process is prevented from running by the lower priority one ­ this is called priorityinversion. This can be prevented by; priority inheritance ­ priority of H is temporarily loaned to L toget it out of the way, priority ceilings ­ the resource is given a priority and no process with higherpriority can lock the resource.

Process protection is enforced by forcing access via the kernel so one user cannot interfere withanother user’s processes. For this, each user has it’s own ID which the OS determines whichprocess can access which files, other processes and so on. A new process inherits the userIDof the parent process. A root/administrator/superuser is a normal user but allows access to allusers files and processes etc. and while is not the kernel, the inter­user protections are notenacted by the OS. The root can change the userID of it’s processes, giving away it’s privilegesbut allowing a normal user to have a process. Root access can be broken down into capabilities,i.e. all the way down to rights access to individual files. These capabilities (or permissions) arelike tokens which can be passed around or inherited by processes and so on, and allow finercontrol of security at the expense of a more complex checking system.

Inter­process communication (IPC) is how processes communicate amongst themselves,sending/receiving data from each other etc. This can be achieved in many ways, but must besupported by the operating system, as the kernel by default stops processes interfering with

10

Page 11: CM10195 Computer Systems Architecture 2 Revision Notes

each other.

IPC can be done using;

Files ­ using an existing resource, PA writes data it wants to send to PB to a file, andPB reads it. They must agree on a filename, know when data has arrived, the file musthave protections from other processes, and files are slow. Not used for IPC in general,but good for huge amounts of data.

Pipes ­ connects two processes taking output from one and giving it an input toanother. Provides coordination and synchronisation (one process can’t use the pipewhilst the other is using it) which affects scheduling of the two processes.Implemented as a buffer held by the kernel so it can control the scheduling, usuallyused when a process forks a child process to channel the data from parent to child.Pipes are simple, efficient, easy to use, unidirectional, only between related processesand so used a lot. Can cause deadlocks. Named pipes are shared between unrelatedprocesses, and sockets allow bi­directional IPC between processes that could be ondifferent machines.

Lecture 11

Shared memory ­ memory shared between the processes, so one process couldwrite to memory for another process to read. Have to decide what memory to use, aflag to a specific memory location to signify the data has finished being written, andmemory protection from other processes. Due to fast speeds, good for IPC and largechunks of data

Signals ­ a software equivalent of a hardware interrupt, raised by the kernel or aprocess with the same userID via the kernel, and the receiving process stops andexecutes a signal handler. Just a flag bit, which can then be either; ignored, acceptedand acted upon, suspended, or terminated. They are asynchronous ­ can arrive at anypoint during the program’s run. Each type of signal (e.g KILL, PIPE, INT etc.) has it’sown signal handler, but the program can include handler functions for doing somethingdifferent. Signals are fast, efficient, used for small data transmissions, and are used alot.

Semaphores ­ a variable that can only be accessed and altered by 2 operations ­signal and wait. Allows mutual exclusion, where only one process is allowed access toa resource at one time. Used to protect shared resources, but hard to implement.Widely used, small & fast, used in OSs and user programs to protect criticalresources. Easy for semaphores to cause deadlock.

Lecture 12

Application level ­ high­level mechanisms of IPC, focusing on passing objects

11

Page 12: CM10195 Computer Systems Architecture 2 Revision Notes

between components. The standard method of representing the objects calledmessage passing.

Memory management

Gates’ Law ­ programs double in size every 18 months

Wirth’s Law ­ software is decelerating faster than hardware is accelerating

The first thing to consider in memory management is how processes ­ their code and data ­should be laid out in physical memory. Allocation needs to happen at several points; initialisation(static), running (dynamic), freeing during running, freeing at end, freeing & allocation within thekernel.

Implicit memory management ­ language controls creation and deletion of objects

Explicit memory management ­ programmer controls the creation and deletion of the objects

Partitioning is a static layout of physical memory, with areas allocated at boot time, and aprocess is loaded into the smallest free partition it will fit into. Equal sized partitions are easy butwaste space, and can’t cope with larger processes, and can only deal with a certain amount ofprocesses.

Overlays are when only part of the process code is loaded into memory at once, with the rest ofthe process code being able to load into memory if needed, overwriting the part of the processthey do not need at the moment in memory. This costs speed and execution, and only for goodprogrammers.

In physical memory we must fit on process into one block of memory as we can’t spread itacross several blocks, as it’s too complicated.

Dynamic partitioning creates and allocates partitions only when needed, allowing a process todetermine how big the partition is. When a process ends it leaves an empty space in memory,creating fragmentation, which is holes of different sizes in the memory, all separated from oneanother, making it harder for larger processes to be loaded into memory. To solve this efficientlyis hard, but we can keep a free list of all free spaces, and we can coalesce physically adjacentblocks. When we want some space we can search the freelist, using a big enough chunk andreturning the unused space back to the freelist.

There are strategies for choosing the blocks of memory from the freelist;

First Fit ­ First available big enough chunk, faster than best fit, leaves larger moreuseful fragments, but leaves smaller fragments near the start of the list, meaning wesearch further and further down the list each time.

Best Fit ­ Smallest available big enough chunk, slower than first fit as we check the

12

Page 13: CM10195 Computer Systems Architecture 2 Revision Notes

entire list, and results in fragments too small to be of any use.

Worst Fit ­ Biggest available chunk to leave behind useful large chunks. Faster thanbest fit due to larger and fewer blocks to search through.

Next Fit ­ continue from where we last allocated. Fast, improves on first fit byspreading small fragments throughout memory rather than at the start of the list.

If there are no memory spaces big enough we use a compaction of memory called garbagecollection. This stops all processes, moves them around in physical memory to close the gaps,and then the processes can continue. This is an expensive operation to move all the blocksaround, and pausing real­time behaviour is not ideal, and so garbage collection is not widelyused.

Lecture 13

Even with garbage disposal, is we can’t find a suitable free space for a new process, we canchoose not to admit it, or kill processes ­ which is not ideal unless the new process is important.It is better to preempt memory ­ take it away from one process and give it to another.

Swapping ­ one or more processes are selected by the OS to have their data copied out to diskto make space for a new process ­ usually blocked or unscheduled processes. When theseprocesses want to be run again, their data needs to be copied back into memory first. This has atrade off between speed (time spent copying to and from disk) and size (memory allocation).Usually is not used due to time consuming nature.

Paging ­ chopping memory into regular sized portions called pages (4096 bytes), and thehardware is arranged so copying pages in and out of memory from disk is as efficient aspossible.

A physical address is a number of the bytes in the system from 0 to n, where a virtual address isa per­process fictional address. The operating system has a table per process containing thevirtual to physical mappings for each process. The tables only contain addresses for pages inuse to keep their size to a minimum. This mapping is done for every memory access by theCPU so must be as fast and efficient as possible, and so is supported by hardware called thetranslation lookaside buffer (TLB) in the MMU. The TLB has it’s own copy of a few of themappings of the current tables to translate very quickly between them. The page tables arestored in normal memory as part of the process’ management information.

When presented with an address from the CPU, the TLB looks it up in it’s cache ­ if it’s there thememory access goes ahead with the physical memory there, if it’s not there then in a hardwaremanaged TLB it will look for the translation in the page table itself called a page walk and stores itin the TLB, in a software managed TLB, the OS has to do the page walk. If the TLB is full with aTLB miss, the OS has to decide which address to delete to make room for the new one, usuallya least recently used (LRU) strategy is used ­ due to temporal locality.

13

Page 14: CM10195 Computer Systems Architecture 2 Revision Notes

There are many strategies to evict a page when physical memory is full ­ random, FIFO, LRU,LFU etc. TLB’s are good but have small capacity and rely on temporal locality to be effective.When a new process is scheduled the TLB has to be flushed for the different mappings of thenew process.

So a page table is a list of pages a process has accessed with relevant virtual­physical mapping.Every page has some permissions attached; read, write and execute. The TLB solves two bigproblems of memory protection and sharing, as all memory accesses go through the TLB whichcan prevent it being possible for one process to overwrite the memory of another.

Copy on write ­ different processes can share data as long as they do not try to update it, atwhich point a page fault occurs and the operating system takes over, and creates a copy of thepage and changes the page table and TLB for that process to point to a new copy, and the writecan then take place on a private unshared copy

Lecture 14

Another problem in physical memory is that it runs a lot slower than processors, called memorylatency ­ a delay between a request to memory and the values coming back. One way ofaddressing this bottleneck is using registers, doing as much computation in them as possible,only calling out when necessary. These registers are small amounts of memory running at CPUrate, called a cache, and it sits between the CPU and main memory. If the CPU needs to readfrom memory, if the location is in the cache the value is returned, if not it is retrieved from mainmemory and also stored in the cache.

A cache line is a small block of bytes that get written/read as a whole, reducing the lookupsystem. Writing to memory is done through the cache via two methods; write­through cache ­value written to cache, cache writes to main memory, or write­back cache ­ main memory isonly written to when convenient/necessary. We can have multi level caches, and cachinghappens at many levels ­ in registers, in the TLB, in L1, L2 & L3 caches, main memory is acache for disk, etc.

Temporal locality ­ recently accessed location will likely be accessed again soon

Special locality ­ locations near recently accessed locations will likely be accessed soon.

Some architectures have two separate caches ­ one for code and one for data, due to theirdifferent behaviour ­ data has lots of reads and writes, code has lots of reads. This architectureis called the Harvard Architecture, with the idea for separating the two produces higher hit ratesfor each cache individually.

Cache flush requires all cached values to be written to main memory and then clears the cache.Cache coherence is when all the processors see the same values in memory. This is difficultwith multi­processors, as each processor has it’s own cache. So each cache must watch everyother cache. The largest multiprocessor machines have either;

14

Page 15: CM10195 Computer Systems Architecture 2 Revision Notes

Non­uniform memory access ­ some parts of memory are slower than others (as one part ofthe memory is design for access by that processor (fast) but the rest is for the other processors(slow).

Non­shared (distributed) memory ­ cluster of processors joined by a network.

Lecture 15

Main memory these days is several gigabytes and is volatile, but to manipulate more data weturn to larger but slower devices like disks, with organised filesystems. A file is a named chunk ofdata stored on a disk, it’s name converts to places on disk using a hierarchy. We gather relatedfiles and put them into a directory (or folder), and names of directories can be collected into adirectory and so on until we reach the root, or the top of the hierarchy. The directory hierarchyforms a directed acyclic graph (DAG), meaning there are no loops for easy traversal of thehierarchy. Each process has a current working directory (cwd), which is a prefix (stored in thePCB) which is glued onto an incomplete filename and the kernel uses that instead.

We want to do a lot of things with a file system, including; creating, deleting, opening, readingfrom, writing to, closing and renaming a file, and creating, deleting, looking through, adding,removing and renaming files from a directory. This is before coming to things like, speed ofaccess/update, scalability, efficient disk space use, reliability, protection, security, backup &recovery.

Lecture 16

The design of a Unix file system is based on inode (data structure), where each file has an inodethat contains information about a file ­ it’s metadata, such as; timestamps, ownership, size, type,access permissions, reference count etc. Filenames are not stored in the inode, but in thedirectories, which are essentially just a list of names of files and subdirectories with their inodenumbers. There can be multiple directory entries of the same inode with a different name ­hence why a file’s inode does not store it’s name. If the reference count (number of names ithas) of an inode is zero, then the operating system can remove the file. Deleting a file willdecrease the inode reference count and removing the name reference in the directory. Inodesare in disk blocks, allowing for quick allocation/deallocation. A soft link (shortcut) is the name ofthe file, whilst a hard link refers to the actual file.

When a program opens a file, the OS must find where on disk the file lives, so say we arelooking for the file prog.c with a cwd of /home/rjb;

The name is incomplete, so the OS prepends the cwd giving /home/rjb/prog.c The OS reads the block containing the root directory off disk and scans through it for the

name home It finds it and gets the inode number for home It reads the inode off disk and finds it refers to a directory It reads the block containing the directory off disk

15

Page 16: CM10195 Computer Systems Architecture 2 Revision Notes

It scans the directory for the name rjb It finds it and gets the inode number for rjb It reads the inode off disk and finds it refers to a directory It reads the block containing the directory off disk It scans the directory for the name prog.c It finds it and gets the inode number for prog.c It reads the inode off disk and finds it refers to a file It reads the blocks containing the file off disk

This must be done for every file opened. Caching can be used here to keep inodes anddirectories in memory for quick access.

If we want more than one file system on one disk we can split the disk into partitions, which isjust a chunk of disk owned by a single filesystem. Each file system has it’s own inode tablesseparate from the others. As inode 23 of one file system is different to inode 23 of anothersystem, we can’t have hard links across partitions, but we can have soft links.

Lecture 17

A disk is a large array of block of either 514 or 1024 bytes, numbered from 0 up to the number ofblocks on the disk. This type of addressing, with a 48 bit address, is called linear blockaddressing (LBA). A disk comprises of 3 or 4 platters covered on one or both faces with amagnetic material which are spinning at great speed. Data is written and read from theseplatters by heads, which all move back and forth as a unit reading from the circular tracks in theplatter. A cylinder is a set of corresponding tracks in a vertical slice throughout the disk, that canbe accessed simultaneously by all of the heads. A sector is a section of one disk (shaped like apiece of pie) and the disk block is the intersection of a sector and a track.

To specify a certain byte on the disk we must determine which block it is in, which cylinder theblock is in, which sector the block is in and which head to use ­ thus a block needs a triplecylinder, head, sector (c,h,s) to identify it. It is easy to convert between CHS and LBA when giventhe numbers of c, h & s.

At the start of every disk, there is partition information, at the start of every partition is the filesystem information. These initial blocks of information are called the superblocks, kept at wellknown points around the disk incase the main superblock gets trashed.When allocating blocksfor a file the file system should try to keep them close together. Fragmentation occurs when wedelete and recover its blocks, so there are the usual strategies for allocation (first fit etc.) with thetwist of physical location.

There are three main ways to access a file; read, write or run it as a program. For a directorythere are three main ways of access; read (list contents), write (edit contents) or search. Fileshave owners and they inherit the userID of the process that created it, and there are also groupsof users. Groups allow access/denial to large numbers of people, and each user can be in morethan one group. Each file has a group ownership as well as a user ownership. Each file therefore

16

Page 17: CM10195 Computer Systems Architecture 2 Revision Notes

has a group of permission bits from the owner, the group owner, and everyone else, and foreach of read/write/execute access.

Lecture 18

Access control lists (ACL’s) generalise the three­part User, Group, Other to arbitrary collections.Basically a list of permissions attached to an object.

I/O devices range across human speed, machine speed, real­time.

Programmed; initiated by the program which then waits for the result.Interrupt driven; initiated by the program which does something else until interrupted when theI/O has finished.Direct memory Acess (DMA); direct device to memory ­ I/O transfer is started and theprocessor is only interrupted when the I/O has finishedSynchronous; wait for the data to arrive after read request/wait for a write to finish after writerequest.Asynchronous; set the read/write going and leave it until an interrupt is received when transferis finished.

Each kind of peripheral needs code to access it (called a driver), some devices have standardinterfaces so we can use a single piece of code to access any disk, but some devices needspecifically written code that only works for that device, usually kept secret for intellectualproperty. Drivers are part of the kernel, so they have access to the fill machine which can causebugs. Microkernels in some OS’s keep driver separate from the main kernel to remedy this byusing a different priority ring. Another approach is to have the main part of the driver in anordinary process and as little of the code in the kernel as possible, providing the most protection.This does involve a lot of crossing the user/kernel divide which is expensive.

Another way to eliminate the cost of calling the kernel is to use buffering (reading a chunk of datato a memory register before flushing and returning), which delays calling the kernel until a decentamount of work can be done.

Lecture 19

This idea can also be used for the I/O inside the kernel, as buffering smooths out peaks in I/Odemand, with the disk cache acting as a buffer. As the disk is central to data we need to makesure it’s being used efficiently, and compared to the CPU it takes a long time for the heads toread the disk. Reads and writes therefore take a long time for the disk, and must therefore bemanaged carefully, using disk scheduling. Platter may be rotating fast but the latency (calledaccess time) is on the order of 10ms, which on a 1GHZ processor is 10 millions cycles. Thedata might come off the disk at a fair rate but the time we have to wait for this to start isconsiderate. Latency is caused by the time it takes for the heads to get into position (seek time)and the time is takes the platters to rotate to get the right block under the heads (rotationaldelay). We therefore need to minimise the physical movement of the disk. One way to do this is

17

Page 18: CM10195 Computer Systems Architecture 2 Revision Notes

schedule the disk requests into an order that needs the smallest amount of physical movementfrom the disk. Scheduling types;

FIFO ­ or no scheduling, access blocks in the order they are requested ­ easy to compute,blocks arranged in cluster so locality comes into play.

LIFO ­ generally from the same process so for blocks that are likely to be related, not good forwait times

Closest Block First ­ pick request that minimises seek time, reduces seek time of FIFO by50%, causes uneven access to outer blocks as heads hover in middle of disk

Elevator ­ (or SCAN) as the heads go inwards/outwards serve the next nearest request in thatdirection. Smooth head movements but biases to most recently covered or most outer cylinders.

Modern disks, have their own cache, and support tagged command queueing (TCQ) and nativecommand queueing (NCQ), which is basically disk drives doing their own scheduling as theyhave a more accurate idea of the placement of the blocks than the OS does. We are seeing arise in solid­state disks (SSDs) which are just chunks of memory wired to look like disks to theoperating system, with faster read times and much less likely to break, but are more expensiveand have slow writing speeds and have a limited lifetime. To cope with the limited lifetime SSDshave wear levelling technologies which redirect writes to a single block to blocks spread over theentire device to increase the lifespan.

Networking

Lecture 1

The Internet is a global collection of networks. An internet is just some collection of networks. Anintranet is a collection of networks owned by one organisation. Note; the web is NOT the internet.The Internet is a collaboration between its member hosts, and data is passed through it fromsource to destination by passing machine to machine ­ called hops.

ARPA started as a project to connect all of their computers allowing the sharing of theirresources, being non­centralised to prevent single points of failure, so there would be multiplepaths between hosts. They could not send data through circuits as this would be too vulnerable,so packet switching was advised. Packet switching is where data is chopped into small chunkscalled packets and each packet is sent individually over different paths, and the receiving hostcan construct the original data.

A packet has it’s destination address in, but relies on routers to direct it to this address. Toensure interoperability, The Internet relies on standards and standardised protocols, so differentmachines completely can still communicate and understand each other. The common languageof the internet is called the Transmission control Protocol/Internet Protocol (TCP/IP) whichhelps communication over several layers. A layering model is just a recommendation on how to

18

Page 19: CM10195 Computer Systems Architecture 2 Revision Notes

approach the design of a standard, which we can then create implementations off.

Lecture 2

There are two main layering models in use: the Open Systems Interconnection (OSI) sevenlayer model, and the Internet four layer model ­ the Internet (TCP/IP) model is used in the realworld mostly.

The layers of these models should be as self contained as possible ­ this is modularity. Theyshould be easy to implement & understand, and only interface with neighbouring layers.

Layers of OSI model ­ Physical ­ deals with transmission of bits over a channel (voltages,encoding of bits, which plugs, how many wires etc), Data link ­ takes physical layer and tries tocreate a channel with no errors in transmission , network ­ controls the routing of data fromsource to destination dealing with flow of control, transport ­ accepts data from session layer andarranges it into packets (packetisation) and from network layer into original data stream(packetization), session ­ manages sessions (set of exchanges) between source anddestination, presentation ­ decides representation of data, and application ­ contains protocols onwhich applications that the application users can see are built. Data is passed from applicationto hardware, being encapsulated (accurate transformation of data, in such a way that the layerbelow can cope with it transparently) from layer to layer. For example there might be added anidentifying header, encoding bit patterns, put items in standard form etc.

Byte stuffing ­ using extra characters to encode a stream of data correctly. Can cause epansionof data, but makes the correct transmission of that data.

Lecture 3

Internet model has four layers (TCP/IP) ­ Link ­ (OSI physical and data link layers) just says ithas to be capable of send and receiving IP packets, Network ­ handles movement (routing) ofpackets and the Internet Protocol (which is unreliable) is defined here, Transport ­ provides flowof data between source and destination and the transmission control protocol (TCP) and userdatagram protocol (UDP) are defined here, and Application (OSI session, presentation and applayers) meaning Internet apps must take care over presentation issues.

Reliable protocols guarantee delivery (waits for lost packets to resend), and unreliable protocolsdo not guarantee delivery (ignores lost packets).

Example: how an email might be transmitted over an Ethernet;

1. We start with the text of the email2. The email application transforms the text using a MIME encoding (presentation)3. The email application adds an envelope header (From, To,etc.)4. TCP adds its header (reliability)5. IP adds its header (routing)6. Ethernet adds a header (local routing) and a trailer (checksum)

19

Page 20: CM10195 Computer Systems Architecture 2 Revision Notes

7. The bits are transformed using a 4B/5B encoding to smooth the bit patterns and are sentusing a three­level electrical coding MLT­3 (physical)

Comparing the two models; OSI model developed before implementation, Internet model after;OSI makes clear distinction between model and implementation, OSI is general and can apply tomany systems (Internet is specific to TCP/IP), TCP/IP is successful and OSI is not. Problemswith the Internet model; specific to TCP/IP, merges data link and physical layers. The OSI modelis used but it’s protocols aren’t, and the TCP/IP’s model isn’t used but it’s protocols are.

There are several popular hardware implementations, the most popular being; ethernet ­ wirednetwork, and WiFi ­ wireless network. Ethernet uses carrier sense, multiple access with collisiondetection (CSMA/CD) on a variety of speeds of hardware. Ethernet is a multiple access medium(several hosts use the same wire to send data to one another), but if two hosts sendsimultaneously there will be a collision, so the host must listen to the ethernet to see if anyoneelse is using it ­ carrier sense ­ and after sending hosts listen for a collision ­ collision detection.If there is a collision each host wait a random amount of time (to prevent another collision) andthen retries with the carrier sense.

Hubs send an incoming signal to all of it’s outputs. Switches only send the input to the wire thathas the destination host.

Ethernet frame looks like;

IP addresses are hardware independent addresses.

Lecture 4

ARP ­ address resolution protocol, a link­layer protocol that given the IP address we can find thecorresponding ethernet address. There is then an ARP cache that records this relation whichcan be used for the rest of the packets in the data transmission.

Gateway host ­ machine on more than one network

Dynamic Host Configuration Protocol (DHCP) ­ used by a machine to get an IP address, aDHCP host returns an unused IP address back to the requesting host. Informs DHCP server

20

Page 21: CM10195 Computer Systems Architecture 2 Revision Notes

when finished with address. A Lease Time is the period of time that a host can use an IPaddress for, and when it expires the client can use DHCP again and the client will get a newlease for the existing address. DHCP can also give the address of the gateway host, nameservers, print servers, mail servers, and also lease times.

There are not enough IP addresses, as 32 bits are not enough (28 = 4 bil, not enough for 7 bil ofthe world). Moving from IPv4 to IPv6 (128 bit address).

The names of websites correspond to an IP address, and a protocol called the domain namesystem (DNS) uses servers to store the translation between IP addresses and website names.

Lecture 5

Name servers are computers whose main purpose is to run DNS. DNS keep table mapping forthe domain, and help local hosts when they need to do a lookup of a non­local address.

Each reply from the server has a time to live attached to it, indicating how long the server shouldkeep the information before asking again. DNS does; A address ­ name to IP address, PTRpointer ­ IP address to name, AAAA address ­ name to IPv6 address, SOA start of authority ­name to responsible name server, MX mail server ­ name to mail server for that domain, etc.]

DNS is many­many relationship of names and address; one address can have many names andmany addresses can have one name. DNS is a fast and easy to manage protocol, so is verysuccessful.

LDAP (lightweight directory access protocol) ­ general database (or directory) aimed at theapplication layer, storing things like usernames, passwords, email addresses etc. and sharesmany properties with DNS ­ it’s distributed, can be replicated, & is hierarchical. Common use isfor a LDAP server of valid users on a network.

Secure sockets layer (SSL) protocol ­ more modern version is the Transport layer security (TLS)protocol, which provide secure communication over the internet. Layers on top of the transportlayer providing security & privacy, as well as authentication. It also prevents tampering, forgery &replay

Lecture 6

SSL/TLS is used as a transport layer by applications, and it lives outside the kernel in the userprogram space, and if a programmer wants security/authenticity, then they do a ssl/tls setup ontop of a normal TCP or UDP connection and uses ssl/tls reads and writes. SSL/TLS allowsauthentication in both directions (server authenticates client & vis versa), and they can then goon to negotiate a secure, private connection. Secure, but had overheads (but are not huge).

The Internet is successful in supplying services ­ client makes a request and server returnssome data, e.g. DNS lookup LDAP, etc.

21

Page 22: CM10195 Computer Systems Architecture 2 Revision Notes

Remote Procedure Call (RPC) ­ compute something on a different machine to offload it.

HyperText Transfer Protocol (HTTP) ­ protocol used to request and return web pages.

22