<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4524264465260496221</id><updated>2025-05-19T17:03:28.656+05:30</updated><title type='text'>Understanding Linux</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-3030836117067028101</id><published>2012-09-05T21:50:00.001+05:30</published><updated>2012-09-05T21:50:42.881+05:30</updated><title type='text'>Disk I/O Basics</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Seek Time: &lt;br /&gt;
Generally speaking, when the I/O request comes in for a particular piece of data, the head will not be above the correct track on the disk. The arm will need to move so that the head is directed over the correct track where it must then wait for the platter spin to present the target data beneath it. As the data could potentially be anywhere on the platter, the average seek time is time taken for the head to travel half way across the disk.&lt;br /&gt;
&lt;br /&gt;
Latency (Rotational Latency):&lt;br /&gt;
Latency is the average time for the sector being accessed to rotate into position under a head, after a completed seek. It is easily calculated from the spindle speed, being the time for half a rotation. &lt;br /&gt;
&lt;br /&gt;
Response Time = (Average Latency) + (Average Seek Time)&lt;br /&gt;
&lt;br /&gt;
Disk Transfer Rates (Aka Sequential Read) :&lt;br /&gt;
how much data the drive could potentially deliver on a sequential read. Mainly dominated by the latency (ie rotational delay).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let&#39;s assume the OS has directed the disk to perform a large sequential read operation. After the initial average overhead (seek + latency delay) to locate the start of the data, the actuator arm need now move only fractionally with each disk rotation to continue the read (assuming the data is contigious). The rate at which we can read data off the disk is now limited by the platter RPM and how much data the manufacturer can pack into each track.&lt;br /&gt;
&lt;br /&gt;
Data per track on a 2.5 inch drive is ~1.43MB&lt;br /&gt;
Now a disk spinning at 7200RPM is actually spinning 120 times per second. Which means that the total amount of data which can pass under the head in 1 second is a massive 173MB (120 * 1.43MB).&lt;br /&gt;
&lt;br /&gt;
Taking into account that perhaps about 87% of a track is data, this gives a maximum disk throughput of about 150MB/s.&lt;br /&gt;
&lt;br /&gt;
Note that this calculation is best case -it assumes the data is being sequentially read from the outermost tracks of the disk and that there are no other delays between the head reading the data and the operating system which requested it. As we start populating the drive with data, the tracks get smaller and smaller as we work inwards (don&#39;t worry -we&#39;ll cover this in Zone Bit Recording below). This means less data per track as you work towards the centre of the platter, and therefore the less data passing under the head in any given time frame.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To see how bad the sequential read rate can get, let&#39;s perform the same calculation for the smallest track which has a 1 inch diameter. This gives a worst case sequential read rate of 60MB/s! So when your users report that their computers get progressively slower with time, they might not actually be imagining it. As the disk fills up, retrieving the data from the end of a 2.5inch drive will be 2.5 times slower than retrieving it from the start. For a 3.5 inch desktop harddisk the difference is 3.5 times.&lt;br /&gt;
&lt;br /&gt;
Disk Operations per Second - IOP:&lt;br /&gt;
&lt;br /&gt;
The problem with databases is that database I/O is unlikely to be sequential in nature. One query could ask for some data at the top of a table, and the next query could request data from 100,000 rows down. In fact, consecutive queries might even be for different databases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If we were to look at the disk level whilst such queries are in action, what we&#39;d see is the head zipping back and forth like mad -apparently moving at random as it tries ro read and write data in response to the incoming I/O requests.&lt;br /&gt;
&lt;br /&gt;
In the database scenario, the time it takes for each small I/O request to be serviced is dominated by the time it takes the disk heads to travel to the target location and pick up the data. That is to say, the disk&#39;s reponse time will now dominate our performance. The response time now reflects the time our storage takes to service an I/O request when the request is random and small.&lt;br /&gt;
&lt;br /&gt;
For seagate drive with RPM of 7200, seek time = 11ms. Avg latency = 4.17ms.&lt;br /&gt;
Response time = 11 + 4.17 = 15.17ms.&lt;br /&gt;
So, for the specific case of our Seagate Drive with a 15.17ms response time, it will take at least on average 15.17ms to service each I/O. Turning this on it&#39;s head to give us our IOPS yeilds (1/ 0.01517) which is 66 IOPS.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Typical IOPS values are:&lt;br /&gt;
Drive RPM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Avg Rotational Latency&amp;nbsp;(ms)&amp;nbsp;&amp;nbsp;&amp;nbsp; Seek Time&amp;nbsp;(ms)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IOPS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TransferRate&lt;br /&gt;&amp;nbsp; 5400&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5.5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7 -&amp;nbsp;14.4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;50 - 80&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~100MB/s&lt;br /&gt;
&amp;nbsp; 7200&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4.2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;5.9 - 9.1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;75 - 100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~150MB/s&lt;br /&gt;
10,000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3.6 - 5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 125 - 150&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~200MB/s&lt;br /&gt;
15,000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2.7 - 3.7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 175 - 200&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;~300MB/s&lt;br /&gt;
&lt;br /&gt;
Typical disk speeds are:&lt;br /&gt;
5400 RPM is standard for laptops&lt;br /&gt;
7200 RPM is the standard for PCs.&amp;nbsp;&lt;br /&gt;
10000 RPM is a mid-range server.&amp;nbsp;&lt;br /&gt;
15000 RPM is a high-end server.&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/3030836117067028101/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2012/09/disk-io-basics.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/3030836117067028101'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/3030836117067028101'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2012/09/disk-io-basics.html' title='Disk I/O Basics'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-5719799557318998565</id><published>2011-01-16T21:53:00.002+05:30</published><updated>2015-02-14T20:22:01.061+05:30</updated><title type='text'>Log Structured File System : An Introduction</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;I am writing this post after a long gap. In this post I am trying to give a basic idea about the log structured file systems. I have oversimplified some of the stuff so as not to confuse you and keep the things as straight forward as possible. &lt;br /&gt;
In traditional file systems the data stored on the disk may not be contiguous and so the seek time is poor which degrades the performance. It is the seek time between various IO requests that dominates the performance. Also, buffer caches are sufficiently large these days and most of the reads can be served by the buffer cache. So, disk traffic is mostly dominated by writes and disk seek time is the major reason that impacts the performance.&lt;br /&gt;
Log structured try to solve this problem by writing all modifications contiguously on the disk in a log-like structure.&lt;br /&gt;
LFS first buffers all updates in memory into a segment (typically MB in size). When the segment is full, it is written to the disk in one long sequential write to the unused part of disk. &lt;br /&gt;
LFS never overwrites the data. The modified data blocks and inodes are written to the new segment. So, we have the old and new copies of the inodes and data blocks spread in different logs and we need to keep track of the latest inodes/data-blocks.&lt;br /&gt;
&lt;b&gt;Lets take a simple example to see what happens when a block is added to a file&lt;/b&gt;&lt;br /&gt;
Here we need to allocate a new block and modify the inode to point to the newly allocated block. Initially the modification are done to the segment which is still in memory. Later, when the segment is full of such modifications it is written to the disk. Note, here there is no problem of finding free disk blocks. The free space in the log implies free blocks and can be allocated to a file. Both the modified inode and the new block are written on to the log.&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5JnpGS4-k95TmeVSRzb7m3hJYROlilowh6yUovpe_T_Vb1FFyhjqXIiHnrWT4B5mjxzH_xGHNW4MmbtV0eeHNJqJYT2MLHIveXnViwGrYdD4NxHNLe5cVgf0JXZZoT3Um7TpGAy_Bezo/s1600-h/image%5B4%5D.png&quot;&gt;&lt;img alt=&quot;image&quot; border=&quot;0&quot; height=&quot;54&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLkhjFMH0zp3tJllibp1DmvR6n3y1dBwZv4qmeMaiTX-aR3svtdgqSQAeORcaOCPMnhrfxDCPGlZmQyhERAvOdykU3rUxqsjsOX9NYEj-X-21SBmAPV_WBLSg-rL4bF6nJjVYoNYPUkSo/?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;&quot; title=&quot;image&quot; width=&quot;576&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
Wait, we have a problem here. How do you find the location of new inode ?&lt;br /&gt;
&lt;b&gt;How to find the location of new modified inode ?&lt;/b&gt;&lt;br /&gt;
On a traditions unix file system it is quite easy to find the location of an inode. The inode table lies at a fixed address on the disk. So, if we know the inode number and its size we can easily determine its location.&lt;br /&gt;
In LFS, the inodes are spread everywhere on the disk in various segments. Moreover there are multiple copies of the inodes (as we never overwrite in place). So, how to find which one is latest and its location in the segment ? The answer is inode map which keeps the disk address of all inodes.&lt;br /&gt;
inode map is an array that keeps the disk addresses of all the inodes. Imagine if there are one hundres thousand inodes what is the size of inode-map. &lt;br /&gt;
inode map size = 100000 * 4 = 400000 bytes = 390 KB.&lt;br /&gt;
So, inode maps are small enough and can be cached completely in memory. Every segment on disk has a copy of the inode map.&lt;br /&gt;
&lt;b&gt;Now the question is where to put the inode map on disk? &lt;/b&gt;&lt;br /&gt;
inode map can be put on a fixed part of the disk. But, it is changed frequently and so whenever a segment is updated we would need to update the inode map. So there would be more disk seeks and performance may be impacted. Please note that we do not wait for the entire segment to be filled with modified inodes/data before the segment is synced to disk. In fact, the segments are updated partially and so for each partial write updating inode map may impact the performance. So, we keep the inode-map also on each segment.&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhL7RuZ-ZsMN1R-FnlVYarGe6M8R9ZVY86tpMYVigFwgYx0IInSVUtkUwnSv6tQur5KQNejyU0RopT8x97Xmd01UkebQN0HMvk7gAY6ro0iieGkzz0xfKjBHJ5SqsDi-tsbSdJHcuXxKm8/s1600-h/image%5B9%5D.png&quot;&gt;&lt;img alt=&quot;image&quot; border=&quot;0&quot; height=&quot;58&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDEkTh-Iuq9BQHTuo8hElDMIHWuvLOjPTCH60BqvUHgAM9irOCwCqrIYF9l1Zj4UzJ5cMSgehUDRfnS9T3jME3hqSlrUQSNK41NQLjDUoz1wyzzziIHm_v9nBmxP6MDebUU2g8-5yoszs/?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;&quot; title=&quot;image&quot; width=&quot;570&quot; /&gt;&lt;/a&gt; &lt;br /&gt;
&lt;b&gt;Now how to find the inode-map ?&lt;/b&gt;&lt;br /&gt;
LFS keeps a fixed place on disk called checkpoint region (CR) that contains the address of the latest inode-map. The CR needs to be updated whenever we write a new segment. So, CR is required to be updated quite infrequently.&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpFWkB86Bz6zXLPJXrDOoR4qL6vMzhk19yl_tXCr-xlLuPTRioccBvdvQNCFgJ3DIzft5lKcus4zwAsCPNSnzQLK6O4CKh7fxwKrdNnS32PvbC4dPr8TZq-8sFuYJLppm5DvFn3llX-lg/s1600-h/image%5B14%5D.png&quot;&gt;&lt;img alt=&quot;image&quot; border=&quot;0&quot; height=&quot;178&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbRj1ALmy_ncTNYgp45VmIJouzRosEzMPwY6GyQEiwoDUoz99lZQyQT9Pg-upcVlQlpVg9hm3ieCoLL2h8e3cryb05Dm2EcgCMW8cZfPSkXccVPsVHYl55WWR238lZgvMlPqXU_v8qGCA/?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;&quot; title=&quot;image&quot; width=&quot;587&quot; /&gt;&lt;/a&gt; &lt;br /&gt;
Note that each segment has pointer to the next segment and that is why the overall name of the file system is log structured file system.&lt;br /&gt;
By starting from the CR we can find the first segment written to the disk and traverse the chain to see what changes were made to the filesystem finally reaching the latest segment.&lt;br /&gt;
&lt;b&gt;Reading files from a Log Structured File System&lt;/b&gt;&lt;br /&gt;
Lets assume that the system is just booted and there is nothing related to LFS in memory. The first thing that is read from the disk is CR (check point region) as this is the only region whose position is fixed. CR contains the pointer to the inode map. LFS then reads the entire inode map into the memory. To get the inode, LFS simply looks the cached copy of inode map and finds the inode number to inode disk address and thus reads the most recent version of inode. To find blocks of a file, rest of the operations are exactly the same like a traditional file system. Once LFS had read the latest copy of inode it can find out the disk addresses of the blocks that belong to this file and read them.&lt;br /&gt;
&lt;b&gt;Garbage collection&lt;/b&gt;&lt;br /&gt;
Garbage collection is very important in LFS. As we have seen that LFS never overwrites any inode/disk-block. So, there are older versions of a file/blocks scattered all over the disk in various segments. LFS must periodically find out these old versions and clean/free them. &lt;br /&gt;
Periodically LFS reads the old segments and determine which blocks are live in this segment. Please note that any segment may contain few old and few latest versions of inodes/data-blocks. We will see how to determine if a particular inod/data-block is latest. Lets assume that we somehow know which inodes/blocks are latest. The cleaner process then writes only the latest/live blocks to a new segment and frees the old segment. So cleaner will read M old segments and compact them to N new segments (where M &amp;gt; N). This results in freeing of (M – N) segments and thereby creating more free space.&lt;br /&gt;
&lt;b&gt;How to determine which inodes/data blocks in a segment are live/latest-version ?&lt;/b&gt;&lt;br /&gt;
How to find if a inode is live or not?&lt;br /&gt;
Fisrt determine the inode number. For this every segment has a segment summary that also keeps information about which inodes are stored in this segment. Looking at the segment summary we can find the inode number and its disk address (say D)&lt;br /&gt;
Now, we have the latest inode map cached in the memory. So, we can always find out the disk address of the latest version (L) of an inode. Then we can compare if the inode address in the segment (D) matches with the address of the latest version (L) in inode-map. If they both are equal that means the inode is live.&lt;br /&gt;
How to find if a data block is live ?&lt;br /&gt;
For this the segment summary block in the segment keeps some information for each data block. LFS includes the inode number and its offset in the file.&lt;br /&gt;
Given this information it is easy to determine if the block is live or not. LFS notes the inode number of block D and offset O from the segment summary and then looks in the latest inode map (cached in memory) to find the disk address of the corresponding inode and reads the inode from the disk. It then finds the disk address of the block at offset O in the file. If the disk address in the inode matches with the disk address of block in the segment, the block is live.&lt;br /&gt;
LFS also keeps the version number of a file. Whenever a new file is created LFS increases its version number. Thus looking at the version number LFS can save the longer checks to determine the liveliness of an inode/disk-block.&lt;br /&gt;
More on this later. Lots of things have been oversimplified for the sake of clarity. I will go into more details in later posts. Till then Have Fun !!!&lt;br /&gt;
And yes if you have any comments positive/negative please feel free to post.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/5719799557318998565/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2011/01/log-structured-file-system-introduction.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/5719799557318998565'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/5719799557318998565'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2011/01/log-structured-file-system-introduction.html' title='Log Structured File System : An Introduction'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLkhjFMH0zp3tJllibp1DmvR6n3y1dBwZv4qmeMaiTX-aR3svtdgqSQAeORcaOCPMnhrfxDCPGlZmQyhERAvOdykU3rUxqsjsOX9NYEj-X-21SBmAPV_WBLSg-rL4bF6nJjVYoNYPUkSo/s72-c?imgmax=800" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-8129356256899350303</id><published>2010-06-02T21:38:00.013+05:30</published><updated>2015-02-14T20:27:18.154+05:30</updated><title type='text'>Exceptions and Interrupts on x86 family of processors (Part 1)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Interrupts and exceptions are special kinds of control transfer; they alter the normal program flow to handle external events or to report errors or exceptional conditions.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The difference between interrupts and exceptions is that interrupts are used to &lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;handle events external to the processor, but exceptions handle conditions detected by the processor itself while executing instructions (like divide by 0, invalid opcode, floating point exception etc).&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Identifying an interrupt/exception&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Intel processor associates an identifying number (ranging from 0 to 255) with each different type of interrupt or exception. Intel calls this identifying number a &lt;i style=&quot;mso-bidi-font-style: normal;&quot;&gt;vector.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;i style=&quot;mso-bidi-font-style: normal;&quot;&gt;&lt;br /&gt;
&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e5e5e5; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Interrupt and Exception numbers on x86 processors &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Vector Number&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;u&gt;Description &lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Divide by Zero &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug exceptions &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;2 &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; Nonmaskable interrupt &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;3 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Breakpoint (one-byte INT 3 instruction) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;4 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Overflow (INTO instruction) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;5 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Bounds check (BOUND instruction) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;6 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Invalid opcode &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;7 &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Coprocessor not available &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;8 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Double fault &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;9 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Reserved &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Invalid TSS &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;11 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Segment not present &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;12 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Stack exception &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;13 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; General protection &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;14 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Page fault &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;15 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Reserved &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;16 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Coprocessor error &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;17-31 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; Reserved &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;32-255 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Available for external interrupts via &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INTR pin &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The vectors of nonmaskable interrupt and exceptions are fixed. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;IRQs and external interrupts&lt;/b&gt;&lt;br /&gt;
The external devices like scsi disks, sound cards, network cards etc may be assigned any vector in the range 32 – 228.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Linux uses vector 28 (0x80) to implement system calls.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The IBM compatible PC architecture requires that some devices should be allocated particular fixed vector number as shown in following table.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoNormalTable&quot; style=&quot;background: #e5e5e5; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-cellspacing: 0in; mso-padding-alt: 3.0pt 3.0pt 3.0pt 3.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; mso-yfti-tbllook: 1184; width: 100%;&quot;&gt;&lt;thead&gt;
&lt;tr style=&quot;mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;IRQ &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;INT&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Hardware device &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/thead&gt;&lt;tbody&gt;
&lt;tr style=&quot;mso-yfti-irow: 1;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;32 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Timer&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 2;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;33 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Keyboard &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 3;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;2 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;34 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;PIC cascading &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 4;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;3 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;35 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Second serial port &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 5;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;4 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;36 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;First serial port &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 6;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;6 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;38 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Floppy disk &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 7;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;8 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;40 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;System clock &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 8;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;10 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;42 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Network card &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 9;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;11 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;43 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;USB port, sound card &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 10;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;12 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;44 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;PS/2 mouse &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 11;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;13 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;45 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Mathematical coprocessor &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 12;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;14 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;46 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;EIDE disk controller&#39;s first chain &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 13; mso-yfti-lastrow: yes;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;15 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;47 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt;&quot;&gt;&lt;div class=&quot;doctext&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;EIDE disk controller&#39;s second chain &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Interrupt vectors as used by Linux on an x86 processor&lt;/span&gt;&lt;/b&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoNormalTable&quot; style=&quot;background: #e5e5e5; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-cellspacing: 0in; mso-padding-alt: 3.0pt 3.0pt 3.0pt 3.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; mso-yfti-tbllook: 1184; width: 100%;&quot;&gt;&lt;thead&gt;
&lt;tr style=&quot;mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Vector Range&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Use&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/thead&gt;&lt;tbody&gt;
&lt;tr style=&quot;mso-yfti-irow: 1;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0 – 16 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Nonmaskable interrupts and exceptions &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 2;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;17 - 31 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Intel-reserved &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 3;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;32 - 127 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Maskable External interrupts (IRQs) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 4;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;128 (0x80) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Software interrupt for system calls (int 0x80) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 5;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;129 - 238 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;External interrupts (IRQs) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 6;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;239 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Local APIC timer interrupt &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 7;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;240 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Local APIC thermal interrupt &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 8;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;241 - 250 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Reserved by Linux for future use &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 9;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;251 – 253 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Interprocessor interrupts &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 10;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;254 (0xfe) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Local APIC error interrupt (generated when the local APIC detects an erroneous condition) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 11; mso-yfti-lastrow: yes;&quot;&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 26.32%;&quot; valign=&quot;top&quot; width=&quot;26%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;255 (0xff) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 3pt; padding-left: 3pt; padding-right: 3pt; padding-top: 3pt; width: 73.68%;&quot; width=&quot;73%&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Local APIC spurious interrupt (generated if the CPU masks an interrupt while the hardware device raises it) &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Enabling and Disabling Interrupts on intel x86 processor&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;The external interrupts that are signaled via the INTR pin of the processor can be enabled/disabled by the IF bit of flag register.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;When IF=0, INTR interrupts are inhibited.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;When IF=1, INTR interrupts are enabled.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The instructions CLI and STI alter the setting of IF.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;CLI (Clear Interrupt-Enable Flag) and STI (Set Interrupt-Enable Flag) explicitly alter IF (bit 9 in the flag register). &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;These instructions may be executed only by the linux kernel code that runs at a higher privilege level as compared to the user program. The user process cannot enable/disable the interrupts.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Programmable Interrupt Controller (PIC 82596)&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Each hardware device (like keyboard, sound card, hard disk etc) capable of issuing an interrupt request has an output line known as IRQ line. These IRQ lines are connected to the input pins of programmable interrupt controller (PIC 82596).&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0Zvjvh_r-72JTG3ym9S85I-OcxRmx_tG4XfQWuZ6spEEflznjHn6fSQzTRl34ML3l24gLKL5gJ9-QJlJ7j9yFt2HukU7P6tBI0jZ7kwe4xKJoWHzOYoCQlJ7FTNfrlbbQDmlu7QMFS3M/s1600/pic+82596.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; gu=&quot;true&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0Zvjvh_r-72JTG3ym9S85I-OcxRmx_tG4XfQWuZ6spEEflznjHn6fSQzTRl34ML3l24gLKL5gJ9-QJlJ7j9yFt2HukU7P6tBI0jZ7kwe4xKJoWHzOYoCQlJ7FTNfrlbbQDmlu7QMFS3M/s640/pic+82596.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;span style=&quot;height: 46px; margin-left: 228px; margin-top: 133px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 199px; margin-top: 133px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 175px; margin-top: 133px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 149px; margin-top: 133px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 46px; margin-top: 133px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 97px; margin-left: 36px; margin-top: 43px; position: absolute; width: 217px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 72px; margin-top: 133px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 67px; margin-left: 422px; margin-top: 128px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 2px; margin-left: 302px; margin-top: 193px; position: absolute; width: 127px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 194px; margin-left: 302px; margin-top: 1px; position: absolute; width: 2px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 556px; margin-top: 128px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 529px; margin-top: 128px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 504px; margin-top: 128px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height: 46px; margin-left: 398px; margin-top: 128px; position: absolute; width: 12px;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The IRQ lines are sequentially numbered from 0. The vector numbers for external interrupts start from 32. So, the PIC should be programmed in a way that it generates the vector number 32 when the hardware device connected to IRQ0 generates an interrupt.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;So, IRQ0 is associated with vector number 32.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IRQN is associated with vector number 32 + N&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Each IRQ can be selectively disabled. The PIC can be programmed to disable IRQ. The disabled interrupts are not lost and are issued to the CPU as soon as they are enabled again.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Typically on a PC, the PICs are implemented by connecting them in cascade as shown in figure above. Each 82596 chip can handle up to 8 IRQ input lines.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The INT output of slave PIC is connected to the IRQ 2 of the master PIC as is used for cascading. So, only 15 IRQ lines are available to the external devices.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimhjpMRfbL5xvYrfIn9gtYxAnRirYwhUxqeq6UXUUpyWVWwKwf_aC8QDxBgP8pUX0rcfmDk9wKBQ5mWUPIzXvHD4PJqqAqsnVUbPqi86Qv-mXBY6EnGyCMaHxDmpvvQkou0ObNVFPVBP4/s1600-h/image%5B5%5D.png&quot;&gt;&lt;img align=&quot;left&quot; alt=&quot;image&quot; border=&quot;0&quot; height=&quot;335&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgD8wgC7hNrLy4kWYiR1fxk89jl_HiTc9WxXpYjaoOv6XT82Pw-qjoPYb7ULU1AI64w5mexWcSv_HuA_BRcmr5ir-QZcuWqMJg1KGuJGS7v9VDot56s6UlYl4MrPLQKJJsrOOPNFfnZUr8/?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; margin-left: 0px; margin-right: 0px;&quot; title=&quot;image&quot; width=&quot;628&quot; /&gt;&lt;/a&gt; &lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;The 8 IRQ lines are first passed through the IMR (Interrupt Mask Register) to see if they are masked or not. If a particular interrupt is masked then it is not processed further. If it is not masked it will register its request with the Interrupt Request Register (IRR) by setting the corresponding bit in it. The priority resolver then selects the highest priority interrupt and set the corresponding bit in the ISR register.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Sequence of events in handling an interrupt request &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;1) One or more IRQ lines (IRQ0 to IRQ15) are raised high by the external device connected to these lines. This results in setting the corresponding bits in IRR (Interrupt Request Register).&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;2) The chip 82596 sends INT to CPU (ie INT line on the processor is asserted) &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;3) &amp;nbsp;CPU acknowledges the INT and responds with INTA pulse&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;4) Upon receiving INTA from CPU, the highest priority bit from IRR is selected and the corresponding bit in ISR (Interrupt Service Register) is set. The selected bit in IRR is reset.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The ISR bit shows which IRQ is being currently served.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The corresponding bit in IRR is reset as it is no longer requesting service but actually getting service.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;5) CPU then initiates a second INTA pulse to tell the PIC to place the 8 bit IRQ number on the the data bus (corresponding the vector number of IRQ being serviced).&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;6) CPU then reads the data bus to find out the interrupt vector number to call the associated Interrupt handler code.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;7) Once the interrupt handler (ISR) is done, it sends an EOI (End of Interrupt) to the PIC. The PIC will then determine the next highest priority interrupt and repeat the same process.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;This is all for today. In the next article we will see how interrupts are handled by linux kernel and discuss in more details about them.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Till then, Have Fun !!!&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/8129356256899350303/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/06/exceptions-and-interrupts-on-x86-family.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/8129356256899350303'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/8129356256899350303'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/06/exceptions-and-interrupts-on-x86-family.html' title='Exceptions and Interrupts on x86 family of processors (Part 1)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0Zvjvh_r-72JTG3ym9S85I-OcxRmx_tG4XfQWuZ6spEEflznjHn6fSQzTRl34ML3l24gLKL5gJ9-QJlJ7j9yFt2HukU7P6tBI0jZ7kwe4xKJoWHzOYoCQlJ7FTNfrlbbQDmlu7QMFS3M/s72-c/pic+82596.JPG" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-6959958423057861223</id><published>2010-05-31T23:47:00.008+05:30</published><updated>2015-02-14T20:22:01.073+05:30</updated><title type='text'>Data Structure Alignment and Padding</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;You must have heard that the fundamental data types must be aligned to a specific byte boundary. Most of you must have seen your programs being killed due to alignment exception generated by the processor. For any C programmer it is very important to understand the reasons behind the alignment restrictions.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;In this article we will see why the compiler align certain data types to a certain boundary.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Why alignment restrictions ?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Typically a 32 bit processor reads or writes from the memory in chunks of 4 bytes (32 bits). The reads and writes can be performed at addresses that are divisible by 4.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Even if you want to read a single byte, 4 bytes are read from the memory and then the processor hardware circuitry will copy the requested byte to the specific position in the register.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;For example, on &lt;b&gt;m68K&lt;/b&gt; processor, a multiplexer sits in between an internal register and the external bus. The multiplexer selects the specified byte and routes it to the appropriate byte position in the register.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-sGf3Brs3Zvuj8Ywxrxdl_RuelXyRhFlCCYA71jg16Os2Y0hrbBUSCciR_xrFe1ggJDbYWtAG0y0h9oMc_uVSxsCBIJCNVHB8A4fNwtAYBbciiJ-NGkql6xT_w8Zif6wpy1asLLpuA8w/s1600/alignment2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: both; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; gu=&quot;true&quot; height=&quot;456&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-sGf3Brs3Zvuj8Ywxrxdl_RuelXyRhFlCCYA71jg16Os2Y0hrbBUSCciR_xrFe1ggJDbYWtAG0y0h9oMc_uVSxsCBIJCNVHB8A4fNwtAYBbciiJ-NGkql6xT_w8Zif6wpy1asLLpuA8w/s640/alignment2.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Lets try to see why processor restricts the data to be aligned to a specific byte boundary. Suppose the processor needs to read an integer (4 bytes in size). &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Case 1: Integer is stored at address that is 4 byte aligned (say 0x100) &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;As the processor can read/write only at addresses that are divisible by 4, all four bytes can be read in a single bus cycle. Note that the address 0x100 is divisible by 4. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;background: #fde9d9; border-bottom: medium none; border-collapse: collapse; border-left: medium none; border-right: medium none; border-top: medium none; mso-background-themecolor: accent6; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;&lt;tbody&gt;
&lt;tr style=&quot;height: 28.75pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 72.9pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Address &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 0.75in;&quot; valign=&quot;top&quot; width=&quot;72&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 0&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 1 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 63pt;&quot; valign=&quot;top&quot; width=&quot;84&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 2 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 3 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;height: 26.5pt; mso-yfti-irow: 1;&quot;&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 72.9pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;0x100 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 0.75in;&quot; valign=&quot;top&quot; width=&quot;72&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X0&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X1 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 63pt;&quot; valign=&quot;top&quot; width=&quot;84&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X2 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X3 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;height: 26.5pt; mso-yfti-irow: 2; mso-yfti-lastrow: yes;&quot;&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 72.9pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 0.75in;&quot; valign=&quot;top&quot; width=&quot;72&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 63pt;&quot; valign=&quot;top&quot; width=&quot;84&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Case 2: Integer is stored at an unaligned address (say 0x101) &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;This read cannot be performed in a single 32 bit bus cycle. The processor will have to issue two different reads at addresses 0x100 and 0x104.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Thus, it takes twice the time to read a misaligned data. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;color: black; font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;background: #fde9d9; border-bottom: medium none; border-collapse: collapse; border-left: medium none; border-right: medium none; border-top: medium none; mso-background-themecolor: accent6; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;&lt;tbody&gt;
&lt;tr style=&quot;height: 28.75pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 72.9pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Address &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 0.75in;&quot; valign=&quot;top&quot; width=&quot;72&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 0&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 1 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 63pt;&quot; valign=&quot;top&quot; width=&quot;84&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 2 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: black 1pt solid; height: 28.75pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Byte 3 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;height: 26.5pt; mso-yfti-irow: 1;&quot;&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 72.9pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;0x100 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 0.75in;&quot; valign=&quot;top&quot; width=&quot;72&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X0 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 63pt;&quot; valign=&quot;top&quot; width=&quot;84&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X1 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X2 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;height: 26.5pt; mso-yfti-irow: 2; mso-yfti-lastrow: yes;&quot;&gt;&lt;td style=&quot;border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 72.9pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;0x104 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 0.75in;&quot; valign=&quot;top&quot; width=&quot;72&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;X3 &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 63pt;&quot; valign=&quot;top&quot; width=&quot;84&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;background: #dbe5f1; border-bottom: black 1pt solid; border-left: medium none; border-right: black 1pt solid; border-top: medium none; height: 26.5pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0in; width: 58.5pt;&quot; valign=&quot;top&quot; width=&quot;78&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;It is important to note that&lt;/span&gt; &lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-family: symbol; font-size: 12pt; mso-bidi-font-family: symbol; mso-fareast-font-family: symbol;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;·&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Some processors allow unaligned access but at a performance penalty as we saw above. Total time to read a misaligned data is more/double as compared to the aligned data Intel processor allow reading/writing data which is unaligned but at the cost of reduced performance.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-family: symbol; font-size: 12pt; mso-bidi-font-family: symbol; mso-fareast-font-family: symbol;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;·&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;Some processors generate an alignment exception on accessing a misaligned data. Then it is up to the exception handler either to report an error (kill the user process) or perform two read cycles to read the misaligned data (user process will run normally in this case but at reduced performance).&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;C compiler allocates addresses such that the data types are properly aligned. This helps in speeding up the read/write operation without causing any exception.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Typically on a 32 bit processor (like 32 bit x86, m68k, PowerPC, ARM etc)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: symbol; font-size: 12pt; mso-ansi-language: en; mso-bidi-font-family: symbol; mso-fareast-font-family: symbol;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;·&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;A &lt;b&gt;char&lt;/b&gt; (one byte) will be 1-byte aligned. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: symbol; font-size: 12pt; mso-ansi-language: en; mso-bidi-font-family: symbol; mso-fareast-font-family: symbol;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;·&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;A &lt;b&gt;short&lt;/b&gt; (two bytes) will be 2-byte aligned. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: symbol; font-size: 12pt; mso-ansi-language: en; mso-bidi-font-family: symbol; mso-fareast-font-family: symbol;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;·&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;An &lt;b&gt;int&lt;/b&gt; (four bytes) will be 4-byte aligned. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: symbol; font-size: 12pt; mso-ansi-language: en; mso-bidi-font-family: symbol; mso-fareast-font-family: symbol;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;·&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;Any &lt;b&gt;pointer&lt;/b&gt; (four bytes) will be 4-byte aligned &lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&lt;/span&gt;(e.g.: char *, int *) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;Structure alignment and padding&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;Compiler add pad bytes in the user defined structures so that the various fields of a structure are properly aligned. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;There are certain rules that can be kept in mind to understand padding in a structure. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;1) No padding before the first element &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt;&quot;&gt;The first element of a struct must come first, and must be preceded by no padding. This allows a struct pointer to be converted to a pointer to the struct&#39;s first element and vice versa, which is a useful property.&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;2) Pad bytes at the end of structure &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;It is obvious to add bytes in between the elements of a structure to keep the elements aligned. But why the compiler will put pad bytes at the end of a structure ? &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;This is because if an array of structure is declared, start of each structure should be properly aligned. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;For an array of structure, &lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;struct A array[10], &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: wingdings; font-size: 12pt; mso-ansi-language: en; mso-bidi-font-family: wingdings; mso-fareast-font-family: wingdings;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;ð&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;array[1]&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt; should be equivalent to &lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;*(array + 1).&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: wingdings; font-size: 12pt; mso-ansi-language: en; mso-bidi-font-family: wingdings; mso-fareast-font-family: wingdings;&quot;&gt;&lt;span style=&quot;mso-list: ignore;&quot;&gt;ð&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;difference between &lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;array + 1&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt; and &lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;array + 0&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;, should exactly equal to the size of &lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;struct A&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;margin-left: 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;To satisfy above constraints, compiler may have to put padding bytes at the end of structure. The rule is that the last member inside a structure should be padded with the number of bytes to make the structure aligned to the size of largest member of structure. &lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;Lets see few examples for structure alignment and padding&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;background: #e5e5e5; border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: black 1pt solid; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-element: para-border-div; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;struct exampleStruct { &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char data1; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;short data2; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int data 3; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char data 4; &lt;/span&gt;&lt;/div&gt;};&lt;/div&gt;&lt;br /&gt;
&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;On a typical 32 bit processor (say m68k or PowerPC), the compiler will insert padding in between various members so that they are properly aligned. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #e5e5e5; border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: black 1pt solid; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-element: para-border-div; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;struct exampleStruct { &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char data1; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char padding[1];&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// pad of 1 byte to keep short to be 2 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// byte aligned &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;short data2; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int data3;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// already aligned to 4 byte boundary, &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// so no padding before this &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char data4; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char padding[3]; &lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// padding at the end of structure so &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&lt;/span&gt;// that if an array of such structures is &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// declared, the start of each structure &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// in the array is properly aligned. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Padding should be done such that &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// start of structure address is aligned &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// to the size of largest data member of &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// structure. In this case, the size of &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// largest data type (int data3) is 4. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// So, 3 pad bytes are added to the end. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;}; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;How to arrange members so as to minimize padding? &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;By changing the ordering of members in a structure, it is possible to minimize the amount of padding. If members are sorted by ascending or descending aligment requirements, minimal amount of padding is required. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;So, if you have a structure with multiple integer, short and char members, then keep all integer members at the beginning, keep all short members after interger members and all chars at the end. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;How to prevent compiler from putting pad bytes in between or at the end of structure? &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;Compiler provides #pragma directives to specify the alignment of members inside a structure. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;background: #e5e5e5; border-bottom: black 1pt solid; border-left: black 1pt solid; border-right: black 1pt solid; border-top: black 1pt solid; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-element: para-border-div; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;#pragma pack(push)&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// save original alignment on stack &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;#pragma pack(1)&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// set alignment to 1 byte boundary &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;struct packedStruct { &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char data1; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int data2;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;/span&gt;// no padding is inserted, &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; // as alignment specified is 1 byte. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;char data3; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;}; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #e5e5e5; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-10 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; mso-ansi-language: en;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;#pragma pack(pop)&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// restore original alignment&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Arial&amp;quot;, &amp;quot;sans-serif&amp;quot;; font-size: 12pt; mso-ansi-language: en;&quot;&gt;Here, we tell the compiler that each element should be aligned to 1 byte boundary. So, no padding is inserted in between elements. The size of this structure aftre compilation will be 6 bytes. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; mso-ansi-language: en;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Thats all for today.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Have Fun !!!&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/6959958423057861223/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/05/data-structure-alignment-and-padding.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/6959958423057861223'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/6959958423057861223'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/05/data-structure-alignment-and-padding.html' title='Data Structure Alignment and Padding'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-sGf3Brs3Zvuj8Ywxrxdl_RuelXyRhFlCCYA71jg16Os2Y0hrbBUSCciR_xrFe1ggJDbYWtAG0y0h9oMc_uVSxsCBIJCNVHB8A4fNwtAYBbciiJ-NGkql6xT_w8Zif6wpy1asLLpuA8w/s72-c/alignment2.JPG" height="72" width="72"/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-5891266992907869187</id><published>2010-05-19T19:39:00.007+05:30</published><updated>2015-02-14T20:22:01.065+05:30</updated><title type='text'>Dissecting FAT 16 File System</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;In this article I will discuss about the internals of FAT16 file system, its layout and how the files and directories are stored in it. We will dissect the FAT file system by reading the important blocks and analyze their contents to get an understanding of how the fat16 file system works.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Sectors Vs Clusters&lt;/span&gt; &lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Data is written on disks in terms of sectors which are typically 512 bytes in size. You cannot read/write data less than sector size. &lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Cluster is a fixed number of contiguous sectors on disk. FAT file system allocate space to a file in terms of clusters and not sectors. If the cluster size is 8, it means that even if you write a single byte to a file, 8 contiguous sectors (ie 8 * 512 = 4096 bytes) will be allocated to the file.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Why FAT uses clusters instead of sectors ?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Like any file system FAT also needs to keep information about which sectors on disk are free and which are allocated to a file. FAT keeps a table, where it uses two bytes for each sector. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;For efficiently reading/writing to a file, this table should be kept in memory. Smaller the size of table, more part of the table can be kept in memory. If instead of sector, cluster is used as a minimum allocation unit, the size of table will be reduced. If we use 8 sectors / cluster, the size of table will be reduces 8 times.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Larger File System&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Also, larger disks can be handled using a larger cluster size. As FAT uses 2 bytes per cluster, total number of clusters that can be addressed are 0xFFFF (65535). Bigger the size of cluster, bigger the size of FAT file system that can be created.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Cluster size (Bytes)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FAT16 File System Size &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; 1024 (1K)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 67MB = (65536 * 1024) Bytes &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;4096 (4K)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; 268 MB &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8192 (8K)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 536 MB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 32768 (32K&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2.14 GB &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Better Performance&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Another advantage of using a cluster is that if we have more contiguous blocks of sectors allocated to a file. This reduces the disk seek time and therefore improves file system performance.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;
&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;Drawbacks (Internal Fragmentation)&lt;/strong&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;The drawback of using a cluster is the internal fragmentation. If we need to write a single byte, the file system will allocate a cluster. If size of cluster is 8 sectors, we will end up allocating (8 * 512) = 4096 bytes, which means a lot of wastage of disk space. Since, most of the files are larger than sector size, the cluster concept works great.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;b&gt;FAT File System Layout on disk&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;A filesystem is created on a partition on the disk. A partition can be created using &lt;b&gt;“fdisk”&lt;/b&gt; command.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg32p7QubkLQd2g1yJDdgjwu-Pk4eV2TT46fPlKpXfn91LcjUrxpA-cbkxFjaXnhPPyJsbfIIdlm4DzDiNeXqYCZYIM5wChZVZLIkvriAjZ3VJRakuDnmUTRc5K1YsLr2j33Zt0gMny1Ak/s1600/fat+layout-2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg32p7QubkLQd2g1yJDdgjwu-Pk4eV2TT46fPlKpXfn91LcjUrxpA-cbkxFjaXnhPPyJsbfIIdlm4DzDiNeXqYCZYIM5wChZVZLIkvriAjZ3VJRakuDnmUTRc5K1YsLr2j33Zt0gMny1Ak/s640/fat+layout-2.JPG&quot; width=&quot;520&quot; wt=&quot;true&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;As you see in the above diagram, essentially a FAT File System comprise of three sections.&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;· Boot Sector&lt;/span&gt; &lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;· Primary And Secondary FAT Tables&lt;/span&gt; &lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;· Data Clusters that contain the root directory and other files and directories.&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets discuss FAT Table first&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;FAT table keeps information about which clusters in the data area are allocated to a file/directory and which are free. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;It uses 2 bytes for each cluster.&lt;/span&gt; &lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 0in;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0in 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;FAT&amp;nbsp; TABLE VALUE&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;MEANING&lt;/b&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0in 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0x0000 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cluster is free and not allocated to any &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0in 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file/directory&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0x0002 – 0xFFEF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Used, Next cluster in file&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0in 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0xFFF7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Bad Cluster &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0in 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0xFFF8 – 0xFFFF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; Used, Last Cluster in file&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;The first two entries in the FAT Table are the special entries that are used to indicate the type of media and the dirty status of the file system.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;The first entry stores the type of storage medium (0xF0 means removable and 0xF8 means non removable)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;The second entry in the table indicates if the file system is dirty or not.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Suppose the file (Hello.txt) is allocated cluster number 3, then to find the next cluster allocated to this file go to third entry in the table which will have the next cluster number (which is 7) allocated. Then go to entry number 7 which has 0xFFFF as the next cluster number. 0xFFFF indicates end of file.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;So, find all the clusters allocated to a file, we need to traverse the FAT table. That is why it will be more efficient to keep the entire FAT Table in the memory.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;FAT 16 BOOT SECTOR&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;It is the first sector in the FAT file system&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Contains information about the file system like Sectors per cluster, Number of FAT Tables, file system type (FAT 16/12/32) etc.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoNormalTable&quot; style=&quot;width: 746px;&quot;&gt;&lt;tbody&gt;
&lt;tr style=&quot;mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Bytes &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Purpose &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 1;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;0-2 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Assembly code instructions to jump to boot code (present in bootable partition) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 2;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;3-10 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;OEM name in ASCII &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 3;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;11-12 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Bytes per sector (512, 1024, 2048, or 4096) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 4;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;13 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Sectors per cluster (Must be a power of 2 and cluster size must be &amp;lt;=32 KB) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 5;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;14-15 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Size of reserved area, in sectors &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 6;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;16 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Number of FAT Tables (usually 2, one primary and other backup) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 7;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;17-18 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Maximum number of files in the root directory &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 8;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;19-20 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Number of sectors in the file system; if 2 Bytes are not large enough, set to 0 and use 4 Byte value in bytes 32-35 below &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 9;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;21 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Media type (0xF0 = removable disk, 0xF8 = fixed disk) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 10;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;22-23 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Size of each FAT in sectors &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 11;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;24-25 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Sectors per track in storage device &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 12;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;26-27 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Number of heads in storage device &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 13;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;28-31 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Number of sectors before the start partition &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 14;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;32-35 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Number of sectors in the file system; this field will be 0 if the 2Byte field above (bytes 19-20) is non-zero &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 15;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;36 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;BIOS INT 13h (low level disk services) drive number &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 16;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;37 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Not used &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 17;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;38 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Extended boot signature to validate next three fields (0x29) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 18;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;39-42 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Volume serial number &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 19;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;43-53 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Volume label, in ASCII &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 20;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;54-61 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;File system type level, in ASCII. (Generally &quot;FAT&quot;, &quot;FAT12&quot;, or &quot;FAT16&quot;) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 21;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;62-509 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Not used &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 22; mso-yfti-lastrow: yes;&quot;&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 67.5pt;&quot; width=&quot;90&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;510-511 &lt;/div&gt;&lt;/td&gt;&lt;td style=&quot;padding-bottom: 3.75pt; padding-left: 3.75pt; padding-right: 3.75pt; padding-top: 3.75pt; width: 226.55pt;&quot; width=&quot;643&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;Signature value (0xaa55) &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;
&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;Lets print the BOOT SECTOR of FAT16 File System.&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;I created this FAT16 File System on 1GB disk. I used “dd” command to read the sectors from the partition.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Bytes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Value&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Meaning&amp;nbsp; &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;00-02:&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;eb 3c 90&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Instructions to jump to boot code &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;03-0a: &amp;nbsp;&amp;nbsp;&amp;nbsp; 4d 53 44 4f 53 35 2e 30&amp;nbsp;&amp;nbsp; Name string (MSDOS5.0) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0b-0c: &amp;nbsp;&amp;nbsp;&amp;nbsp; 00 02&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Bytes/sector (0x0200 = 512)&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0d&amp;nbsp;&amp;nbsp; :&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; Sectors/cluster (0x20 = 32)&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0e-0f: &amp;nbsp;&amp;nbsp;&amp;nbsp; 02 00 &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Size of reserved area (2 sectors) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;10&amp;nbsp;&amp;nbsp; : &amp;nbsp;&amp;nbsp;&amp;nbsp; 02&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number of FAT Tables (2) primary and secondary&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;11-12:&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;00 02&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Max. number of root directory entries &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (0x0200 = 512) &lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;13-14: &amp;nbsp;&amp;nbsp;&amp;nbsp; 00 00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Total number of sectors (0x0000) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; 2 Bytes are not large enough to &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; store the size of this fileSystem &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;15&amp;nbsp;&amp;nbsp; : &amp;nbsp;&amp;nbsp;&amp;nbsp; f8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;Media type (non removable) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;16-17: &amp;nbsp;&amp;nbsp;&amp;nbsp; ff 00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; FAT size (0x00ff = 255 sectors)&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;18-19: &amp;nbsp;&amp;nbsp;&amp;nbsp; 20 00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;Sectors/track (0x0020 = 32) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1a-1b: &amp;nbsp;&amp;nbsp;&amp;nbsp; 80 00&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;Number of heads (0x0080 = 128) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1c-1f: &amp;nbsp;&amp;nbsp; &amp;nbsp;20 00 00 00&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; Number of sector before partition (0x20 = 32)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;20-23: &amp;nbsp;&amp;nbsp;&amp;nbsp; c3 dd 1f 00 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;Total number of sectors (0x001fddc3 ~ 1MB)&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;24&amp;nbsp;&amp;nbsp; : &amp;nbsp;&amp;nbsp;&amp;nbsp; 80&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Drive number (80) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;25&amp;nbsp;&amp;nbsp; : &amp;nbsp;&amp;nbsp;&amp;nbsp; 00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Unused &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;26&amp;nbsp;&amp;nbsp; : &amp;nbsp;&amp;nbsp;&amp;nbsp; 29&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Extended boot signature &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;27-2a:&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;fa 1e e7 34 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Volume serial number &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;2b-35: &amp;nbsp;&amp;nbsp; &amp;nbsp;4e 4f 20 4e 41 4d 45 20 20 20 20&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Volume label (&quot;NO NAME &quot;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;36-3d: &amp;nbsp;&amp;nbsp; &amp;nbsp;46 41 54 31 36 20 20 20&lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;File system type label (&quot;FAT16 &quot;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;3e-1fd : &amp;nbsp;&amp;nbsp;[snip]&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Not used &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span lang=&quot;EN&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1fe-1ff: &amp;nbsp;&amp;nbsp;55 aa&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; Signature value (0xaa55) &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;
&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;DATA AREA&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Data area contains the root directoy and all other files and sub directories.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Root Directory store the directory entries for each file in the root directory. The most important data in each directory entry is the name of the file and the first cluster number of the file. If you the first cluster number then all other clusters that belong to this file can be found.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;After the root directory, data belonging to all the files and the subdirectories is stored. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;
&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;First lets have a look at the various fields of DIRECTORY ENTRY&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;VARIOUS FIELDS OF DIRECTORY ENTRY &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Bytes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Purpose &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First character of file name Or allocation status&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;(0x00 = unallocated, 0xe5 = deleted)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1-10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Characters 2-11 of the file name&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; File Attributes&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x01&amp;nbsp;&amp;nbsp; =&amp;gt;&amp;nbsp; Read Only &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x02&amp;nbsp;&amp;nbsp; =&amp;gt;&amp;nbsp; Hidden File &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x04&amp;nbsp;&amp;nbsp; =&amp;gt;&amp;nbsp; System File&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0x08&amp;nbsp;&amp;nbsp; =&amp;gt;&amp;nbsp; Volume Label &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x0f&amp;nbsp;&amp;nbsp; =&amp;gt;&amp;nbsp; Long File Name &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x10&amp;nbsp;&amp;nbsp; =&amp;gt;&amp;nbsp; Directory &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;0x20&amp;nbsp;&amp;nbsp; =&amp;gt;&amp;nbsp; Archive &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Reserved &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;13&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;File Creation Time (in tenths of seconds) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;14-15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Creation Time (Hours, minutes, seconds) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;16-17&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Creation date &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;18-19&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Access Date &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;20-21&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; High order 2 Bytes of address of first cluster &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (0 for FAT16) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;22-23&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Modified Time (hours, minutes, seconds) &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;24-25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Modified Date &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;26-27&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Low order two bytes of address of first cluster&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;28-31&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; File size (0 for directories)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets do the dissection of a real FAT16 File System&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;I printed the boot sector of the FAT file system above that has the overall information about the entire file system. We will use this information to create the layout of the file system&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;No of sectors per cluster &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;= 32&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;No of reserved sectors &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; = 2&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Number of FAT tables &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; = 2 (one primary and one secondary)&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Size of each FAT Table &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; = 255 sectors&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Size of single directory entry &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; = 32 bytes &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Max number of Root Directory Entries &amp;nbsp; = 512&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Size of root directory = 512 * 32 = 32 sectors &amp;nbsp; &amp;nbsp;= 1 cluster &lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;So the LAYOUT of the file system is&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnKeipS4vXGNwEKZlCzEXLHJUR4RtddYCJfm8m3OlYIcw_Cl1hunnFJn-f0grn6aPpvuv7Yw_Nof4J-5fkRC3Q_1OPlD5dY84k8uJYJKqzImBFlWOSPxXMVdvtW5Y7ybybGiaUGJ2mFw4/s1600-h/layout[4].jpg&quot;&gt;&lt;img alt=&quot;layout&quot; border=&quot;0&quot; height=&quot;281&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEingdCliHP2AA_xh6a3IU-BXRVlBdBHFi7MCzMV1LF-ZrqPcKvhUNbcQlo1Lji5uF3lAlxMcZ_ourQwG3AOrnig7BrddqzC3G2d3CEXrsmdjz-sZBb3JcevEPiuVFXC4EL8t2Jiieo5AiI/?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; margin: 0px;&quot; title=&quot;layout&quot; width=&quot;883&quot; /&gt;&lt;/a&gt; &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;Now lets analyze the contents of Root Directory and other files.&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&amp;nbsp;In my filesystem, I created following folders and files.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;/folder1 &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;/folder1/test3.txt &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;/folder2 &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;/folder2/test4.txt &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;/test1.txt &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;/test2.txt &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;Lets print the root directory contents of my file system&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;Byte &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;Offset&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Directory Entry (32 Bytes)&lt;/strong&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;-------------------------------------------------------&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: white; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1000000&amp;nbsp;&lt;span style=&quot;background-color: yellow;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;T&amp;nbsp; E&amp;nbsp; S&amp;nbsp; T &amp;nbsp;&amp;nbsp; &amp;nbsp;F&amp;nbsp; A&amp;nbsp; T&amp;nbsp; 1 &amp;nbsp;6&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 20 &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;08&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 00 00 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000020 &lt;/span&gt;00 00 00 00 00 00 fd 00 b1 3c 00 00 00 00 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;First directory entry (32 bytes)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Volume Name = TEST FAT16&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial;&quot;&gt;File Attribute = 08 = Volume Label&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;-------------------------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: white; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1000040 &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: red; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;e5&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 4e 00 65 00 77 00 20 00 46 00 0f 00 dd 6f 00 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000060 &lt;/span&gt;6c 00 64 00 65 00 72 00 00 00 00 00 ff ff ff ff&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Second directory entry (32 bytes) &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: red; color: black;&quot;&gt;e5&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: red; background-origin: initial;&quot;&gt; = Deleted&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;-------------------------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-clip: initial; background-color: white; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1000140 &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;F&amp;nbsp; O&amp;nbsp; L&amp;nbsp; D&amp;nbsp; E&amp;nbsp; R&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 00 4c 02 01&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000160 &lt;/span&gt;b1 3c b1 3c 00 00 03 01 b1 3c &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;02 00 &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;00 00 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Fourth Directory Entry &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Directory Name = FOLDER1&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial;&quot;&gt;Attribute = 10 = Directory&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial;&quot;&gt;First Cluster Address = 0002&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;-------------------------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;snip&gt;(removed deleted/unallocated directory entries&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000300&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;F&amp;nbsp; O&amp;nbsp; L&amp;nbsp; D&amp;nbsp; E&amp;nbsp; R &amp;nbsp;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 00 91 07 01 &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000320 &lt;/span&gt;b1 3c b1 3c 00 00 08 01 b1 3c &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;03 00&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 00 00 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Directory Name = FOLDER2&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial;&quot;&gt;Attribute = 10 = Directory&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial;&quot;&gt;First cluster address = 0003&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;--------------------------------------------------------&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;/div&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000500&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;T&amp;nbsp; E&amp;nbsp; S&amp;nbsp; T&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp; X&amp;nbsp; T &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 00 7e 0b 01 &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000520 &lt;/span&gt;b1 3c b1 3c 00 00 22 01 b1 3c &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;04 00 &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;b8 01 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;File Name = TEST1.TXT&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial;&quot;&gt;Attributes = 20 = Archive&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial;&quot;&gt;First Cluster address = 0004&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;--------------------------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000700&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;T&amp;nbsp; E&amp;nbsp; S&amp;nbsp; T&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp; X&amp;nbsp; T &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 00 7e 0b 01 &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;1000720 &lt;/span&gt;b1 3c b1 3c 00 00 3a 01 b1 3c &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;05 00 &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;2b 01 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;File Name = TEST2.TXT&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial;&quot;&gt;Attribute = 20 = Archive&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial;&quot;&gt;First cluster address = 0005&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;So, we saw that the root directory contains the directory entries of all folders and files that are created inside it. The directory entry contains the first sector containing the directory or file, so we can find all subdirectories and files inside it.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Lest look at FOLDER1. I created the file &lt;b&gt;test3.txt&lt;/b&gt; inside FOLDER1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;As per the directory entry of FOLDER1 (printed above) the first cluster number that contains the contents of FOLDER1 is 0x02.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;We will print the contents of cluster no 2. Note that cluster numbering starts from 2. Cluster 2 lies just after the end of root directory. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Sector number corresponding to cluster 2 = 2 + 255 *2 + 32 = 544&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;strong&gt;CONTENTS OF FOLDER1&amp;nbsp; (cluster 2) Sector #544&amp;nbsp; &lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1040000 2e 20 20 20 20 20 20 20 20 20 20 10 00 4c 02 01 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1040020 b1 3c b1 3c 00 00 03 01 b1 3c 02 00 00 00 00 00 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;lt;snip&amp;gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1040240&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;T&amp;nbsp; E&amp;nbsp; S&amp;nbsp; T&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; T&amp;nbsp; X&amp;nbsp; T &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; 00 59 45 01 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1040260 b1 3c b1 3c 00 00 54 01 b1 3c &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;06 00 &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: cyan; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;f2 00 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;File Name = TEST3.TXT&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial;&quot;&gt;Attributes = 0x20 (Archive)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-clip: initial; background-color: magenta; background-origin: initial;&quot;&gt;First Cluster address = 0006&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;The first cluster that contains the file TEST3.TXT is 0x06. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;I&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt; stored following contents in TEST3.TXT&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;# cat /folder1/test3.txt &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TXT CONTENTS oF TEST3.TX &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets print the contents of cluster 6 to see if the contents match with the contents of file test3.txt&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif;&quot;&gt;Cluster 6 correspond to sector number 2 + 255 * 2 + 32 + 32 * 4 = 672 &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240000 &amp;nbsp;C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240020 &amp;nbsp;3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240040 &amp;nbsp;F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240060 &amp;nbsp;E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240100 &amp;nbsp;T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp; &amp;nbsp;T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240120&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240140 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240160&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; . &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240200&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240220&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240240&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240260&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240300 &amp;nbsp;3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240320 &amp;nbsp;F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240340&amp;nbsp; E&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o&amp;nbsp;&amp;nbsp; F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; S&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; T&amp;nbsp;&amp;nbsp; X &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240360&amp;nbsp; T&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;\0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;1240400 \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0&amp;nbsp; \0 &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;So, we saw that the contents matches with what we stored in test3.txt.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;You can follow the same approach to traverse the directory hierarchy to print the contents of other files.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;Lets print the primary FAT Table as well&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #dfdfdf; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0002000 &lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial;&quot;&gt;f8&lt;/span&gt; ff ff ff ff ff &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;ff&lt;/span&gt; ff ff ff ff ff ff ff ff ff &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;0002020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;background-clip: initial; background-color: lime; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Entry 0 = 0xf8&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;(media type non removable) &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Entry 1 = 0xff &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Entry 2 = 0xff &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;.. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial; font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Entry 6 = 0xff&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; which indicates the end of file for test3.txt &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNoSpacing&quot; style=&quot;background: #dfdfdf; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-pattern: gray-125 auto; mso-shading: windowtext; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (ie test3.txt has only one cluster allocated) &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Thats all for today. I hope you find this article useful. If you have any questions or find something missing or incorrect please post your comments. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Have Fun !!!&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/5891266992907869187/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/05/dissecting-fat-16-file-system.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/5891266992907869187'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/5891266992907869187'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/05/dissecting-fat-16-file-system.html' title='Dissecting FAT 16 File System'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg32p7QubkLQd2g1yJDdgjwu-Pk4eV2TT46fPlKpXfn91LcjUrxpA-cbkxFjaXnhPPyJsbfIIdlm4DzDiNeXqYCZYIM5wChZVZLIkvriAjZ3VJRakuDnmUTRc5K1YsLr2j33Zt0gMny1Ak/s72-c/fat+layout-2.JPG" height="72" width="72"/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-2001261728966398821</id><published>2010-04-17T18:19:00.025+05:30</published><updated>2015-02-14T20:22:01.055+05:30</updated><title type='text'>Executable File Formats (ELF)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;What happens when you compile your C code ?&lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=1558604960&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Answer is simple, the compiler generates the executable. On a linux/unix system, by deafult the name of the executable generated is “a.out”.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;What’s there inside an executable file (a.out) ?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Have you ever tried dissecting an a.out file ? Its not a plain binary file of machine codes. It is much more than that and has lot of other information that helps Operating System to load it in memory. The executable files have various formats like COFF, ELF etc.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Now a day, most of the unix like operating systems (linux, BSD, Solaris, IRIX) etc use ELF (&lt;b&gt;Executable and Linkable Format&lt;/b&gt;) format for their executables.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Typically an elf executable includes&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;ELF Header&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;Program Headers&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;Section Headers&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;Data referred by program or section headers&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDCqEdcQPpTGum0bPUtjZil5T9T5Xs8WWsHPacJdBSRV74okr5eRzmvl_EA-C1kHccP2dCVHndfHsDqv9NGV_r32_qdEaJli-8UABJlb499Eeq-trNI9qfOH9ynLyr62o4QNyLDEzfKpY/s1600/elf.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDCqEdcQPpTGum0bPUtjZil5T9T5Xs8WWsHPacJdBSRV74okr5eRzmvl_EA-C1kHccP2dCVHndfHsDqv9NGV_r32_qdEaJli-8UABJlb499Eeq-trNI9qfOH9ynLyr62o4QNyLDEzfKpY/s1600/elf.JPG&quot; width=&quot;610&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Dissecting an ELF File&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;We will take a simple C Program, compile it and see what all is there in the generated a.out (ELF) file.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;/************************* test.c ************************/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;int global1 = 100;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;int global2;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;int main (void)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; global2 = 200;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; global1 = 300;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; printf(“global1 = %d global2 = %d\n”, global1, global2);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; return 0;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;On compiling it on a linux system, a.out is generated with elf file format.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;You can determine the file format using the &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;file&lt;/b&gt; command.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;# file a.out&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;a.out ELF 32-bit&amp;nbsp; LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux&amp;nbsp; 2.6.9, dynamically linked (user shared libs), for GNU/Linux 2.6.9, not stripped&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;“file” command determines this information by reading the Elf Header which lies at the start of file.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;ELF Header&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Always lie at the start of&amp;nbsp; the executable file. ELF header has an overall information about the entire elf file. It describes the target architecture (Intel 80386 in this case), version of elf, location and number of&amp;nbsp; program and section headers. It also contains the location of the first executable instruction (called entry point).&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets print the contents of ELF header for our “a.out” elf executable. You can use the tool “readelf” to dissect the elf executable. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;ELF HEADER&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;-----------&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;#define EI_NIDENT 16&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;typedef struct {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;unsigned char &amp;nbsp;e_ident[EI_NIDENT];&amp;nbsp; // elf magic&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half &amp;nbsp;e_type;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half&amp;nbsp; e_machine;&amp;nbsp; &amp;nbsp;// target machine architecture&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp; e_version;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Addr&amp;nbsp; e_entry;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;// entry point address&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Off&amp;nbsp;&amp;nbsp; e_phoff;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;// program hdr table’s file offset&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Off&amp;nbsp;&amp;nbsp; e_shoff;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;// section hgr table’s file offset &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp; e_flags;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half&amp;nbsp; e_ehsize; &amp;nbsp; &amp;nbsp;// elf header size in bytes&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half&amp;nbsp; e_phentsize; // size of one entry in program&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// header table in bytes. All&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Entries are of equal size&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half&amp;nbsp; e_phnum;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;// number of entries in programm header table&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half&amp;nbsp; e_shentsize; // size of section header in bytes&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half&amp;nbsp; e_shnum; &amp;nbsp; &amp;nbsp; // number of section headers in section header table&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Half &amp;nbsp;e_shstrndx; &amp;nbsp;// index of .shstrtab section in section header table.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;} Elf32_Ehdr;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;# readelf&amp;nbsp; -h a.out&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;ELF Header:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Magic:&amp;nbsp;&amp;nbsp; 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Class:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ELF32&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Data:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&#39;s complement, little endian&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Version:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 (current)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; OS/ABI:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UNIX - System V&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; ABI Version:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Type:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EXEC (Executable file)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Machine:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Intel 80386&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Version:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x1&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Entry point address:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x80482b0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Start of program headers:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 52 (bytes into file)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Start of section headers:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1980 (bytes into file&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Flags:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x0&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Size of this header:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 52 (bytes)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Size of program headers:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 32 (bytes)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Number of program headers:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Size of section headers:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 40 (bytes)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;Number of section headers:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;28&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;Section header string table index: 25&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;The first four bytes hold a magic number identifying the file as ELF executable.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The second (0x45), third (0x4c) and fourth (0x46) characters are in fact the ASCII values for &lt;b&gt;‘E’, ‘L’, ‘F’&lt;/b&gt;. The “file” command reads this magic number to determine if this is an ELF file or not. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Note the entry point address. This is the address of first instruction where the control is transferred after loading the executable in memory.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Elf Header &amp;nbsp;also contains the offset at which the program header table and section header table are placed in the a.out file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;ELF Section Headers&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The elf executable contains various sections and each section has a corresponding section header that contains the section name, the virtual address at which this should be loaded, the type of section, offset from the beginning file at which the first byte of the section resides, the size of section etc.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Few important sections are:&lt;/span&gt;&lt;/div&gt;&lt;ul style=&quot;margin-top: 0in;&quot; type=&quot;disc&quot;&gt;&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;.&lt;/b&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;text&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt; : This section hold the executable instructions of the program. &lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;.bss&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt; : This holds the uninitialized global data. In our example code, the variable &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;global2&lt;/b&gt; will go to the .bss section. All data in this section is initialized with 0, when program is loaded into memory. This section occupies no space in elf file. We only have a header for .bss section in the elf file. There is no need to allocate any space in the a.out (elf file) as we know that the initial value of the variables inside .bss is 0. &lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;.data &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;: Global initialized data goes here. &lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;.strtab &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;: It holds the names of various symbols.&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt; &lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;.symtab &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;: It holds a symbol entry for each symbol.&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt; &lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;.shstrtab : &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;This section holds sections names.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;There are various other sections as well. But we will concentrate only on the above sections.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets print the section header for above sections. Again, readelf can be used to print the section headers.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;ELF SECTION HEADER&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;------------------&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;typedef struct {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; sh_name;&amp;nbsp;&amp;nbsp; // offset into .shstrtab section&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; sh_type;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word &amp;nbsp;&amp;nbsp;sh_flags;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Addr &amp;nbsp;&amp;nbsp;sh_addr;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Off&amp;nbsp;&amp;nbsp;&amp;nbsp; sh_offset;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; sh_size;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; sh_link;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word &amp;nbsp;&amp;nbsp;sh_info;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word &amp;nbsp;&amp;nbsp;sh_addralign;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word &amp;nbsp;&amp;nbsp;sh_entsize;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;} Elf32_Shdr;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;# readelf –S a.out&amp;nbsp; &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;(only important fields are shown below)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Section Headers:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; [Nr] Name&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Addr &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Off&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Size&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;Flg&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; [12] .text&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PROGBITS 080482b0&amp;nbsp;&amp;nbsp; 0002b0&amp;nbsp; 0001d8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AX&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; [22] .data&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PROGBITS 080495c4 &amp;nbsp;&amp;nbsp;0005c4&amp;nbsp; 000008&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WA&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; [23] .bss&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NOBITS&amp;nbsp;&amp;nbsp; 080495cc&amp;nbsp; &amp;nbsp;0005cc&amp;nbsp; 00000c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WA&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; [25] .shstrtab STRTAB&amp;nbsp;&amp;nbsp; 00000000 &amp;nbsp;&amp;nbsp;0006e0 &amp;nbsp;0000db&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; [26] .symtab&amp;nbsp;&amp;nbsp; SYMTAB&amp;nbsp;&amp;nbsp; 00000000&amp;nbsp; &amp;nbsp;000c1c &amp;nbsp;000460&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; [27] .strtab&amp;nbsp;&amp;nbsp; STRTAB&amp;nbsp;&amp;nbsp; 00000000 &amp;nbsp;&amp;nbsp;00107c &amp;nbsp;00026a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;The sections flags have following meanings:&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;margin-top: 0in;&quot; type=&quot;disc&quot;&gt;&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;A (ALLOC)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt; The space should be allocated in memory to load this section. See that symbol and string table are not loaded in memory &lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;X (EXEC INSTRUCTIONS)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt; The section contians executable machine instructions. See that .text section has this flag set. &lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;MsoNormal&quot; style=&quot;mso-list: l0 level1 lfo1; tab-stops: list .5in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;W (WRITE)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt; The section has data that can be modified during program execution. &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Note the &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;section type&lt;/b&gt; (NOBITS) of .bss section. NOBITS indicates that section does not occupy any space in th executable file. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Also, note that the &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;virtual address&lt;/b&gt; of sections .symtab, .strtab is 0, which means that they are not loaded in memory. They are only used during debugging of the program.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;offset&lt;/b&gt; specifies where the actual bytes for that section reside in the elf file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;For eg. offset for .text section is 0x2b0, which means that the machine instructions for this program lie at an offset of 0x2b0 from the start of a.out file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;offset for .text section is 0x2b0, which means that the machine instructions for this program lie at an offset of 0x2b0 from the start of a.out file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;name&lt;/b&gt; is not the actual name of the section. We cannot store the name of section in section header as &amp;nbsp;want all section headers to be of equal size. Its easier to parse the sections if all of them are of equal size. So, instead of keeping the name an offset is stored. The offset is actually an index into the &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;“.shstrtab”&lt;/b&gt; section, giving the location of null terminated string.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;You can also print the &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;symbol table&lt;/b&gt; of elf file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;SYMBOL TABLE ENTRY&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;-------------------&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;typedef struct {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;Elf32_Word&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; st_name; &amp;nbsp;&amp;nbsp;// offset into .strtab section&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;Elf32_Addr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; st_value;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;Elf32_Word&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; st_size;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;unsigned char&amp;nbsp; st_info;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;unsigned char &amp;nbsp;st_other;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp;Elf32_Half &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;st_shndx;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;} Elf32_Sym;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;# readelf&amp;nbsp; -s a.out &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Symbol table &#39;.symtab&#39; contains 70 entries:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Num:&amp;nbsp;&amp;nbsp;&amp;nbsp; Value&amp;nbsp; Size Type&amp;nbsp;&amp;nbsp;&amp;nbsp; Bind&amp;nbsp;&amp;nbsp; &lt;place w:st=&quot;on&quot;&gt;Vis&lt;/place&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ndx Name&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; …&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; …&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;62: 080495d4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4 OBJECT&amp;nbsp; GLOBAL DEFAULT&amp;nbsp;&amp;nbsp; 23 global2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;63: 080495c8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4 OBJECT&amp;nbsp; GLOBAL DEFAULT&amp;nbsp;&amp;nbsp; 22 global1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; …&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 68: 08048384&amp;nbsp;&amp;nbsp;&amp;nbsp; 82 FUNC&amp;nbsp;&amp;nbsp;&amp;nbsp; GLOBAL DEFAULT&amp;nbsp;&amp;nbsp; 12 main&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 69: 08048250&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0 FUNC&amp;nbsp;&amp;nbsp;&amp;nbsp; GLOBAL DEFAULT&amp;nbsp;&amp;nbsp; 10 _init&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; …&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;Symbol table has a &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;symbol table entry&lt;/b&gt; for each symbol. &amp;nbsp;Each entry is of fixed size. As each entry is of fixed size, we cannot keep “symbol” name in the entry. Here also an offset is stored. The offset is an index into the &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;“.strtab”&lt;/b&gt; section, giving the location of null terminated symbol name.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Note the virtual address (0x080495c8) of symbol “global1”. It is an initialized global symbol, so it must go to .data section. The start address of section .data = 0x080495c4 and its size is 0x8 bytes.&lt;/span&gt; &lt;span style=&quot;font-family: Arial;&quot;&gt;Hence, we can see that “global1” resides in .data section.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;# objdump –S a.out&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;..&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;..&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;08048384 &lt;main&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;/main&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;8048384:&amp;nbsp;&amp;nbsp; 8d 4c 24 04&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lea&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x4(%esp),%ecx&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;8048388:&amp;nbsp;&amp;nbsp; 83 e4 f0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp;&amp;nbsp; $0xfffffff0,%esp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;804838b:&amp;nbsp;&amp;nbsp; ff 71 fc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pushl&amp;nbsp; 0xfffffffc(%ecx)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;804838e:&amp;nbsp;&amp;nbsp; 55&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; push&amp;nbsp;&amp;nbsp; %ebp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;804838f:&amp;nbsp;&amp;nbsp; 89 e5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mov&amp;nbsp;&amp;nbsp;&amp;nbsp; %esp,%ebp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;8048391:&amp;nbsp;&amp;nbsp; 51&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; push&amp;nbsp;&amp;nbsp; %ecx&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;8048392:&amp;nbsp;&amp;nbsp; 83 ec 14&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sub&amp;nbsp;&amp;nbsp;&amp;nbsp; $0x14,%esp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;8048395:&amp;nbsp;&amp;nbsp; c7 05 d4 95 04 08 c8&amp;nbsp;&amp;nbsp;&amp;nbsp; movl&amp;nbsp;&amp;nbsp; $0xc8,0x80495d4&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;804839c:&amp;nbsp;&amp;nbsp; 00 00 00 &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;804839f:&amp;nbsp;&amp;nbsp; c7 05 c8 95 04 08 2c&amp;nbsp;&amp;nbsp;&amp;nbsp; movl&amp;nbsp;&amp;nbsp; $0x12c,0x80495c8&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;..&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;..&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;Note: we are storing 300 (0x12c) at address 0x80495c8 which is the address of variable &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;global1.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Simlilarly, 200 (0xc8) is stored at 0x80495d4 which is the address of variable &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;global2&lt;/b&gt;. Also, see that global2 is uninitialized global variable so it must reside in &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;.bss&lt;/b&gt; section. The start virtual address of .bss = 0x&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;080495cc&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;and its size is 0xc bytes. So, we can clearly see that global2 resides in .bss section.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Program Header Table&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Program Header Table are used meaningful only for executable files and shared object files. Or you can say, that any object file that needs to be loaded into memory for execution needs a program header table.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Each entry in the program header table describes a segment in the process address space. It has the information needed to create an executable process image in memory. The operating system copies the loadable segment (PT_LOAD) into the memory according to the location and size information. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;So, various sections having common attributes/types are combined together to form a single segment.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The sections like &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;.text&lt;/b&gt;, &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;.init&lt;/b&gt;, &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;.fini&lt;/b&gt;, &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;.plt &lt;/b&gt;etc all have machine executable code and have same attributes. So, they all can be combined together to form a single entry in program header table or single segment.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Similarly, sections like .&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;bss, .data, .got&lt;/b&gt; etc all have data corresponding to various variables that can be modified during program execution. So, all these sections are combined together to form a single segment.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets print the program header table for our a.out file&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;PROGRAM HEADER&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;--------------&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;typedef struct {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; p_type;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Off&amp;nbsp;&amp;nbsp;&amp;nbsp; p_offset;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Addr&amp;nbsp;&amp;nbsp; p_vaddr;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Addr&amp;nbsp;&amp;nbsp; p_paddr;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; p_filesz;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; p_memsz;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; p_flags;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Elf32_Word&amp;nbsp;&amp;nbsp; p_align;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;} Elf32_Phdr;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;# readelf -l .a.out&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Elf file type is EXEC (Executable file)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Entry point 0x80482b0&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;There are 7 program headers, starting at offset 52&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Program Headers:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Type&amp;nbsp;&amp;nbsp; Offset&amp;nbsp; VirtAddr&amp;nbsp; PhysAddr&amp;nbsp; FileSiz MemSiz Flg Align&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;…&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;…&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;LOAD&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x000000 0x08048000 0x08048000 0x004cc 0x004cc R E 0x1000&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;LOAD&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x0004cc 0x080494cc 0x080494cc 0x00100 0x0010c RW&amp;nbsp; 0x1000&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;…&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;…&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW&amp;nbsp; 0x4&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Section to Segment mapping:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp; Segment Sections...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; 00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; 01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .interp &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: yellow;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;02&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .interp .note.ABI-tag .gnu.hash .dynsym .dynstr &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-origin: initial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: yellow;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.gnu.version .gnu.version_r .rel.dyn .rel.plt .init &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span style=&quot;background-clip: initial; background-origin: initial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: yellow;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.plt.text .fini .rodata .eh_frame&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: yellow;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;background-clip: initial; background-color: yellow; background-origin: initial;&quot;&gt;03&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ctors .dtors .jcr .dynamic .got .got.plt .data .bss&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; …&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; 06&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;If &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;type&lt;/b&gt; of segment is &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;PT_LOAD&lt;/b&gt;, it indicates that the Operating System should load this segment in memory.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;The type &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;GNU_STACK&lt;/b&gt; indicates that program needs a&amp;nbsp;stack segment. Its virtual address and size is 0. It is upto the operating system to decide on size and where to create the stack segment.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The command output also displays the section to segment mapping to tell which all sections are combined together to form a particular segment.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;That’s all for today.&amp;nbsp;Hope you find it useful. In case you have any suggestions or you find any errors please provide your comments below.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Till then, Have Fun !!!&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/2001261728966398821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/executable-file-formats-elf.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/2001261728966398821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/2001261728966398821'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/executable-file-formats-elf.html' title='Executable File Formats (ELF)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDCqEdcQPpTGum0bPUtjZil5T9T5Xs8WWsHPacJdBSRV74okr5eRzmvl_EA-C1kHccP2dCVHndfHsDqv9NGV_r32_qdEaJli-8UABJlb499Eeq-trNI9qfOH9ynLyr62o4QNyLDEzfKpY/s72-c/elf.JPG" height="72" width="72"/><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-1172843757410408633</id><published>2010-04-10T14:47:00.020+05:30</published><updated>2015-02-14T20:26:33.295+05:30</updated><title type='text'>Process Address Space</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&amp;nbsp;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;What is Process Address Space ?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;In simple words, address space is a range of addresses that &lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;a process can access. If a process tries to access any &lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;address which does not lie in its address space, the operating &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;system kills the process. The process dies with the following &lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;prints &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;“Segmentation Fault: core dumped”&lt;/b&gt;. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets take a simple example&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;test.c&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;int globalx = 100;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;int globaly;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;int main (void)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; If (globalx == 100) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; globaly = 200;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; return 0;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;When you compile this program, you get an executable. The executable files have various formats like elf, coff etc. On a linux system the executable generated has elf file forma&lt;u&gt;t&lt;/u&gt;. There are various tools available that can be used to read the elf executable file. One of such tools is &lt;b&gt;readelf&lt;/b&gt;. This can display all the sections generated and the virtual addresses assigned to them.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;# gcc test.c &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Executable a.out is created. Use &lt;b&gt;readelf&lt;/b&gt; to read the various sections of a.out&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;# readelf&amp;nbsp; -e a.out&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;a.out has lots of other sections, but lets concentrate only on the following important sections and fields&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Section Headers:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Addr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Size&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;[ 0 ] .text&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0x08048280&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x1b8&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;[ 1 ] .data&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0x08049558&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x8&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;[ 2 ] .bss&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0x08049560&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0xc&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;All the machine instructions corresponding to your code go to .text sections. All initialized global variables go to .data section. In this case, globalx will go to .data section. All uninitialized global variables (globaly) will go to .bss sections.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;b&gt;So, what is the address space for this process ?&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;All .text/.data/.bss addresses consitute the address space of this process.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;.text address range&amp;nbsp; (0x08048280 to 0x08048437)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i style=&quot;mso-bidi-font-style: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;Note: size of .text section is 0x1b8&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;.data address range &amp;nbsp;(0x08049558 to 0x0804955F)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;.bss address range&amp;nbsp;&amp;nbsp; (0x08049560 to 0x0804956B)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;Access to any address outside the above address ranges will cause segmentation fault and your process will be killed by the Operating System.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Apart from this there are other sections (like .stack etc) as well but we will not discuss them at this time.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Virtual to physical address translation&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The above addresses are all virtual addresses and not the actual addresses of physical RAM. The MMU (memory management unit) translates these virtual addresses to physical addresses using page tables.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Page table for a process is created by the Operating System at the time when the process is created. OS reads the executable file and finds various sections and their addresses. The page table mapping is created on demand ie when the process accesses any virtual address, OS checks in the page table if the mapping already exists for this address. If no mapping exists, it finds a free page in memory and creates a mapping for that page.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The size of page depends on the processor architecture. Typically it is 4K on various processors. On such processors, address space is divided into pages of &amp;nbsp;4K and each entry in the page table maps 4K of virtual addresses to a physical page.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh616ktDQ8UCD2BzlAZzx1fILh4-lmvBkyYH4e3S_wZap2CSZb1iU7LcYCcQSPYr0x7p2urL7ICUOkEEgat8TNZvUYFkHqmAQGDbHekcEZTleeFYMAXDM7OTAigEr9HBdd_-w0XSKl7RE4/s1600/page+table.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;537&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh616ktDQ8UCD2BzlAZzx1fILh4-lmvBkyYH4e3S_wZap2CSZb1iU7LcYCcQSPYr0x7p2urL7ICUOkEEgat8TNZvUYFkHqmAQGDbHekcEZTleeFYMAXDM7OTAigEr9HBdd_-w0XSKl7RE4/s640/page+table.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;An entry (page table entry or PTE) in the page table typically has the following fields. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Page Frame Address &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |User /Kernel| Read/Write | Present |&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;Page Frame Address&lt;/b&gt; is the physical address of the page in memory.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;User bit (User = 0, Kernel = 1)&lt;/b&gt; specifies whether the page can be accessed in user mode or not. If this is 1, it cannot be accessed by a user process. Only kernel can access such a page. If any user tries to access such a page, the processor will generate an exception. OS will find that the user is trying to access a kernel page and so it will kill the process.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Read/Write (Read = 0, Write = 1&lt;/b&gt;) indicates if the process can read or write on this page. The text section pages are all mapped Read only.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Present bit&lt;/b&gt; indicates if the mapping is valid or not. If this bit is not set, it indicates there is no mapping for this virtual address and OS should create it.&lt;/li&gt;
&lt;/ul&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Process Segments&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Apart from page tables, OS also keeps the information about each process sections. It creates a segment for each section and keeps the information like start virtual address of this section, protections for entire section (like read only for text sectioon), size of the section in the segment. This information is used to determine if process is accessing any address outside its address space.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;So, for our example, OS will create three segments for .text, .bss and .data section. There’s one more for stack as well.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhikgx-I1n0kbdJyerTx-BbUaNxFxkebqWmbXVaVTrniPr9J-v-jLbQoSgwi0Kfbys4nbEdpNB_IMnAglxtAmjUqpUXQt01mSKcxuYz3SzN5-nSGWHrM0Q6pbjvegvVYWTkcH3z0y3tQFo/s1600/process+segments.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;579&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhikgx-I1n0kbdJyerTx-BbUaNxFxkebqWmbXVaVTrniPr9J-v-jLbQoSgwi0Kfbys4nbEdpNB_IMnAglxtAmjUqpUXQt01mSKcxuYz3SzN5-nSGWHrM0Q6pbjvegvVYWTkcH3z0y3tQFo/s640/process+segments.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Accessing a valid address for the first time&lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0131482092&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Kernel creates the mapping on demand. When the virtual address is first accessed, the MMU (harware) first finds out its mapping in the page table. As this is the first time this address is accessed, MMU will not find any mapping in the page table. So, it will generate an exception and the control will be given to the exception handler code of kernel. The handler first checks if the virtual address is a valid address or not. It checks all the segments of that process one by one and validates that the address should lie in one of the segments. If the address lie in one of the segment , it will find a free page in memory and create a mapping for that page in the page table. It will also set various bits in the PTE. For eg. Present = 1, User = 0, Read = 1 or 0&amp;nbsp; (depending on type of section). &amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Accessing an address outside the address space&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Suppose due to some bug, your process try to access some random address, the MMU first tries to find the physical address in the page table. In this case, it will not find any translation for this address. So, a page fault will be generated by the processor. The control will be transferred to the kernel. The page fault handler inspects all the segments of this process to determine if this is a valid address. In this case, the address will not lie in any of the segments. So, the kernel will not create any mapping for this and kill the process. You will see the prints “Segmentation Fault: core dumped”.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Writing to a read only section&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;If the process try to write to a read only page, there are two cases.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Case 1: &amp;nbsp;The page table entry is already created earlier when the address was accessed for read. The PTE has Read bit = 0. ie page is read only. In this case, the MMU will find the PTE in the page table. As the process is trying to write on this page, MMU will generate an access fault exception. The exception handler will then kill the process.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Case 2: The page table doesn&#39;t have an entry for this address. Page fault is generated and the exception handler will find out if the address is a valid address. As the address is valid, it will find the corresponding section in which the address lie. Next&amp;nbsp;it will check the permissions on the section. Here the section is Read Only, so the handler will kill the process.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;How to prevent any process from accessing/corrupting the pages of another page&lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=B000M85BS6&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;One process should not be allowed to access the data of another page for security reasons. How this is done?&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Note that the access to the physical page is controlled by the page table of that process. The page tables are created by the kernel. Kernel will never create a mapping so as to allow one process to access another process pages. For eg. the data pages for a process A will be mapped only by its page table. No other page table will have a mapping for the data pages of process A.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;How to prevent any process from accessing kernel pages&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;In Linux, user and kernel both share the same address space. On a 32 bit system out of 4GB, first 3GB is reserved for user and last 1 GB is used by kernel.All kernel pages will have User Bit = 1 in their PTEs (ie only kernel can access these pages).&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;If the user tries to access any kernel page (any address greater that or equal to 3GB), MMU during converting the virtual to physical address will find that the User Bit = 1 in the PTE. So, it will generate an exception and the process will be killed by the kernel.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;That’s all for today.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Have Fun !!!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/1172843757410408633/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/process-address-space.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/1172843757410408633'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/1172843757410408633'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/process-address-space.html' title='Process Address Space'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh616ktDQ8UCD2BzlAZzx1fILh4-lmvBkyYH4e3S_wZap2CSZb1iU7LcYCcQSPYr0x7p2urL7ICUOkEEgat8TNZvUYFkHqmAQGDbHekcEZTleeFYMAXDM7OTAigEr9HBdd_-w0XSKl7RE4/s72-c/page+table.JPG" height="72" width="72"/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-8147097830993495670</id><published>2010-04-09T10:21:00.024+05:30</published><updated>2015-02-14T20:22:01.079+05:30</updated><title type='text'>VMWare Player 3.0 (A boon to Linux Hackers/Newbies)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;In my previous &lt;a href=&quot;http://havefunwhileulearn.blogspot.com/2010/04/hacking-linux-using-virtual-machine_02.html&quot;&gt;post&lt;/a&gt;, I discussed about VMWare Workstation, on how it can be used to create virtual machines and run various operating systems on them. However, it doesn’t come for free. It has lots of advanced features (like taking a snapshot of the virtual machine, cloning a virtual machine etc), but they may not be so important for newbies/students.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;VMWare Player 3.0&lt;/b&gt; is a nice alternative to Workstation for the linux beginners/learners/students. &lt;b&gt;It comes for free&lt;/b&gt; and is very user friendly.You can create new virtual machines in few simple steps and connect an ISO image to the virtual CD drive to install your operating system. Not only that, you can also connect the physical drive of your Desktop PC to the virtual machine; And then the physical drive can be used to load OS from a boot CD.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;You can also edit the settings of your virtual machine (like adding/removing a new virtual hard disk, increasing/decreasing the size of memory available to the virtual machine etc etc).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The user interface is quite similar to that of Workstation. See my previous&amp;nbsp;&lt;a href=&quot;http://havefunwhileulearn.blogspot.com/2010/04/hacking-linux-using-virtual-machine_02.html&quot;&gt;post&lt;/a&gt;&amp;nbsp;on how to create a linux virtual machine. The Player itself comes up with a very nicely written User guide.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;So, what are you waiting for ? Go get it and&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Have Fun !!!&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Enjoy this video while you download your player&lt;/strong&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;object height=&quot;385&quot; width=&quot;480&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/kKT_09pARN4&amp;hl=en_US&amp;fs=1&amp;rel=0&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/kKT_09pARN4&amp;hl=en_US&amp;fs=1&amp;rel=0&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;480&quot; height=&quot;385&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/8147097830993495670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/vmware-player-30-boon-to-linux.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/8147097830993495670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/8147097830993495670'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/vmware-player-30-boon-to-linux.html' title='VMWare Player 3.0 (A boon to Linux Hackers/Newbies)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-6580899874754610194</id><published>2010-04-02T09:29:00.113+05:30</published><updated>2015-02-14T20:22:01.069+05:30</updated><title type='text'>Hacking Linux using Virtual Machine (Vmware Workstation)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Best way to learn linux is to modify it and see how it bahaves. The biggest problem the newbies/students find is the unavailability of a Test Machine to do their experiments.&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;But, you don’t need a separate physical machine to load/test your linux OS. If you have a desktop PC, you can create as many virtual machines as you want&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;out of it and run as many Operating Systems (Linux/BSD/Solaris etc) on it. The solution is &lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;VMWare Workstation&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-weight: normal;&quot;&gt;In this post we will see how to create a Virtual Machine using VMware Workstation and load Linux on it.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;You can get the trial verison of VMWare Workstation from VMWare Website.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;Step 1&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Start VMWare Workstation. You will get the following screen.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwJnV1g6sMp5lwVrl8IhXkDlpXrBjvZ2KoF-dIdC24Op_QJgd7_6JllSpkVTioVqQa_ZnCIN6OmUne8bndNJLLm_5g_BhQUTzQPnpI4t5eRK4bRkL9lVwBTuZSsZD9NlCcK5ygGu4EDlc/s1600/step-0.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwJnV1g6sMp5lwVrl8IhXkDlpXrBjvZ2KoF-dIdC24Op_QJgd7_6JllSpkVTioVqQa_ZnCIN6OmUne8bndNJLLm_5g_BhQUTzQPnpI4t5eRK4bRkL9lVwBTuZSsZD9NlCcK5ygGu4EDlc/s1600/step-0.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;b&gt;Step 2&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;On the left panel, Right Click and Select New &amp;gt; Virtual Machine&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;Or Chose File &amp;gt; New &amp;gt; Virtual Machine&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVDjcAPLVziYNiwzzmMUStVTJvYxvX845wZkWGs-TTCwvFh8tQ8hQkBikaEnxOJQHbgiaxVvIfxJtOryCyu3c4XqOH4MM9bIwQqXTmQMMdedxsvNVMwboIbLM4RQIvYuoX66GWK76I4RU/s1600/step-1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVDjcAPLVziYNiwzzmMUStVTJvYxvX845wZkWGs-TTCwvFh8tQ8hQkBikaEnxOJQHbgiaxVvIfxJtOryCyu3c4XqOH4MM9bIwQqXTmQMMdedxsvNVMwboIbLM4RQIvYuoX66GWK76I4RU/s1600/step-1.jpg&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVDjcAPLVziYNiwzzmMUStVTJvYxvX845wZkWGs-TTCwvFh8tQ8hQkBikaEnxOJQHbgiaxVvIfxJtOryCyu3c4XqOH4MM9bIwQqXTmQMMdedxsvNVMwboIbLM4RQIvYuoX66GWK76I4RU/s1600/step-1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;/a&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Step 3&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;Chose the type of Configuration. Select Typical&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgF_aFJ7abNigv8aoSCTaO0PEVwzIoseLFwgxAk1bAsOPFxPvBS2LKt1au_QhSEN4NgLjiB7ThtLsBUwfeCF-jBDO4hZ5QHJqapdYwOvYSBsQLi-Zve3iosu_KNuQ6rlZlveZrmoKutU0I/s1600/step-2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgF_aFJ7abNigv8aoSCTaO0PEVwzIoseLFwgxAk1bAsOPFxPvBS2LKt1au_QhSEN4NgLjiB7ThtLsBUwfeCF-jBDO4hZ5QHJqapdYwOvYSBsQLi-Zve3iosu_KNuQ6rlZlveZrmoKutU0I/s1600/step-2.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 4&lt;/span&gt;&lt;/strong&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Guest Operating System Installation. Chose “I will install Operating System Later”.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQa2dB_8rWAagVl3JsoDm0zM_UNYWuQkH6tDFlHWdiJt5BKHGVAV2XNoQkvHDoNrvPMwQHevx3jwTrh_ljzOEw-HOl_YBxVuotDDU1Tsd2NOuKSyxJPcWN-wf_OwsgWwZNDxSxUkTO0yg/s1600/step4.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQa2dB_8rWAagVl3JsoDm0zM_UNYWuQkH6tDFlHWdiJt5BKHGVAV2XNoQkvHDoNrvPMwQHevx3jwTrh_ljzOEw-HOl_YBxVuotDDU1Tsd2NOuKSyxJPcWN-wf_OwsgWwZNDxSxUkTO0yg/s1600/step4.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 5&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Select a Guest Operating System. You may chose whatevere OS you would like to install. I chose Linux&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpjUPWCKKJZpoNFzaDY0qt7YW1pBdT_k0q1ygwuXYvhyy05bYBSYC_pHbQgbDFbsT5nRZH2FOjeem-JRmrHLp-RvaMFch6xGYy_H58by1DeOJxVQNjEyEpfkPlzX_gKiNNNb8XClCMHpk/s1600/step5.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpjUPWCKKJZpoNFzaDY0qt7YW1pBdT_k0q1ygwuXYvhyy05bYBSYC_pHbQgbDFbsT5nRZH2FOjeem-JRmrHLp-RvaMFch6xGYy_H58by1DeOJxVQNjEyEpfkPlzX_gKiNNNb8XClCMHpk/s1600/step5.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 6&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Chose the Version of OS. For eg. I want to install Red Hat Enterperise 5, So from the dropdown menu I select that.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLgGTH6TnPS63QaYEdxsgqgi48e1TF4lDZ1ApTiErBmSM4avJc7oDpaoDxSXElb-N0HYtTIwYNSw-tJtRrypVZSBx1ogq9mNLRC49zYCJNbeLLAF_qe-ROpQxLQr6PHGTuA7J-xkjCyC0/s1600/step6.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLgGTH6TnPS63QaYEdxsgqgi48e1TF4lDZ1ApTiErBmSM4avJc7oDpaoDxSXElb-N0HYtTIwYNSw-tJtRrypVZSBx1ogq9mNLRC49zYCJNbeLLAF_qe-ROpQxLQr6PHGTuA7J-xkjCyC0/s1600/step6.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 7&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Name the Virtual Machine. Here you chose a name for your virtual machine. By default, workstation will assign some meaningful name to your virtual machine. In this case, it assigns “Red Hat Enterprise Linux 5”.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;You also need to chose a location on your hard disk where you want to store the files related to your virtual machine being created. You may go ahead with the deafult location that Vmware Workstation choses for you by default&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7FSAbVNuLCpTzMhoo64gDX79S14t8SrgosS4fzp5y2QpRgOjO3muAYPG3AJVUAd3St7Lb3k0DcpZpiU8qUQ98ewZsJzs2WlM5pFoERl569gyCkKjOIM5KZ_A4MoGuQMjcaxDB67nTI1U/s1600/step7.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7FSAbVNuLCpTzMhoo64gDX79S14t8SrgosS4fzp5y2QpRgOjO3muAYPG3AJVUAd3St7Lb3k0DcpZpiU8qUQ98ewZsJzs2WlM5pFoERl569gyCkKjOIM5KZ_A4MoGuQMjcaxDB67nTI1U/s1600/step7.JPG&quot; /&gt;&lt;/a&gt; &lt;br /&gt;
&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 8&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Chose the number of processors for your virtual machine&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtwesIgkxVQ7dVDPrhUr55IRyUmckGcbp7X-19i1n1Bc4QaokQjGsI_rMJv1fjBDRQe3go0sl0e7hJ9ZlIZET7oHHe5xiyroWbJLwEOq131c5DEwhbs6aKOxHap0WUgGUzGHumgDHSTn4/s1600/step8.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtwesIgkxVQ7dVDPrhUr55IRyUmckGcbp7X-19i1n1Bc4QaokQjGsI_rMJv1fjBDRQe3go0sl0e7hJ9ZlIZET7oHHe5xiyroWbJLwEOq131c5DEwhbs6aKOxHap0WUgGUzGHumgDHSTn4/s1600/step8.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 9&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Memory for your virtual machine. Chose how much memory (RAM) you would like to give to your virtual machine, Minimum recommended is 512 MB. &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9Tz_i8Wpve9KwK_FJ1KKj6s2ZxqzeD1nN8A0NZt1JRMg850n7w2_nykwcodYR6Yp2lvFqZWhDxYa-6VrsCylWRwXwOipmJEuG98efn54XyijvHjl-2KzYs9YFnt1FjjfWLqLhgIZkyDQ/s1600/step9.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9Tz_i8Wpve9KwK_FJ1KKj6s2ZxqzeD1nN8A0NZt1JRMg850n7w2_nykwcodYR6Yp2lvFqZWhDxYa-6VrsCylWRwXwOipmJEuG98efn54XyijvHjl-2KzYs9YFnt1FjjfWLqLhgIZkyDQ/s1600/step9.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 10&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Chose the network type. Select Bridged Netwroking&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj2cRU_Tig6SKFEu_mrKpNTA8tVatKI90Vm04HLJaZ9fYlNn-nd9PSJKzJ59VAL59q7mpnqMm5Ln6bw2DL3vyBODS0xkbEtGdRgr_wvUpxRWz4mCyTpPaFbdMDFqqE5vBBNrFnVxPhj5M/s1600/step10.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj2cRU_Tig6SKFEu_mrKpNTA8tVatKI90Vm04HLJaZ9fYlNn-nd9PSJKzJ59VAL59q7mpnqMm5Ln6bw2DL3vyBODS0xkbEtGdRgr_wvUpxRWz4mCyTpPaFbdMDFqqE5vBBNrFnVxPhj5M/s1600/step10.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 11&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Select I/O Adapte types. Chose SCSI Adpater &amp;gt; LSI Logic&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyR-0SnoVhsAlL9tqA3mKxmTaSlpSpNuLQ9lRkDYT1uX29KM9y30zvi9pcu5ssQyTX6m_bCBiEQ42ZdMzxhiYUPSyyjc9cGgiUtHTNxcWXLNmCGNOx2oSRUWHqeq7CmQ__HgASjrC2I9c/s1600/step11.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyR-0SnoVhsAlL9tqA3mKxmTaSlpSpNuLQ9lRkDYT1uX29KM9y30zvi9pcu5ssQyTX6m_bCBiEQ42ZdMzxhiYUPSyyjc9cGgiUtHTNxcWXLNmCGNOx2oSRUWHqeq7CmQ__HgASjrC2I9c/s1600/step11.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 12&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Add a hard disk to your Virtual Machine. Chose “Create a new virtual disk”.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDFgMrE1deJ2EHGT4y-uhtGCjWtEcbAQyKATr8nVbk0KrSPy2I3Yq2Git1wK_QjdFjNl32kHuV22I63Z8BU9asjDmYNyhes01-CQDdC6oxtToB9hoFPMt64lKKz8wKkV9PqEXM8s4YkzE/s1600/step12.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDFgMrE1deJ2EHGT4y-uhtGCjWtEcbAQyKATr8nVbk0KrSPy2I3Yq2Git1wK_QjdFjNl32kHuV22I63Z8BU9asjDmYNyhes01-CQDdC6oxtToB9hoFPMt64lKKz8wKkV9PqEXM8s4YkzE/s1600/step12.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Step 13&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Select Disk Type. Chose SCSI&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisVSDOqmjsVLIXJ5u3z5Vj-Hjjsoo8fEIbRNO3vYTeV_flOawf1U8dGuA0Fle3LJs8AAZUR93RJsPWz5-pO34CX6sbL5pYFhxvoS4yLd6MYxTvdQlXPJWJyRHUgvmiUst2SmwhSXbF2EI/s1600/step13.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisVSDOqmjsVLIXJ5u3z5Vj-Hjjsoo8fEIbRNO3vYTeV_flOawf1U8dGuA0Fle3LJs8AAZUR93RJsPWz5-pO34CX6sbL5pYFhxvoS4yLd6MYxTvdQlXPJWJyRHUgvmiUst2SmwhSXbF2EI/s1600/step13.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 14&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Specify the size of your disk. Chose 8GB and select “store virtual disk as a single file”.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLbMbB756JhWlsvRDX2hMGI6eNO3Ysy05lYWXhIKgR7g5RDLhf3_MHtqoxB2EZyUWQ8StjvfJQdtJgd267p9z18nwVgS9RhzMFc13M7jZtuT3rZ2r-JpBNc_ddOQhRqSHt9WmQu265E10/s1600/step14.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLbMbB756JhWlsvRDX2hMGI6eNO3Ysy05lYWXhIKgR7g5RDLhf3_MHtqoxB2EZyUWQ8StjvfJQdtJgd267p9z18nwVgS9RhzMFc13M7jZtuT3rZ2r-JpBNc_ddOQhRqSHt9WmQu265E10/s1600/step14.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 15&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Specify the location where you would like to keep your disk file&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmITao5AxczEsoUhV0fAQiJYxhj9qy5l9oOdc6nVZlnrfEgrcw-aKqVZN1VehFjJoQ2McZczWqTVTjigepyc0Y_5wkD9ilo3u36AdRqUtiQDjX4-tsU2EBR0fEgi-O9Qq1bfadX5cPmLw/s1600/step15.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmITao5AxczEsoUhV0fAQiJYxhj9qy5l9oOdc6nVZlnrfEgrcw-aKqVZN1VehFjJoQ2McZczWqTVTjigepyc0Y_5wkD9ilo3u36AdRqUtiQDjX4-tsU2EBR0fEgi-O9Qq1bfadX5cPmLw/s1600/step15.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 16&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: red;&quot;&gt;Congrats. Your virtual machine is ready. Click Finish to download some OS on it.&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgupt-AT9BgGjx9uZoFVosiTLWEwknK-1zbH-sh_WFCXtf_wyXY7OMQWbEJ4klOYfq0rqNx41U39BgaLaIs2imgL2wA5pOHylOnYbUkyvXJRWsEOGfMsVnNUdiq74Zr1EjuNNMuwcfBasM/s1600/step16.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgupt-AT9BgGjx9uZoFVosiTLWEwknK-1zbH-sh_WFCXtf_wyXY7OMQWbEJ4klOYfq0rqNx41U39BgaLaIs2imgL2wA5pOHylOnYbUkyvXJRWsEOGfMsVnNUdiq74Zr1EjuNNMuwcfBasM/s1600/step16.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 17&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;After you successfully create a virtual machine you will see the following screen&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;I already had one Virtual machine created (Red Hat Enterprise 3) and below you see the new virtual machine (Red Hat Enterprise Linux 5) that we created just now&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2JNOlzW1FEvNFFwBtSumT1gh0PpHwDdfCl59yFEfgR6PkJ3h2k3_DVcQRZFEvxmgS4ydZOIoYMtcTpUu7trMDFPQOy_9qb2pQ8KapY6PEFsJOJC2HtfyGGprWZPHhW4J6bOMbvloNajA/s1600/step17.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; nt=&quot;true&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2JNOlzW1FEvNFFwBtSumT1gh0PpHwDdfCl59yFEfgR6PkJ3h2k3_DVcQRZFEvxmgS4ydZOIoYMtcTpUu7trMDFPQOy_9qb2pQ8KapY6PEFsJOJC2HtfyGGprWZPHhW4J6bOMbvloNajA/s1600/step17.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 18&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Now, we need to load the OS on our virtual machine. We will use the virtual CD to load the OS on it. We can attach an ISO image of the Red Hat Enterpise CD to our virtual CD. There are software available that may be used to convert your bootable CD to an ISO image file.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;To do this, click “Edit virtual machine settings”&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitEHT2AHZdmIgq_k59kHGXyo4qWWEN3rTrSuHCwIqTLa_au2mJtpJu2xAufIB-eEbFY9ekIuvV854ixCFamu8BhexIT-ObKKITh1nm8-NC9ehNORSlrSBsiSpIElmba7Feq-VBzPaUVuo/s1600/PO-1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitEHT2AHZdmIgq_k59kHGXyo4qWWEN3rTrSuHCwIqTLa_au2mJtpJu2xAufIB-eEbFY9ekIuvV854ixCFamu8BhexIT-ObKKITh1nm8-NC9ehNORSlrSBsiSpIElmba7Feq-VBzPaUVuo/s1600/PO-1.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 19&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Chose ISO image file and specify the location of the ISO image file of your OS.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Chose &quot;Connect at power on&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFYdJ5ActkykOHx56NcN204rGk4SrpluiS2tiJrSq0cZCfF6MHPOPinj-MYG6B1CcQ-svMKbxkJW5bHn4FD7r8XNI02LawZEd_EigeBMUfdAs0SJgo1osPsoSeQJKXUUyhhIwEwXwlrRU/s1600/PO-2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFYdJ5ActkykOHx56NcN204rGk4SrpluiS2tiJrSq0cZCfF6MHPOPinj-MYG6B1CcQ-svMKbxkJW5bHn4FD7r8XNI02LawZEd_EigeBMUfdAs0SJgo1osPsoSeQJKXUUyhhIwEwXwlrRU/s1600/PO-2.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 20&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Now, lets Power On the virtual machine. Click the green button &quot;Power On this virtual machine&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOP2YxqohxXcT1P7WQm2WobMLyBFqKZS904uMuvE7DCYBsyxJrGgnfNVXxsLAXh9gZ0r__QFuwV2oyfy3bGM_Xi8UrIOSWs5JwI6NI2kGDLKYLr8oAP1ZNHbCwhQq9McqjIysnZnB7fN8/s1600/PO-3.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOP2YxqohxXcT1P7WQm2WobMLyBFqKZS904uMuvE7DCYBsyxJrGgnfNVXxsLAXh9gZ0r__QFuwV2oyfy3bGM_Xi8UrIOSWs5JwI6NI2kGDLKYLr8oAP1ZNHbCwhQq9McqjIysnZnB7fN8/s1600/PO-3.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;Step 21&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;As you have already attached the ISO image of your bootable CD to the virtual CD drive, the virtual machine starts booting from it. You will see the following screen.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8FgoTNVbR_HsxlUuAYh_icPmPMK3VbcKBszoSSSK46dzAhpzN2N9Gup6d_DysWspNIBFPa-gAG4KTf5-xxXddk5s7SOF5JM9VmB1CpaNKt6sy5PrgvL1khlDQxx6OIODaIosl1swYp-0/s1600/PO-4.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8FgoTNVbR_HsxlUuAYh_icPmPMK3VbcKBszoSSSK46dzAhpzN2N9Gup6d_DysWspNIBFPa-gAG4KTf5-xxXddk5s7SOF5JM9VmB1CpaNKt6sy5PrgvL1khlDQxx6OIODaIosl1swYp-0/s1600/PO-4.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;From here you need to follow the same steps as you do for any normal installation.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;So, you have a complete virtual machine with hard disk, network card, CD Drive, USB Controller, floppy, CPU and a dislay.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;You may create as many virtual&amp;nbsp; machines as you want and load whatever OS you want to load on it. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Isn&#39;t this amazing.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Have Fun !!!&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/6580899874754610194/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/hacking-linux-using-virtual-machine_02.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/6580899874754610194'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/6580899874754610194'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/04/hacking-linux-using-virtual-machine_02.html' title='Hacking Linux using Virtual Machine (Vmware Workstation)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwJnV1g6sMp5lwVrl8IhXkDlpXrBjvZ2KoF-dIdC24Op_QJgd7_6JllSpkVTioVqQa_ZnCIN6OmUne8bndNJLLm_5g_BhQUTzQPnpI4t5eRK4bRkL9lVwBTuZSsZD9NlCcK5ygGu4EDlc/s72-c/step-0.JPG" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-4304585921938715880</id><published>2010-03-30T19:25:00.006+05:30</published><updated>2015-02-14T22:56:09.308+05:30</updated><title type='text'>System Calls (Linux/Unix Kernel)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;System Calls&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;In simple words, it is a call from the user application to the Operating System to do some work &lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0596005903&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;for it.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Why do we need a System Call?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;A very common example is reading a document from the hard disk.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;To read the document from the scsi disk, you must know about the hardware details of disk. You need to program its controller so that you can pass scsi read command to it and tell the controller that you want to read a specific block. You must know the device specific protocol &amp;nbsp;(like scsi) so that you can talk to the disk. Overall it will be a nightmare for the user to do a simple read/write from the disk.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Solution is let the operating system (linux/unix/windows) do it for you. All operating systems have a driver code for any hardware device (disk, mouse, keyboard, memory, ethernet card etc) attached to the system. The driver code knows how to talk to the hardware devices. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;So, application/user can simply make a call to the system saying “Hey ! can you please do this for me”.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;How to make a System Call ?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Unix/Linux systems include various standard libraries that provide various APIs to the users. These APIs are defined by the POSIX standard. The advantage of using these standard APIs is that these are available on all unix systems which makes the application portable.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;These APIs in turn call the platform specific system calls. These systems calls are generally a part of standard C library libc and are typically written in Assembly language. These are highly system dependent (both OS and processor specific).&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;A user program can make a system-call directly but it is not advisable to do so as these system calls may vary from system to system. There’s no guarantee that a system call present on one system (say HP-UX) will also be present on a different system (say Solaris). But, the APIs defined by POSIX are guaranteed to be present on all POSIX compliant systems.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Typically the flow is as follows:&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;Application ----&amp;gt; POSIX library APIs (fread) ----&amp;gt; System Call (read)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;How System Call works&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;The purpose of system call is to transfer control from user application to operating system. User application cannot directly call kernel functions as the kernel has protected address space which is essential for system stability and security.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial; font-size: 12pt;&quot;&gt;A processor provides certain instructions (like “int 0x80” on i386, “sc” on PowerpC) which are used to tarnsfer the control from user to kernel. When a user program execute these instructions, an exception is taken which changes the processor mode from user to kernel. The Program counter is changed&amp;nbsp;so as to execute the exception handler at a specific location. The exception handler is nothing but the system call handler in kernel. This can then invoke appropriate driver code to interact with hardware. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;System Call Number and Arguments&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Typically there are 100s of different system calls to do different kinds of tasks. All of them &lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0596002556&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;execute the same instruction (“int 0x80” on i386,&amp;nbsp; “sc” on PowerPC). So, user must tell the kernel that which system call is being made. This is done by assigning a unique number to each system call. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Every processor specify an Appilication Binary Interface (ABI) that describes the low level interface between and application and operating system. The ABI describes how the system call number and arguments etc should be passed to OS. They also define how the OS will tell the user if the system call was executed successfully or not. This is done typicallly by passing the error number in a specific register.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Lets see how it happens on i386&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;As per the System V ABI for x86 (Linux, NetBSD follow System V ABI)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;System call number is passed into &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;eax&lt;/b&gt; register.&lt;/li&gt;
&lt;li&gt;Arguments are passed in registers &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;ebx, ecx, edx, esi and edi&lt;/b&gt; in order (the first five arguments). In case, the number of arguments are more than 5, then a single register is used to pass a pointer to user space address where&amp;nbsp;the parameters are stored.&lt;/li&gt;
&lt;li&gt;The return value is passed to the user via &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;eax&lt;/b&gt; register. If return value is between -1 and -125 (value of eax), it means system call finished with an error. The actual error code (&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;-eax&lt;/b&gt;) is then copied to “errno” variable and -1 is returned in &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;eax&lt;/b&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;read system call library function for i386&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;// read (fd, buf, numBytes)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;read:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pushl %ebx&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// save ebx on stack &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mov1&amp;nbsp; 8(%esp), %ebx&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // pass first argument in ebx&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; movl 12(%esp), %ecx&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // pass second parameter in ecx&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; movl 16(%esp), %edx&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// pass third parameter in edx&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; movl $3, %eax&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // pass system call no in eax&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&amp;nbsp; $0x80&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // invoke system call&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cmpl $-126, %eax&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // check return value&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; jbe&amp;nbsp; out&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // if no error goto out&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; negl %eax &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// negate the value of eax&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mov %eax, errno&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // save the error code in errno&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mov, $-1, %eax&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // pass –1 to eax&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;out:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop %ebx&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// return&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;System call interface on PowerPC&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;As per System V PowerPC ABI (linux also follows System V ABI),&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;ul&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;System call number is passed in register &lt;b&gt;r0&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;The arguments are passed in registers &lt;b&gt;r3 to r10 &lt;/b&gt;(total 8 argumenst in order).&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;The error code from OS to system call is passed in &lt;b&gt;r3&lt;/b&gt;. The summary overflow bit (&lt;b&gt;CR0_SO&lt;/b&gt;) in the condition register &lt;b&gt;CR0&lt;/b&gt; indicates if error occurred.&amp;nbsp;A unix system checks if there is an error, it copies error code to “errno” and return -1 to user application.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;The return value(s) from system call to user application are passed in &lt;b&gt;r3 &lt;/b&gt;and &lt;b&gt;r4&lt;/b&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;read system call library function for PowerPC&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;// read (fd, buf, numBytes)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;read:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; li&amp;nbsp;&amp;nbsp; r0, 3 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// pass system call number to r0&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// arguments have already been passed&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // in r3, r4, r5 by the caller.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; sc&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// invoke system call&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; bnslr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // if no error return (Summary &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Overflow bit not set)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lis&amp;nbsp; r4, errno@ha&amp;nbsp; // copy error code to errno variable&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; stw&amp;nbsp; r3, errno@l(r4)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; li&amp;nbsp;&amp;nbsp; r3, -1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // return -1 to user application&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; blr&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Accessing/Copying user data from kernel&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;User is not allowed to access kernel pages which is essential for system security and stablity. However, kernel can read/write data from/to user space. Linux kernel provides two functions for this:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;copy_to_user() is used to copy data from kernel address space to user address space. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;copy_from_user() is used to read the data from user buffer to kernel page.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;I will discuss more about these in some other post.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Kernel must verify all the arguments passed by user. The most important is the validity of the pointer provided by the user. Kernel must ensure that&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;address should lie with in user address space. It should not lie in kernel region else user may corrupt the kernel data.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;user has valid permissions to read/write data for that address.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;This is all for today. We will discuss more on how kernel handles the system call later.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Till then, Have Fun !!!&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/4304585921938715880/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/systems-calls.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/4304585921938715880'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/4304585921938715880'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/systems-calls.html' title='System Calls (Linux/Unix Kernel)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-6151770794842212096</id><published>2010-03-28T21:29:00.004+05:30</published><updated>2015-02-14T22:56:09.317+05:30</updated><title type='text'>Synchronization in Linux/Unix Kernel - Part 4 (Reader-Writer Lock)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Till now we saw how we can atomically read-modify-write global variables using special &lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0131019082&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;instructions provided by the processor. We also saw the use and implementation of Spin Locks and Semaphores. In this post we will discuss about a new locking mechanism called Reader-Writer Lock.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Consider a case where we have a file shared across various processes with the following constraints.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;ol&gt;&lt;li&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Multiple processes can read the file simultaneously&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Only one process can write to the file at a time&lt;/span&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;b&gt;. &lt;/b&gt;No other process should be allowed to read/write the file while one of the process is writing to the file.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;We can’t use Spin Lock here as it allows only one process to access the file. Spin Lock can’t distinguish between readers and writers. Also, read/write to file on disk is time consuming operation and spin lock is not suitable here.&lt;/span&gt; &lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Semaphores or Counting Semaphores are also not suitable here as they can’t distinguish between reader and writer.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;We need a separate type of lock here called Reader Writer Lock.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;It keeps track of number of readers/writers reading/writing to the file. It also keep track of number of readers/writers that are waiting to take the lock.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Reader Write Lock&lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0471694665&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;If there are only readers it allows all of them to take the lock.&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;If a new writer comes it blocks the writer until all readers are done. But, if new readers keep coming and we allow them to take the lock, it will cause writers to starve.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;Similarly, if a writer is active, all new readers and writers will be blocked.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;Question is whom to wake up after the writer is done ? Should it wake up another writer or all other readers ? If writers are given preference, readers could starve.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;To solve the problem of starvation&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;All new readers are blocked if there is any writer already waiting so that we don’t starve writers.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;If a reader releases the lock, it takes no action if some readers are already active. When the last reader releases the lock, it issues wakeup to the waiting writer.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;If a writer realease the lock, it issues wakeup to all the blocked readers. If there are no blocked readers, it issues wakeup to the single blocked writer.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;With this scheme we can alternate access to the resource between a writer and bunch of readers.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Implementation of reader writer lock&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;typedef struct&amp;nbsp; rwLock {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; spinLock sl;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// spin lock to synchronize access &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // to following members of lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // by multiple threads.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; int nActive;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // number of active readers.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // if -1, a writer is active&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; int nReadPending;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;// number of blocked readers&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; int nWritePending; &amp;nbsp;&amp;nbsp;&amp;nbsp;// number of blocked writers&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; queue readers;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// queue to put blocked readers&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; queue writers;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// queue to put blocked writers&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;} rwLock;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;void LockRead (rwLock *lock)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; acquire the spin lock&amp;nbsp; // acquire the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nReadPending++;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; if (nWritePending) {&amp;nbsp; &amp;nbsp;// if any writer already blocked&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add itself to readers wait queue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; release the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sleep&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; acquire the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; while (lock-&amp;gt;nActive == -1) {&amp;nbsp; // if any writer already&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //acquired the lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add itself to reader wait queue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; release the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sleep&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; acquire the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nActive++;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nReadPending--;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; release the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;void LockWrite (rwLock *lock)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; spinLock(&amp;amp;lock-&amp;gt;sl);&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;// acquire the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nWritePending++;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; while (lock-&amp;gt;nActive) {&amp;nbsp; // any reader/writer active&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add itself to writer queue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;release spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sleep&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; acquire the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nWritePending--;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nActive = -1;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; release the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;void UnlockRead (rwLock *lock)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; acquire the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nActive--;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; if (lock-&amp;gt;nActive == 0) {&amp;nbsp; // no readers/writers active&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; issue wakeup to writers&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; release spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; } else {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; release the spin lock&amp;nbsp;&amp;nbsp; // if any reader writer &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // active, don’t do anything&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;} &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;void UnlockWrite (rwLock *lock)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; Acquire the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; lock-&amp;gt;nActive = 0;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; if (nReadPending) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // if any readers blocked&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wake all readers&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; release the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; } else {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wake the single writer&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; release the spin lock&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Note that&amp;nbsp; the structure rwLock lock has several members. All these members can be accessed by various processes concurrently. So, to synchronize access to all these members a single spin lock is used. This spin lock should be taken before accessing any member of the rwLock.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Reader Writer lock is used by filesystem code while doing any read/write to a file in a filesystem. The lock is acquired at the start of read/write system call and released as soon as the system call is done. It allows multiple readers to read the file but allows only a single writer to write the file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/6151770794842212096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linuxunix-kernel_28.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/6151770794842212096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/6151770794842212096'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linuxunix-kernel_28.html' title='Synchronization in Linux/Unix Kernel - Part 4 (Reader-Writer Lock)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-8042609907473968199</id><published>2010-03-27T12:53:00.156+05:30</published><updated>2015-02-14T22:56:09.321+05:30</updated><title type='text'>Synchronization in linux/unix kernel - Part 3 (Semaphores)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0672327201&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;In my p&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;reviou&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;s post we discussed how Spin Locks can be useful when we need to update multiple global variables atomically. We saw that Spin Locks should be acquired only for a short duration of time as the blocking threads spin in a loop and not doing any thing useful. In this post we will discuss the scenarios where we need to grab a lock for long duration and the ways to solve such problems.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Single Printer and Multiple Users Problem&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Lets consider a common example where there is a single printer and multiple threads (thread1, thread2, thread3) that want to print their files (say file1, file2, file3 respectively) As there is only one printer, while one of the threads is printing its file, the others should wait. Also, we are not sure about how much time a single job will take to finish. It may take from few seconds to few hours depending on the size of the file. So, the waiting threads cannot spin in a loop as they will waste lot of cpu cycles. The better option for them is to sleep so that some other thread can run. When the printing job is done the thread at the front of queue should be awakened so that it can start printing its file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;This kind of problem is solved by using a synchronization mechanism called Semaphore.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;To implement a semaphore we need a flag to indicate if printer is in use and a queue to keep info about waiting threads. &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;struct semaphore {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int &amp;nbsp; flag; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// flag = 0, means resource BUSY, &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;// flag = 1, means resource is FREE&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; queue q; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // queue to store pids of all waiting threads&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;// pid can be later used to issue wakeup to the&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// sleeping threads&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The thread that needs to print the file will first check if the flag is equal to 0 (printer is available). If flag == 0, it will set flag to 1 (to indicate printer is in use) and start printing its file. The &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;test-and-set&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; should be done atomically else two or more threads may feel that they all have acquired the lock. &lt;/span&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;See my previous posts to find out the problems if test-and-set is not done atomically&lt;/span&gt;&lt;/i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;.&lt;/span&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If any thread finds that the flag is already set, (printer is busy), it will put itself on the queue and will sleep.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;When the printing job is done, flag is again set to 0 to indicate that the printer is free And wakeup is issued to the threads waiting for the printer to become free. No need to issue wakeup to all waiting threads. Wakeup the thread which is waiting for the longest time (ie the thread at the front of queue). The woken-up thread again sets the flag = 1 and starts printing its file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Semaphores &lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;are also called &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;SLEEPING LOCKS&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; as the waiting threads don’t spin but sleep.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Semaphore and related operations&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;typedef struct&amp;nbsp; semaphore {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int &amp;nbsp; &amp;nbsp;flag;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; queue &amp;nbsp;q;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;} semaphore;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;semaphore sem;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a semaphore for printer&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;void initSem(semaphore *sem) &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; sem-&amp;gt;flag = 0;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Mark resource as free. Printer is available&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;initialize the queue with all 0s.&amp;nbsp;&amp;nbsp;&amp;nbsp;// No thread is waiting initially&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;void acquireSem(semaphore *sem)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; while (test_and_set(sem-&amp;gt;flag))&amp;nbsp;{&amp;nbsp;&amp;nbsp; // atomically test-and-set the flag&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;add itself to the end of queue&amp;nbsp;&amp;nbsp;&amp;nbsp;// if flag is set, add itself to queue&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sleep;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;void releaseSem(semaphore *sem)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; atomic_set(sem-&amp;gt;flag, 0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Mark the resource free. Set flag to 0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;if threads are waiting on queue,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;issue wakeup to the first thread on queue&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Note: Care must be taken while adding/removing threads from the queue. As multiple threads may be &lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0131019082&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;adding/removing themselves to the queue, a spin lock should be used to prevent queue from corruption. Spin lock will ensure that only a single thread is being added/removed from the queue. In fact, a single spin lock can be used to synchronize access to both flag and queue. The spin lock should be released before going to sleep.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;See the example at the end of this post to see how spin lock can be used to implement a semaphore.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;This is not the actual implementation but gives you a basic idea on how semaphores work.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The semaphore discussed above can be used to implement a &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;mutex &lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;as it allows only a single thread to access the shared resource. These are also called &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Binary Semaphore&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Multiple Printers and Multiple Users Problem&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;We saw how a semaphore can be used to synchronize the access to a shared resource. Think of a case where there are multiple resources of same type eg multiple printers. If we have 10 printers then we should allow 10 users to print their files simultaneously. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Solution is Counting Semaphore&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;This problem is solved by using &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Counting Semaphores&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;. Here a counter is used to keep track of the threads that are using the shared resources.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;struct semaphore {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;int &amp;nbsp; count; &amp;nbsp; &amp;nbsp; &amp;nbsp;// initial value of count is equal to the number of shared &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// resources.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;queue q; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Implementing a Counting Semaphore&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;To implement a Counting Semaphore, &amp;nbsp;the initial value of the count is set to the number of available resources. So, if we have 10 printers initial value of count = 10.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;To aquire a semaphore, thread first decrements the value of count by 1. And then tests if count &amp;gt;= 0, which means some printers are available and the thread can start printing his job. So, atmost 10 threads can be printing their files simultaneously.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If the value of count after decrement becomes negative, it means no printer is free and &amp;nbsp;the incoming thread will put itself on the sleep queue. After any printing job is done, count is inceremented by 1 and wakeup is issued to the first thread on the queue.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;There are three cases&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Case 1:&amp;nbsp; &lt;b&gt;count is &amp;gt; 0&lt;/b&gt;. This mean that resource is available. For our 10 printer example, if value of count is 4, it means 4 printers are free.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Case 2:&amp;nbsp; &lt;b&gt;count = 0&lt;/b&gt;. No resource available. For our 10 printer case, 0 means no printer is available and no one is waiting on the queue.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;Case 3:&amp;nbsp; &lt;b&gt;count &amp;lt; 0&lt;/b&gt;. No resource is available. For our 10 printer case, if value of count is -3, it means all printers are busy and 3 threads are waiting in the queue.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;So, the value of count may be used to determine if any thread is waiting in the queue. If value of lock &amp;lt; 0, it means some threads are waiting in the queue.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Implementing a Counting Semaphore&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;typedef struct semaphore {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int &amp;nbsp; &amp;nbsp;count;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; queue &amp;nbsp;q&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;} semaphore;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;semaphore sem;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;void initSem(semaphore *sem) &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; sem-&amp;gt;count = 10; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;// Initialize the count to 10, to indicate&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;// 10 resources (printers) are available&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;initialize the queue with all 0s&amp;nbsp;&amp;nbsp;// No thread is waiting initially&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;void acquireSem(semaphore *sem)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;decrement sem-&amp;gt;count&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (sem-&amp;gt;count &amp;lt; 0)&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;add itself to the end of queue.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sleep; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Sleep if no resource is available&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;void releaseSem(semaphore *sem)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; increment sem-&amp;gt;count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (sem-&amp;gt;count &amp;lt;= 0)&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;remove thread from the fron of waiting queue and&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;issue wakeup to it&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Again, this is not the actual implementation. The count should be modified/checked atomically. Similarly, a thread may be added only if the resource is not available. While acquiring the semaphore, all the operations like decrementing the count, testing its value and then putting the thread on sleep queue should be done atomically. So, a single spin lock should be used to prevent access to both count and queue.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Sempahore using a spin lock to prevent access to its data&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;typedef struct semaphore {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int spinLock;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;// spin lock to prevent access to count and queue.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int &amp;nbsp; &amp;nbsp;count;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;queue &amp;nbsp;q&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Spin lock is accessed only for short time, while modifying/testing count value and while adding thread to the queue. The spin lock should be released before going to sleep.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Semaphore operations using a spin lock&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;void acquireSem(semaphore *sem)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;acquire spin lock&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;decrement sem-&amp;gt;count&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (sem-&amp;gt;count &amp;lt; 0)&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;add itself to the end of queue.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;release spin lock&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sleep;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Sleep if no resource is available&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;void releaseSem(semaphore *sem)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; Acquire spin lock&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; increment sem-&amp;gt;count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (sem-&amp;gt;count &amp;lt;= 0)&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;remove thread from the front of waiting queue&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;release spin lock&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;issue wake up to removed thread&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Note: Counting semaphores can be used to implement Mutex or Binary Semaphores by initializaing the count to 1.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Hope you find this article useful. If you have any comments/suggestions please post them below.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Have Fun !!!&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/8042609907473968199/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linuxunix-kernel.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/8042609907473968199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/8042609907473968199'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linuxunix-kernel.html' title='Synchronization in linux/unix kernel - Part 3 (Semaphores)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-4952820529885343951</id><published>2010-03-21T18:37:00.293+05:30</published><updated>2015-02-14T22:56:09.311+05:30</updated><title type='text'>Synchronization in Linux Kernel - Part 2  (Spin Locks)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0471694665&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;In continuation to my previous post, lets discuss other &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;concurrency &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;related problems and their solutions. Earlier, we saw how concurrent&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;access to a shared variable may result in loss of information or data &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;corruption. In this article, we will explore more such&amp;nbsp;problems&amp;nbsp; and &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;ways to solve them.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Consider a scenario where there is a need to &lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;update multiple shared&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;global variables atomically. &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;No other thread should be allowed to&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;acess the global variables until the thread that is updating them is done.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;For eg. &amp;nbsp;take the case of multiple threads adding/removing/reading &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;elements from a linked list concurrently. This may result in linked list &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;corruption if done without using appropriate synchronization mechanism &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;as shown in following example.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Consider a linked list &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Head -----&amp;gt; Element1 -----&amp;gt; Element2 -----&amp;gt; Element3 -----&amp;gt; Element4 -----&amp;gt; Null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;To add a new element (say NewElement) in this list after a specific element (say Element3) following operations need to be performed.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Step 1) First update NewElement to point to Element4&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Head ----&amp;gt; Element1 ----&amp;gt; Element2 -----&amp;gt; Element3 -----&amp;gt; Element4 -----&amp;gt; Null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NewElement -------+ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Step 2) Update Element3 to point to NewElement&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Head ----&amp;gt; Element1 ----&amp;gt; Element2 ----&amp;gt; Element3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Element4 ----&amp;gt; Null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;+--- NewElement --+ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Now, suppose thread-1 needs to add NewElement1 after Element3 and &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; thread-2 wants to add NewElement2 after Element3.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;thread-1 is running on cpu-1 and thread-2 is running on cpu-2 concurrently.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;1) &amp;nbsp;thread-1 executes Step-1 to make NewElement1 point to Element4. Just after that thread-2 executes and updates NewElement2 to point to Element4 as shown below.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NewElement1 -----+&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Head ----&amp;gt; Element1 ----&amp;gt; Element2 -----&amp;gt; Element3 -----&amp;gt; Element4 ----&amp;gt; Null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NewElement2 -----+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;2) thread-1 then updates Element3 to point to NewElement1.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;+--- NewElement1 ---+ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Head ----&amp;gt; Element1 ----&amp;gt; Element2 -----&amp;gt; Element3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Element4 ----&amp;gt; Null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NewElement2---+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;3) And then thread2 also updates Element3 to point to the NewElement2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NewElement1 ---+&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Head ----&amp;gt; Element1 ----&amp;gt; Element2 ------&amp;gt; Element3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Element4 ----&amp;gt; Null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;+--- NewElement2 ---+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;So, finally we have NewElement2 successfully inserted into the linked list. But, NewElement1 is not a part of the linked list, So we lost Element1.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;How to solve the problem where multiple global shared variables need to be updated atomically ? &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Answer is simple. We need to prevent all threads from accessing the linked list while one of the thread is manipulating the list.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;To achieve this, use one global variable as a lock. Any thread that needs to access the list must acquire the lock by setting its value to 1. This thread will first test if the value of lock variable is 0, then set it to1 to acquire the lock. This test-and-set should be done atomically. If any thread is already holding a lock, all others threads should&amp;nbsp;wait. The thread holding the lock will release the lock (by setting the&amp;nbsp;lock to 0) after it is done with the list.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;This locking variable is called&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Spin Lock. &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;A Spin Lock can be implemented as a single bit in an integer variable. The most important thing here is that the testing and setting of the lock bit should be done atomically. Performing read-modify-write operation atomically is one of the prime requirement of implementing a spin lock.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If test-and-set is not done atomically then multiple threads may erroneously come under the impression that they all hold the lock simultaneouly which will lead to data corruption. See the example below how this may happen.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Problem if test-and-set is not done atomically&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;u&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;thread-1&lt;/span&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;u&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;thread-2&lt;/span&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Lock Variable = 0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;1) Reads the value of lock variable (=0) &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; to its&amp;nbsp; register.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2) Reads the value of lock variable (=0)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;to its register.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;(Both of them find the value of Lock variable to be 0, So they assume that the lock is free.)&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;3) Checks the value of local register.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; Value is 0, So try to acquire the &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; lock by &amp;nbsp;writing 1 to lock variable&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; in memory. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4) thread-2 also finds that the value of&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; global variable read in its register&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;=&amp;nbsp;0, which means lock is free. So,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;writes 1 to the lock variable to&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;acquire the lock. &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;(Both of&amp;nbsp; them now feel that have acquired the lock which is wrong)&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;So, test and set should be done atomically.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Lets first see a simple implementation of test_and_set()&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;int test_and_set (int *lock)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; int temp = *lock;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // save the original value of lock&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; *x = 1;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// always set lock to 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return temp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// return the original value&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Note: this is not atomic.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;test_and_set() saves the original value of lock, sets the lock to 1 and returns the old value of &lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0131019082&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;lock. Of course, this should be done atomically&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The return value indicates whether it was already locked or not. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If return value is 0, it means that lock was free and by writing 1, the caller has acquired the lock.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If return value is 1, it means that the lock was already acquired by someone else. So, the caller needs to call this function again. The caller will keep calling this in a loop until this function returns 0.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Why it always set lock to 1 ?&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;You may be the thinking why this function always sets the value of lock to 1 ? What if the value is already 1? Why don’t we first check the value of lock? We should set it to 1 only if the initial value of lock is 0.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Well, answer is that there is nothing wrong in testing the value before setting it to 1. But, to test we need an extra instruction which can be avoided by always setting it to 1.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-style: normal;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;How to make test_and_set atomic ?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;As I discussed my previous post, we need support from processor to achieve this. &amp;nbsp;For eg i386 provides an “xchg” instruction that can atomically exchange the value of memory location with its internal register.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;int atomic_test_and_set (int *lock)&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;.text&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;.globl atomic_test_and_set&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
atomic_test_and_set:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; pushl&amp;nbsp;&amp;nbsp;&amp;nbsp;%ebx&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Preserve ebx. We will save original&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // value of lock in ebx. &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;movl&amp;nbsp;&amp;nbsp;&amp;nbsp; 8(%esp), %ebx&amp;nbsp; // save original value of lock to&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ebx&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;movl&amp;nbsp;&amp;nbsp;&amp;nbsp; $1, %eax&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// 1 (true) to eax&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;xchg&amp;nbsp;&amp;nbsp;&amp;nbsp; %eax, (%ebx)&amp;nbsp;&amp;nbsp;&amp;nbsp;// Atomically exchange eax with&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // lock. Sets lock = 1.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;popl&amp;nbsp;&amp;nbsp;&amp;nbsp; %ebx&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Restor ebx&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;ret&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// return old value of lock&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // which is already in eax.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Lets see how PowerPC does it.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;int atomic_test_and_set (int *lock)&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;atomic_test_and_set:&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;loop:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;lwarx&amp;nbsp;&amp;nbsp;r4, 0, r3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Save the initial value of lock.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; // r3 = address of lock variable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; li&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r5, 1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;1 (true) to r5&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; stwcx&amp;nbsp;&amp;nbsp;r5, 0, r3&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// store 1 to lock&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;bne-&amp;nbsp;&amp;nbsp;&amp;nbsp;loop&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// go back to loop if store is&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;not&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // done atomically&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;mr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r3, r4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// move original value of lock saved&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // in r4 to r3.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;blr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// return valus is in r3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Lets come back to our original discussion on Spin Locks. We will see how we can implement a Spin Lock on i386 and PowerPC shortly.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Why the word “spin” is used before lock ?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0596002556&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The reason is that while one of the thread has acquired the lock, all other threads spin in a loop repeatedly checking until the lock becomes available.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Spin Locks should be acquired for a short time&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;As the threads spinning in loop are not doing any useful work they are wasting costly cpu cycles. So, spinlocks should be held only for a short duration of time.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Why not deschedule the thread instead of spinning ?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Descheduling a thread means two context switches which are far more costlier than to spin in a loop for a short duration.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Thread should never sleep/descheduled after acquiring the spin lock&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If a thread goes to sleep or descheduled after acquiring the spin lock all other threads that need the same lock will be blocked spinning in a loop wasting cpu cycles.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Disabling interrupts&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If a shared variable is also accessed by an interrupt handler then local interrupts should also be disabled. Otherwise it is possible that the thread after acquiring the lock is preempted by the interrupt which tries to acquire the same lock as well. The interrupt handler will stuck in the busy-wait loop as the thread which is holding the lock never gets the chance to run to release the lock. So, both of them deadlock.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Uniprocessor systems don’t need spin locks&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;.&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal; font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Disabling interrupts is sufficient on a single cpu system.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Spin Lock implementation&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;atomic_test_and_set() discussed above is the buliding block of implementing a spin lock. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;void SpinLock(boolean *lock)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; while (atomic_test_and_set(lock) == 1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* If already locked spin */&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The caller obtains the lock if the old value of lock is 0, else it spins calling atomic_test_and_set() in a while loop, until it return 0.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Spin Lock is implementation on i386&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;SpinLock:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;mov &amp;nbsp; eax, 0x1 &amp;nbsp; &amp;nbsp;// Set the register eax = 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;loop:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;xchg &amp;nbsp;eax, [lock] // Atomically exchange the values of lock and register eax&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;test &amp;nbsp;eax, eax &amp;nbsp; &amp;nbsp;// Check the old value of lock which is now in eax&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;jnz &amp;nbsp; loop &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If old value of lock is not 0 (lock not free), spin in&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// loop&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ret&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;SpinUnlock:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;mov &amp;nbsp; eax, 0x0 &amp;nbsp; &amp;nbsp;// Set eax register = 0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;xchg &amp;nbsp;eax, [lock] // Exchange the values of lock and eax register.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// This will set the value of lock to 0.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ret&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Spin Lock implmentation on PowerPC&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background: #e6e6e6; border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;SpinLock:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;loop:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;lwarx &amp;nbsp; r4, 0, r3 &amp;nbsp; // register r3 has the address of lock variable&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Load the initial value of lock variable to register r4&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// and place a reservation on lock address&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;cmpwi &amp;nbsp; r4, 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; // compare value of lock with 0 &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;bne- &amp;nbsp; &amp;nbsp;loop &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// if value of lock is 1, go to loop&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;li &amp;nbsp; &amp;nbsp; &amp;nbsp;r5, 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; // Set r5 = 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;stwcx &amp;nbsp; r5, 0, r3 &amp;nbsp; // Store 1 to lock, &amp;nbsp;if reservation exists&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;bne- &amp;nbsp; &amp;nbsp;loop &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If reservation lost, try again&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// lock acquired successfully&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;background: #e6e6e6; border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;blr &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;// return&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/4952820529885343951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linux-kernel-part-2.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/4952820529885343951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/4952820529885343951'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linux-kernel-part-2.html' title='Synchronization in Linux Kernel - Part 2  (Spin Locks)'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4524264465260496221.post-5364745808615778211</id><published>2010-03-14T16:44:00.002+05:30</published><updated>2015-02-14T22:56:09.314+05:30</updated><title type='text'>Synchronization in Linux Kernel</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;&lt;iframe align=&quot;right&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=understan03-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0672327201&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: right; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;Linux Synchronization&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;We need synchronization mechanisms when two or more threads try to access a shared resource simultaneously. By using synchronization mechanisms kernel allows only one thread to execute while the other threads wait.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;Concurrency in Uniprocessor systems&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;You may be thinking that how multiple threads may execute concurrently when there is only one CPU. In a single cpu system, although multiple threads can not run simultaneously but a thread may be preempted by an interrupt Or it may be descheduled to run another thread on the cpu.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;The preemption can lead to same problems of concurrency like data corruption, deadlock etc.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Masking an interrupt is a simple technique used on single cpu systems to solve concurrency related problems. If a &amp;nbsp; global variable is shared by both an interrupt handler and a kernel thread, that interrupt should be masked by the thread while reading/modifying the shared variable.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;Concurrency on SMP systems&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;On an SMP system, two or more processors are connected to a single shared main memory. Multiple threads can run concurrently on different processors and can access the data on main memory simultaneously. Here, masking the interrupts on a single cpu or on all cpus does not solve the problem as we still have threads on other cpus running.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;We need more complex mechanisms here which we will discuss later.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;Problems due to concurrent access of shared resource&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;It is very important to understand the problems or the undesirable effects due to concurrent access of a shared resource by multiple threads.&amp;nbsp;Lets try to understand this using a simple example.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
Suppose there is a global variable called “state” (8 bits wide).&amp;nbsp;Consider two threads where thread-1 wants to set bit number one whereas thread-2 wants to set bit number two in the shared variable &quot;state&quot;. If any bit is already set, it should remain set.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;To set a bit, a processor needs to do following three operations&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;1)&amp;nbsp;&amp;nbsp; &amp;nbsp;Read the current value&amp;nbsp;from memory in its internal register.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;2) &amp;nbsp;&amp;nbsp;&amp;nbsp;Set the bit in register.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;3) &amp;nbsp;&amp;nbsp;&amp;nbsp;Store the modified value of register to memory.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;In fact, these are the common steps to perform any read-modify-write operations&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: 12pt;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Let the initial value of global variable &quot;state&quot; be 0xF0 (11110000b). The final value after both threads finished execution should be 0xF3 (11110011b).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;However this is not the case. It may happen that the final value of &quot;state&quot; is either 0xF1or 0xF2 or 0xF3. This may happen&amp;nbsp;when both the threads execute simultaneously. See the following example.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Thread 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Thread 2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Loads the value (0xF0) of global variable&amp;nbsp;&quot;state&quot;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&amp;nbsp;to its&amp;nbsp;register. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Loads the value (0xF0) of&amp;nbsp; global variable&amp;nbsp;&quot;state&quot;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; to its register.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;(As only one processor can access the memory at a time, lets assume that thread-1 loads the value first and&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;then the thread-2)&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Sets bit number 1 in its &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;register&amp;nbsp;(0xF0 -&amp;gt; 0xF1) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Sets bit number 2 in its register (0xF0 -&amp;gt; 0xF2)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;(Both the processors can do this at the same time as they are using their internal registers.)&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Stores the updated&amp;nbsp; value&amp;nbsp;(0xF1)&amp;nbsp;in its register&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;to&amp;nbsp;memory. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Stores the updated value (0xF2)&amp;nbsp;in its register&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; to memory&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;(Again lets assume thread-1 stores first)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;So, the final value is 0xF2 whereas it should have been 0xF3. We lost the bit number 1 set by thread-1.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;If thread-2 would have stored its value first, the final value will be 0xF1. In that case, the bit set by thread-2 would have been lost.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;So, concurrency access may lead to data corruption.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;How to solve the above problem&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The above problem can be solved if all the three operations are performed in single step (ie all other cpus are prevented from accessing the shared variable while one of the cpu is read-modify-writing to it.)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;display: inline !important;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;display: inline !important;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;For this we need support from the processor hardware. Most of the modern day&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;processors provide instructions to perform above operations atomically.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Lets take an example of&amp;nbsp; 80386 processor&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;80386 provides an instruction “btsl” to set a bit in memory. The format is as follows&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 2pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;btsl memAddr, bitNum&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // This will set bitNum at memAddr&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;But there is no guarantee that the above operation is atomic. To solve this, 80386 provides a mechanism where the memory bus may be locked until read-modify-write instruction has finished its execution. This is done by prefixing the instruction opcode by “lock”. When the contol unit detects the “lock” prefix, it locks the memory bus until that instruction is finished. No other processor can access the memory bus until the locked instruction has finished execution.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: 12pt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;lock; btsl memAddr, bitNum&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Atomically sets biNum at memAddr&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Similarly, “lock&quot;&lt;/span&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;prefix may be used to atomically increment a number.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(224,224,224); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;lock; incl memAddr&amp;nbsp;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp; &amp;nbsp; // Atomically increments the number&lt;/span&gt; &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;stored&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;at&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // memAddr&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Lets see how this happens on PowerPC&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;PowerPC provides two instructions to make read-modify-write instructions atomic. It doesn’t lock the memory bus but informs the programmer if the read-modify-write was atomic (by setting a bit in one of its conditional registers). The programmer can then retry the operation if the opertaion was not atomic.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The two instructions to provide atomicity are “lwarx” and “stwcx”.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;b&gt;l&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;warx&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;: Reads the word from memory and places a reservation on the loaded address.&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Think reservation as a kind of register that keeps the address of reserved memory.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;display: inline !important;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If the processor detects that another processor writes to the reserved address (using any store instruction), it clears the reservation.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;stwcx&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; : Stores only if the reservation exists on that address else no store will be done. It also informs the programmer if the store was done or not.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;The stwcx. instruction sets the CR0[EQ] bit if the store was successful and clears it if it failed. If the store is perfomed successfully, the reservation is cleared.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;display: inline !important;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Atomically incrementing an integer&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; border-right: windowtext 1pt solid; border-top: windowtext 1pt solid; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;retry:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;lwarx&amp;nbsp; r4, 0, r5&lt;/span&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;//&lt;/span&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Read integer from memory (whose address is stored in&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;// register r4) into r5, placing reservation on that&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // address &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;addi&amp;nbsp;&amp;nbsp; r4, r4, 1 &amp;nbsp;&amp;nbsp;// Add 1 to r4.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;stwcx. r4, 0, r5 &amp;nbsp;&amp;nbsp;// Attempt to store incremented value back to memory.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;bne-&amp;nbsp;&amp;nbsp; retry&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;// If the store failed (some other processor wrote at the&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;// same address&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;// in between due to which reservation was lost), retry.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;// Reservation is cleared if the store is performed&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(230,230,230); border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;// successfully.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Similarly, any read-modify-write operation may be perfomed using lwarx/stwcx.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;We will discuss on other concurrency problems and techniques to solve them later in the second part.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Till then enjoy and have fun ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;i&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://havefunwhileulearn.blogspot.com/feeds/5364745808615778211/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linux-kernel.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/5364745808615778211'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4524264465260496221/posts/default/5364745808615778211'/><link rel='alternate' type='text/html' href='http://havefunwhileulearn.blogspot.com/2010/03/synchronization-in-linux-kernel.html' title='Synchronization in Linux Kernel'/><author><name>Have Fun</name><uri>http://www.blogger.com/profile/10503742041500750416</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry></feed>