You are on page 1of 1

Scheduling Strategies : Priority Scheduling Each process is assigned a priority, High-priority processes are executed rst, Use a second

strategy when multiple processes have equal priority | Round-Robin (RR)Like FCFS, but each process takes turn, Each process gets a small slice of time to work towards completion before being pre-empted Resource Management Whats in a Hard Disk?A hard disk consists of a stack of platters Platter: thin disk with a magnetic coating Read/write heads store and retrieve data Disks are divided into tracks (concentric rings) and sectors (small arcs within tracks) A stack of sectors is a cylinder Disk Requests The time needed to retrieve a piece of data from a disk is broken down into three components Seek Time time needed to move the disk head between tracks Latency (rotational delay) time needed for the disk to revolve into position. Transfer rate : the rate at which data can be read from/written to the disk Scheduling Strategies I : First Come, First Served (FCFS) Disk requests are processed in the order that they arrived, This is the most fair strategy in terms of waiting time. The disk head may move a very long distance between requests, ignoring later requests that lie along the way, Scheduling Strategies II: Shortest Seek Time First (SSTF) Arrange all requests by their position on disk, Select the next request to service based on how close it is to the current head position, This guarantees the minimum head Movement, Distant requests may be starved in favor of late-arriving requests that are closer Solution tip:Write out all of the disk requests in order of address, and use this chart to find the closest one(s).Scheduling Strategies III : SCAN and C-SCAN (Circular SCAN) Disk head repeatedly sweeps back and forth across the disk, from one end to the other. Requests are processed as the disk head reaches them. Requests are guaranteed to be reached in (2 * disk length) time.C-SCAN wraps around from one end of the disk to the other.Scheduling Strategies IV : LOOK and C-LOOK (Circular LOOK) Nearly identical to SCAN and C-SCAN. The disk head only moves to the most distant request in a given direction. SCAN goes all the way to the end.This eliminates wasted movement by the disk head. Allocation Algorithms Holes in memory can be assigned to new programs using: First-t Place the program in the rst hole that is big enough to hold it Best-t Place the program in the smallest hole that will hold it. Worst-t: Place the program in the largest hole available, and make the eftover space available to programs as a new hole. FragmentationAs we load and unload programs, free RAM is broken into little pieces. The result is external fragmentation: 50-percent rule: Given N allocated blocks (in rst-t), another 0.5N blocks will be lost due to fragmentation Suppose that we divided memory up into a number of xed-size blocks (called frames) We could assign each program a number of frames equal to its total size. e.g., a 400K program would receive 100 4K frames. A program is divided into small blocks called pages; each page is matched to a single frame When a program tries to access page N, the operating system converts that request to a frame number. Virtual Memory. stores all of the memory contents of active programs on disk.Only part of each program is physically present in RAM. As programs run and request data that isnt in memory, the operating system copies data back and forth between physical RAM and the disk. This is slower than real RAM, but much cheaper. Why Virtual Memory Works: Virtual memory relies on the principle of locality. When a program executes, it tends to stay in the same block of instructions (e.g., a loop) for a while. When it nishes and moves on, it probably wont return to that block of code for a while (if it ever does). At any point in time, a program has a working set of pages that it needs for active (immediate) use. Only the working set needs to be present in RAM Finding Fault(s)...An operating system component (the pager) monitors all memory access attempts: addresses are marked as valid or invalid. invalid = not accessible or not loaded.Attempting to access a page that has not been loaded causes a page fault.The pager must load the desired page into a frame of RAM. It may need to discard another frame to make room. Page Replacement: How does the pager decide when to discard pages to make room for more? First-In-First-Out (FIFO) Optimal (difcult to implement)Least-Recently-Used (LRU)Least-Frequently-Used (LFU)Most-Frequently-Used (MFU). Deadlock: Processes are unable to execute because they need resources held by other processes. Deadlock requires four conditions to be true: Mutual exclusion, Hold and wait, No preemption, Circular wait. Compilers vs. Interpreters.Both perform the same basic function. A compiler translates an entire le/program at once. An interpreter translates and executes a program one line at a time. Translation Steps: Lexical analysis (lexing): Identies tokens (lexical units). Parsing: Identies the programs grammarCode generation:Creates machine code that corresponds to the parser output.Role of the Lexical Analyzer Dening the patterns that identify different parts of speech are called lexemes or tokens, Processing an input le and reporting the sequence of lexemes it contains. The lexical analyzer does this by constructing a complex nite-state machine. The user compiler designer creates a list of regular expressions and tokens. This list is passed to a special program that translates the regular expressions into a piece of computer code that implements the nite-state machine. Parsing: tokens into meaningful groups.Parsers are written to match tokens to elements of a grammar. The parser works hand-in-hand with the lexer: The parser compares the token sequence to a set of rules.When the parser nds a rule that matches, it uses an elaborate form of search and replace to translate the input into machine language Grammars: set of legal ways to combine tokens Nonterminal: a term that requires further decomposition.Terminal: requires no further decomposition.Each nonterminal corresponds to a sequence of terminals and nonterminals.As tokens are found, they are matched to terminals.When all of a nonterminals components have been accounted for, that token sequence can be collapsed into the single nonterminal. The ultimate goal is to collapse the entire token sequence into a single nonterminal. Production GrammarsA grammar is represented by a series of production rulesEach production has the formnonterminal <terminal/nonterminal sequence>. The rst (topmost) production represents the start symbol Production rules are combined to generate a parse tree that indicates the grammatical structure of a statement. Start symbol is the root of the tree Parsing Directions Bottom-UpMatch symbols to terminals and groups of terminals to nonterminals, until we eventually match the start symbol Top-Down Beginning with the start symbol, expand nonterminals via production rules until we reach a desired sequence of terminals. Types of Networks Star topology Each node connects to a shared central node. Problem: what if the central node fails? Ring topology Circular connection between computers.Messages travel in a xed direction.Problem: any device failure breaks the loop Bus topologyUses a shared common communication line.Easy to add nodes, but shared backbone is a common point of failure Mesh topologyEach node is connected to every other node. Very failure-proof, but very expensive. Message Exchange Protocols . Token Ring. All nodes in ring transmit in the same direction. Token possession allows a machine to speak. Carrier Sense w/ Collision Detection (CSMA/CD) Messages are broadcast to everyone. Machines ignore messages for others. Only transmit when bus is silent. Internet Addressing.Current system (IPv4) uses 32 bits.Next-generation system (IPv6) uses 128 bits. IP addresses are written in dotted quad form. Four one-byte sections (each ranges from 0-255).Address parts run from most general to most specic. The Domain Name Service (DNS) assigns mnemonic .Domain names are read in reverse. Most-specic to least-specic.The domain name sufx corresponds to a Top-Level Domain (TLD) classication. Domains are grouped together.Common TLDs are .com, .edu, .org, .net. The OSI Protocol Stack.7 layers.Application.Presentation.Session.Transport.Network.Datalink.Physical.TCP/IP Protocol Stack.4 layers.Application.Transport.Network.Link. Messages originate in the application layer. IP: The Internet Protocol. IP provides basic network delivery functions. It assigns addresses to machines (hosts) and forwards messages between hosts Packets are more efcient than large messages.Data can be split among different routes.Errors only require the offending packet to be resent (instead of the entire message) TCP: The Transmission Control Protocol.IP works via best effort..TCP adds reliable delivery over unreliable networksEvery packet has a sequence number (23 of 34) TCP uses these sequence numbers to:Assemble arriving packets into the correct order.Detect missing packets and request new copies. Malware.Viruses: programs that (passively) self-replicate, Worms: programs that (actively) self-replicate.Trojan Horses: programs that masquerade as something else. Virus.computer program that spreads when its host (the le/disk that it resides on) is transferred to another computer.Viruses attach themselves to executable les that are part of other programs (the carrier).Types of Virus 1.Nonresident viruses immediately search for other hosts to infect before letting their carrier execute2.Resident viruses run in the background while searching for new les/hosts to infect Polymorphic viruses modify their own code each time they spread. Worms almost always harm the host network, by consuming bandwidth; viruses only harm the targeted computer. Methods.Return Type Example:: int timesThree (int value) // return type: int {return (value * 3); // the type being returned must match the declared type} Linear Search On average, you have to examine half of all elements. Binary Search.Method: choose an element at random.(usually the middle) and decide whether to search the left or right half.At each decision point, the space to besearched is cut in half.Requires sorted data to work properly.Very efcient: only needs log(n) comparisons.Bubble Sort Method: compare pairs of adjacent items, and swap them if they are out of order.At the end of each pass, the largest remaining element is in its proper place. Elements bubble to their proper places.This is a very easy algorithm to implement.Its also very inefcient (n^2 time) Pseudocode: Repeat N-1 times:Repeat N-1 times: if A[x] > A[x+1], swap them. Insertion Sort: Method: select one element at a time and insert it into its proper position.Begin by dividing the array into two regions: sorted and unsorted.Initially, the sorted region is empty.At each step, move rst unsorted item into its proper position in the sorted region. Pseudocode 1.A[0] is sorted; A[1]-A[N-1] are unsorted 2. Repeat N times: 1. nextItem = rst unsorted element 2. Shift sorted elements > nextItem over one position (A[x] = A[x-1]) 3. Insert nextItem into correct positionSelection Sort.Method: Search for the largest remaining item, and put it in its proper sorted position. On each pass, swap the largest item with the last (unsorted) element: Pseudocode1. Repeat N-1 times:1. Find the largest (unsorted) element2. Swap A[last] with A[largest] Radix Sort - faster than other/"regular" sorts. never directly compares elements.Number of comparisons is linear with regard to # of digits. time needed is dn (d = # of digits, n = # of values). only need d passes total (1 per digit).Create ten lists, one per digit value (0-9). For each number, move it to list that matches the value in the current position(e.g., look at ten's place for all numbers) General algorithm:For each digit place (ones, tens, hundreds):Assign each number to a list based on its value in the current place. Recombine lists, from list 0 to list 9, andrepeat with the next place. Order: ones place, then tens, then hundreds, etc. Requires many fewer passes, but more space for lists Abstract Data Types (ADTs).Data type - way to store information.Abstract data type - describes behavior of a particular data storage variable without specifying exactly how it works.We talk about interface rather Focus on behavior rather than actual detailed workings.Stack: Last-In, First-Out data structure (LIFO)Always add and remove from same end ("top")push - add element to stack top. pop - removes element at top of stack peek - look at element on top of stack. Ex. Add 1, 2, 3, 4, 5, then remove twice :1 2 3 Queue.First-In, First-Out (FIFO) data structure.Add at one end (tail), remove from other end (front).enqueue - add element to tail of queue. dequeue - remove element from front of queue. EX: Add 1, 2, 3, 4, 5, then remove twice: you get 3 4 5 - Tree - list of nodes, each with its own value - each node has one ancestor and 0-n children - e.g., a binary tree node has 0-2 children - root: single node with no ancestor - leaf: node with 0 children - binary search tree (BST) - values are always ordered - all nodes/children to left of a node have smaller values - all nodes/children to right of a node have greater/equal values Three ways to traverse/"print out" a tree: - Pre-order traversal - node, left child, right child - In-order traversal - left child, node, right child - binary search tree: ordered data - Post-order traversal - left child, right child, node To add a value, start at the root and follow the path downward -- move left when smaller than a node's value, or right if larger/equal - The height of a tree is the length of the longest path from the root to a leaf 1. A leaf has height 1 2. A non-leaf's height is 1 plus the maximum of its children's heights This requires us to "pause" while we calculate our children's heights - Tree height is an example of a recursive algorithm -- we need to solve the same problem multiple times, on smaller and smaller versions of the original.

You might also like