1、Linux内核分析与应用进阶,2007,7,Linux!,Linux是通过网络合作写成的一个Free/Open的操作系统内核。它采用单一内核(monolithic)结构,而支持内核模块运行时动态加载(Loadable Kernel Module)。广义的Linux是指结合了大量的GNU应用软件的发行版本,著名的有Redhat、Debian、Mandrake等。Linux系统在服务器、安全、专用及嵌入式系统等领域有应用潜力。Linux是一个实际的系统,是一个优秀的学习对象。对于深入了解操作系统的原理,探索系统软件的开发方式都很有好处。 Linux是一个软件系统模式,是一种精神,一种信念。,ToC
2、,1 Linux is a UNIX clone 2 GNU/GPL许可 3 Linux目录结构 4 GNU/Linux 软件包 5 Linux发行版 6 Linux试用 7 Linux管理 8 Programming in Linux,【1】 Linux is a Unix clone,Windows http:/ UNIX AT&T - Novell - SCO BSD FreeBSD、NetBSD、 OpenBSD Macintosh OSOS目录 http:/tunes.org/Review/OSes.html http:/ AIX SGI Irix Compaq Tru64 Unix
3、 HP-UX SCO UnixWare / OpenServer Sun Solaris 关于差异和IEEE/POSIXBSD:FreeBSD, OpenBSD MINIX、Nachos,UNIXs Tree,Solaris 9,The Single UNIX,The Single UNIX Specification, Version 3 Base Definitions - a list of definitions and conventions used in the specifications and a list of C header files Shell and Utili
4、ties System Interfaces - a list of available C system calls which must be provided Rationale - the explanation behind the standard. IEEE/ISO POSIX - Portable Operating System Interface for uniX based on the BSD and System V releases non-Unix systems such as Microsofts Windows NT are POSIX compliant
5、POSIX thread,Linux功能,兼容unix 多用户环境 多进程、SMP 进程调度 进程间通信 IPC、Pipe、Socket 网络支持 TCP/IPv4/IPv6,各种硬件平台支持 CPUs 存储管理 内存管理 磁盘交换空间 高速缓存 动态和共享库(.so) 文件系统 ext2、ext3、FAT、 NTFS、ISO9660,Linux特色,特色 Open 开发工作活跃 可靠性 性能 移植性好 性价比,硬件要求低 可为我所用,前景和趋势 服务器领域 桌面 嵌入式商业化cn,Linux Kernel,内核源代码(2.6.21.5)bz2压缩有40M,展开有200多M计21k多个文件,编
6、译后可压缩为约几M的核心(vmlinuz),及几十M的驱动文件。 在开机启动运行后,内核提供了300多个系统调用,这是内核和用户进程交互的接口。 内核管理CPU、存储器、外设及进程调度等。 内核功能可以在运行过程中以内核模块(kernel module)的方式得以扩充。,Linux当前状态,Linux Kernel 2.6.21.5 GNU/Linux Distribution Fedora 7, Redhat Enterprise Linux 5 Debian 3.1/4.0, Ubuntu 7.04 Office Suite: staroffice / openoffice Web/App
7、lication Server Develop Game,Linux应用现状,应用 网络服务器 (Netcraft) 集群和分布式 (Top500和Beowulf/Networx) 嵌入式/设备驱动 安全领域 进一步 个人桌面应用 服务和支持 付费/盈利方式 教学与科研,Linux in Top500,Top500 http:/www.top500.org/ Linux Networx http:/ Beowulf based on Linux built on commodity hardware deploying Linux OS and open source software htt
8、p:/www.beowulf.org/ Self-made,【2】 GNU/GPL:Free/Open,The GNU Project Open Sources Richard Stallman Eric Raymond Linux,gcc,Emacs GPL: GNU General Public License GPL v3 http:/gplv3.fsf.org/ 其他License BSD Apache Mozilla http:/www.opensource.org/licenses/,许可比较,MS, GPL, BSD, Apache Following Wheeler 2001,
9、 free licenses: 15185987 (50.36%) GPL 2498084 (8.28%) MIT2305001 (7.64%) LGPL 2065224 (6.85%) MPL1315348 (4.36%) BSD 907867 (3.01%) BSD-like455980 (1.51%) GPL, LGPL 323730 (1.07%) GPL/MIT321123 (1.07%) Artistic or GPL 比较 GNU保护用户的利益,要求GNU被作为商业软件时必须附带源代码 BSD许可更宽松,允许商业随便使用 于商业软件相比,自由软件不提供功能承诺和服务支持 Free
10、和Open之别,GNU及其他,The GNU Project GNU:GNUs Not Unix! http:/www.gnu.org/ http:/www.fsf.org/ Richard Stallman http:/www.stallman.org/ http:/ GPL, gcc, emacs, Free Software,开源运动,Eric Raymond Cathedral vs. Bazaar http:/catb.org/esr/ http:/www.opensource.org/ http:/catb.org/esr/writings/ http:/catb.org/esr/
11、faqs/ http:/catb.org/esr/jargon/ http:/ http:/gro.clinux.org/ http:/ OpenSource vs. Free Software,业余 vs. 专业,Open/Free软件的开发 人员多数是世界各地的志愿者开发的,如操作系统,编译器,编辑器,Web Server等。这些软件不需要非计算机领域的知识,并且它们的用户多是有熟练计算机知识的人。 而专业软件,如CAD、IC设计、企业管理等软件则不是志愿者能够完成的。,版权保护 还是开放,计算机软件保护条例 2002年1月1日版,第17、24条关于FreeSoftware和OpenSou
12、rce学习并利用前面提到的资料和参考网址,写一个关于GNU/Open/Free的报告和总结,并阐述自己的看法 M$怎么想?,【3】 Linux运行时目录结构,以Redhat/Fedora系列为例 /bin linux常用的命令,如cp,ls,ping,mount,vi,tar,kill,grep,rpm,dd,ps,ln,gzip,cat等 /boot 启动文件,如vmlinuz /dev 设备文件,如fd0,hda,hda1,hdb,sda,sda1,cdrom(hdc),mouse(psaux),等 /etc 配置文件,如hosts,lilo.conf,inittab,rc.d(dir),
13、rc.d/rc0-6.d,samba/smb.conf等,/home, /root 每个用户在home下有一个自己的目录。 root的在/root 权限和保护 /lost+found Lost? found! /lib 共享库相关文件 .so /lib/modules/build/include/ /mnt mount文件系统到这个目录 如cdrom,floppy,/var 执行中记录数据 /proc 内存文件,报告程序执行的在线动态信息 如进程id数字目录,sys,net等 /sbin 启动时需要和系统管理员程序工具、配置等 如fsck,lilo,init,fdisk等 /tmp 临时文件夹
14、 /usr 用户应用程序和文件 ./X11R6 http:/www.x.org/,Basic Component / in GNU,Kernel Linux kernel, Hurd, init Shell bash Editor vi, ed, emacs Compiler gcc Apps,【4】 Packages (in Redhat 8/9),(适用于Redhat/Fedora系列发行版)rpm rpm -qa whereis ls rpm -qf /bin/ls rpm -ql coreutils,bash GNU Bourne-Again Shell http:/www.gnu.o
15、rg/software/bash/ 内置命令cd、exec、exit、export、pwd、echo、set 重定向(redirection)stdout2 stderr 管道a | b a的输出做为b的输入,fileutils ls cp dd :dd if=/dev/fd0 of=bflp bs=1440 count=1 ln :ln mkdir mv rm chmod: rwx chgrp chown,procps ps report process status w who is on and what doing sysctl configure kernel parameters
16、at runtime top display top CPU processes uptime Tell how long the system has been running pstree /in psmisc iputils ping ping6 traceroute(tracepath),textutils cat concatenate sort head tail md5sum split split a file into pieces wc print the number of bytes, words, and lines in files sh-utils date sl
17、eep su who uname print system information,util-linux kill more mkfs make a linux file system mkswap set up a linux swap area fdisk whereis dmesg print or control the kernel ring buffer,coreutils su pwd uname md5sum who du printf ,e2fsprogs fsck,mke2fs 例子:内存虚拟盘mkdir /mnt/ramdiskmke2fs /dev/ramdiskmou
18、nt /dev/ramdisk /mnt/ramdisk 例子:文件里的虚拟盘(loopback)dd if=/dev/zero of=myd bs=1k count=1024losetup -e des | -e xor /dev/loop0 mydmke2fs /dev/loop0 mount /dev/loop0 /mnt/mydumount /mnt/mydlosetup -d /dev/loop0 (detach),net-tools ifconfig netstat route arp hostname SysVinit init process control initializ
19、ation reboot poweroff halt shutdown,Binutils as assembler ar archives objdump display information from object files ld the GNU linker,Others mail ftp telnet ssh/putty ssh -l login_name hostname | userhostname command rpm rpm -qf /bin/ls 或 rpm -qa rpm -q fileutils 或 rpm -ql fileutils rpm -i install r
20、pm -U upgrade rpm -e erase -v verbose -h hash,cvs Concurrent Versions System vi gcc/g+ gdb nasm grep ps -A | grep -v init gzip tar cfz / zxf,mountmount -fnrsvw -t vfstype -o options device dir umount less/more find find . -name xxx.doc -print finger user information lookup program gpg pgp man whatis
21、,samba /etc/samba/smb.conf/etc/samba/smbusers smbapsswd -a 启用smb服务 samba start 或 smbd; nmbd 或 用setup或service smb start smbclient -L xxx.xxx.xxx 列目录 smbmount windows中映射即可,genpath = /writable = yes,xinetd the extended Internet services daemon /etc/xinetd.conf /etc/xinetd.dtalk ftp disable yesno telnet
22、 finger在redhat中把ftp后来换成了vsftpd,【5】 Linux Distribution,发行版的共同基本组成 Kernel linux vs, hurd GNU packages bash,gcc,vi,emacs,gnome,gimp Open/Free software apache、openssl、perl,KDE(koffice),PostgreSQL,BIND,SendMail, 其他软件 对标准组件的定制:优点与缺点,不同发行版的区别 Kernel版本 rh71/2.4.2, rh73/2.4.18, rh9/2.4.20 fc1/2.4.22, fc2/2.6
23、.5, fc3/2.6.10 fc4/2.6.11, f7/2.6.21 不同的侧重 服务器、桌面或综合 不同的发行版,不同的安装模式 商业、自由 redhat,debian 本国语言、本地/国际 裁剪、加强 实时的、嵌入的,Linux发行版举例,Fedora / Redhat Debian / Ubuntu Gentoo OpenSUSE Mandriva KNOPPIX Slackware http:/www.linux.org/dist/index.html http:/ 9,Fedora core 1,Fedora core 3,Fedora 7,Redhat AS 4,Redhat
24、AS 5,Debian安装,Debian ?r?安装源 3.1r6a / 4.0 http:/www.debian.org/CD/http-ftp/ 最小安装 debian-?r?-i386-netinst.iso 从U盘安装也是可以的 组件安装工具 # apt-get/aptitude install ? 安装中文环境 # apt-get install zhcon,knoppix,中文linux发行版,北京中科红旗Linux http:/www.redflag- http:/ 国际化 i18ninternationalization http:/www.li18nux.org/ l10nl
25、ocalization (本地化) unicode http:/www.unicode.org/,Redflag4,Redflag work station 5,coCreate,Tiny Linux,小Linux 在12张软盘上 Tomsrtbt http:/ TINY Linux http:/tiny.seul.org/ embed Linux 在DOM(Disk On Module)电子盘 容量限制、可靠性、读写次数 Linux裁减 裁减内核 裁剪文件系统,Linux From Scratch,LFS Linux From Scratch (LFS) is a project that
26、provides you with the steps necessary to build your own custom Linux system.http:/www.linuxfromscratch.org/ Gentoo,Linux Standard Base,LSB http:/www.linuxbase.org/ 标准和流派http:/www.unix.org/,【6】 Linux试用,了解不同的发行版 取得安装介质 决定安装方式 选择安装组件 登录方式 网络设置,了解发行版,上网查询 了解Linux的基础知识 了解不同的Linux发行版之差别和擅长 选择合适的发行版,并注意其最新
27、版本 推荐 Fedora 7 http:/ Debian 3.1/4 http:/www.debian.org/CD/http-ftp/ 中文版 红旗Linux WS5 http:/www.redflag- 全装约6G(Fedora 7) 刻盘安装 从硬盘上的iso文件安装 在虚拟机中安装 VMware http:/ ftp:/211.86.49.110/pub/Linux_in_vmware/ Linux Run-ready in VMware http:/ ftp:/211.86.49.110/pub/Linux_in_vmware/,选择Package,建议 kernel开发得安装 X可
28、以不安装 关于完全安装 必须安装 内核源代码 GCC等开发和编译环境 网络工具,登录和共用,SSH Server:OpenSSH by OpenBSD SSH Client: puTTY Secure Shell Client ssh in cygwinUnix体验中心 http:/www.unix- X,Remote desktop connection “远程桌面连接” rdesktop www.rdesktop.org Xserver for WIN32 xmanager 演示,文件和数据交换,FTP 在Linux上开FTP服务 #service vsftpd start 在Window
29、s上开FTP服务 通过第三方FTP站点交换文件 从Linux访问Windows分区 mount /dev/hdax /mnt/hdax Samba in Linux man samba 在linux中用samba访问windows的共享目录 在Window中映射虚拟驱动器,【7】 Linux管理,Shell磁盘操作服务相关,Shell目录,Shell BASH Shell Program Shell implement,Shell,OS kernel Shell App programscommand cmd explorer/desktop *sh*KDE Gnome,sh,shksh cs
30、h bsh bash zsh,/bin/sh /bin/bash /sbin/nologin /bin/bash2 /bin/ash /bin/bsh /bin/tcsh /bin/csh /bin/ksh /bin/zsh,/etc/shells:,bash,home http:/www.gnu.org/software/bash/bash.html RPM rpm -ql bash“Linux & UnixShell 编程指南”.boook,Bash Programming,例子 /etc/rc.d/init.d/cat tree-a.txt | grep 011 | sort cat t
31、ree-a.txt | | wc* cygwin,Linux中的磁盘,/dev/hda, /dev/hdb /dev/hda1, /dev/hda2, /dev/sda, /dev/sdb /dev/sda1, /dev/sda2, fdisk mke2fs, mkreiserfs, mkswap, swapon e2label,Linux中的服务,xinetd rsh rexec ssh,init,/etc/inittab /etc/rc.d /etc/rc.d/init.d =?= /etc/init.d /etc/rc.d/rc06.d,login,/etc/passwd /etc/sh
32、adowlogout reboot CTL+ALT+DEL poweroffuser management,service,SERVICEDIR=“/etc/init.d“ /etc/init.d/smb #service smb restart,xinetd,* the extended Internet services daemon /etc/xinetd.conf includedir /etc/xinetd.d /etc/xinetd.d/telnet /etc/rc.d/init.d/telnett start | stop | status /etc/serviceshttp:/
33、www.xinetd.org/faq.html,xinetd.conf,xinetd.conf.xls 属性 适用范围 socket_type 所有服务 user Non_internal service only 非内部服务 server Non_internal service only 非内部服务 wait 所有服务 protocol 不在/etc/services中的所有RPC服务和所有其他服务 rpc_vision 所有RPC服务 rpc_number 不列在/etc/rpc中的任何RPC服务 port 不在/etc/services中的非RPC服务,rsh,port 514 /et
34、c/xinetd.d/rshpvm & rshrsh in windows,【8】 Programming in Linux,libc fork open/read/write/close printf,Windows中的进程,进程对象 session、job、process、thread、fiber 进程包含一个自然的主线程,但可以通过beginthread创建新的thread 优先级 空闲、低于正常、正常、高于正常、高、实时(最高和关键) 但是内部优先级分031递增,比如1,6,7,8,9,(10,15)线程 调度 使用starvation机制,即只在没有高优先级进程等待运行时才给低的机会
35、,fork(),fork()wait()signal(),about fork(),fork1.c:#include main() fork(); / 1分为2fork(); / 2分为4puts(“hi”); / 给点提示 output: hihihihi 编译和运行# gcc fork1.c -o fork1#./fork1,about fork(): parent/child,#include int i=5; main() i =6;if (fork()=0) / 在子进程 printf(“in child %d i=%d”, getpid(),i);sleep(2); / 等会els
36、e / 在父进程 printf(“in parent %d i=%d”,getpid(),i);wait(0); / 等儿子退出printf(“end.n”); ,output:in parent 1503 i=6in child 1504 i=6end.,signal,#include #include #include void handler(int i) / 给点提示puts(“in handler, I will exit“);exit(1); ,输出:in handler,-,main() pid_t pid = fork();if (pid!=0) / 在父进程sleep(1);
37、/ 等儿子准备好kill(pid, SIGUSR1); / 给儿子发个信号wait(0); / 等儿子退出puts(“bye”); /else / 在儿子的地盘/ 当收到信号SIGUSR1时, 执行handlersignal(SIGUSR1, handler);sleep(100); / 等死,其实等不了100秒return 0; ,Threads,Thread 一个进程上可以同时有多个线程 共享地址空间,方便通信 切换迅速,支持多CPU Solaris的处理 把线程在用户空间实现,内核感觉不到线程 某用户的线程们被局限在某单一处理器上 Linux的线程实现 内核能区分和调度不同的线程,Thr
38、ead编程例子,在windows中 beginthread()/CreateThread()在linux/unix中 pthread_create()/ 请看 #man pthread_createpthread link option:-lpthreadfork() ,Threads in WIN32,#include #include #include void worker(void* p) while (1)puts(char*)p);return; main() _beginthread(worker, 0, (void*)“A“);_beginthread(worker, 0, (
39、void*)“B“);Sleep(1*1000);puts(“ok“);return 0; ,thread,作业1. 写一个例子程序演示使用pthread_create等线程函数用法。2. 比较其和fork( )的区别。* # man pthread_create,多进程编程,Linux守护进程的编程方法,fork() 变为后台 setsid() 变为sessionleader fork() 禁止重新打开终端 close() 关闭文件 chdir() 改变当前目录inetd相关,Automake,diff,patch,rcs,cvs,rpm,阅读,生成简练的执行文件,阅读 in WINs in Linux,Socket Programming,例子移植,links,Google(”*”) * any subject/key word you like GNU Linux Kernel FreeBSD Apachehttp:/ Movie,The Code http:/ Revolution OS http:/www.revolution- http:/ http:/lxr.linux.no/source/ Fedora 7,Q & A,休息时间到!Lin Fengbo,