How to realize mutual exclusion?.Kernel-Preemptivekernels and nonpreemptivekernels: Allows (not allow) a process to be preempted while it isrunninginkernel mode-Anonpreemptivekernel is essentiallyfreefrom raceconditions on kernel data structures, and also easy todesign (especially for SMP architecture)-Whywould anyonefavorapreemptivekernel·Moreresponsive: More suitable for real-time programming26
How to realize mutual exclusion? • Kernel – Preemptive kernels and nonpreemptive kernels • Allows (not allow) a process to be preempted while it is running in kernel mode – A nonpreemptive kernel is essentially free from race conditions on kernel data structures, and also easy to design (especially for SMP architecture) – Why would anyone favor a preemptive kernel • More responsive • More suitable for real-time programming 26
MutualExclusionMore generally, how to realize?Program codeProgramcodeofprocessnofprocess 1Shared Object (manipulated by nCode forCode forprocesses)manipulatingmanipulatingChanging common variablesshared objectshared objectUpdating a tableWriting a filecriticalsectionSolution:To guarantee that when oneprocess is executing in its criticalsection,nootherprocessisallowedexecuteinitscriticalsection.27
Mutual Exclusion • More generally, how to realize? 27 Program code of process 1 . critical section . Program code of process n Shared Object (manipulated by n processes) Changing common variables Updating a table Writing a file . Solution: To guarantee that when one process is executing in its critical section, no other process is allowed execute in its critical section. Code for manipulating shared object Code for manipulating shared object
Critical Section -General StructureTo guarantee that when one process is executing in its criticalsection, no other process is allowed execute in its critical sectionDeclaring the start of the critical section.ProgramcodeAsiftellingotherprocessesthat:"I start accessing the shared object."SectionentryCritical sectionsisthe code segmentthatisaccessingthesharedobjectcritical sectionReadingShared ObjectWritingDeclaringthe end of thecritical sectionSectionexitAsiftellingotherprocessesthat:Remindersection"Ifinish accessing the shared object."28
Critical Section – General Structure 28 critical section Section entry Section exit Program code Critical sections is the code segment that is accessing the shared object. Declaring the start of the critical section. Declaring the end of the critical section. As if telling other processes that: “I start accessing the shared object.” As if telling other processes that: “I finish accessing the shared object.” . . Reading Writing Shared Object To guarantee that when one process is executing in its critical section, no other process is allowed execute in its critical section. Reminder section
Critical Section-ExampleNeed a section entry hereNeed a section entry hereCritical Section2.1load memory X to2.1load memory X toregister A;register A;2.22.2add 1o to register A;minus 1o from register A;2.32.3write register A towrite register A tomemory X;memory X;Need asectionexithereNeeda sectionexithereImportant concept here.ProcessAProcessBBoth regions are called critical sections,yet they can be different.29
Critical Section – Example 29 Process A Process B 2.1 load memory X to register A; 2.2 add 10 to register A; 2.3 write register A to memory X; 2.1 load memory X to register A; 2.2 minus 10 from register A; 2.3 write register A to memory X; Need a section entry here Need a section exit here Need a section entry here Need a section exit here Important concept here. Both regions are called critical sections, yet they can be different. Critical Section
Summary...for the content so far..Race condition is a problem.- It makes a concurrent program producing unpredictableresults if you are using shared objects as thecommunication medium. The outcome of the computation totally depends on theexecution sequences of the processes involved.Mutual exclusion is a requirement.- If it could be achieved, then the problem of the racecondition would be gone.-Mutual exclusion hinders the performance of parallelcomputations.30
Summary.for the content so far. • Race condition is a problem. – It makes a concurrent program producing unpredictable results if you are using shared objects as the communication medium. – The outcome of the computation totally depends on the execution sequences of the processes involved. • Mutual exclusion is a requirement. – If it could be achieved, then the problem of the race condition would be gone. – Mutual exclusion hinders the performance of parallel computations. 30