.

Friday, April 5, 2019

Introduction to Synchronization

Introduction to SynchronizationSynchronization is a mechanism which helps to put on shargond memory resources in an operating system of rules. In the current world, intimately of the calculating machines argon multi-tasking computers. So these computers mess do more than cardinal sue at the aforesaid(prenominal) time. And the ne 2rking technology is has be number sensation of the most modern and developed technologies in the current worlds. So both(prenominal) these computer and ne twork technologies work unitedly in order to achieve usual goals. While working together these technologies eat to sh ar resources such as memory at the same time. While sharing memory, in that respect evict be many another(prenominal) problems. To debar these problems synchronization mechanisms were implemented.ConcurrencyAs it is menti whizzd above, operating systems sh ar memory in order to achieve common goals. The learning substructure be divided out among two deales in one o perating system or two operating systems over a network. However there are sometimes that the sha vehement data laughingstock be nettleed by only one process at a time. As a real life example, dual-lane bathroom can be adjournn. A divided bathroom can be hired by many pile just now it can be substance ab white plagued by only one person at the time. If someone is victimisation the bathroom, others have to carry until that person to come out. Then the one of the abideed people can use the bathroom. Another real life example for this is shared streets or junctions. In shared streets, people can use only one direction at one time. If the shared street is used by any direction, people from other direction have to wait till others to stop utilise the shared street. Otherwise there can be clashes and a lot of problems. Like these, when the operating systems use shared memory, sometimes only one process can advance the data at one time. So this is auspicateed concurrency.Th ere must be some mechanisms to avoid this concurrency. In the above give two real life examples, the shared bathroom can use a mesh in its door to ca-ca sure that only one person is using the bathroom at one time. In the shared streets, traffic lights can be used to make sure that only one direction is using the street and avoid the clashes. Like this, curling and synchronizations are the mechanisms that can be used to avoid concurrency in operating systems.Properties of systems with concurrencyThere are some properties of the systems with concurrency. Those properties are, tenfold actors, shared resources and rules for nettleing the resources. In the above given two real life examples, the multiple actors are people and vehicles while the shared resources become bathroom and street. The rules for accessing these shared resources are one person at one time for the bathroom and one direction for one time for the shared streets. When talking about these triple properties in rel ating to the operating systems, it can be explained like this. Here, the multiple actors are processes within the operating system or meander in processes. Shared memory, orbiculate multivariates and shared devices can be interpreted as the examples for the shared resources property while incarcerateing and semaphores techniques become the rules for accessing the shared resources in operating systems. These are the third properties of the systems with concurrency and it helps to understand what the concurrency is.Situations with no risk of concurrencyIn the current computer technology, there are some situations that there is no risk of concurrency but the shared resources are accessed by more than one processes or wind at the same time. Those situations are as follows,No shared memory or communication - Here, the processes or go do not have shared memory or communication. So those processes or go work only with its private memory.Shared memory with shew-only access - Here, the shared memory can be accessed by one or more processes or threads at the same time. nevertheless in this situation, the shared memory is only available for processes or threads to read-only. So the shared memory cannot be modified in this situation.Copy-on-Write (COW) - Here, the shared memory is accessed by one or more processes or threads at the same time and each process or thread has a separate replica of the shared memory. So each process is accessing its own copy of the shared memory and there is no risk of concurrency.Situation with risk of concurrencyAs well as the situations with no risk of concurrency, there are some situations with risk of concurrency in the current operating systems. Those situations can be explained as follows,Using of shared memory without any synchronization - Here, if the shared memory is accessed by more than one processes or threads at the same time without and synchronization (without having any separate copy for each process or threads), t here is a risk of concurrency.Any modification to the shared memory - If the shared memory is accessed by more than one processes or threads, and at least one of the processes or threads makes any changes to the shared memory, then there is a risk of concurrency.These are the situations with risk of concurrency. If one of these happens, then there is a risk of concurrency in the system. If a concurrency is happened, then there may be a lot of problems in the system. And the risk of happening the concurrency is known as race condition.Race conditionUnder this topic, the race conditions is discussed by using an example. Here, an example of a bank bill is taken to discuss the race condition. In this example, there are two peoples and a bank placard. Execution of the order of this example as follows, fib. correspondence = 200int take away ( greenback, amount = 50)balance = answer for.balancebalance -= amountint deposit (account, amount=100)balance = account.balancebalance += amount account.balance = balancereturn balanceaccount.balance = balancereturn balanceFor this example, the two persons are named as person1 and person2. The code punish by the person1 is coloured with blue colour while the code executed by the person2 is coloured with red colour. This is a sketch of the programme but not coded with any programming language. In the very prototypic bank note, the balance of the account is set to 200. The rest of the code is explained as follows, job 1 - person1 bags executing the code and c exclusivelys the withdraw() by giving the account and 50 of amount as the parameters.Line 2 - person1 reads the balance from the account class and assigns the foster of balance uncertain in account class to his balance variable in his withdraw().Line 3 - person1 modifies the economic look upon of the balance variable in his withdraw() by subtracting the value of amount. So the value of the balance variable in his withdraw() becomes 150.Line 4 - In this line, the pe rson2 starts executing the code by barter his deposit() by parsing account and 100 of amount as the parameters.Line 5 - Here, person2 reads the value of the balance variable in account class and assigns it to the balance variable in his deposit(). Here, still the value of balance variable in account class is 200 as the person1 has not updated the balance in account class.Line 6 - soul2 modifies the value of the balance variable by adding the value of amount variable to it. So the value of balance variable in deposit() becomes three hundred.Line 7 - person1 updates the value of the balance variable in the account class as 150.Line 8 - person 1 returns the value of the balance variable in withdraw()Line 9 - person2 updates the value of the balance variable in the account class as 300Line 10 - person2 returns the value of the balance variable in his deposit()After executing this code 2 peoples comp allowe their transactions by leaving the final value of the balance variable in accou nt class as 300 while the real value of the balance variable in account class has to be 250. So there is clear error in the final output of the process and this is called as the race condition.Manage concurrencyManage Synchronization means, use synchronization mechanism programs to write rues for hold back concurrency situations. One of these rules are as followsAtomicityAtomicity allows one nemesis to access data to manipulate at a single situation. In another way, leave behind allows no other flagellums to change data while one is running. This get out either willing fix the little terror is in progress of manipulating the record or let other records to be waiting while one record access record.The other rule is conditional synchronizationIn this explain threats will be in a particular order to access the record. When threats arrives to access record rule will check the order and add to the queue and let wait until the turn of the threat to access the records.All above acc ess orders will be easy to implement when can identify which is the vituperative part of the process should allow to access at a time. i.e. When Person A accessing account balance person B not allows to access balance at the same time. If extract advance as bellow.1.int withdraw (account, amount) 2.int balance = account.balance3.balance -= amount4.account.balance = balance5.return balance6.Line 1 will initialize threat and pass external parameter values into the threat withdraw as account and amount in this situation.Line 2 will declare balance variable topically to retrieve stored account balance from the database for threat to process and pass account balance in the account table account.balance.Line 3 will manipulate balance variable value (in this situation, we will come since it is withdrawal) from the amount has been pass form external parameter. -= notation for the deduction.Line 4 will update database with rising balance value after withdrawal amount deducted from origi nal balance. balance which is a local variable property the new balance transfer to the account.balance and update record with new balance value.Line 5 will return new balance value to the screen if necessaryLine 6 will terminate the process of withdraw by There will be no concurrent when two threat access lines 1, 5 and 6. unless if any threat try to access lines 2, 3 and 4 will be given incorrect data for one of the threats. Therefore from line 2 to 4 is very decisive to let access only for one threat at any given time of the process as explain bellow.int withdraw (account, amount) int balance = account.balancebalance -= amountCritical regionaccount.balance = balancereturn balanceThis identified arms will call as a unfavourable naval division in another word, no other threats will allows to access while one threat is using at any given time because data will be manipulate when access line 2, 3 and 4.Critical portionCritical section is set of codes access shared resources an d there are several ways of developing critical section such asLocks, Semaphores, Monitors and Messages. In this report will describe engrosss and semaphores how behave and how priority will work.Locks in synchronizationFirst method of synchronization is locks. secure is a very primitive system been used. Lock will lock the threat in the critical section while it is processing the record. Mainly lock has two stats Held and Not Held.At the Held state, one threat is in the critical section and at the Not Held state no threats in the critical section and can prioritised threats to have access.Also locks having two surgical processs Acquire and Release. Which threat will request the lock to be held to access critical section and once threat previously use critical section kick the lock arrive atd thread will get a chance and change the state as held. When threat finish it process in the critical section must release the lock to use by the other threats which are waiting in the queu e.FigureThe above figure has explain how acquire and release will behave at the critical section access, i.e. threats A, B and C request access permission to critical section at the same time by Acquire operation and threat A will grant access with the lock Held state enable. Once threat terminated the process will call release operation to change the lock state to Not Held and lock will change state to Held with threat B and so on will be proceed to complete different threat request at the critical section.By using previous example take a look where to use Acquire and Release operations threat.int withdraw (account, amount) acquire (lock) // Request lock to held to access critical sectionint balance = account.balancebalance -= amountCritical Sectionaccount.balance = balancerelease (lock) // Release lock after complete critical section accessreturn balanceWith further explanation considering previous withdraw and deposit situation Figure 2. Overcome concurrency situation had occurr ed while both people access same account balance to manipulate the amount.FigureWhen implement locks use Boolean variable to check is lock held TRUE or FALSE? Acquire operation will wait other threats in the waiting state while lock held values is TRUE. If lock held VALUE is false, will change to TRUE with requested threat. When the threat completed process in critical section will call release operation and change lock held value as FALSE to make lock available for other threats will show in below construct.First create a global Boolean type variable held values TRUE or FALSE to check is lock holding by a threat or not and by default lock will not hold by any threat and value will be FALSE.structure lock bool held // initial value FALSEConstruct acquire by requesting a lock to be held and if lock held value TRUE request threat will wait in the queue for lock to be released. If lock held is FALSE and by acquire change value of lock held to TRUE and take the control of the critical section access.void acquire (lock) while (lock held) // Hold other threats in the wait queuelock held = TRUE // Once get the lock, change lock value to TRUEConstruct release operation simply will change the start of lock held value to false and allows other threat to access critical section for the next process.void release (lock) lock held = FALSE // When complete process in the critical section, change lock value to FALSESemaphores in synchronizationThis is the morsel method of synchronization. This method is a very basic and powerful mechanism but difficult Implementation method and use blocking threats to access critical section instead of locking threats while using critical section.Semaphores has two operations and they arewait (semaphore) or P() First of all as soon as threat request access permission P() or wait (semaphore) will decrease the riposte values which is 1 by default. Then check is counter value become 0 or signal (semaphore) or V() Once a threat complete p rocess in the critical section will call V() or signal (semaphore) and will increase counter value by 1. i.e. if a P() had decrease counter value 0 while it was in critical section and no other threats can access, but second threat has decreased to -1 while requesting permission to access critical section and waited. Also since second threat is still waiting to get permission it goes to sleep mode, hence V() will wake up threat in a sleep to start it process in critical section explains in Figure .Figure ..FigureReaders/Writers synchronizationThis is another powerful method of synchronization. In this method, the data is accessed by two different ways. Those two ways are, readers and generators. Here, the readers only read files and writers update file contents or data. There are four (4) rules while using the readers/writers synchronization methods and they are as follows,If any reader(s) read the file and no writer is pending, the next reader can read the file without waiting.Wri ters have to wait till the reader(s) finish reading file. After readers finish reading, writer(s) will can start create verbally.If any writer is writing to the file, acquiring readers have to wait till writer finish writing.If one writer is writing to the file and if both readers and writers are waiting, at this situation priority will be given for writers who are waiting. Therefore readers will wait till all writers to compete writing and then readers will start reading. So readers can read most updated file.Future trends of synchronizationIn the current situation, locks and semaphores are astray used in order to prevent the concurrency. So it is very important to deal with the codes in critical section as otherwise there will be a lot of problems with the reliability of information systems. However using of systems with the locks are not time efficiency. So in the current situation of the computer technology, there is a trend of implementing lock foreswear systems. According t o the previous example of shared street, the shared street has to be controlled by using traffic lights. So on the shared streets, only one direction is allowed at one time. So the other directions are blocked at the same time. But there is a technique that does not need of using any traffic lights on the shared street. Here, fly over technique can be used and avoid blocking the directions. So all the directions on the shared street can be used at once without having any problem. But here, this technique may has some more waiting time as the length of the street can be increased while using fly over technique. Like in this example, lock free systems can be implemented in information systems. Semaphore method can be taken as an example for lock free system. But here as well, the processes have to wait sometimes as in the give example of fly over technique on shared streets. The weakness of this method is that lock free and wait free features cannot be implemented in the same informa tion system. So only one of these methods (lock free or wait free) can be implemented in information system.

No comments:

Post a Comment