Upload
samit-kumar
View
219
Download
0
Embed Size (px)
Citation preview
8/12/2019 Dekker Algo
1/7
Critical Section Problem: Dekker's Algorithm
Dekker's algorithm was the first provably-correct solution to the
critical section problem. It requires both an array of booleanvalues and an integer variable:
varflag: array[0..1] ofboolean;
turn: 0..1;
repeat
flag[i] := true;whileflag[j] do
ifturn=jthen
begin
flag[i] :=false;
whileturn=jdono-op;
flag[i] := true;
end;
critical section
turn:=j;
flag[i] :=false;
remainder section
untilfalse;
Dekker's Algorithm
8/12/2019 Dekker Algo
2/7
Definition - What does Dekker's Algorithm mean?
Dekkers algorithm is the first known algorithm that solves the mutual exclusion problem inconcurrent programming. It is credited to Th. J. Dekker, a Dutch mathematician who created the
algorithm for another context. Dekker's algorithm is used in process queuing, and allows two
different threads to share the same single-use resource without conflict by using shared memoryfor communication.
Dekkers algorithm will allow only a single process to use a resource if two processes are trying to use it
at the same time. The highlight of the algorithm is how it solves this problem. It succeeds in preventing
the conflict by enforcing mutual exclusion, meaning that only one process may use the resource at a
time and will wait if another process is using it. This is achieved with the use of two "flags" and a
"token". The flags indicate whether a process wants to enter the critical section (CS) or not; a value of 1
means TRUE that the process wants to enter the CS, while 0, or FALSE, means the opposite. The token,
which can also have a value of 1 or 0, indicates priority when both processes have their flags set to
TRUE.
This algorithm can successfully enforce mutual exclusion but will constantly test whether the critical
section is available and therefore wastes significant processor time. It creates the problem known as
lockstep synchronization, in which each thread may only execute in strict synchronization. It is also non-
expandable as it only supports a maximum of two processes for mutual exclusion.
Spooling - simultaneous peripheral operations on-line, spooling refers to as a processthat putting jobs in a buffer or say spool, or temporary storage area, a special area inmemoryor on a disk where a device can access them when it is ready. Spooling is usefulbecause devices access data at different rates.
The buffer provides a waiting station where data can rest while the slower device catchesup.However, unlike a spool of thread, the first jobs sent to the spool are the first ones tobe processed (FIFO, not LIFO).
The most common spooling application is print spooling. When you choose to print adocument, the computer sends the document information to the printervery quickly,but the printer can't accept it at the same rate. The printer can only handle a chunk ofinformation at a time, and it pauses to process and print that chunk before it's ready formore. Meanwhile, you have to wait until the printer has accepted the whole document,piece by piece, before you can use your computer again because the computer has tohang around and feed the information through. That's why you need a print spooler-
software that reduces the amount of time during which you can't work while you waitfor a job to print.
A spooler works by intercepting the information going to the printer, parking ittemporarily on disk or in memory. The computer can send the document information tothe spooler at full speed, then immediately return control of the screen to you. Thespooler, meanwhile, hangs onto the information and feeds it to the printer at the slow
http://ecomputernotes.com/fundamental/input-output-and-memory/what-are-the-different-types-of-ram-explain-in-detailhttp://ecomputernotes.com/fundamental/input-output-and-memory/what-are-the-different-types-of-ram-explain-in-detailhttp://ecomputernotes.com/fundamental/introduction-to-computer/what-is-computerhttp://ecomputernotes.com/fundamental/introduction-to-computer/what-is-computerhttp://ecomputernotes.com/fundamental/input-output-and-memory/what-is-a-printer-and-what-are-the-different-types-of-printershttp://ecomputernotes.com/fundamental/input-output-and-memory/what-is-a-printer-and-what-are-the-different-types-of-printershttp://ecomputernotes.com/fundamental/input-output-and-memory/what-is-a-printer-and-what-are-the-different-types-of-printershttp://ecomputernotes.com/fundamental/introduction-to-computer/what-is-computerhttp://ecomputernotes.com/fundamental/input-output-and-memory/what-are-the-different-types-of-ram-explain-in-detail8/12/2019 Dekker Algo
3/7
speed the printer needs to get it. So if your computer can spool,you can work while adocument is being printed.
You will notice during spooling, though, that your work gets slightly interrupted for afew seconds here and there because the computer cannot really do more than one thing
at a time, meaning it can't keep the spooler running and your monitor running at thesame time. So if your cursor doesn't move or the letters type sporadically here and there,don't worry. Total control will be returned to you when the printer is done
Sometimes spooling software is built into the system software, as in "Backgroundprinting" in System 7 or the "Backgrounder" file on System 6 on the Mac, or the PrintManager in Windows. Sometimes you buy extra software that allows you to spool.
Introduction to Spooler Components
The primary components of the Microsoft Windows 2000 and later print spooler are illustrated inthe following diagram.
Application
The print application creates a print job by calling GDI functions.
GDI
The Graphics Device Interface (GDI)includes both user-mode and kernel-mode
components. The user-mode component, Microsoft Win32 GDI, is used by Win32
applications that require graphics support. The kernel-mode component, thegraphics
engine(or graphics rendering engine), exports services and functions that graphics devicedrivers can use.
http://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_device_interface__gdi_http://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_device_interface__gdi_http://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_device_interface__gdi_http://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_enginehttp://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_enginehttp://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_enginehttp://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_enginehttp://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_enginehttp://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_enginehttp://msdn.microsoft.com/en-us/library/windows/hardware/ff556283%28v=vs.85%29.aspx#wdkgloss.graphics_device_interface__gdi_8/12/2019 Dekker Algo
4/7
Winspool.drv
Winspool.drv is the client interface into the spooler. It exports the functions that make upthe spooler's Win32 API, and provides RPC stubs for accessing the server. (GDI is the
primary client, but applications also call some of its Win32 functions.)
Spoolsv.exe
Spoolsv.exe is the spooler's API server. It is implemented as a Windows 2000 (or later)
service that is started when the operating system is started. This module exports an RPC
interface to the server side of the spooler's Win32 API. Clients of Spoolsv.exe includeWinspool.drv (locally) and Win32spl.dll (remotely). The module implements some API
functions, but most function calls are passed to aprint providerby means of the router
(Spoolss.dll).
Router
The router, Spoolss.dll, determines which print provider to call, based on a printer nameor handle supplied with each function call, and passes the function call to the correct
provider.
Print Provider
The print provider that supports the specified print device.
Print Monitor
Windows XP supports two types of print monitors: language monitors, and port monitors.
If printer hardware is local to the system on which the application is running, the "client" and
"server" are the same system (although this is not evident in the diagram).
All spooler components execute in user mode.
Operating System Design Issues
Efficiency
o Most I/O devices slow compared to main memory (and the CPU)
Use of multiprogramming allows for some processes to be waiting on I/Owhile another process executes
Often I/O still cannot keep up with processor speed Swapping may used to bring in additional Ready processes; More I/O
operations
Optimise I/O efficiency especially Disk & Network I/O
The quest for generality/uniformity:
http://msdn.microsoft.com/en-us/library/windows/hardware/ff561076%28v=vs.85%29.aspxhttp://msdn.microsoft.com/en-us/library/windows/hardware/ff561076%28v=vs.85%29.aspxhttp://msdn.microsoft.com/en-us/library/windows/hardware/ff561076%28v=vs.85%29.aspxhttp://msdn.microsoft.com/en-us/library/windows/hardware/ff561076%28v=vs.85%29.aspx8/12/2019 Dekker Algo
5/7
o Ideally, handle all I/O devices in the same way; Both in the OS and in user
applicationso Problem:
Diversity of I/O devices
Especially, different access methods (random access versus stream based)
as well as vastly different data rates. Generality often compromises efficiency!
o Hide most of the details of device I/O in lower-level routines so that processes
and upper levels see devices in general terms such as read, write, open, close,
lock, unlock
Definitions: Real Time and Virtual Time
Real Time
:as used in these notes, real time refers to wall clock time, i.e. the sense of time as itexists in the real physical world. It is assumed that real time is real number valued,i.e. can be measured as a real number.Note: the expression real time is sometime used in a very different sense to express atiming constraint in which computation must happen within a defined period of time fromthe arrival of an event. But in these notes, that constraint is called real time hard. Oftenreal time hard implies some notion of really fast, but in fact really fast is arbitrarilydetermined by the observer, or by the applicable laws of physics in the case of processcontrol systems. Later in these notes, both pre-paid authorization and the batch billingcycle are seen to contain real time hard constraints!
Virtual Time:virtual time is the sense of time inside of an application or informational model. Manycomputations involve time as an explicit parameter whereby the parametric valueserves to determine the state of virtual time.Virtual time for rating and billing purposes needs to be represented numerically,generally as a real number or at least as an integer (sequence by itself is not enough!).Time Warp is a technique from the field of Distributed Discrete State Event Simulation(D-DSES). Global virtual time is coordinated among the distributed elements of thesimulation with the understanding that roll back may be required if an element workingin future time finds one of its earlier assumptions has been violated (a.k.a. optimisticcomputation). The element will be required to reset itself back to the point in earliervirtual time and to the corresponding earlier internal state, and then move forward invirtual time while computing with the revised conditions.
8/12/2019 Dekker Algo
6/7
Solution to the Readers and
Writers problem usingMonitors
Monitorscan be used to restrict access to the database. In this example, the read and write
functions used by processes which access the database are in a monitor called ReadersWriters. If
a process wants to write to the database, it must call the writeDatabasefunction. If a process
wants to read from the database, it must call the readDatabasefunction.
Remember that monitors use the primitives Waitand Signalto put processes to sleep and to
wake them up again. In writeDatabase, the calling process will be put to sleep if the number ofreading processes, stored in the variable count, is not zero. Upon exiting the readDatabase
function, reading processes check to see if they should wake up a sleeping writing process.
monitorReadersWritersconditionOKtoWrite, OKtoRead;intReaderCount = 0;Booleanbusy = false;
procedureStartRead()
{if(busy) // if database is not free, block
OKtoRead.wait;ReaderCount++; // increment reader ReaderCountOKtoRead.signal();
}
procedureEndRead(){
ReaderCount-- ; // decrement reader ReaderCountif ( ReaderCount == 0 )
OKtoWrite.signal();
}
procedureStartWrite(){if ( busy || ReaderCount != 0 )
OKtoWrite.wait();busy = true;
}
http://www.cs.gmu.edu/cne/modules/ipc/pink/swmon.htmlhttp://www.cs.gmu.edu/cne/modules/ipc/pink/swmon.htmlhttp://www.cs.gmu.edu/cne/modules/ipc/pink/swmon.html8/12/2019 Dekker Algo
7/7
procedureEndWrite(){busy = false;If (OKtoRead.Queue)
OKtoRead.signal();else
OKtoWrite.signal();}
Reader(){
while(TRUE) // loop forever{
ReadersWriters.StartRead();readDatabase(); // call readDatabase function in
monitorReadersWriters.EndRead();
}}
Writer(){
while(TRUE) // loop forever{
make_data(&info); // create data to writeReaderWriters.StartWrite();writeDatabase(); // call writeDatabase function in
monitorReadersWriters.EndWrite();
}}