Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
OperatingSystems
OPERATING SYSTEMS:
Lesson 3:Introduction to Process Management
1
Jesús Carretero PérezDavid Expósito SinghJosé Daniel García SánchezFrancisco Javier García BlasFlorin Isaila
OperatingSystems
Contents
¨Processconcept.¨Basiclifecycleofaprocess.¨Processinformation¨Multitasking.¨Contextswitch.¨Generatinganexecutable.
2
OperatingSystems
Process
• Process:Programinexecution.– Eachexecutionofaprogramleadstoaprocess.– Processistheunitofmanagementforoperatingsystem
• Aprocessconsistsof:– Programtext:Instructions.– Setofdataassociatedtoprogramexecution.
3
OperatingSystems
Programexecution
ExecutableProgram
A
Mai
n M
emor
y
Process A1
Process A2
4
OperatingSystems
Memoryrepresentation
• Aprocessneedsmemoryforinstructionsanddata.
• Differentinstancesofaprogramneedindependentareasfordata.
text
data
heap
stack
5
OperatingSystems
Contents
¨Processconcept.¨Basiclifecycleofaprocess.¨Processinformation¨Multitasking.¨Contextswitch.¨Generatinganexecutable.
6
OperatingSystems
Basiclifecycleofprocess
Running
BlockedReady
Wait an event
End of event blocking
Time-slicefinished
Activation
As many asprocessors
7
New processes
Fin
OperatingSystems
Simplifiedqueuingmodel:Singleprocessor
Processor
Ev 1
New processes Finished processes
Ev 2
Ev 3…
End of time-slice
8
OperatingSystems
Simplifiedqueuingmodel:Multipleprocessors
Processor
Ev 1
New processes
Ev 2
Ev 3
…
End of time-slice
FinishedProcesses
9
OperatingSystems
Contents
¨Processconcept.¨Basiclifecycleofaprocess.¨Processinformation¨Multitasking.¨Contextswitch.¨Generatinganexecutable.
10
OperatingSystems
Processinformation
• Alltheinformationallowingtheprocesscorrectexecution.
• Threecategories:– Informationstoredintheprocessor.– Informationstoredinmemory.– Additionalinformationmanagedbyoperatingsystem.
11
OperatingSystems
Processorstate
¨Processorstateincludesvaluesofprocessorregisters.¤Registersaccessibleinusermode.
¤ Generalregisters: Registerfile.¤ Programcounter.¤ Stackpointer.¤ Userpartinstatusregister.
¤Registersaccessibleinprivilegedmode:n Privilegedpartfromstatusregister.nMemorymanagement registers(e.g.PTBR).
¨Contextswitch:¨ Saveprocessorstateforoutgoingprocess.¨ Restoreprocessorstateforincomingprocess.
12
OperatingSystems
Memoryimageofaprocess
¨Memoryimageconsistsofthememoryspacesthataprocessisauthorizedtouse.
¨Ifaprocessgeneratesanaddressoutoftheaddressspace,hardwaregeneratesatrap.
¨Dependingonspecificcomputer,memoryimagemaybereferredtovirtualmemoryorphysicalmemory.
13
OperatingSystems
Memoryimagemodels:Singleregion
• Processwithasinglefixedsizeregion.– Usedinsystemswithoutvirtualmemory.
• Processwithasinglevariablesizedregion.– Systemswithoutvirtualmemory:
• Needreservespace" Memorywaste.
– Systemswithvirtualmemory:• Virtualreservespace" Feasiblebutlessflexiblethanmultipleregion.
• Notused.
14
OperatingSystems
Memoryimagemodels:Multipleregions
• Processwithfixednumberofregionsofvariablesize.– Prefixedregions(text,data,stack).– Eachregionmaygrow.– Withvirtualmemory,theholebetweenstackandheapdoesnotconsumeresources.
text
data
stack
15
OperatingSystems
Memoryimagemodels:Multipleregions
• Processwithvariablenumberofregionsofvariablesize.– More advancedoption(usedincurrentversionsinWindowsandUNIX).
– Processstructuredasanumberofregions.– Veryflexible:
• Sharedregions.• Regionsmaydifferinpermissions.
16
OperatingSystems
Operatingsysteminformation
17
• Operatingsystemkeepsadditionalinformationonprocesses.
• Operatingsystemkeepsinformationinatable:ProcessTable.
• ProcessControlBlock (PCB):Eachentryintablekeepsinformationaboutoneprocess.
• AlmostallinformationaboutprocessstoredinPCB.nSomeinformationelementskeptoutsideduetoimplementationreasons.
OperatingSystems
Example:•Process identification.•Parent process identification.•User information.
ContenidosdelBCP
• Identificationinformation.
• Processorstate.• Processcontrolinformation.
18
On process start:•Initial values for processor state.After context switch:•Copy state values to processor.
Scheduling and state information:•Process state.•Waited event (if blocked).•Process priority.•Scheduling information.Allocated regions description.•Per region information.Allocated resources:•Open files.•Used communication ports.•Timers.Pointers for structuring process queues (or rings).Information for inter process communication.
OperatingSystems
InformationoutofPCB
• NotalltheinformationreferredtoaprocessisstoredinitsPCB.
• Decisiontakeninfunctionof:– Efficiency.
• Tablesshouldhaveaprefixedsizeandalwaysbeinmemory.• Sizeneedstobeoptimized.
– InformationSharing.• IfdataneedstobeshareditcannotbeinthePCB.• Pointersareusedtopointtootherstructures (tables)allowingforinformation sharing.
– Openfiles.– Memorypages.
19
OperatingSystems
PageTable
• PlacedoutsidePCB.
• Describesprocessmemoryimage.• PCBcontainspointertopagetable.• Reasons:
– Variablesize.– MemorysharingamongprocessesrequiresittobeexternaltoPCB.
20
OperatingSystems
Filepositionpointers
• PlacedoutsidePCB.
• Ifaddedtoopenfilestable(inPCB)cannotbeshared.
• Ifassociatedtoi-nodeisalwaysshared.
• StoredinacommonstructureformultipleprocessesandanewoneallocatedwithOPENservice.
21
OperatingSystems
Example:Runningacommand
#include <sys/types.h>
#include <stdio.h>int main(int argc, char** argv) {pid_t pid;pid = fork();switch (pid) {
case -1: /* error */exit(-1);
case 0: /* proceso hijo */if (execvp(argv[1], &argv[1])<0) { perror(“error”); }break;
default:printf(“Proceso padre”);
}
return 0;}
prog cat f1
22
OperatingSystems
Forkservice
• pid_t fork(void);
¨ Duplicatesprocessinvokingthecall.¨ Parentprocessandchildprocessgoonrunningthesameprogram.¨ Childprocessinheritsopenfilesfromparentprocess.
¨ Openfiledescriptorsarecopied.¨ Pendingalarmsaredeactivated.
• Returns:– -1onerror.– Inparentprocess:childprocessdescriptor.– Inchildprocess:0.
23
OperatingSystems
Forkservice
24
Process A
Process A’
FORK
OperatingSystems
Execservice
• Singleservicewithmultiple libraryfunctions.int execl(const char *path, const char *arg, ...);int execv(const char* path, char* const argv[]);int execve(const char* path, char* const argv[], char* const envp[]);int execvp(const char *file, char *const argv[])
• Changescurrentprocessimage.– path:pathtoexecutable file.– file:LooksfortheexecutablefileinalldirectoriesspecifiedbyPATH.
• Description:– Returns-1onerror,otherwiseitdoesnotreturn.– Thesameprocessrunsanotherprogram.– Openfilesremainopen.– Signalswithdefaultactionremaindefaulted,signalswithhandlertakedefault
action.
25
OperatingSystems
Execservice
26
Process A
Proceso A’
EXEC
Process B
OperatingSystems
Exitservice
• Finalizesprocessexecution.
void exit(status);
• Allopenfilesdescriptorsareclosed.• Allprocessresourcesarereleased.• PCB (ProcessControlBlock)isreleased.
27
OperatingSystems
Contents
¨Processconcept.¨Basiclifecycleofaprocess.¨Processinformation¨Multitasking.¨Contextswitch.¨Generatinganexecutable.
28
OperatingSystems
Operatingsystemtypes
Operating Systems
Multiprocess(several processes
running)
Multiuser(several users
at a time)
Monouser(a single user
at a time)
Monoprocess(single process)
Monouser(a single user at a
time)
29
OperatingSystems
Principlesofmultitasking
• RealparallelismbetweenI/OandCPU(DMA).• ProcessalternatebetweenI/Oandprocessingphases.
• Severalprocessesstoredinmemory.
30
OperatingSystems
Advantagesofmultitasking
• Easesprogramming,dividingaprograminmultipleprocesses(modularity).
• Allowssimultaneousinteractiveserviceofmultipleusersinanefficientway.
• TakesadvantageoftimesaprocessspendswaitingforanI/Ooperationtobecompleted.
• IncreasesutilizationofCPU.
31
OperatingSystems
Multiprogrammingdegree
• Multiprogrammingdegree:Numberofactiveprocesses.
• Mainmemoryneeds:Systemwithoutvirtualmemory.
32
100%
Multiprogramming degree
% C
PU u
tiliz
atio
n
OperatingSystems
Multiprogramming:CPUuse
1 process
2 processes
Execution
I/O
33
OperatingSystems
Multiprogrammingandvirtualmemory
• Systemswithvirtualmemory:– Divideaddressingspaceofprocessesinpages.– Dividephysicalmemoryaddressingspaceinmainmemoryinpageframes.
• Atagiventime,eachprocesshasacertainnumberofitspagesinmainmemory(residentset).
34
OperatingSystems
Memoryneeds:Virtualmemorysystem
Page
fram
es p
er p
roce
ss
Multiprogramming degree
35
OperatingSystems
Performance:Smallphysicalmemory
¨Whenmultiprogrammingdegreeincreases:¨ Residentsetsize
decreasesforeachprocess.
¨TrashinghappensbeforeachievingahighCPUutilizationpercentage.
¨Solution:Addmoremainmemory.
100%
Multiprogramming degree
% C
PU u
tiliz
atio
n
36
OperatingSystems
Performance:Largephysicalmemory
• Whenmultiprogrammingdegreeincreases:– Residentsetsizedecreasesfor
eachprocess.
• HighCPUutilizationpercentageisachievedwithlessprocessesthatfitinmemory.
• Solution:Improveprocessororaddmoreprocessors.
100%
Multiprogramming degree
% C
PU u
tiliz
atio
n
37
OperatingSystems
Contents
¨Processconcept.¨Basiclifecycleofaprocess.¨Processinformation¨Multitasking.¨Contextswitch.¨Generatinganexecutable.
38
OperatingSystems
Contextswitching
• Whenoperatingsystemassignsprocessortoanewprocess.
• Actions:– SaveprocessstateinPCBforprocessinexecution.– Restorestateofnewprocessinprocessor.
39
OperatingSystems
Contextswitch
Save state in PCB0
Restore state from PCB1
Save state in PCB1
Restore state from PCB0
40
OperatingSystems
Contextswitchingtypes
¨Voluntarycontextswitch:¤ Processperformscalltooperatingsystem(orgeneratesexceptionlike
pagefault)implyingwaitingforanevent.¤Running" Blocked.¤ Examples:readingfromterminal,pagefault.¤Reason⇒ Processoruseefficiency.
¨ Involuntarycontextswitch:¤OSappropriatesCPU.¤Running" Ready.¤ Examples:timesliceellapsed orprocessmovesfromblockedtoready
andhashigherpriority.¤Reason⇒ Processorusesharing
41
OperatingSystems
Contents
¨Processconcept.¨Basiclifecycleofaprocess.¨Processinformation¨Multitasking.¨Contextswitch.¨Generatinganexecutable.
42
OperatingSystems
Executablegeneration
Linker (ld)
Compiler
a.c
a.o
Compiler
b.c
b.o
p
Relocatable object files separately generated
Executable object file (contains code and data for all defined functions in a.c and b.c).
43
OperatingSystems
LinkEditor(linker)
n Combinesobjectfiles:¨ Mergesseveralrelocatableobjectfiles(.o)inasingle
executableobjectfile:inputforloader.
n Resolvesexternalreferences:¨ Referencestosymbolsdefinedinotherobjectfile.
n Relocatessymbols:n Fromrelativepositionsin.otoabsolutepositionsin
executable:adjustrefstothesenewpositions.n Symbols:refstofunctionsanddata.
44
OperatingSystems
Ejemplo:FormatoELF
n ELF:ExecutableandLinkableFormat
¨ formatobinarioestándarparaficherosobjeto¨ originaldeSystemVà BSD,Linux,Solaris¨ formatounificadopara:
n ficherosobjetoreubicablesn ficherosobjetoejecutablesn ficherosobjetocompartidos
45
OperatingSystems
ELFformat
ELF header
Program header table(required for executables)
.text section
.data section
.bss section
.symtab
.rel.txt
.rel.data
.debug
Section header table(required for relocatables)
0
Code
“block started bysymbol” Non-
initialized staticdata
Type (.o, .exe, .so, etc); machine; Big /Little end; etc
Symbol table
Relocationinfo
Iinitialized staticdata
46
OperatingSystems
Executableload
ELF header
Program header table(required for executables)
.text section
.data section
.bss section
.symtab
.rel.text
.rel.data
.debug
Section header table(required for relocatables)
0
.text segment(r/o)
.data segment(initialized r/w)
.bss segment(uninitialized r/w)
Process image
0x08048494
init and shared libsegments
0x080483e0
Virtual addr
0x0804a010
0x0804a3b0
MEMORYDISK
47
OperatingSystems
Staticlibrary
Compiler
a.c
a.o
Compiler
b.c
b.o libc.aStatic library: file with contatenation of relocatableobject files.
Executable object file: includes code from functions in libc used in the program.
Linker (ld)
p
48
OperatingSystems
Staticanddynamiclibraries
n Drawbacksofstaticlibraries:¨ Codepotentiallyduplicatedinexecutables:
¨ Disk(filesystem).¨ Virtualmemoryspaceinprocesses.
¨ Bugsinlibraries" newversion" needtorelink
n Solution:dynamiclibraries(*.so) (dynamiclinklibraries,DLLs):n Componentsloadedinmemoryandexecutedatruntime.n Functionsfromlibrariesmaybesharedamongmultipleprocesses.
49
OperatingSystems
Dynamiclibraries
Functions from libc.so and invoked from a.c and b.c are loaded and linked.Potentially shared among multiple processes.
Shared library
Compiler
a.c
a.o
Compilerb.c
b.o
libc.so
Linker (ld)
p
Cargador / Enlazador dinámico
(ld-linux.so)
Executable in memory P’50
OperatingSystems
Reminder
¨ Differencebetweenprogramandprocess.¨ Aprocessisaporgram inexecution.
¨ Operatingsystemmanagesrunningprocesses(processlifecycle).¨ Processinformationconsistingof:processstate,memoryimage
andPCB.¨Multitaskingallowsabetteruseofcomputerresources.¨ Contextswitchingintroducesasmalloverhead.¨ Staticlibrariesarelinkedatcompiletimewhiledynamiclibraries
arelinkedatprocesscreationtime.¨ Processcreationimpliescreationofitsmemoryimageandthe
allocationofaPCB.
51
OperatingSystems
OPERATING SYSTEMS:
Lesson 3:Introduction to Process Management
52
Jesús Carretero PérezDavid Expósito SinghJosé Daniel García SánchezFrancisco Javier García BlasFlorin Isaila