6.087 practical programming in C, lec9

Source: Internet
Author: User
External libraries. B-trees, priority queues. Symbols and libraries

• External libraries provide a wealth of functionality-example: C standard library

• Programs access libraries 'functions and variables via identi referers known as symbols

• Header fi le declarations/prototypes mapped to symbols at compile time

• Symbols linked to de provided nitions in external libraries during linking

• Our own program produces symbols, too

The so-called Symbol should be a method signature. Due to future link needs, there should be a corresponding method signature in the binary library file, which can be obtained through decompilation.

Functions and variables as symbols

• Consider the simple hello world program written below:

# Include <stdio. h>

Const char msg [] = "Hello, world .";

Int main (void ){

Puts (msg );

Return 0;

}

• What variables and functions are declared globally?

Msg, main (), puts (), others in stdio. h

In my opinion, the execution of the C program is the operation of global variables, static variables, and methods (in fact, it can also be seen as a variable. After all, the final result of method execution is a number. They are the top-level part of the program, so they are represented by Symbol.

• Let's compile, but not link, the role le hello. c to create hello. o:

Athena % gcc-Wall-c hello. c-ohello. o

•-C: compile, but do not linkhello. c; result will compile the code into machine instructions but not make the program executable

• Addresses for lines of code and static and global variables not yet assigned

• Need to perform link step on hello. o (using gcc or ld) to assign memory to each symbol

• Linking resolves symbols de specified ned else where (like the C standard library) and makes the code executable

• Let's look at the symbols in the compiled into le hello. o:

Athena % nm hello. o

• Output:

0000000000000000 T main

0000000000000000 R msg

U puts

• 'T'-(text) code; 'r'-read-only memory; 'U'-unde incluned symbol

• Addresses all zero before linking; symbols not allocatedmemory yet

• Unde incluned symbols are de incluned externally, resolved during linking

• Why aren't symbols listed for other declarations instdio. h?

• Compiler doesn' t bother creating symbols for unused function prototypes (saves space)

• What happens when we link?

Athena % gcc-Wall hello. o-o hello

• Memory allocated for de incluned symbols

• Unde incluned symbols located in external libraries (like libcfor C standard library)

• Let's look at the symbols now:

Athena % nm hello

• Output:

(Other default symbols)

.

0000000000400524 T main

2017100000040062c R msg

U puts @ GLIBC_2.2.5

• Addresses for static (allocated at compile time) symbols

• Symbol puts located in shared library GLIBC_2.2.5 (gnu c standard library)

• Shared symbol puts not assigned memory until run time

The link phase mainly involves the symbols assigned address. I think the underlying variables should be calculated by relative positions.

Static and dynamic linkage

• Functions, global variables must be allocated memory before use

• Can allocate at compile time (static) or at run time (shared)

• Advantages/disadvantages to both

• Symbols in same fi le, other. O fi les, or static libraries (archives,. A fi les)-static Linkage

• Symbols in shared libraries (. So fi les)-dynamic linkage

• GCC links against shared libraries by default, can forcestatic linkage using-static dynamic AG

I personally prefer static because of the previous incompatibility of N multi-database versions...

Loading shared libraries

• Shared library located during compile-time linkage, but needs to be located again during Run-Time Loading

• Shared libraries located at run-time using linker library lD. So

• Whenever shared libraries on system change, need to run ldconfig to update links seen by LD. So

• During loading, symbols in dynamic library are allocated memory and loaded from shared library role le

Now, I know why ldconfig is always used to solve the version problem of the Linked Library. It's also strange that I am too lazy. man knows everything at that moment.

Loading shared libraries on demand

• In Linux, can load symbols from shared libraries on demand using functions in dlfcn. h

• Open a shared library for loading:

Void comment dlopen (const char character le, int mode );

Values for mode: combination of RTLD_LAZY (lazy loading of library), RTLD_NOW (load now), RTLD_GLOBAL (make symbols in library available to other libraries yet to beloaded ), RTLD_LOCAL (symbols loaded are accessible only to your
Code)

• Get the address of a symbol loaded from the library:

Void ∗ dlsym (void ∗ handle, const char ∗ symbol_name );

Handle from call to dlopen; returned address is pointer to variable or function identi partitioned ed by symbol_name

• Need to close shared library using le handle after done with symbols in Library:

Int dlclose (void handle );

• These functions are not part of C standard library; need tolink against library libdl:-LDL compiler extends AG

Symbol resolution issues

• Symbols can be de incluned in multiple places

• Suppose we de sort ne our own puts () function

• But, puts () de incluned in C standard library

• When we call puts (), which one gets used?

• Our puts () gets used since ours is static, and puts () in C standard library not resolved until Run-Time

• If statically linked against C standard library, linker extends NDS two puts () de between Nitions and aborts (multiple de between Nitions not allowed)

Symbol resolution issues

• How about if we de sort ne puts () in a shared library and attempt to use it within our programs?

• Symbols resolved in order they are loaded

• Suppose our library containing puts () is libhello. so, located in a standard library directory (like/usr/lib), and we compile our hello. c code against this library:

Athena % gcc-g-Wall hello. c-lhello-o hello. o

• Libraries speci inclued using-l incluagare loaded in order speci inclued, and before C standard library

• Which puts () gets used here?

Athena % gcc-g-Wall hello. c-lc-lhello-o hello. o

A problem with static links in C is naming conflicts. Dynamic Links can solve this problem in sequence and have a taste of heavy load.

Creating libraries

• Libraries contain C code like any other program

• Static or shared libraries compiled from (un-linked) object names les created using gcc

• Compiling a static library:

• Compile, but do not link source les:

Athena % gcc-g-Wall-c infile. c-o outfile. o

• Collect compiled (unlinked) into les into an archive:

Athena % ar-RCS libname. A outfile1.o outfile2.o...

Creating shared libraries

• Compile and do not link into les using gcc:

Athena % gcc-g-wall-FPIC-C infile. C-o OUTFILE. o

•-FPIC option: Create position-independent code, since code will be repositioned during loading

• Link Between les using LD to create a shared object (. So) using le:

Athena % LD-shared-so name libname. So-O libname. So. Version-LC outfile1.o outfile2.o...

• If necessary, add directory to LD_LIBRARY_PATH environment variable, so lD. So can merge nd into Le when loading at run-time

• Con keep gure lD. So for new (or changed) Library:

Athena % ldconfig-V

I don't understand the role of PIC. Isn't symbol having no address in the compilation phase?

B-tree structure

• Binary Search Tree with variable number of children (at leastt, up to 2 T)

• Tree is balanced-all leaves at same level

• Node contains list of "keys"-divide range of elementsin children

Initializing a B-tree

• Initially, B-tree contains root node with no children (leafnode), no keys

• Note: root node exempt from minimum children requirement

Tree B requires that the number of child elements of each node be between the T-1 and 2 T-1, and the root node obviously cannot meet this requirement at the initial stage of growth, so it can be free from the constraints of this rule.

Inserting elements

• Insertion complicated due to maximum number of keys

• At high level:

1. traverse tree down to leaf node

2. if leaf already full, split into two leaves:

(A) move median key element into parent (splitting parent already full)

(B) split remaining keys into twoleaves (one with lower, one with higher elements)

3. add element to sorted list of keys

• Can accomplish in one pass, splitting full parent nodesduring traversal in step 1

The number of neutron elements in the B-Tree node is between the T-1 and 2 T-1, which ensures that the merge and split of the points in the insertion and deletion of the elements meet this requirement. 2t-1 ensures that there is only one intermediate element, the size of the two nodes after the split is T-1, and then adding an element becomes a T and A T-1. When a node is merged, if you delete an element from a node that contains an T-1 element, after deletion, the node will be merged with the adjacent nodes with 2 T-1 elements and the parent elements of the two nodes in the upper layer. A total of 3 T-3 elements will be merged, and more than 2 T-1 will be merged to continue the split, the mean value of 1.5t-1.5 must meet the condition between the T-1 and 2t-1. This condition is also met if both are T-1 and 2t-2 after merging.

Searching a B-tree

• Search like searching a binary search tree:

1. Start at root.

2. If node empty, element not in tree

3. search list of keys for element (using linear or binary search)

4. If element in list, return Element

5. Otherwise, element between keys, and repeat search on child node for that range

• Tree is balanced-search takes O (log n) Time

B-tree search is essentially no different from binary tree search. It is searched by comparison. However, a node in Tree B can have multiple elements. accessing these elements is random access, while Binary Trees need to be accessed through pointers. In this respect, Tree B is more efficient. In addition, Tree B selects the intermediate element of the node during split and split, which makes the tree more balanced. Of course, to ensure B-tree balance, merge and split the most important sibling nodes, and the minimum and maximum number of elements behind the merge and split operations.

Deletion

• Deletion complicated by minimum children Restriction

• When traversing tree to pair nd element, need to ensure childnodes to be traversed have enough keys

• If adjacent child node has atleast t keys, move separating key from parent to child and closestkey in adjacent child to parent

• If no adjacent child nodes haveextra keys, merge child node with adjacent child

• When removing a key from a node with children, need torearrange keys again

• If child before or after removedkey has enough keys, move closest key from child to parent

• If neither child has enough keys, merge both children

• If child not a leaf, have torepeat this process

Delte is the most complicated part. I remember that the last exercise in 6.087 was to implement a B-tree. At that time, the Detle of B-tree made me very stressed, later, we found that the job does not need to implement Delete ....

Priority queue

• Abstract data structure ordering elements by priority

• Elements enqueued with priority, dequeued in order of highest priority

• Common implementations: heap or binary search tree

• Operations: insertion, peek/extract max-priority element, increase element priority

Linear queues are easy to implement. The simplest is to use the insert sorting algorithm. It usually involves a large number of inserts and deletes, so it is better to use a linked list.

Heaps

• Heap-tree with heap-ordering property: priority (child) ≤ priority (parent)

• More sophisticated heaps exist-e.g. binomial heap, Fibonacci heap

• We'll focus on simple binary heaps

• Usually implemented as an array with top element at beginning

• Can sort data using a heap-O (nlogn) worst case in-place sort!

Extracting data

• Heap-ordering property extends maximum priority element at top ofheap

• Can peek by looking at top element

• Can remove top element, move last element to top, and swap top element down with its children until it satis has es heap-ordering property:

1. start at top

2. Unknown nd largest of element And leFTand right child; if element is largest, we are done

3. Otherwise, swap element with largest child and repeat with element in new position

Inserting data/increasing priority

• Insert element at end of heap, set to lowest priority −∞

• Increase Priority of element to real priority:

1. Start at Element

2. If new priority less than parent's, we are done

3. Otherwise, swap element with parent and repeat

The biggest advantage of hierarchy such as tree and heap over linear structure is that the jump distance is relatively large and the access efficiency is high. The disadvantage is that the implementation is slightly complicated. When an element is inserted, the search order of the two is very different. The tree structure is to locate the element layer by layer from top to bottom, while the maximum heap is to put the new element to the bottom, then, you can search from the bottom up to ensure a perfect tree structure. The largest heap can play this game because the element positions in the heap are not absolutely fixed. They do not need to maintain a specific sequence. They only need to satisfy the requirement that the child nodes are smaller than the parent node, so that they can stop when they reach the top node at most, there is no process from top to bottom, so its computing efficiency is also very high.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.