Wednesday, June 5, 2019

Introduction to Synchronization

Introduction to SynchronizationSynchronization is a mechanism which helps to use shargond retention resources in an run system. In the current world, most of the computers ar multi-tasking computers. So these computers fag end do much than nonp atomic number 18il process at the same time. And the networking technology is has become unmatchable of the most modern and developed technologies in the current worlds. So both these computer and network technologies work to astonishher in crop to achieve common goals. While working together these technologies put on to share resources such as memory at the same time. While sharing memory, there sens be many problems. To avoid these problems synchronizing mechanisms were implemented.ConcurrencyAs it is menti hotshotd to a higher place, operating systems share memory in order to achieve common goals. The info can be overlap among two processes in one operating system or two operating systems over a network. However there are rou ghlytimes that the divided data can be assentinged by nevertheless one process at a time. As a real intent example, shared bathing tub can be claimn. A shared bathroom can be used by many people but it can be used by only one soulfulness at the time. If someone is using the bathroom, differents have to hold until that psyche to come out. Then the one of the waited people can use the bathroom. Another real life example for this is shared courses 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 using the shared street. Otherwise there can be clashes and a gang of problems. wish well these, when the operating systems use shared memory, sometimes only one process can find the data at one time. So this is called concurrency.There must be some mechanisms to avoid this concurrency. In the above given two real life examples, the shared bath room can use a enmesh in its door to make 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, engageing 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, multiple actors, shared resources and rules for accessing 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 three properties in relating to the operating systems, it can be explained like this. Here, the multiple actors are processes within the operating system or threads in processes. Shared memory, global removeables and shared devices can be taken as the examples for the shared resources property while cast outing and semaphores proficiencys become the rules for accessing the shared resources in operating systems. These are the three properties of the systems with concurrency and it helps to understand what the concurrency is.Situations with no peril of exposure of concurrencyIn the current computer technology, there are some authoritys that there is no risk of concurrency but the shared resources are accessed by more than one processes or threads at the same time. Those situations are as follows,No shared memory or communication - Here, the processes or threads do not have shared memory or communication. So those processes or threads work only with its private memory.Shared memory with read-only access - Here, the shared memory can be accessed by one or more processes or threads at the same time. But in this situ ation, 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 to each one(prenominal) process or thread has a separate copy 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), there is a risk of concurrency.Any modification to the shared memory - If the shared memory is acce ssed by more than one processes or threads, and at least one of the processes or threads makes any changes to the shared memory, thus there is a risk of concurrency.These are the situations with risk of concurrency. If one of these happens, whence 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 cognize as accelerate condition.Race conditionUnder this topic, the race conditions is discussed by using an example. Here, an example of a bank account is taken to discuss the race condition. In this example, there are two peoples and a bank account. Execution of the code of this example as follows,account. symmetricalness = 200int sequestrate (account, total = 50) oddment = account. sense of equilibrium issueweight -= amountint deposit (account, amount=century) residuum = account.balancebalance += amountaccount.balance = balancereturn balanceaccount.balance = ba lancereturn balanceFor this example, the two persons are named as person1 and person2. The code executed 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 syllabus but not coded with any programming language. In the very first line, the balance of the account is set to 200. The rest of the code is explained as follows,Line 1 - person1 amazes murder the code and calls 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 honour of balance variable in account class to his balance variable in his withdraw().Line 3 - person1 modifies the note set of the balance variable in his withdraw() by subtracting the protect of amount. So the value of the balance variable in his withdraw() becomes 150.Line 4 - In this line, the person2 starts executing the code by calling 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 - Person2 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 ccc.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 permite their transactions by leaving the net value of the balance variable in account class as 300 while the real value of the balance variable in account class has to be 250. So t here 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 control concurrency situations. One of these rules are as followsAtomicityAtomicity allows one bane to access data to see to it at a single situation. In another way, impart allows no other curses to change data while one is running. This will either will hire the menace is in progress of manipulating the record or let other records to be wait while one record access record.The other rule is conditional synchronizationIn this explain little terrors will be in a crabbed 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 access rules will be easy to implement when can identify which is the vituperative part of the process should allow to acces s at a time. i.e. When Person A accessing account balance person B not allows to access balance at the same time. If extract further 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 locally 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 deduct since it is withdrawal) from the amount has been pass form external parameter. -= notation for the deduction.Line 4 will update database with new balance value subsequently withdrawal amount deducted from original balance. balance which is a local variable holding the new balance transfer to the account.balance and update record w ith 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. But if any threat try to access lines 2, 3 and 4 will be given incorrect information for one of the threats. Therefore from line 2 to 4 is very critical 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 Sectionaccount.balance = balancereturn balanceThis identified sections will call as a critical section 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 sectionCritical section is set of codes access shared resources and there are several ways of developing critical section such asLocks, Semaphores, Monitors and Messages. In this report will descri be locks and semaphores how behave and how priority will work.Locks in synchronizationFirst method of synchronization is locks. Locking is a very primitive system been used. Lock will lock the threat in the critical section while it is treat 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 mathematical processs Acquire and Release. Which threat will put across the lock to be held to access critical section and once threat previously use critical section release the lock scramd 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 queue. configurationThe above figure has explain how acquire and release will behave at the critical section access, i.e. threats A, B an d C request access permission to critical section at the same time by Acquire operation and threat A will concession access with the lock Held state enable. Once threat complete 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 afterward complete critical section accessreturn balanceWith further explanation considering previous withdraw and deposit situation Figure 2. Overcome concurrency situation had occurred while both people access same account balance to manipulate the amount.FigureWhen implement locks use Boolean varia ble to check is lock held TRUE or FALSE? Acquire operation will keep 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 constrain 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. anatomical 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 second method of synchronization. This method is a very basic and powerful mechanism but difficult execution of instrument method and use close up 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 counter values which is 1 by default. Then check is counter value become 0 or signal (semaphore) or V() Once a threat complete process in the critical section will call V() or signal (semaphore) and will increase counter v alue 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 reduced 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 charges 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.Writers have to wait till the reader(s) finish reading file. After readers finish reading, write r(s) will can start writing.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 impetuss of synchronizationIn the current situation, locks and semaphores are widely 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 free systems. According to the previous example of shared street, the shared street has to be controlled by using traffic lights. S o 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 information system. So only one of these methods (lock free or wait free) can be implemented in information system .

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.