If you can't read please download the document
Upload
tuxologynet
View
46
Download
1
Tags:
Embed Size (px)
DESCRIPTION
The slides for a 5 days course in embedded Linux for students with embedded background but no Linux expereince
Citation preview
ProgrammingEmbeddedLinux
Unabletohandlekernelpagingrequestatvirtualaddress4d1b65e8 Unabletohandlekernelpagingrequestatvirtualaddress4d1b65e8 Covers Linux version 2.6.24 pgd=c0280000 pgd=c0280000 Version 1.4 [4d1b65e8]*pgd=00000000[4d1b65e8]*pgd=00000000 Internalerror:Oops:f5[#1] Internalerror:Oops:f5[#1] Moduleslinkedin:Moduleslinkedin:hx4700_udchx4700_udcasic3_baseasic3_base CPU:0 CPU:0 PCisatset_pxa_fb_info+0x2c/0x44 PCisatset_pxa_fb_info+0x2c/0x44 LRisathx4700_udc_init+0x1c/0x38[hx4700_udc] LRisathx4700_udc_init+0x1c/0x38[hx4700_udc] pc:[]lr:[]Nottainted sp:c076df78ip:60000093fp:c076df84 pc:[]lr:[]Nottainted
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
1
RightstoCopyThiskitcontainsworkbytheAttributionShareAlike2.0 Youarefree tocopy,distribute,display,andperformthework tomakederivativeworks tomakecommercialuseofthework Underthefollowingconditions Attribution.Youmustgivetheoriginalauthorcredit. ShareAlike.Ifyoualter,transform,orbuilduponthiswork, youmaydistributetheresultingworkonlyunderalicense identicaltothisone. Foranyreuseordistribution,youmustmakecleartoothersthe licensetermsofthiswork. Anyoftheseconditionscanbewaivedifyougetpermissionfrom thecopyrightholder. Yourfairuseandotherrightsareinnowayaffectedbytheabove. Licensetext:http://creativecommons.org/licenses/bysa/2.0/legalcodeCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
followingauthors: Copyright20042006 MichaelOpdenacker [email protected] http://www.freeelectrons.com Copyright20032006 OronPeled [email protected] http://www.actcom.co.il/~oron Copyright20042008 Codefidenceltd. [email protected] http://www.codefidence.com
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
2
WhatisLinux?LinuxisakernelthatimplementsthePOSIXandSingleUnixSpecification standardswhichisdevelopedasanopensourceproject. UsuallywhenonetalksofinstallingLinux,oneisreferringtoaLinux distribution. AdistributionisacombinationofLinuxandotherprogramsandlibrarythat formanoperatingsystem. Thereexistsmanysuchdistributionforvariouspurposes,fromhighend serverstoembeddedsystems. Theyallsharethesameinterface,thankstotheLSBstandard. Linuxrunson24mainplatformsandsupportsapplicationsrangingfrom ccNUMAsuperclusterstocellularphonesandmicrocontrollers. Linuxis15yearsold,butisbasedonthe40yearsoldUnixdesignphilosophy.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
3
LayersinaLinuxSystem
Kernel KernelModules Clibrary Systemlibraries Applicationlibraries Userprograms
Userprograms
Kernel Clibrary
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
4
Linuxvs.LegacyRTOSProcess Library Process
KernelProcess
Shell
RTOS
Process Process
RTOS are like the Linux kernel: Single program with single memory space that manages memory, scheduling and interrupts. Linux also has user tasks that run in their own memory space. One of them is the shell.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
5
ThisCourseProcess Library Process Shell
KernelProcess Process Process
In this course we will go from the shell, through the system libraries and applications and unto the kernel.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
6
TableofContentBasicInteractionFilesandfilesystem Theshell Processes Settingupnetworking
KernelSourcelayout Codingconvention Versions Kernelmodules MemoryManagement Characterdevice Scheduling Softandhardinterrupts NetworkingForfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
ApplicationProgrammingMakefile Processes Threads IPCCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
7
TheUnixandGNU/LinuxCommandLine
TheUnixFilesystem
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
8
EverythingisaFileAlmosteverythinginUnixisafile! Regularfiles Directories Directoriesarejustfiles listingasetoffiles Symboliclinks Filesreferringtothenameof anotherfile Devicesandperipherals Readandwritefromdevicesas withregularfiles Pipes Usedtocascadeprograms cat*.log|greperror Sockets Interprocesscommunication
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
9
FilenamesFilenamefeaturessincethebeginningofUnix: Casesensitive. Noobviouslengthlimit. Cancontainanycharacter(includingwhitespace,except/). Filetypesstoredinthefile(magicnumbers). Filenameextensionsnotneededandnotinterpreted.Justusedfor userconvenience. Filenameexamples: README .bashrc index.htm index.htmlCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
WindowsBuglist index.html.old
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
10
FilePathsApathisasequenceofnesteddirectorieswithafileordirectoryat theend,separatedbythe/character. Relativepath:documents/fun/microsoft_jokes.html Relativetothecurrentdirectory Absolutepath:/home/bill/bugs/crash9402031614568 /:rootdirectory. Startofabsolutepathsforallfilesonthesystem(evenforfileson removabledevicesornetworkshared).
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
11
GNU/LinuxFilesystemStructure(1)Notimposedbythesystem.Canvaryfromonesystemtotheother, evenbetweentwoGNU/Linuxinstallations! / /bin/ /boot/ /dev/ /etc/ /home/ /lib/ Rootdirectory Basic,essentialsystemcommands Kernelimages,initrdandconfigurationfiles Filesrepresentingdevices /dev/hda:firstIDEharddisk Systemconfigurationfiles Userdirectories BasicsystemsharedlibrariesForfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
12
GNU/LinuxFilesystemStructure(2)/lost+found /mnt/ /opt/ /proc/ /root/ /sbin/ /sys/ Corruptfilesthesystemtriedtorecover Mountedfilesystems /mnt/usbdisk/,/mnt/windows/... Specifictoolsinstalledbythesysadmin /usr/local/oftenusedinstead Accesstosysteminformation /proc/cpuinfo,/proc/version... rootuserhomedirectory Administratoronlycommands Systemanddevicecontrols (cpufrequency,devicepower,etc.)Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
13
GNU/LinuxFilesystemStructure(3)/tmp/ /usr/ /usr/local/ /var/ Temporaryfiles Regularusertools(notessentialtothesystem) /usr/bin/,/usr/lib/,/usr/sbin... Specificsoftwareinstalledbythesysadmin (oftenpreferredto/opt/) Datausedbythesystemorsystemservers /var/log/,/var/spool/mail(incoming mail),/var/spool/lpd(printjobs)...
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
14
TheUnixandGNU/LinuxCommandLine
ShellsandFileHandling
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
15
CommandLineInterpretersShells:toolstoexecuteusercommands. Calledshellsbecausetheyhidethedetailsonthe underlyingoperatingsystemundertheshell'ssurface. Commandsareenteredusingatextterminal:eitherawindow inagraphicalenvironment,oratextonlyconsole. Resultsarealsodisplayedontheterminal.Nographicsare neededatall. Shellscanbescripted:providealltheresourcestowrite complexprograms(variable,conditionals,iterations...)Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
16
lsCommandListsthefilesinthecurrentdirectory,inalphanumericorder, exceptfilesstartingwiththe.character. lsa(all) Listsallthefiles(including.* files) lsl(long) Longlisting(type,date,size, owner,permissions) lst(time) Liststhemostrecentfilesfirst lsS(size) Liststhebiggestfilesfirst lsr(reverse) Reversesthesortorder lsltr(optionscanbe combined) Longlisting,mostrecentfilesat theend
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
17
FilenamePatternSubstitutionsBetterintroducedbyexamples: ls*txt Theshellfirstreplaces*txtbyallthefileanddirectorynames endingbytxt(including.txt),exceptthosestartingwith., andthenexecutesthelscommandline. lsd.* Listsallthefilesanddirectoriesstartingwith. dtellslsnottodisplaythecontentsofdirectories. ls?.log Listsallthefileswhichnamesstartby1characterandendby .logCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
18
SpecialDirectories(1)./ Thecurrentdirectory.Usefulforcommandstakingadirectory argument.Alsosometimesusefultoruncommandsinthecurrent directory(seelater). So./readme.txtandreadme.txtareequivalent. ../ Theparent(enclosing)directory.Alwaysbelongstothe.directory (seelsa).Onlyreferencetotheparentdirectory. Typicalusage: cd..Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
19
ThecdandpwdCommandscd Changethecurrentdirectoryto. pwd Displaysthecurrentdirectory("workingdirectory").
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
20
ThecpCommandcp Copiesthesourcefiletothetarget. cpfile1file2file3...dir Copiesthefilestothetargetdirectory(lastargument). cpi(interactive) Asksforuserconfirmationifthetargetfilealreadyexists cpr(recursive) Copiesthewholedirectory.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
21
ThemvandrmCommandsmv (move) Renamesthegivenfileordirectory. mvi(interactive) Ifthenewfilealreadyexits,asksforuserconfirm rmfile1file2file3... (remove) Removesthegivenfiles. rmi(interactive) Alwaysaskforuserconfirm. rmrdir1dir2dir3(recursive) Removesthegivendirectorieswithalltheircontents.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
22
CreatingandRemovingDirectoriesmkdirdir1dir2dir3...(makedir) Createsdirectorieswiththegivennames. rmdirdir1dir2dir3...(removedir) Removesthegivendirectories Safe:onlyworkswhendirectoriesandempty. Alternative:rmr(doesn'tneedemptydirectories).
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
23
DisplayingFileContentsSeveralwaysofdisplayingthecontentsoffiles. catfile1file2file3...(concatenate) Concatenatesandoutputsthecontentsofthegivenfiles. morefile1file2file3... Aftereachpage,askstheusertohitakeytocontinue. Canalsojumptothefirstoccurrenceofakeyword (/command). lessfile1file2file3... Doesmorethanmorewithless. Doesn'treadthewholefilebeforestarting. Supportsbackwardmovementinthefile(?command).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
24
TheUnixandGNU/LinuxCommandLine
TaskControl
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
25
FullControlOverTasksSincethebeginning,Unixsupportstruepreemptive multitasking. Abilitytorunmanytasksinparallel,andabortthemevenif theycorrupttheirownstateanddata. Abilitytochoosewhichprogramsyourun. Abilitytochoosewhichinputyourprogramstakes,and wheretheiroutputgoes.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
26
ProcessesEverythinginUnixisafile EverythinginUnixthatisnotafileisaprocess Processes Instancesofarunningprograms Severalinstancesofthesameprogramcanrunatthesametime Dataassociatedtoprocesses: Openfiles,allocatedmemory,processid,parent,priority,state...
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
27
RunningJobsinBackgroundSameusagethroughoutalltheshells. Useful:Forcommandlinejobswhichoutputcanbeexaminedlater, especiallyfortimeconsumingones. Tostartgraphicalapplicationsfromthecommandlineand thencontinuewiththemouse.
Startingatask:add&attheendofyourline:find_prince_charmingcutecleverrich&
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
28
BackgroundJobControljobs Returnsthelistofbackgroundjobsfromthesameshell[1]Running~/bin/find_meaning_of_lifewithoutgod& [2]+Runningmakemistakes&
fg fg% Putsthelast/nthbackgroundjobinforegroundmode Movingthecurrenttaskinbackgroundmode: [Ctrl]Z bg kill% Abortsthenthjob.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
29
JobControlExample>jobs [1]Running~/bin/find_meaning_of_lifewithoutgod& [2]+Runningmakemistakes& >fg makemistakes >[Ctrl]Z [2]+Stoppedmakemistakes >bg [2]+makemistakes& >kill%1 [1]+Terminated~/bin/find_meaning_of_lifewithoutgod
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
30
ListingAllProcesses...whatevershell,scriptorprocesstheyarestartedfrom psux Listsalltheprocessesbelongingtothecurrentuser psaux(Note:psedfonSystemVsystems) Listsalltheprocessesrunningonthesystempsaux USERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMAND bart30390.00.259161380pts/2S14:350:00/bin/bash bart31340.00.253881380pts/3S14:360:00/bin/bash bart31900.00.263681360pts/4S14:370:00/bin/bash bart34160.00.000pts/2R15:070:00[bash]... PID: VSZ: RSS: TTY: STAT: Processid Virtualprocesssize(code+data+stack) Processresidentsize:numberofKBcurrentlyinRAM Terminal Status:R(Runnable),S(Sleep),D(Uninterruptedsleep),Z(Zombie),T(Traced)Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
31
LiveProcessActivitytopDisplaysmostimportantprocesses,sortedbycpupercentagetop15:44:33up1:11,5users,loadaverage:0.98,0.61,0.59 Tasks:81total,5running,76sleeping,0stopped,0zombie Cpu(s):92.7%us,5.3%sy,0.0%ni,0.0%id,1.7%wa,0.3%hi,0.0%si Mem:515344ktotal,512384kused,2960kfree,20464kbuffers Swap:1044184ktotal,0kused,1044184kfree,277660kcached PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 3809jdoe250625639321312R93.80.80:21.49bunzip2 2769root160157m80m90mR2.716.05:21.01X 3006jdoe1503092815m27mS0.33.00:22.40kdeinit 3008jdoe16056248924468S0.30.20:06.59autorun 3034jdoe1502676412m24mS0.32.50:12.68kscd 3810jdoe16028929161620R0.30.20:00.06top
Youcanchangethesortingorderbytyping M:Memoryusage,P:%CPU,T:Time. Youcankillataskbytypingkandtheprocessid.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
32
KillingProcesses(1)kill Sendsaterminationsignal(SIGTERM)tothegivenprocesses.Lets processessavedataandexitbythemselves.Shouldbeusedfirst. Example: kill3039313431903416 kill9 Sendsanimmediateterminationsignal(SIGKILL).Thesystem itselfterminatestheprocesses.Usefulwhenaprocessisreallystuck (doesn'tanswertokill1). kill91 Killsalltheprocessesofthecurrentuser.1:meansallprocesses.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
33
KillingProcesses(2)killall[] Killsallthejobsrunning.Example: killallbash
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
34
SequentialCommandsCantypethenextcommandinyourterminalevenwhenthe currentoneisnotover. Canseparatecommandswiththe;symbol: echonIlovethee;sleep5;echonot Conditionals:use||(or)or&&(and): moreGod||echoSorry,Goddoesn'texist Runsechoonlyifthefirstcommandfails lsmydir&&catmydir/* Onlycatsthedirectorycontentsifthelscommandsucceeds (meansreadaccess).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
35
TheUnixandGNU/LinuxCommandLine
SystemAdministrationBasics
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
36
ShuttingDownshutdownh+5(h:halt) Shutsthesystemdownin5minutes. Usersgetawarningintheirconsoles. shutdownrnow(r:reboot) init0 Anotherwaytoshutdown(usedbyshutdown). init6 Anotherwaytoreboot(usedbyshutdown). [Ctrl][Alt][Del] AlsoworksonGNU/Linux(atleastonPCs!).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
37
NetworkSetup(1)ifconfiga Printsdetailsaboutallthenetworkinterfacesavailable onyoursystem. ifconfigeth0 Listsdetailsabouttheeth0interface ifconfigeth0192.168.0.100 Assignsthe192.168.0.100IPaddress toeth0(1IPaddressperinterface). ifconfigeth0down Shutsdowntheeth0interface (freesitsIPaddress).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
38
NetworkSetup(2)routeadddefaultgw192.168.0.1 Setsthedefaultrouteforpacketsoutsidethelocalnetwork. Thegateway(here192.168.0.1)isresponsiblefor sendingthemtothenextgateway,etc.,untilthefinal destination. route Liststheexistingroutes routedeldefault routedel Deletesthegivenroute Usefultoredefineanewroute.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
39
NetworkTestingpingfreshmeat.net ping192.168.1.1 Triestosendpacketstothegivenmachineandgetacknowledgment packetsinreturn.PING192.168.1.1(192.168.1.1)56(84)bytesofdata. 64bytesfrom192.168.1.1:icmp_seq=0ttl=150time=2.51ms 64bytesfrom192.168.1.1:icmp_seq=1ttl=150time=3.16ms 64bytesfrom192.168.1.1:icmp_seq=2ttl=150time=2.71ms 64bytesfrom192.168.1.1:icmp_seq=3ttl=150time=2.67ms
Whenyoucanpingyourgateway,yournetworkinterfaceworksfine. WhenyoucanpinganexternalIPaddress,yournetworksettingsare correct!Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
40
NetworkSetupSummaryOnlyforsimplecaseswithoneinterface,noDHCPserver... Connecttothenetwork(cable,wirelesscardordevice...) Identifyyournetworkinterface: ifconfiga AssignanIPaddresstoyourinterface(assumingeth0) ifconfigeth0192.168.0.100(example) Addaroutetoyourgateway(assuming192.168.0.1)for packetsoutsidethenetwork: routeadddefaultgw192.168.0.1Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
41
NameResolutionYourprogramsneedtoknowwhatIPaddresscorrespondstoa givenhostname(suchaskernel.org) DomainNameServers(DNS)takecareofthis. YoujusthavetospecifytheIPaddressof1ormoreDNS serversinyour/etc/resolv.conffile: nameserver217.19.192.132 nameserver212.27.32.177 Thechangestakeseffectimmediately!
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
42
MountingDevices(1)Tomakefilesystemsonanydevice(internalorexternalstorage) visibleonyoursystem,youhavetomountthem. Thefirsttime,createamountpointinyoursystem: mkdir/mnt/usbdisk(example) Now,mountit: mounttvfat/dev/sda1/mnt/usbdisk /dev/sda1:physicaldevice t:specifiesthefilesystem(format)type (ext2,ext3,vfat,reiserfs,iso9660...)
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
43
MountingDevices(2)Lotsofmountoptionsareavailable,inparticulartochoosepermissionsorthe fileownerandgroup...Seethemountmanualpagefordetails. Mountoptionsforeachdevicecanbestoredinthe/etc/fstabfile. Youcanalsomountafilesystemimagestoredinaregularfile(loopback devices) UsefultoaccessthecontentsofanISOcdromimagewithouthavingto burnit. UsefultocreateaLinuxpartitiononaharddiskwithonlyWindows partitions cp/dev/sda1usbkey.img mountolooptvfatusbkey.img/mnt/usbdiskCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
44
ListingMountedFilesystemsJustusethemountcommandwithnoargument:/dev/hda6on/typeext3(rw,noatime) noneon/proctypeproc(rw,noatime) noneon/systypesysfs(rw) noneon/dev/ptstypedevpts(rw,gid=5,mode=620) usbfson/proc/bus/usbtypeusbfs(rw) /dev/hda4on/datatypeext3(rw,noatime) noneon/dev/shmtypetmpfs(rw) /dev/hda1on/wintypevfat(rw,uid=501,gid=501) noneon/proc/sys/fs/binfmt_misctypebinfmt_misc(rw)
Ordisplaythe/etc/mtabfile (sameresult,updatedbymountandumounteachtimetheyarerun)Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
45
UnmountingDevicesumount/mnt/usbdisk Commitsallpendingwritesandunmountsthegivendevice, whichcanthenberemovedinasafeway. Tobeabletounmountadevice,youhavetocloseallthe openfilesinit:Closeapplicationsopeningdatainthemountedpartition Makesurethatnoneofyourshellshaveaworkingdirectoryin thismountpoint. Youcanrunthelsofcommand(listopenfiles)toview whichprocessesstillhaveopenfilesinthemountedpartition.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
46
CreatingFilesystemsExamples mkfs.ext2/dev/sda1 FormatsyourUSBkey(/dev/sda1:1stpartitionrawdata)inext2format. mkfs.ext2Fdisk.img Formatsadiskimagefileinext2format mkfs.vfatvF32/dev/sda1(v:verbose) FormatsyourUSBkeybacktoFAT32format. mkfs.vfatvF32disk.img FormatsadiskimagefileinFAT32format. Blankdiskimagescanbecreatedasinthebelowexample: ddif=/dev/zeroof=disk.imgbs=1024count=65536Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
47
TheUnixandGNU/LinuxCommandLine
Help
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
48
CommandHelpSomeUnixcommandsandmostGNU/Linuxcommandsoffer atleastonehelpargument: h (ismostlyusedtointroduce1characteroptions) help (isalwaysusedtointroducethecorrespondinglong optionname,whichmakesscriptseasiertounderstand) Youalsooftengetashortsummaryofoptionswhenyouinput aninvalidargument.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
49
ManualPagesman[section] Displaysoneorseveralmanualpagesforfrom optional[section]. manfork Manpageofthefork()systemcall manfstab Manpageofthefstabconfigurationfile manprintf Manofprintf()shellcommand man3printf Manofprintf()libraryfunctionCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
50
CodingEmbeddedLinuxApplications
WritingApplications
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
51
ASimpleMakefileMakefile variables
CC=/usr/local/arm/2.95.3/bin/armlinuxgcc CFLAGS=g LDFLAGS=lpthreadsTo build target all build target test
Always build the clean and all targets, if asked
.PHONY:cleanall all:test
These must be a TAB, not a SPACE! To link test from test.o, run the following commandCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
To compile test.o test.o:test.ctest.h from test.c and test.h, run the $(CC)$(CFLAGS)Ctest.c following command
test:test.o $(CC)$(LDFLAGS)test.ootest clean: @rmf*.o*~testForfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
To clean, run the following command
52
ANotSoSimpleMakefile.PHONY:cleanall SRC:=$(shellls*.c) EXE:=test OBJ:=$(SRC:.c=.o) LDFLAGS:=lsqlite3 CFLAGS:=g all:$(EXE)
%.o:%.c $(COMPILE.c)MDo$@$< @cp$*.d$*.P;\ sede's/#.*//'e's/^[^:]*:*//'e's/*\\$$//'\ e'/^$$/d'e's/$$/:/'>$*.P;\ rmf$*.d $(EXE):$(OBJ) $(CC)$(LDFLAGS)o$@$(OBJ) clean: rmf$(EXE)$(OBJ)$(SRC:.c=.P)*~ include$(SRC:.c=.P) #Source: http://make.paulandlesley.org/autodep.html
This Makefile automatically compiles all files ending in .c in the current directory, computes dependency information for them and links them into an executable called test.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
53
HelloWorld!Linux Application API is ISO C and POSIX!#include intmain(intargc,char**argv) { inti=1; printf("HelloWorld!%d\n",i); return0; } Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
54
CreatingProcessesUsingfork()pid_tpid; if(pid=fork()){ intstatus; printf("I'mtheparent!\n"); wait(&status); if(WIFEXITED(status)) printf("Childexistwithstatusof%d\n",WEXITSTATUS(status)); }else{ printf("I'mthechild!\n"); exit(0);
}Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
55
Howfork()SeemstoWorkParent ProcessA complete copy is created of the parent process. Both parent and child continue execution from the same spot.
Copy Parent Process Child Process
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
56
Howfork()ReallyWorksParent Process RW MemoryChild process gets a copy of stack and file descriptors. Copy On Write reference is used for the memory.
Parent Process
RO
Memory
RO
Child Process
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
57
WhatHappensDuringWrite?When write is attempted, a single memory page is copied and references updated. This is called: breaking the CoW.
Original Memory
RO
RW
Child Process
Parent Process
RO
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
58
CreatingProcesses(2)This call will replace the program memory (code and data) with the image from storage at the specified path. Open file descriptors, priority and other properties remains the same.
pid_tpid; if(pid=fork()){ intstatus; printf("I'mtheparent!\n"); wait(&status); }else{ printf("I'mthechild!\n"); execve(/bin/ls,argv,envp); }
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
59
ExitingProcessesAprocessexistswheneitherofthefollowingoccurs:Returnfromthemainfunction. Callingtheexit()function. Exception(moreonthoselater).
Aprocessshouldreturnanexitstatustoit'sparentprocess Uponexit:Allexithandlersarecalled(useatexit()toregisterthem) Allmemory,filedescriptorsandotherresourcesarereleased bythesystem.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
60
LinuxProcessStackLinuxprocessstackisautoexpanding. Adefaultstack(8Mb)isallocatedatprocesscreation. Bydefault,useofadditionalstackwillautomaticallytrigger allocationofmorestackspace. Thisbehaviorcanbelimitedbysettingaresourcelimiton thestacksize.Seesetrlimit()
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
61
LinuxPrioritiesNice level -20 -19 -18 Real Time priority 99 98 97
...16 17 18 19Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
...4 3 2 1 0
Real time processes SCHED_FIFO SCHED_RR
Non real-time processes SCHED_OTHER
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
62
POSIXPriorities(2)SCHED_OTHER:DefaultLinuxtimesharingscheduling Priority0isreservedforit. Fair,nostarvation. Usethisforanynontimecriticaltasks. SCHED_FIFO:FirstInFirstOutscheduling Priorities199. Preemptive. SCHED_RR:RoundRobinscheduling LikeSCHED_FIFO+timesliceCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
63
ChangingRealTimePrioritiesintsched_setscheduler(pid_tpid,intpolicy,conststructsched_param*p); structsched_param{ intsched_priority }; sched_setscheduler()setsboththeschedulingpolicyandtheassociated parametersfortheprocessidentifiedbypid.Ifpidequalszero,the schedulerofthecallingprocesswillbeset.Theinterpretationofthe parameterpdependsontheselectedpolicy.Currently,thefollowingthree schedulingpoliciesaresupportedunderLinux:SCHED_FIFO, SCHED_RR,andSCHED_OTHER; Thereisalsoasched_getscheduler().Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
64
LockingMemoryintmlock(constvoid*addr,size_tlen);mlockdisablespagingforthememoryintherangestartingataddrwithlengthlen bytes.
intmlockall(intflags);mlockall()disablespagingforallpagesmappedintotheaddressspaceofthecalling process. MCL_CURRENTlocksallpageswhicharecurrentlymappedintotheaddress spaceoftheprocess. MCL_FUTURElocksallpageswhichwillbecomemappedintotheaddressspace oftheprocessinthefuture.Thesecouldbeforinstancenewpagesrequiredbya growingheapandstackaswellasnewmemorymappedfilesorsharedmemory regions. Mustlockmemorytoguaranteerealtimeresponses!Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
65
RealTimeResponsesTogetdeterministicrealtimeresponsefromaLinuxprocess, makesureto:Puttheprocessinarealtimeschedulingdomain. mlockall()processmemory. Prefaultstackpages Todothiscalladummyfunctionthatallocatesonstackan automaticvariablebigenoughforyourentirefuturestack usageandwritestoit.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
66
ThreadsandProcessesStack State Signal Mask Priority Stack State Signal Mask Priority Stack State Signal Mask Priority Stack State Signal Mask Priority Stack State Signal Mask Priority
Thread 1Process 123
Thread
Thread
Thread
Thread
1
2Process 124
3
4
File Signal Memory Descriptors Handlers
File Signal Memory Descriptors Handlers
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
67
POSIXThreadsLinuxusesthePOSIXThreadsthreadingmechanism. LinuxthreadsareLightWeightProcesseseachthreadisatask scheduledbythekernel'sscheduler. Processcreationtimeisroughlydoublethanthatofathread's. ButLinuxprocesscreationtimeisrelativelylow... Tousethreadsinyourcode: #include InyourMakefile: Add-lpthreadtoCFLAGS.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
68
CreatingThreadsFunction:pthread_create() intpthread_create(pthread_t*thread,constpthread_attr_t*attr, void*(*start_routine)(void*),void*arg); Thepthread_create()routinecreatesanewthreadwithina process.Thenewthreadstartsinthestartroutinestart_routine whichhasastartargumentarg.Thenewthreadhasattributes specifiedwithattr,ordefaultattributesifattrisNULL. Ifthepthread_create()routinesucceedsitwillreturn0andputthe newthreadIDintothread,otherwiseanerrornumbershallbe returnedindicatingtheerror.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
69
CreatingThreadAttributesFunction:pthread_attr_init() intpthread_attr_init(pthread_attr_t*attr); Settingattributesforthreadsisachievedbyfillingathreadattribute objectattroftypepthread_attr_t,thenpassingitasasecondargument topthread_create(3).PassingNULLisequivalenttopassingathread attributeobjectwithallattributessettotheirdefaultvalues. pthread_attr_init()initializesthethreadattributeobjectattrandfillsit withdefaultvaluesfortheattributes. Eachattributeattrnamecanbeindividuallysetusingthefunction pthread_attr_setattrname()andretrievedusingthefunction pthread_attr_getattrname().Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
70
DestroyingThreadAttributesFunction:pthread_attr_destroy() intpthread_attr_destroy(pthread_attr_t*attr); pthread_attr_destroy()destroysathreadattributeobject,which mustnotbereuseduntilitisreinitialized.pthread_attr_destroy() doesnothingintheLinuxThreadsimplementation. Attributeobjectsareconsultedonlywhencreatinganewthread. Thesameattributeobjectcanbeusedforcreatingseveralthreads. Modifyinganattributeobjectafteracalltopthread_create()does notchangetheattributesofthethreadpreviouslycreated.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
71
DetachStateThreadAttribute:detachstate Controlwhetherthethreadiscreatedinthejoinablestateorinthedetached state.Thedefaultisjoinablestate. Inthejoinablestate,anotherthreadcansynchronizeonthethreadtermination andrecoveritsterminationcodeusingpthread_join(3),butsomeofthethread resourcesarekeptallocatedafterthethreadterminates,andreclaimedonly whenanotherthreadperformspthread_join(3)onthatthread. Inthedetachedstate,thethreadresourcesareimmediatelyfreedwhenit terminates,butpthread_join(3)cannotbeusedtosynchronizeonthethread termination. Athreadcreatedinthejoinablestatecanlaterbeputinthedetachedthread usingpthread_detach(3).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
72
SchedPolicyThreadAttribute:schedpolicy Selecttheschedulingpolicyforthethread:oneof SCHED_OTHER(regular,nonrealtimescheduling), SCHED_RR(realtime,roundrobin)orSCHED_FIFO(realtime, firstinfirstout). Defaultvalue:SCHED_OTHER. TherealtimeschedulingpoliciesSCHED_RRandSCHED_FIFO areavailableonlytoprocesseswithsuperuserprivileges. Theschedulingpolicyofathreadcanbechangedaftercreation withpthread_setschedparam(3).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
73
SchedParamThreadAttribute:schedparam Containtheschedulingparameters(essentially,thescheduling priority)forthethread. Defaultvalue:priorityis0. Thisattributeisnotsignificantiftheschedulingpolicyis SCHED_OTHER;itonlymattersfortherealtimepolicies SCHED_RRandSCHED_FIFO. Theschedulingpriorityofathreadcanbechangedaftercreation withpthread_setschedparam(3).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
74
InheritSchedThreadAttribute:inheritsched Indicatewhethertheschedulingpolicyandschedulingparameters forthenewlycreatedthreadaredeterminedbythevaluesofthe schedpolicyandschedparamattributes (PTHREAD_EXPLICIT_SCHED)orareinheritedfromtheparent thread(valuePTHREAD_INHERIT_SCHED). Defaultvalue:PTHREAD_EXPLICIT_SCHED.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
75
DestroyingThreadsFunction:pthread_exit() voidpthread_exit(void*status); Thepthread_exit()routineterminatesthecurrentlyrunningthread andmakesstatusavailabletothethreadthatsuccessfullyjoins, pthread_join(),withtheterminatingthread.Inaddition, pthread_exit()executesanyremainingcleanuphandlersinthe reverseordertheywerepushed,pthread_cleanup_push(),after whichallappropriatethreadspecificdestructorsarecalled. Animplicitcalltopthread_exit()ismadeifanythread,otherthan thethreadinwhichmain()wasfirstcalled,returnsfromthestart routinespecifiedinpthread_create().Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
76
ThreadandProcessTerminationActionpthread_exit()
Main ThreadThread terminates All threads terminate All threads terminate
Other ThreadThread terminates All threads terminate Thread terminates
exit() Thread function returns
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
77
WaitingForaThreadtoFinishFunction:pthread_join() intpthread_join(pthread_tthread,void**status); Ifthetargetthreadthreadisnotdetachedandtherearenootherthreads joinedwiththespecifiedthreadthenthepthread_join()function suspendsexecutionofthecurrentthreadandwaitsforthetargetthread threadtoterminate.Otherwisetheresultsareundefined. Onasuccessfulcallpthread_join()willreturn0,andifstatusisnon NULLthenstatuswillpointtothestatusargumentofpthread_exit(). Onfailurepthread_join()willreturnanerrornumberindicatingthe error. Alsoexistspthread_tryjoin_np()andpthread_timedjoin_np().Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
78
CancelingThreadsFunction:pthread_cancel() intpthread_cancel(pthread_tthread); Cancellationisthemechanismbywhichathreadcanterminatethe executionofanotherthread.Moreprecisely,athreadcansenda cancellationrequesttoanotherthread.Dependingonitssettings,the targetthreadcantheneitherignoretherequest,honoritimmediately,or deferittillitreachesacancellationpoint. Whenathreadeventuallyhonorsacancellationrequest,itperformsas ifpthread_exit(PTHREAD_CANCELED)hasbeencalledatthatpoint. pthread_cancel()sendsacancellationrequesttothethreaddenotedby thethreadargument.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
79
CancelStateFunction:pthread_setcancelstate() intpthread_setcancelstate(intstate,int*oldstate); pthread_setcancelstate()changesthecancellationstateforthecalling threadthatis,whethercancellationrequestsareignoredornot. Thestateargumentisthenewcancellationstate:either PTHREAD_CANCEL_ENABLEtoenablecancellation,or PTHREAD_CANCEL_DISABLEtodisablecancellation (cancellationrequestsareignored). IfoldstateisnotNULL,thepreviouscancellationstateisstoredinthe locationpointedtobyoldstate,andcanthusberestoredlaterby anothercalltopthread_setcancelstate().Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
80
CancelTypeFunction:pthread_setcanceltype() intpthread_setcanceltype(inttype,int*oldtype); pthread_setcanceltype()changesthetypeofresponsestocancellation requestsforthecallingthread:asynchronous(immediate)ordeferred. Thetypeargumentisthenewcancellationtype:either PTHREAD_CANCEL_ASYNCHRONOUStocancelthecallingthread assoonasthecancellationrequestisreceived,or PTHREAD_CANCEL_DEFERREDtokeepthecancellationrequest pendinguntilthenextcancellationpoint.IfoldtypeisnotNULL,the previouscancellationstateisstoredinthelocationpointedtobyoldtype, andcanthusberestoredlaterbyanothercallto pthread_setcanceltype().Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
81
CancellationPointDefaultsThreadsarealwayscreatedbypthread_create(3)withcancellation enabledanddeferred.Thatis,theinitialcancellationstateis PTHREAD_CANCEL_ENABLEandtheinitialtypeis PTHREAD_CANCEL_DEFERRED.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
82
CancellationPointsCancellationpointsarethosepointsintheprogramexecutionwherea testforpendingcancellationrequestsisperformedandcancellationis executedifpositive. ThefollowingPOSIXthreadsfunctionsarecancellationpoints: pthread_join(3),pthread_cond_wait(3),pthread_cond_timedwait(3), pthread_testcancel(3),sem_wait(3),sigwait(3). AllotherPOSIXthreadsfunctionsareguaranteednottobecancellation points.Thatis,theyneverperformcancellationindeferredcancellation mode. Anumberofsystemcalls(basically,allsystemcallsthatmayblock, suchasread(2),write(2),wait(2),etc.)andlibraryfunctionsthatmay callthesesystemcalls(e.g.fprintf(3))arecancellationpoints.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
83
TestCancelFunction:pthread_testcancel() voidpthread_testcancel(void); pthread_testcancel()doesnothingexcepttestingforpending cancellationandexecutingit.Itspurposeistointroduceexplicit checksforcancellationinlongsequencesofcodethatdonotcall cancellationpointfunctionsotherwise.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
84
LinuxIPCPOSIXIPC SysVIPC Pipes UnixDomainSockets Signals TCP/IPSocketsCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
85
CreatingaMutexFunction:pthread_mutex_init() intpthread_mutex_init(pthread_mutex_t*mutex,const pthread_mutex_attr*attr); Thepthread_mutex_init()routinecreatesanewmutex,with attributesspecifiedwithattr,ordefaultattributesifattrisNULL. Ifthepthread_mutex_init()routinesucceedsitwillreturn0and putthenewmutexIDintomutex,otherwiseanerrornumbershall bereturnedindicatingtheerror.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
86
DestroyingaMutexFunction:pthread_mutex_destroy() intpthread_mutex_destroy(pthread_mutex_t*mutex); Thepthread_mutex_destroy()routinedestroysthemutexspecified bymutex. Ifthepthread_mutex_destroy()routinesucceedsitwillreturn0, otherwiseanerrornumbershallbereturnedindicatingtheerror.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
87
LockingMutexesFunction:pthread_mutex_lock() intpthread_mutex_lock(pthread_mutex_t*mutex); Thepthread_mutex_lock()routineshalllockthemutexspecifiedbymutex.Ifthe mutexisalreadylocked,thecallingthreadblocksuntilthemutexbecomesavailable. Ifthepthread_mutex_lock()routinesucceedsitwillreturn0,otherwiseanerror numbershallbereturnedindicatingtheerror. Function:pthread_mutex_trylock() intpthread_mutex_trylock(pthread_mutex_t*mutex); Thepthread_mutex_trylock()routineshalllockthemutexspecifiedbymutexand return0,otherwiseanerrornumbershallbereturnedindicatingtheerror.Inallcases thepthread_mutex_trylock()routinewillnotblockthecurrentrunningthread.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
88
LockingMutexesFunction:pthread_mutex_timedlock() intpthread_mutex_timedlock(pthread_mutex_t*mutex,structtimespec*restrict abs_timeout); Thepthread_mutex_timedlock()functionshalllockthemutexobjectreferenced bymutex.Ifthemutexisalreadylocked,thecallingthreadshallblockuntilthe mutexbecomesavailableasinthepthread_mutex_lock()function.Ifthemutex cannotbelockedwithoutwaitingforanotherthreadtounlockthemutex,this waitshallbeterminatedwhenthespecifiedtimeoutexpires. Thetimeoutshallexpirewhentheabsolutetimespecifiedbyabs_timeout passes,asmeasuredbytheclockonwhichtimeoutsarebased(thatis,whenthe valueofthatclockequalsorexceedsabs_timeout),oriftheabsolutetime specifiedbyabs_timeouthasalreadybeenpassedatthetimeofthecall.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
89
UnlockingMutexesFunction:pthread_mutex_unlock() intpthread_mutex_unlock(pthread_mutex_t*mutex); Ifthecurrentthreadistheownerofthemutexspecifedbymutex, thenthepthread_mutex_unlock()routineshallunlockthemutex. Ifthereareanythreadsblockedwaitingforthemutex,the schedulerwilldeterminewhichthreadobtainsthelockonthe mutex,otherwisethemutexisavailabletothenextthreadthat callstheroutinepthread_mutex_lock(),or pthread_mutex_trylock(). Ifthepthread_mutex_unlock()routinesucceedsitwillreturn0, otherwiseanerrornumbershallbereturnedindicatingtheerror.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
90
PriorityInversion2. High Priority task preempts low priority task
99 Task Priority3. Hi Priority task block on mutex
50
4. Medium Priority task preempts low priority task and high priority task
31. Low Priority task takes mutex
TimeCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
91
PriorityInheritanceandCeilingsPriorityinheritanceandceilingsaremethodstoprotect againstpriorityinversions. Linuxonlygotsupportforthemin2.6.18. Patchestoaddsupportforolderversionexists.EmbeddedLinuxvendorsusuallyprovidepatchedkernels.
Ifthekernelversionyou'reusingisnotpatched,makesureto protectagainstthisscenarioindesignOnepossibleway:raisethepriorityofeachtaskstryingto grabamutextothemaximumpriorityofallpossible contenders.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
92
POSIXConditionVariablesintpthread_cond_wait(pthread_cond_t*cond, pthread_mutex_t*mutex); intpthread_cond_timedwait(pthread_cond_t*cond, pthread_mutex_t*mutex,conststructtimespec *abstime); intpthread_cond_signal(pthread_cond_t*cond); intpthread_cond_broadcast(pthread_cond_t*cond); Thefirsttwofunctioncallsareusedforwaitingontheconditionvar. Thelattertwofunctioncallsareusedtowakeupwaitingtasks: pthread_cond_signal()wakesuponesingletaskthatiswaitingforthe conditionvariable. pthread_cond_broadcast()wakesupallwaitingtasks.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
93
ConditionVariableUsageExamplevoidmy_wait_for_event(pthread_mutex_t*lock,pthread_cond_t*cond){ pthread_mutex_lock(lock); while(flag==0) pthread_cond_wait(cond,lock); flag=0; pthread_mutex_unlock(lock); } voidmy_post_event(pthread_mutex_t*lock,pthread_cond_t*cond){ pthread_mutex_lock(lock); flag=1; pthread_cond_signal(cond); pthread_mutex_unlock(lock); }
Theconditionvariablefunctioncallsareusedwithinanareaprotectedbythe mutexthatbelongtotheconditionvariable.Theoperatingsystemreleasesthe mutexeverytimeitblocksataskontheconditionvariable;andithaslocked themutexagainwhenitunblocksthecallingtaskfromthesignalingcall.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
94
POSIXSemaphoresPOSIXSemaphoresalsoavailable:#include intsem_init(sem_t*sem,intpshared,unsignedintvalue); intsem_wait(sem_t*sem); intsem_trywait(sem_t*sem); intsem_post(sem_t*sem); intsem_getvalue(sem_t*sem,int*sval); intsem_destroy(sem_t*sem);
ThepsharedflagismeaninglessinLinuxandmustbe0.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
95
SharedMemoryUsingmmap()void*mmap(void*start,size_tlength,intprot,intflags,intfd, off_toffset); Themmap()functionaskstomaplengthbytesstartingatoffset offsetfromthefile(ordevice)specifiedbythefiledescriptorfd intoprocessvirtualmemoryatakernelchosenaddress,but preferablystartwithprotectionofprot. Theactualplacewheretheobjectismappedisreturnedby mmap()(apointer). ThereturnvalueincaseofanerrorisMAP_FAILED(1)and NOT0!Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
96
MMAPFlagsMAP_SHARED:Sharethismappingwithallotherprocessesthatmapthis object.Storingtotheregionisequivalenttowritingtothefile.Thefilemay notactuallybeupdateduntilmsync(2)ormunmap(2)arecalled. MAP_FIXED:Donotselectadifferentaddressthantheonespecified.Ifthe specifiedaddresscannotbeused,mmap()willfail.IfMAP_FIXEDis specified,startmustbeamultipleofthepagesize. MAP_PRIVATE:Createaprivatecopyonwritemapping.Storestothe regiondonotaffecttheoriginalfile.Itisunspecifiedwhetherchangesmade tothefileafterthemmap()callarevisibleinthemappedregion. MAP_ANONYMOUS:Themappingisnotbackedbyanyfile;thefdand offsetargumentsareignored.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
97
POSIXSharedMemoryvoid*shm_open(constchar*name,intoflag,mode_tmode); shm_open()createsandopensanew,oropensanexisting,POSIXshared memoryobject.APOSIXsharedmemoryobjectisineffectahandlewhich canbeusedbyunrelatedprocessestommap(2)thesameregionofshared memory.Theshm_unlink()functionperformstheconverseoperation, removinganobjectpreviouslycreatedbyshm_open(). Theoperationofshm_open()isanalogoustothatofopen(2).namespecifies thesharedmemoryobjecttobecreatedoropened.Forportableuse,name shouldhaveaninitialslash(/)andcontainnoembeddedslashes. Tip:makesuretoreservethesizeofthesharedmemoryobjectusing ftruncate()!Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
98
SysVMailboxeskey_tftok(constchar*pathname,intproj_id); intmsgget(key_tkey,intmsgflg); intmsgsnd(intmsqid,structmsgbuf*msgp,size_tmsgsz,intmsgflg); ssize_tmsgrcv(intmsqid,structmsgbuf*msgp,size_tmsgsz,longmsgtyp, intmsgflg); ftok()generatesakeyfromthepathnameandprojectIDproj_id. msgget()createsamailboxfromthekey. msgsnd()sendsamessageandmsgrcv()retrievesit. Thedifferentflagscancontrolmessagestypes(priorities),ifreceivingisa blockingornonblockingoperationsandmailboxpermissions.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
99
SysVMailboxMessagesstructmsgbuf{ longmtype;/*messagetype,mustbe>0*/ charmtext[1];/*messagedata*/ }; Themtextfieldisanarray(orotherstructure)whosesizeis specifiedbymsgszparameter. Themtypefieldmusthaveapositiveintegervaluethatcan beusedbythereceivingprocessformessageselection.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
100
SysVMailboxMessageTypesFormsgrcv(),themsgtypargumentspecifiesthetypeof messagerequestedasfollows:Ifmsgtypis0,thenthefirstmessageinthequeueisread. Ifmsgtypisgreaterthan0,thenthefirstmessageonthequeue oftypemsgtypisread,unlessMSG_EXCEPTwasassertedin msgflg,inwhichcasethefirstmessageonthequeueoftype notequaltomsgtypwillberead. Ifmsgtypislessthan0,thenthefirstmessageonthequeue withthelowesttypelessthanorequaltotheabsolutevalueof msgtypwillberead.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
101
SysVMailboxesFlagsThemsgflgargumentassertsnone,oneormore(oringthem) ofthefollowingflags:IPC_NOWAITForimmediatereturnifnomessageofthe requestedtypeisonthequeue.Thesystemcallfailswitherrno settoENOMSG. MSG_EXCEPTUsedwithmsgtypgreaterthan0toreadthe firstmessageonthequeuewithmessagetypethatdiffersfrom msgtyp. MSG_NOERRORTotruncatethemessagetextiflonger thanmsgszbytes.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
102
POSIXPipesintpipe(intfiledes[2]); pipe()createsapairoffiledescriptors,pointingtoapipeinode, andplacestheminthearraypointedtobyfiledes.filedes[0]is forreading,filedes[1]isforwriting. FILE*popen(constchar*command,constchar*type); Thepopen()functionopensaprocessbycreatingapipe,forking, andinvokingtheshell.Sinceapipeisbydefinitionunidirectional, thetypeargumentmayspecifyonlyreadingorwriting,notboth; theresultingstreamiscorrespondinglyreadonlyorwriteonly. Anamedversionofpipes,calledfifoalsoexists.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
103
UNIXDomainSocketsFilesinthefilesystemthatactslikesockets. Allnormalsocketoperationsapplies. structsockaddr_unserver; intsock; sock=socket(AF_UNIX,SOCK_STREAM,0); server.sun_family=AF_UNIX; strcpy(server.sun_path,SOCKET_NAME); bind(sock,(structsockaddr*)&server,sizeof(structsockaddr_un)); UDScanbeusedtopassfiledescriptorsbetweenprocessesusinga specialsocketoptionoperation.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
104
SignalsSignalsareasynchronousnotificationssenttoaprocessby thekerneloranotherprocess Signalsinterruptwhatevertheprocesswasdoingatthetime tohandlethesignal. Eachsignalmayhaveasignalhandler,whichisafunction thatgetscalledwhentheprocessreceivesthatsignal. Twodefaultsignalhandlersalsoexist:SIG_IGN:Causestheprocesstoignorethespecifiedsignal. SIG_DFL:Causesthesystemtosetthedefaultsignalhandler forthegivensignal.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
105
RegularSignals
SIGHUP SIGINT SIGILL SIGFPE
Hangupdetectedoncontrollingterminal Interruptfromkeyboard IllegalInstruction Floatingpointexception
SIGBUS
Buserror(badmemoryaccess)
SIGQUIT Quitfromkeyboard SIGABRT Abortsignalfromabort(3) SIGKILL Killsignal SIGSEGV Invalidmemoryreference SIGPIPE Brokenpipe:writetopipewithnoreaders SIGALRM Timersignalfromalarm(2) SIGTERM Terminationsignal
SIGPOLL Pollableevent(SysV).SynonymofSIGIO SIGPROF Profilingtimerexpired SIGSYS SIGURG SIGIO Badargumenttoroutine(SVID) Urgentconditiononsocket(4.2BSD) I/Onowpossible(4.2BSD) SIGTRAP Trace/breakpointtrap
Seesignal(7)fordefaultbehaviors. Therearetwosignalhandlersyou cannotmodifyorignoreSIGKILL andSIGSTOP.
SIGUSR1 Userdefinedsignal1 SIGUSR2 Userdefinedsignal2SIGCHLD Childstoppedorterminate SIGCONT Continueifstopped SIGSTOP Stopprocess SIGTSTP Stoptypedattty SIGTTIN ttyinputforbackgroundprocess SIGTTOU ttyoutputforbackgroundprocess
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
106
LimitationsofRegularSignalsToofewsignals Only2signalsavailableforuserdefinedpurposes:USR1,USR2.Alltheresthave predefinedmeaning. Noqueuing Ifthesamesignalissentmultipletimesuntilataskprocessesit,itisonlydeliveredonce. Nopriority Multiplesignalsdeliveredaccordingtoordersent. Portability SysVsignalhandlersarepoppedbeforeexecuted(sadly,alsoaracecondition). SignalMasking Installingthesamesignalhandlerformultiplesignalsmaycausereentrancy. NopassingofavaluewiththesignalCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
107
RealTimeSignalsAdditional32signalsfromSIGRTMINtoSIGRTMAX Nopredefinedmeaning...ButLinuxThreadslibmakesuseofthefirst3.
Multipleinstancesofthesamesignalsarequeued. Valuecanbesentwiththesignal. Priorityisguaranteed:Lowestnumberrealtimesignalsaredeliveredfirst.Same signalsaredeliveredaccordingtoordertheyweresent. Regularsignalshavehigherprioritythenrealtimesignals.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
108
SignalActionintsigaction(intsignum,conststructsigaction*act,structsigaction *oldact); Registerasignalhandler. signum:signalnumber. act:pointertonewstructsigaction. oldact:pointertobuffertobefilledwithcurrentsigaction(or NULL,ifnotinterested).
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
109
SignalActioncont.Thesigactionstructureisdefinedassomethinglike:structsigaction{ void(*sa_handler)(int); void(*sa_sigaction)(int,siginfo_t*,void*); sigset_tsa_mask; intsa_flags; ... } sa_maskgivesamaskofsignalswhichshouldbeblocked duringtheexecutionofthesignalhandler.Thesignalwhichtriggeredthehandlerwillalsobeblocked, unlesstheSA_NODEFERorSA_NOMASKflagsareused.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
110
RealTimeSignalsFlagssa_flagscanbeusedtopassflagstochangebehavior:SA_ONESHOT:Restorethesignalactiontothedefaultstate oncethesignalhandlerhasbeencalled. SA_RESTART:Makeblockingsystemcallsrestart automaticallyaftyerasignalisreceived. SA_NODEFER:Donotpreventthesignalfrombeing receivedfromwithinitsownsignalhandler. SA_SIGINFO:Thesignalhandlertakes3arguments,notone. Inthiscase,sa_sigactionshouldbesetinsteadofsa_handler.Fordetailsaboutsiginfo_tstructure,seesigaction(2).Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
111
SendingSignalsintsigqueue(pid_tpid,intsig,constunionsigvalvalue); Queuesignaltoprocess. pidistheprocessIDtosendthesignalto. sigisthesignalnumber. sigvalis: unionsigval{ intsival_int; void*sival_ptr; }; Thesigvalisavailabletothehandlerviathesig_valuefieldof siginfo_t.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
112
SignalMaskingThesigprocmask()callisusedtochangethelistofcurrently blockedsignals.intsigprocmask(inthow,constsigset_t*set,sigset_t*oldset); Thebehaviorofthecallisdependentonthevalueofhow,as follows:SIG_BLOCK:Thesetofblockedsignalsistheunionofthe currentsetandthesetargument. SIG_UNBLOCK:Thesignalsinsetareremovedfromthe currentsetofblockedsignals. SIG_SETMASK:Thesetofblockedsignalsissettothe argumentset.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
113
SignalSetsThesefunctionsallowthemanipulationofPOSIXsignalsets:intsigemptyset(sigset_t*set);Initializesthesignalsetgivenbysettoempty,withallsignals excludedfromtheset.
intsigfillset(sigset_t*set);Initializessettofull,includingallsignals.
intsigaddset(sigset_t*set,intsignum); intsigdelset(sigset_t*set,intsignum);Addanddeleterespectivelysignalsignumfromset.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
114
Signals&ThreadsSignalmasksareperthread. Signalhandlersareperprocess. Exceptionsignals(SIGSEGV,SIGBUS...)willbecaughtby threaddoingtheexception. Othersignalswillbecaughtbyanythreadintheprocess whosemaskdoesnotblockthesignaluse pthread_sigmask()tomodifythethread'ssignalmask. Tip:Useasignalhandlerthreadthatdoessigwait(3)to makethreadcatchinglessrandom!Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
115
ProcessesTimers#include intgetitimer(intwhich,structitimerval *value); intsetitimer(intwhich,conststructitimerval *value,structitimerval*ovalue);
Thesystemprovideseachprocesswiththreeintervaltimers, eachdecrementinginadistincttimedomain. Whenanytimerexpires,asignalissenttotheprocess,and thetimer(potentially)restarts.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
116
TimersExpiryThetimerintervaliscontrolledbythefollowingstructures:structitimerval{ structtimevalit_interval;/*nextvalue*/ structtimevalit_value;/*currentvalue*/ }; structtimeval{ longtv_sec;/*seconds*/ longtv_usec;/*microseconds*/ };
Timersdecrementfromit_valuetozero,generateasignal,and resettoit_interval. Atimerwhichissettozero(it_valueiszeroorthetimerexpires andit_intervaliszero)stops.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
117
TimeDomainsITIMER_REALDecrementsinrealtime,anddeliversSIGALRMupon expiration.
ITIMER_VIRTUALDecrementsonlywhentheprocessisexecuting,anddelivers SIGVTALRMuponexpiration.
ITIMER_PROFDecrementsbothwhentheprocessexecutesandwhenthe systemisexecutingonbehalfoftheprocess. SIGPROFisdelivereduponexpiration.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
118
MoreonTimersTimerswillneverexpirebeforetherequestedtime,instead expiringsomeshort,constanttimeafterwards,dependenton thesystemtimerresolution. Ifthetimerexpireswhiletheprocessisactive(alwaystrue forITIMER_VIRT),thesignalwillbedeliveredimmediately whengenerated. Otherwise,thedeliverywillbeoffsetbyasmalltime dependentonthesystemload. Analternateinterfacecalledtimer_create()allows specifyingwhichsignalwillbesent(utilizerealtimesignals) andspawningofathreadontimerexpiry.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
119
DebuggingGDB:TheGNUDebugger GDBrunsonthehost EitherstandaloneorviaagraphicalfrontendlikeEclipse/CDT. GDBserverrunsonthetarget GDBservercanattachtoanalreadyrunningprocessesorstartnew processesunderthedebugger. GDBtalkstoGDBserverviaTCPorUART Needtohavetheexecutablewithdebuginformationonthehost. Also,systemandapplicationdynamiclibraries,ifused. Noneedtohavedebugsymbolsinexecutableontarget.Seehttp://www.codefidence.com/sourcedrop/course/gdb_with_embedded_linux_cheat_sheet.pdfCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
120
EmbeddedLinuxDriverDevelopment
KernelOverviewLinuxFeatures
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
121
LinuxKernelDevelopmentTimeline
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
122
LinuxStableReleasesMajorversions 1majorversionevery2or3years Examples:1.0,2.0,2.4,2.6 Stablereleases 1stablereleaseevery1or2months Examples:2.0.40,2.2.26,2.4.27,2.6.7... Stablereleaseupdates(sinceMarch2005) Updatestostablereleasesuptoseveraltimesaweek Addressonlycriticalissuesinthelateststablerelease Examples:2.6.11.1to2.6.11.7Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
Evennumber
123
LinuxDevelopmentandTestingReleasesTestingreleases Severaltestingreleasespermonth,beforethenextstableone. Youcancontributetomakingkernelreleasesmorestableby testingthem! Example:2.6.12rc1 Developmentversions Unstableversionsusedbykerneldevelopers beforemakinganewstablemajorrelease Examples:2.3.42,2.5.74
Oddnumber
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
124
ContinuedDevelopmentinLinux2.6Since2.6.0,kerneldevelopershavebeenabletointroducelots ofnewfeaturesonebyoneonasteadypace,withouthavingto makemajorchangesinexistingsubsystems. OpeninganewLinux2.7(or2.9)developmentbranchwillbe requiredonlywhenLinux2.6isnolongerabletoaccommodate keyfeatureswithoutundergoingtraumaticchanges. Thankstothis,morefeaturesarereleasedtousersatafasterpace. However,theinternalkernelAPIcanundergochangesbetween two2.6.xreleases.Amodulecompiledforagivenversionmay nolongercompileorworkonamorerecentone.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
125
2.4vs.2.6Linux2.4Mature Butdevelopmentsstopped;very fewdeveloperswillingtohelp. Nowobsoleteandlacksrecent features. Stillfineifyougetyour sources,toolsandsupportfrom commercialLinuxvendors.
Linux2.63.5yearsoldstableLinuxrelease! SupportfromtheLinux developmentcommunityandall commercialvendors. Nowmatureandmoreexhaustive. Mostdriversupgraded. Cuttingedgefeaturesand increasedperformance.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
126
LinuxKernelKeyFeaturesPortabilityandhardwaresupport Runsonmostarchitectures. Scalability Canrunonsupercomputersas wellasontinydevices (4MBofRAMisenough). Compliancetostandardsand interoperability. Exhaustivenetworkingsupport. Security Itcan'thideitsflaws.Itscodeis reviewedbymanyexperts. Stabilityandreliability. Modularity Canincludeonlywhatasystem needsevenatruntime. Easytoprogram Youcanlearnfromexistingcode. Manyusefulresourcesonthenet.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
127
SupportedHardwareArchitecturesSeethearch/directoryinthekernelsources Minimum:32bitprocessors,withorwithoutMMU 32bitarchitectures(arch/subdirectories) alpha,arm,cris,frv,h8300,i386,m32r,m68k,m68knommu, mips,parisc,ppc,s390,sh,sparc,um,v850,xtensa 64bitarchitectures: ia64,mips64,ppc64,sh64,sparc64,x86_64 Seearch//Kconfig,arch//README,or Documentation//fordetails
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
128
EmbeddedLinuxDriverDevelopment
KernelOverviewKernelCode
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
129
LinuxSourcesStructure(1)arch/ arch//mach COPYING CREDITS crypto/ Documentation/ drivers/ fs/ include/ include/asm include/linux init/ ipc/Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Architecturespecificcode Machine/boardspecificcode Linuxcopyingconditions(GNUGPL) Linuxmaincontributors Cryptographiclibraries Kerneldocumentation.Don'tmissit! Alldevicedrivers(drivers/usb/,etc.) Filesystems(fs/ext3/,etc.) Kernelheaders Architectureandmachinedependentheaders Linuxkernelcoreheaders Linuxinitialization(includingmain.c) Codeusedforprocesscommunication
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
130
LinuxSourcesStructure(2)kernel/ lib/ MAINTAINERS Makefile mm/ net/ README REPORTINGBUGS scripts/ security/ sound/ usr/Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Linuxkernelcore(verysmall!) Misclibraryroutines(zlib,crc32...) Maintainersofeachkernelpart.Veryuseful! TopLinuxmakefile(setsarchandversion) Memorymanagementcode(smalltoo!) Networksupportcode(notdrivers) Overviewandbuildinginstructions Bugreportinstructions Scriptsforinternalorexternaluse Securitymodelimplementations(SELinux...) Soundsupportcodeanddrivers Earlyuserspacecode(initramfs)Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
131
LXR:LinuxCrossReferencehttp://sourceforge.net/projects/lxr Genericsourceindexingtoolandcode browser Webserverbased Veryeasyandfasttouse Identifierortextsearchavailable Veryeasytofindthedeclaration, implementationorusagesofsymbols SupportsCandC++ Supportshugecodeprojectssuchasthe Linuxkernel(260MinApr.2006)Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
Takesalittlebitoftimeandpatiencetosetup (configuration,indexing,server configuration). Initialindexingquiteslow: Linux2.6.11:1h40minonP4M 1.6GHz,2MBcache Youdon'tneedtosetupLXRbyyourself. Useourhttp://lxr.freeelectrons.comserver! OtherserversavailableontheInternet:http://freeelectrons.com/community/kernel/lxr/
132
ImplementedinCImplementedinClikeallUnixsystems. (CwascreatedtoimplementthefirstUnixsystems) AlittleAssemblyisusedtoo: CPUandmachineinitialization,criticallibraryroutines. Seehttp://www.tux.org/lkml/#s153 forreasonsfornotusingC++ (mainreason:thekernelrequiresefficientcode).
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
133
CompiledwithGNUCNeedGNUCextensionstocompilethekernel. So,youcannotuseanyANSICcompiler! SomeGNUCextensionsusedinthekernel:InlineCfunctions Inlineassembly Structurememberinitialization inanyorder(alsoinANSIC99) Branchannotation(seenextpage)
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
134
HelpgccOptimizeYourCode!Usethelikelyandunlikelystatements (include/linux/compiler.h) Example: if(unlikely(err)){ ... } TheGNUCcompilerwillmakeyourcodefaster forthemostlikelycase. Usedinmanyplacesinkernelcode! Don'tforgettousethesestatements!Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
135
NoClibraryThekernelhastobestandaloneandcan'tuseuserspacecode. Userspaceisimplementedontopofkernelservices,nottheopposite. Kernelcodehastosupplyitsownlibraryimplementations (stringutilities,cryptography,uncompression...) So,youcan'tusestandardClibraryfunctionsinkernelcode. (printf(),memset(),malloc()...). YoucanalsousekernelCheaders. Fortunately,thekernelprovidessimilarCfunctionsforyour convenience,likeprintk(),memset(),kmalloc()...
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
136
KernelStackVerysmallandfixedstack.2pagestack(8k),pertask. Or1pagestack,pertaskandoneforinterrupts. 2.6 Choseninbuildtimeviamenu. Notforallarchitectures
Forsomearchitectures,thekernelprovidesdebugfacilityto detectstackoverruns.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
137
ManagingEndianessLinuxsupportsbothlittleandbigendianarchitectures Eacharchitecturedefines__BIG_ENDIANor__LITTLE_ENDIAN in Canbeconfiguredinsomeplatformssupportingboth. Tomakeyourcodeportable,thekerneloffersconversionmacros (thatdonothingwhennoconversionisneeded).Mostusefulones: u32cpu_to_be32(u32); //CPUbyteordertobigendian u32cpu_to_le32(u32); //CPUbyteordertolittleendian u32be32_to_cpu(u32); //LittleendiantoCPUbyteorder u32le32_to_cpu(u32); //BigendiantoCPUbyteorderCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
138
KernelCodingGuidelinesNeverusefloatingpointnumbersinkernelcode.Yourcode mayberunonaprocessorwithoutafloatingpointunit(likeon arm).Floatingpointcanbeemulatedbythekernel,butthisis veryslow. Defineallsymbolsasstatic,exceptexportedones(avoidname spacepollution) Allsystemcallsreturnnegativenumbers(errorcodes)for errors:#include
SeeDocumentation/CodingStyleformoreguidelinesCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
139
KernelLogPrintingtothekernellogisdoneviatheprintk()function. Thekernelkeepsthemessagesinacircularbuffer (sothatdoesn'tconsumemorememorywithmanymessages). Kernellogmessagescanbeaccessedfromuserspacethroughsystem calls,orthrough/proc/kmsg Kernellogmessagesarealsodisplayedinthesystemconsole.
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
140
printk()Theprintkfunction: Similartostdlib'sprintf(3) Nofloatingpointformat. Logmessageareprefixedwitha,wherethenumber denotesseverity,from0(mostsevere)to7. Macrosaredefinedtobeusedforseveritylevels: KERN_EMERG,KERN_ALERT,KERT_CRIT, KERN_ERR,KERN_WARNING,KERN_NOTICE, KERN_INFO,KERN_DEBUG. Usageexample:printk(KERN_DEBUGHelloWorldnumber%d\n,num);Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
141
AccessingtheKernelLogManywaysareavailable!Watchthesystemconsole syslogd/klogd Daemongatheringkernelmessages in/var/log/messages Followchangesbyrunning: tailf/var/log/messages Caution:thisfilegrows! Uselogrotatetocontrolthis dmesg Foundinallsystems DisplaysthekernellogbufferCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
logread Same.Oftenfoundinsmall embeddedsystemswithno /var/log/messagesorno dmesg.ImplementedbyBusybox. cat/proc/kmsg Waitsforkernelmessagesand displaysthem. Usefulwhennoneoftheabove userspaceprogramsareavailable (tinysystem)
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
142
LinkedListsManyconstructsusedoublylinkedlists. Listdefinitionandinitialization:structlist_headmylist=LIST_HEAD_INIT(mylist);
orLIST_HEAD(mylist);
orINIT_LIST_HEAD(&mylist);
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
143
ListManipulationListdefinitionandinitialization:voidlist_add(structlist_head*new,struct list_head*head); voidlist_add_tail(structlist_head*new,struct list_head*head); voidlist_del(structlist_head*entry); voidlist_del_init(structlist_head*entry); voidlist_move(structlist_head*list,struct list_head*head);
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
144
ListManipulation(cont.)Listsplicingandquery:voidlist_splice(structlist_head*list,struct list_head*head); voidlist_add_splice_init(structlist_head*list, structlist_head*head); voidlist_empty(structlist_head*head);
In2.6,therearevariantsoftheseAPI'sforRCUprotected lists(seesectionaboutLocksahead).
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
145
ListIterationListsalsohaveiteratormacrosdefined:list_for_each(pos,head); list_for_each_prev(pos,head); list_for_each_safe(pos,n,head); list_for_each_entry(pos,head,member);
Example:structmydata*pos; list_for_each_entry(pos,head,dev_list){ pos>some_data=0777; }
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
146
EmbeddedLinuxDriverDevelopment
KernelOverviewBootSequence
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
147
LinuxBootProcessBIOSand/orbootloaderinitializeshardware. Bootloaderloadskernelimageintomemory.Bootloadercangetkernelimagefromflash,HD,network. PossiblyalsoloadsafilesystemtoRAMintheformofinitrd orinitramfsarchives.
Bootloaderorkerneldecompresscompressedkernel. Kernelperformsinternal(hashtable,listsetc.)andhardware (devicedriver)setup. Kernelfindsandmountstherootfilesystem. Kernelexecutesthe/sbin/initapplication.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
148
BootSequencesx86 PCInitCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
ArmKernel
PowerPC
Setup and BIOS and Decompression Boot loader
bzImage Real Mode trampoline zImage
[ U-boot decompresses the kernel image on it's own ]
GRUB RedBoot BIOSForfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
U-Boot
149
RootFilesystemOptionsExternal FSKernel Version Storage format Provided by Stored in Run time Location
Initrd Style 2.4 and 2.6 Filesystem image
Initramfs style 2.6 only CPIO archive
2.4 and 2.6 Filesystem on storage device or network Passed to kernel as parameter (root=) On device Copied to page cache
Loaded and Like initrd or location provided statically linked by boot loader into kernel image Dynamically Fixed allocation allocated RAM disk page cache Copied to page cache Already in page cache
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
150
inittheFirstProcessThefirst(andonly)processthekernelstartsisinit.Bydefaultitissearchedin/sbin/init Canbeoverriddenbythekernelparameterinit=.
inithasthreeroles:Tosetupthesystemconfigurationandstartapplications. Toshutdownthesystemapplications. Theserveastheparentprocessofallchildprocesseswhose parenthasexited.
Thedefaultinitimplementationreadsitsinstructionsfrom thefile/etc/inittabCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
151
inittabThis is an Busybox style inittab (for an example for Sys V inittab see the Appendix.) Format: id: runlevel : action : command id: To which device should std input/output go (empty means the console) runlevel: ignored. For compatibility with Sys V init. action: one of sysinit, respawn, askfirst, wait, shutdown and once command: shell command to executeCopyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
#Startupthesystem ::sysinit:/bin/mountoremount,rw/ ::sysinit:/bin/mounttprocproc/proc ::sysinit:/bin/mounta ::sysinit:/sbin/ifconfiglo127.0.0.1up #Putagettyontheserialport ::respawn:/sbin/gettyLttyS1115200vt100 #Startsystemloggers null::respawn:/sbin/syslogdnm0 null::respawn:/sbin/klogdn #Stufftodobeforerebooting null::shutdown:/usr/bin/killallklogd null::shutdown:/usr/bin/killallsyslogd null::shutdown:/bin/umountar
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
152
EmbeddedLinuxDriverDevelopment
KernelOverviewKernelSubsystems
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
153
KernelArchitectureApp1 App2 Clibrary Systemcallinterface Process management Memory management Filesystem support Filesystem types CPUsupport code CPU/MMU supportcode Storage drivers Character devicedrivers Network devicedrivers Hardware CPU RAM Storage Device control Networking ... User space
Kernel space
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
154
KernelModevs.UserModeAllmodernCPUssupportadualmodeofoperation:Usermode,forregulartasks. Supervisor(orprivileged)mode,forthekernel.
ThemodetheCPUisindetermineswhichinstructionsthe CPUiswillingtoexecute:SensitiveinstructionswillnotbeexecutedwhentheCPUis inusermode.
TheCPUmodeisdeterminedbyoneoftheCPUregisters, whichstoresthecurrentRingLevel0forsupervisormode,3forusermode,12unusedbyLinux.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
155
TheSystemCallInterfaceWhenauserspacetasksneedstouseakernelservice,itwillmakea SystemCall. TheClibraryplacesparametersandnumberofsystemcallinregisters andthenissuesaspecialtrapinstruction. Thetrapatomicallychangestheringleveltosupervisormodeandthe setstheinstructionpointertothekernel. Thekernelwillfindtherequiredsystemcalledviathesystemcalltable andexecuteit. Returningfromthesystemcalldoesnotrequireaspecialinstruction, sinceinsupervisormodetheringlevelcanbechangeddirectly.Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd. Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
156
LinuxSystemCallPathdo_name() sys_name() entry.S Function call Trap
Kernel
Task
Glibc Task
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
157
EmbeddedLinuxDriverDevelopment
DriverDevelopmentLoadableKernelModules
Copyright20062004,MichaelOpdenacker Copyright20032006,OronPeled Copyright20042006CodefidenceLtd.
Forfullcopyrightinformationseelastpage. CreativeCommonsAttributionShareAlike2.0license
158
LoadableKernelModules(1)Modules:addagivenfunctionalitytothekernel(drivers, filesystemsupport,andmanyothers). Canbeloadedandunloadedatanytime,onlywhentheir functionalityisneed.Onceloaded,havefullaccesstothe wholekernel.Noparticularprote