intel day3

70
Introduction to Linux Administration & Programming Intel, Hillsboro, OR Jan 18 2012

Upload: amikeliunas

Post on 05-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 1/70

Introduction to Linux

Administration & ProgrammingIntel, Hillsboro, OR

Jan 18 2012

Page 2: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 2/70

2

Content

Linux Kernel

GNU tools

Commercial tools Languages &

Portability

Assembly and C

Linux C++

Make, build, install

Perl, Python, PHP GUI frameworks

Linux Programming

Page 3: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 3/70

Linux Programming

Ada

C, C++, C#

Java Haskell

LUA

Pascal

Python

Perl

PHP Ruby

Tcl/Tk

Vala

Linux Programming 3

Page 4: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 4/70

Group Discussion

1. Programming, Art or Science?

2. Interpreted vs Compiled Languages

3. Compiler Machines

4. Web Programming

5. Mobile Programming

Linux Programming 4

Page 5: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 5/70

Extreme Programming

Linux Programming 5

Page 6: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 6/70

Target Software Environment

Same computer

Local or remote server

Embedded system

Mobile systems

Web platform

DB platform

Linux Programming 6

Page 7: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 7/70

Software Projects

System:

Kernel or driver modules

Server (web, file, email) Applications

Local

Tools or Utilities Installer

Compiler

Linux Programming 7

Page 8: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 8/70

Distribution

Target Audience

Internal Use

CD

Download from Web site

Frequency

One time

Progressive Releases

Linux Programming 8

Page 9: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 9/70

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 wouldyou use?

Linux Programming 9

Page 10: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 10/70

Daemons

Network services are implemented bydaemons

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

10Linux Programming

Page 11: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 11/70

C++

C++ is one of the most popularprogramming languages in the world!

statically typed free-form

multi-paradigm

Compiled

Linux Programming 11

#include <iostream>

int main()

{ std::cout << "Hello, world!\n";}

Page 12: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 12/70

C++ History

Began as enhancements to C,

adding classes

virtual functions operator overloading

multiple inheritance

templates exception handling

Linux Programming 12

Page 13: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 13/70

C++ Uses

Highly Portable!

Most Hardware Support

Most Commercial Tools Most Applications

Linux Kernel!

Linux Programming 13

Page 14: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 14/70

kernel.org

Linux Programming 14

Page 15: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 15/70

Linux Programming 15

Linux Architecture

Page 16: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 16/70

Kernel Subsystems

Linux Programming 16

Page 17: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 17/70

Architecture Layer Details

Linux Programming 17

Page 18: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 18/70

Linux Programming 18

Android Architecture

Page 19: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 19/70

Kernel Compiling Steps

Compiling step

Configure kernel features

make

Install steps

sudo make install

sudo make modules_install

Linux Programming 19

Page 20: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 20/70

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 kernelimages: /boot/config-2.6.17-11-generic

The configuration file can also be found in the kernel itself: 

> zcat /proc/config.gz

(if enabled in General Setup -> Kernel .config support)

Linux Programming 20

Page 21: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 21/70

make xconfig

The graphical interface to configure theLinux kernel

Make sure you readhelp -> introduction: useful options!

Includes a File browser: easier to loadconfiguration files

New search interface to look forparameters

Linux Programming 21

Page 22: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 22/70

make xconfig example

Linux Programming 22

Page 23: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 23/70

make xconfig search interface

Linux Programming 23

Looks for a keywordin the description

string

Allows to selector unselect foundparameters.

Page 24: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 24/70

Kernel configuration options

Linux Programming 24

Compiled as a module (separate file) CONFIG_ISO9660_FS=m

Compiled statically into the kernel CONFIG_UDF_FS=y

Page 25: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 25/70

Corresponding .config excerpt #

# CD-ROM/DVD Filesystems#CONFIG_ISO9660_FS=mCONFIG_JOLIET=yCONFIG_ZISOFS=yCONFIG_UDF_FS=yCONFIG_UDF_NLS=y

## DOS/FAT/NT Filesystems#

# CONFIG_MSDOS_FS is not set# CONFIG_VFAT_FS is not setCONFIG_NTFS_FS=m# CONFIG_NTFS_DEBUG is not setCONFIG_NTFS_RW=y

Linux Programming 25

Section name

All parameters are prefixedwith CONFIG_ 

Page 26: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 26/70

make menuconfig

Linux Programming 26

make menuconfig

Useful when no graphicsare available. Prettyconvenient too!

Same interface found inother tools: BusyBox,buildroot...

Page 27: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 27/70

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 .config.old .config

All the configuration interfaces of the kernel(xconfig, menuconfig, allnoconfig...)

keep this .config.old backup copy.

Linux Programming 27

Page 28: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 28/70

Customizing the version string

To identify your kernel image with others built from the samesources (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

Page 29: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 29/70

Kernel Compiling Steps

Compiling step

Configure kernel features

make

Install steps

sudo make install

sudo make modules_install

Linux Programming 29

Page 30: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 30/70

Compiling Tips

Compiling faster on multiprocessor hosts

make -j <n>Runs several targets in parallel

Ccache (Compiler cache for C and C++)

Use it by adding a ccache prefixto the CC and HOSTCC definitions in Makefile

CC = ccache $(CROSS_COMPILE)gcc HOSTCC = ccache gcc

Linux Programming 30

Page 31: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 31/70

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 filesCaution: also removes your .config file!make mrproper

Also remove editor backup & patch reject files:make distclean

Linux Programming 31

Page 32: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 32/70

Generated filesWhat 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

Page 33: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 33/70

Files created by make install

 /boot/vmlinuz-<version> Compressed kernel image. Same as the

one in arch/<arch>/boot /boot/System.map-<version> 

Stores kernel symbol addresses

 /boot/config-<version> Kernel configuration for this version

Linux Programming 33

Page 34: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 34/70

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(module.symVers), kernel headers (include/ and include/asm/ )

kernel/  Module .ko (Kernel Object) files, in the same directory structureas in the sources.

Linux Programming 34

Page 35: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 35/70

Files by make modules_install (2)

 /lib/modules/<version>/ (continued)

modules.alias Module aliases for module loading utilities. Example line:alias sound-service-?-0 snd_mixer_oss

modules.dep Module dependencies

modules.symbols 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

Page 36: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 36/70

Compiling the kernel in a nutshell

make xconfigmake

sudo make installsudo make modules_install

Linux Programming 36

Page 37: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 37/70

Kernel and Device Drivers

In order to talk to the kernel, the driverregisters with subsystems to respond to

events Some Examples of Events:

Opening a file

A page fault Plugging a new USB device

Linux Programming 37

Page 38: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 38/70

Linux Architecture 

Applications,

GNU tools,

Shells 

System Call Interface 

VFS 

HD 

IDE, SCSI 

Mem Mgr  Proc MgrNet Sockets

TCP/IP driver

NIC driverFloppy driver 

FD NIC 

User 

Kernel 

Hardware 

38Linux Programming

Page 39: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 39/70

User Interface of a Device driver

Since Linux follows the UNIX model, and in UNIXeverything is a file, users talk with device driversthrough 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 talksto it through a character device file

The other common kind of device file is a blockdevice file

Linux Programming 39

Page 40: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 40/70

NETSubsystem

Linux Programming 40

Page 41: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 41/70

GNU Tools

Make

AutoMake

AutoConf Gcc, the GNU Compiler for:

C, C++, Fortran, Java, Ada, Go

http://gcc.gnu.org  Libtool

Linux Programming 41

Page 42: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 42/70

GNU Architectures

Alpha PA-RISC

ARM PDP-11

Atmel AVR PowerPC

Blackfin R8C/M16C/M32CH8/300 SPARC

HC12 SPU

IA-32 (x86) SuperH

IA-64 System/390/zSeries

MIPS VAX

Motorola 68000 x86-64

Linux Programming 42

Page 43: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 43/70

A Simple Makefile

Linux Programming 43

Page 44: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 44/70

A Not-So-Simple Makefile

Linux Programming 44

Page 45: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 45/70

Hello World in C

Linux Programming 45

Page 46: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 46/70

Hello World

vi `hello.c„ 

#include <stdio.h>

main()

{printf("Hello world!\n");

}

`Makefile.am„ 

bin_PROGRAMS = hello

hello_SOURCES = hello.c

Linux Programming 46

Page 47: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 47/70

Building Hello World

% ./configurechecking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... Yes

checking for gcc... gcc

… 

% makesource='hello.c' object='hello.o' libtool=no \ 

depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' \ depmode=gcc3 /bin/sh ./depcomp \  … 

gcc -g -O2 -o hello hello.o

Linux Programming 47

Page 48: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 48/70

Software Building

Hands-on Lab 1

Sample C program and GCC use

Linux Programming 48

Page 49: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 49/70

Commercial Tools

IBM

Ada, Rational, COBOL

Intel®  C++ Compiler Professional for Linux

Intel® Parallel Studio XE

Zend http://framework.zend.com/  

Linux Programming 49

Page 50: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 50/70

What are you going to build today?

Linux Programming 50

Page 51: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 51/70

Powerful Software!

Content Management System

Drupal.org

Joomla.org Wordpress.org

Client Relationship Management

SugarCRM Learning Management System

Moodle.org

Linux Programming 51

Page 52: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 52/70

Source Forge SF.NET

Linux Programming 52

Page 53: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 53/70

Platforms

Linux Programming 53

Page 54: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 54/70

Early Web Days

2 Tiers:

Web

Logic

Linux Programming 54

Page 55: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 55/70

Scripting

Perl

Python

PHP

Linux Programming 55

Page 56: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 56/70

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 Unixscripting 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

Page 57: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 57/70

Perl Code

#!/usr/local/bin/perl#### printenv—demo CGI program which just prints its environment##

#print "Content-type: text/plain\n\n";foreach $var (sort(keys(%ENV))) {$val = $ENV{$var};$val =~ s|\n|\\n|g;

$val =~ s|"|\\"|g;print "${var}=\"${val}\"\n";

}

Linux Programming 57

#!/usr/local/bin/perl

print "Hi there!\n"; 

Page 58: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 58/70

Perl Evaluation

Multiplatform

Open Source

Free to use Early start,

Extensive library!

Complex syntax Not the best performance

Linux Programming 58

Page 59: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 59/70

Software Building

Hands-on Lab 2

Sample Perl program and use

Linux Programming 59

Page 60: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 60/70

Python

General-purpose, high-levelprogramming language which

emphasizes code readability It claims to combine "remarkable power

with very clear syntax“ with fun

Red Hat installer is written in Python! It‟s called Anaconda! 

Linux Programming 60

Page 61: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 61/70

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

Page 62: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 62/70

Python Evaluation

Multiplatform

Open Source

Free to use VERY popular

Complex test environment

Linux Programming 62

Page 63: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 63/70

Software Building

Hands-on Lab 3

Sample Python program and use

Linux Programming 63

Page 64: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 64/70

PHP

Server-side scripting language originally designedfor web development to produce dynamic web pages Competes with Microsoft's Active Server Pages (ASP)

PHP can be deployed on most web servers andalmost every OS free of charge!

It‟s 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

Page 65: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 65/70

PHP Codeclass 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

Page 66: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 66/70

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

Page 67: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 67/70

PHP Evaluation

Multiplatform

Open Source

Free to use VERY popular

Extensive libraries!

Complex test environment

Linux Programming 67

Page 68: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 68/70

Software Building

Hands-on Lab 4

Sample PHP program and use

Linux Programming 68

Page 69: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 69/70

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

Page 70: Intel Day3

8/2/2019 Intel Day3

http://slidepdf.com/reader/full/intel-day3 70/70

Summary

Linux Kernel

GNU tools

Commercial tools Languages &

Portability

Assembly and C

Linux C++

Make, build, install

Perl, Python, PHP GUI frameworks