Introduction to Linux
Administration & Programming Intel, Hillsboro, OR Jan 18 2012
Content
Linux Kernel GNU tools Commercial tools Languages & Portability Assembly and C
Linux C++ Make, build, install Perl, Python, PHP GUI frameworks
Linux Programming
Linux Programming
Ada C, C++, C# Java Haskell LUA Pascal
Python Perl PHP Ruby Tcl/Tk Vala
Linux Programming
Group Discussion
1.
2.
3.
4.
5.
Programming, Art or Science? Interpreted vs Compiled Languages Compiler Machines Web Programming Mobile Programming
Linux Programming
Extreme Programming
Linux Programming
Target Software Environment
Same computer Local or remote server Embedded system Mobile systems Web platform DB platform
Linux Programming
Software Projects
System:
Kernel or driver modules Server (web, file, email)
Applications
Local
Tools or Utilities
Installer Compiler
Linux Programming 7
Distribution
Target Audience
Internal Use CD Download from Web site
Frequency
One time Progressive Releases
Linux Programming 8
Case Study
Build a software module that:
Processes data from USB port Communicates via log or email Bundled with a hardware component
What language and distribution would you use?
Linux Programming 9
Daemons
Network services are implemented by daemons
Listen on a particular network port
Usually defined in the services file
Run in the background Have no controlling terminal Use the system log to report any messages Often run from the boot scripts
Linux Programming 10
C++
C++ is one of the most popular programming languages in the world!
statically typed free-form multi-paradigm Compiled
#include <iostream> int main() { std::cout << "Hello, world!\n"; }
Linux Programming
11
C++ History
Began as enhancements to C,
adding classes virtual functions operator overloading multiple inheritance templates exception handling
Linux Programming 12
C++ Uses
Highly Portable!
Most Hardware Support Most Commercial Tools Most Applications
Linux Kernel!
Linux Programming
13
[Link]
Linux Programming
14
Linux Architecture
Linux Programming
15
Kernel Subsystems
Linux Programming
16
Architecture Layer Details
Linux Programming
17
Android Architecture
Linux Programming
18
Kernel Compiling Steps
Compiling step Configure kernel features make Install steps sudo make install sudo make modules_install
Linux Programming
19
Kernel configuration
Defines what features to include in the kernel: Stored in the .config file at the root of kernel sources. Most useful commands to create this config file: make [xconfig | gconfig | menuconfig | oldconfig] To modify a kernel in a GNU/Linux distribution: config files usually released in /boot/, together with kernel images: /boot/config-2.6.17-11-generic The configuration file can also be found in the kernel itself: > zcat /proc/[Link] (if enabled in General Setup -> Kernel .config support)
Linux Programming
20
make xconfig
The graphical interface to configure the Linux kernel
Make sure you read help -> introduction: useful options!
Includes a File browser: easier to load configuration files New search interface to look for parameters
Linux Programming
21
make xconfig example
Linux Programming
22
make xconfig search interface
Looks for a keyword in the description string
Allows to select or unselect found parameters.
Linux Programming
23
Kernel configuration options
Compiled as a module (separate file) CONFIG_ISO9660_FS=m
Compiled statically into the kernel CONFIG_UDF_FS=y
Linux Programming 24
Corresponding .config excerpt
# # CD-ROM/DVD Filesystems # CONFIG_ISO9660_FS=m CONFIG_JOLIET=y CONFIG_ZISOFS=y CONFIG_UDF_FS=y CONFIG_UDF_NLS=y # # DOS/FAT/NT Filesystems # # CONFIG_MSDOS_FS is not set # CONFIG_VFAT_FS is not set CONFIG_NTFS_FS=m # CONFIG_NTFS_DEBUG is not set CONFIG_NTFS_RW=y
Section name
All parameters are prefixed with CONFIG_
Linux Programming
25
make menuconfig
make menuconfig Useful when no graphics are available. Pretty convenient too! Same interface found in other tools: BusyBox, buildroot...
Linux Programming
26
Undoing configuration changes
A frequent problem: After changing several kernel configuration settings, your kernel no longer works. If you don't remember all the changes you made, you can get back to your previous configuration: > cp .[Link] .config All the configuration interfaces of the kernel (xconfig, menuconfig, allnoconfig...) keep this .[Link] backup copy.
Linux Programming
27
Customizing the version string
To identify your kernel image with others built from the same sources (but a different configuration), use the LOCALVERSION setting (in General Setup) Example: # # General setup # CONFIG_LOCALVERSION="-acme1" The uname -r command (in the running system) will return: 2.6.20-acme1
Linux Programming
28
Kernel Compiling Steps
Compiling step Configure kernel features make Install steps sudo make install sudo make modules_install
Linux Programming
29
Compiling Tips
Compiling faster on multiprocessor hosts
make -j <n> Runs several targets in parallel Use it by adding a ccache prefix to the CC and HOSTCC definitions in Makefile CC = ccache $(CROSS_COMPILE)gcc HOSTCC = ccache gcc
Ccache (Compiler cache for C and C++)
Linux Programming
30
More compiling tips
View the full (gcc, ld...) command line: make V=1 Clean-up generated files (to force re-compiling drivers): make clean Remove all generated files Caution: also removes your .config file! make mrproper Also remove editor backup & patch reject files: make distclean
Linux Programming
31
Generated files
What gets created when you run the make command
vmlinux Raw Linux kernel image, non compressed. arch/<arch>/boot/zImage (default image on arm) zlib compressed kernel image arch/<arch>/boot/bzImage (default image on i386) Also a zlib compressed kernel image. Caution: bz means big zipped but not bzip2 compressed!
Linux Programming
32
Files created by make install
/boot/vmlinuz-<version> Compressed kernel image. Same as the one in arch/<arch>/boot /boot/[Link]-<version> Stores kernel symbol addresses /boot/config-<version> Kernel configuration for this version
Linux Programming 33
Files by make modules_install (1)
/lib/modules/<version>/: Kernel modules + extras build/ Everything needed to build more modules for this kernel: Makefile, .config file, module symbol information ([Link]), kernel headers (include/ and include/asm/) kernel/ Module .ko (Kernel Object) files, in the same directory structure as in the sources.
Linux Programming
34
Files by make modules_install (2)
/lib/modules/<version>/ (continued) [Link] Module aliases for module loading utilities. Example line: alias sound-service-?-0 snd_mixer_oss [Link] Module dependencies
[Link] Tells which module a given symbol belongs to.
All the files in this directory are text files. Don't hesitate to have a look by yourself!
Linux Programming 35
Compiling the kernel in a nutshell
make xconfig make sudo make install sudo make modules_install
Linux Programming
36
Kernel and Device Drivers
In order to talk to the kernel, the driver registers with subsystems to respond to events Some Examples of Events:
Opening a file A page fault Plugging a new USB device
Linux Programming 37
Linux Architecture
Applications,
GNU tools,
Shells
User
System Call Interface VFS
Net Sockets TCP/IP driver
IDE, SCSI Floppy driver
Mem Mgr
Proc Mgr
Kernel
NIC driver
HD
FD
Linux Programming
NIC
Hardware
38
User Interface of a Device driver
Since Linux follows the UNIX model, and in UNIX everything is a file, users talk with device drivers through device files Device files are a mechanism, supplied by the kernel, precisely for this direct User-Driver interface. Koolchar is a character device, & thus the user talks to it through a character device file The other common kind of device file is a block device file
Linux Programming
39
NET Subsystem
Linux Programming
40
GNU Tools
Make
AutoMake AutoConf
Gcc, the GNU Compiler for:
C, C++, Fortran, Java, Ada, Go [Link]
Libtool
Linux Programming 41
GNU Architectures
Alpha ARM Atmel AVR Blackfin PA-RISC PDP-11 PowerPC R8C/M16C/M32C
H8/300 HC12 IA-32 (x86)
IA-64 MIPS Motorola 68000
SPARC SPU SuperH
System/390/zSeries VAX x86-64
Linux Programming
42
A Simple Makefile
Linux Programming
43
A Not-So-Simple Makefile
Linux Programming
44
Hello World in C
Linux Programming
45
Hello World
vi `hello.c #include <stdio.h> main() { printf("Hello world!\n"); } `[Link] bin_PROGRAMS = hello hello_SOURCES = hello.c
Linux Programming
46
Building Hello World
% ./configure
checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... Yes checking for gcc... gcc
% make
source='hello.c' object='hello.o' libtool=no \ depfile='.deps/[Link]' tmpdepfile='.deps/[Link]' \ depmode=gcc3 /bin/sh ./depcomp \ gcc -g -O2 -o hello hello.o
Linux Programming 47
Software Building
Hands-on Lab 1
Sample C program and GCC use
Linux Programming
48
Commercial Tools
IBM
Ada, Rational, COBOL
Intel
C++ Compiler Professional for Linux Intel Parallel Studio XE
Zend
[Link]
Linux Programming 49
What are you going to build today?
Linux Programming
50
Powerful Software!
Content Management System
[Link] [Link] [Link]
Client Relationship Management
SugarCRM [Link]
Linux Programming 51
Learning Management System
Source Forge [Link]
Linux Programming
52
Platforms
Linux Programming
53
Early Web Days
2 Tiers:
Web Logic
Linux Programming
54
Scripting
Perl Python PHP
Linux Programming
55
Perl
General-purpose, interpreted, dynamic language
Developed by Larry Wall [a linguist working as a systems administrator for NASA] in 1987 as a general-purpose Unix scripting language to make report processing easier
Popularity in 1990s as a CGI scripting language
The "duct tape that holds the Internet together"
Linux Programming
56
Perl Code
#!/usr/local/bin/perl ## ## printenvdemo CGI program which just prints its environment ## # print "Content-type: text/plain\n\n"; foreach $var (sort(keys(%ENV))) { $val = $ENV{$var}; #!/usr/local/bin/perl $val =~ s|\n|\\n|g; print "Hi there!\n"; $val =~ s|"|\\"|g; print "${var}=\"${val}\"\n"; }
Linux Programming
57
Perl Evaluation
Multiplatform
Open Source Free to use
Early start,
Extensive library!
Complex syntax Not the best performance
Linux Programming 58
Software Building
Hands-on Lab 2
Sample Perl program and use
Linux Programming
59
Python
General-purpose, high-level programming language which emphasizes code readability It claims to combine "remarkable power with very clear syntax with fun Red Hat installer is written in Python!
Its called Anaconda!
Linux Programming 60
Python Code
print 'Hello, world! name = raw_input('What is your name?\n') print 'Hi, %s.' % (name) my_list = ['john', 'pat', 'gary', 'michael'] for i, name in enumerate(my_list): print "iteration %i is %s" % (i, name)
Linux Programming
61
Python Evaluation
Multiplatform Open Source Free to use VERY popular
Complex test environment
Linux Programming 62
Software Building
Hands-on Lab 3
Sample Python program and use
Linux Programming
63
PHP
Server-side scripting language originally designed for web development to produce dynamic web pages
Competes with Microsoft's Active Server Pages (ASP)
PHP can be deployed on most web servers and almost every OS free of charge! Its embedded into an HTML source document, rather than calling an external file to process data
It can directly retrieve or update a database!
Linux Programming
64
PHP Code
class Person { public $firstName; public $lastName; public function __construct($firstName, $lastName = '') { //Optional parameter $this->firstName = $firstName; $this->lastName = $lastName; public function greet() { return "Hello, my name is " . $this->firstName . " " . $this->lastName . "."; } public static function staticGreet($firstName, $lastName) { return "Hello, my name is " . $firstName . " " . $lastName . "."; } }
Linux Programming
65
PHP code results
$he = new Person('John', 'Smith'); $she = new Person('Sally', 'Davis'); $other = new Person('Joe'); echo $he->greet(); // prints "Hello, my name is John Smith." echo '<br />'; echo $she->greet(); // prints "Hello, my name is Sally Davis." echo '<br />'; echo $other->greet(); // prints "Hello, my name is Joe ." echo '<br />'; echo Person::staticGreet('Jane', 'Doe'); // prints "Hello, my name is Jane Doe."
Linux Programming
66
PHP Evaluation
Multiplatform Open Source Free to use VERY popular Extensive libraries!
Complex test environment
Linux Programming 67
Software Building
Hands-on Lab 4
Sample PHP program and use
Linux Programming
68
Integrated Development Environments
Anjuta 3.2.2, GNOME based CodeLite 3.5.5375 cross platform Eclipse 3.7.1 cross platform Kdevelop 4.2.3 KDE based cross platform NetBeans 7.1 cross platform
Linux Programming
69
Summary
Linux Kernel GNU tools Commercial tools Languages & Portability Assembly and C
Linux C++ Make, build, install Perl, Python, PHP GUI frameworks
Linux Programming
70