sysprog4
Click here to load reader
-
Upload
ahmed-mekkawy -
Category
Technology
-
view
714 -
download
0
description
Transcript of sysprog4
![Page 1: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/1.jpg)
C for Linux
Session 4
BY:Eng.Ahmed Teirelbar
Software EngineerShuja' Consulting
![Page 2: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/2.jpg)
Your own .so
● gcc line – in the makefile● Versioning● Overview on how it works
● Dynamic loader● Relocation - fPIC● Sharing - mmap
![Page 3: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/3.jpg)
Code Organization
● Public/Private includes● Reflection of your abstractions● Delivery: install, lib, bin
![Page 4: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/4.jpg)
Multi-file Makefiles
● Example● .h Dependency ● Multiple directories
![Page 5: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/5.jpg)
Command Line Processing
● argc, argv● usage()● get_opt – libsysfs example
– optarg
– optind
● getopt_long – ls example– options structure
![Page 6: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/6.jpg)
File I/O Intro
● FILE * (stream) interface– fopen/fclose
– fread/fwrite binary
– fprintf/fscanf formatted
● Descriptor interface– open/close
– read/write
● Note: Filesystem is an abstraction
![Page 7: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/7.jpg)
Function Pointers and Callback
● ret (*name)(args)● Can store (ptr) and execute (fun)● Callback concept
– Flexible behavior
– Decoupling/Abstraction
![Page 8: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/8.jpg)
User Configuration
● Simple guidelines:– Code encapsulates the parsing
– Collect the data once
● (Attribute, value) pairs– Xinetd
![Page 9: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/9.jpg)
Logging
● Your eyes on the software● Debug levels
– Run-time (debug.on/msg in xinetd)
– Compile-time
● Lookup tables with functions– Xinetd - parser
![Page 10: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/10.jpg)
API Examples – Libsysfs
● Safe string macros● Attr/Device/Bus/Driver/Module structures and
corresponding functions (forced by model)● Private/public includes and functions● dlist: generic, separate testing (could be
opaque)● Compare callback
![Page 11: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/11.jpg)
Linked List
● Concepts– Node
– Next/prev
– Head
– Tail
– Size
– Singly vs Doubly Linked
![Page 12: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/12.jpg)
Linked List – Cont'd
● Operations– add/add_tail
– remove
● Example from kernel list● Example from libsysfs dlist
![Page 13: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/13.jpg)
BIGNUM
● Independent functionality● Modularity ● Decoupling● Testability
● Abstraction – dealing with bignums
![Page 14: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/14.jpg)
BIGNUM
● Some of what we have discussed● Initialization/Termination● Memory alloc macros● Expansion macro● Debug macro● BN_MASK2 -- SIXTY_FOUR
![Page 15: sysprog4](https://reader038.fdocuments.net/reader038/viewer/2022100420/558ed8d71a28ab34628b45b4/html5/thumbnails/15.jpg)
Void * and Handle
● Void * ● Opaqueness● Handle - Reference counting ● Handle - Pimpl Idiom (*)