锘??xml version="1.0" encoding="utf-8" standalone="yes"?>高清免费久久午夜精品,亚洲人成无码网站久久99热国产 ,国产亚洲精久久久久久无码AVhttp://www.shnenglu.com/beautykingdom/category/7623.htmlzh-cnSat, 13 Oct 2012 12:05:15 GMTSat, 13 Oct 2012 12:05:15 GMT60memcached瀹屽叏鍓栨瀽緋誨垪鏁欑▼銆婅漿銆?/title><link>http://www.shnenglu.com/beautykingdom/archive/2012/06/16/179023.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Sat, 16 Jun 2012 01:17:00 GMT</pubDate><guid>http://www.shnenglu.com/beautykingdom/archive/2012/06/16/179023.html</guid><wfw:comment>http://www.shnenglu.com/beautykingdom/comments/179023.html</wfw:comment><comments>http://www.shnenglu.com/beautykingdom/archive/2012/06/16/179023.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/beautykingdom/comments/commentRss/179023.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/beautykingdom/services/trackbacks/179023.html</trackback:ping><description><![CDATA[     鎽樿: memcached瀹屽叏鍓栨瀽緋誨垪鏁欑▼–1. memcached鐨勫熀紜memcached鏄粈涔堬紵memcached 鏄互LiveJournal 鏃椾笅Danga Interactive 鍏徃鐨凚rad Fitzpatric 涓洪寮鍙戠殑涓嬈捐蔣浠躲傜幇鍦ㄥ凡鎴愪負(fù) 璞嗙摚銆丗acebook銆?nbsp;Vox 絳変紬...  <a href='http://www.shnenglu.com/beautykingdom/archive/2012/06/16/179023.html'>闃呰鍏ㄦ枃</a><img src ="http://www.shnenglu.com/beautykingdom/aggbug/179023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/beautykingdom/" target="_blank">chatler</a> 2012-06-16 09:17 <a href="http://www.shnenglu.com/beautykingdom/archive/2012/06/16/179023.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>姝婚攣鍜屾椿閿?deadlock and livelockhttp://www.shnenglu.com/beautykingdom/archive/2012/06/08/178097.htmlchatlerchatlerFri, 08 Jun 2012 09:15:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2012/06/08/178097.htmlhttp://www.shnenglu.com/beautykingdom/comments/178097.htmlhttp://www.shnenglu.com/beautykingdom/archive/2012/06/08/178097.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/178097.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/178097.html
涓銆佹椿閿?
濡傛灉浜嬪姟T1灝侀攣浜?jiǎn)鏁版嵁R錛屼簨鍔2鍙堣姹傚皝閿丷錛屼簬鏄疶2絳夊緟銆俆3涔熻姹傚皝閿丷錛?/pre>
褰揟1閲婃斁浜?jiǎn)R涓婄殑灝侀攣涔嬪悗緋葷粺棣栧厛鎵瑰噯浜?jiǎn)T3鐨勮姹傦紝T2浠嶇劧絳夊緟銆傜劧鍚嶵4鍙?/pre>
璇鋒眰灝侀攣R錛屽綋T3閲婃斁浜?jiǎn)R涓婄殑灝侀攣涔嬪悗緋葷粺鍙堟壒鍑嗕簡(jiǎn)T4鐨勮姹傦紝...錛孴2鏈夊彲
鑳芥案榪滅瓑寰咃紝榪欏氨鏄椿閿佺殑鎯呭艦,閬垮厤媧婚攣鐨勭畝鍗曟柟娉曟槸閲囩敤鍏堟潵鍏堟湇鍔$殑絳栫暐銆?/pre>
浜屻佹閿?
濡傛灉浜嬪姟T1灝侀攣浜?jiǎn)鏁版嵁R1錛孴2灝侀攣浜?jiǎn)鏁版嵁R2錛岀劧鍚嶵1鍙堣姹傚皝閿丷2錛屽洜T2宸?/pre>
灝侀攣浜?jiǎn)R2錛屼簬鏄疶1絳夊緟T2閲婃斁R2涓婄殑閿併傛帴鐫T2鍙堢敵璇峰皝閿丷1錛屽洜T1宸插皝閿佷簡(jiǎn)R1錛?/pre>
T2涔熷彧鑳界瓑寰匱1閲婃斁R1涓婄殑閿併傝繖鏍峰氨鍑虹幇浜?jiǎn)T1鍦ㄧ瓑寰匱2錛岃孴2鍙堝湪絳夊緟T1鐨勫眬闈紝
T1鍜孴2涓や釜浜嬪姟姘歌繙涓嶈兘緇撴潫錛屽艦鎴愭閿併?
1. 姝婚攣鐨勯闃?
鍦ㄦ暟鎹簱涓紝浜х敓姝婚攣鐨勫師鍥犳槸涓や釜鎴栧涓簨鍔¢兘宸插皝閿佷簡(jiǎn)涓浜涙暟鎹璞★紝鐒跺悗鍙堥兘
璇鋒眰瀵瑰凡涓哄叾浠栦簨鍔″皝閿佺殑鏁版嵁瀵硅薄鍔犻攣錛屼粠鑰屽嚭鐜版絳夊緟銆傞槻姝㈡閿佺殑鍙戠敓鍏跺疄灝?/pre>
鏄鐮村潖浜х敓姝婚攣鐨勬潯浠躲傞闃叉閿侀氬父鏈変袱縐嶆柟娉曪細(xì) 
① 涓嬈″皝閿佹硶  
涓嬈″皝閿佹硶瑕佹眰姣忎釜浜嬪姟蹇呴』涓嬈″皢鎵鏈夎浣跨敤鐨勬暟鎹叏閮ㄥ姞閿侊紝鍚﹀垯灝變笉鑳界戶緇墽琛屻?

涓嬈″皝閿佹硶铏界劧鍙互鏈夋晥鍦伴槻姝㈡閿佺殑鍙戠敓錛屼絾涔熷瓨鍦ㄩ棶棰橈紝涓嬈″氨灝嗕互鍚庤鐢ㄥ埌鐨勫叏
閮ㄦ暟鎹姞閿侊紝鍔垮繀鎵╁ぇ浜?jiǎn)灏侀攣鐨勮寖鍥村Q屼粠鑰岄檷浣庝簡(jiǎn)緋葷粺鐨勫茍鍙戝害銆?
② 欏哄簭灝侀攣娉?
欏哄簭灝侀攣娉曟槸棰勫厛瀵規(guī)暟鎹璞¤瀹氫竴涓皝閿侀『搴忥紝鎵鏈変簨鍔¢兘鎸夎繖涓『搴忓疄琛屽皝閿併?

欏哄簭灝侀攣娉曞彲浠ユ湁鏁堝湴闃叉姝婚攣錛屼絾涔熷悓鏍峰瓨鍦ㄩ棶棰樸備簨鍔$殑灝侀攣璇鋒眰鍙互闅忕潃浜嬪姟鐨?/pre>
鎵ц鑰屽姩鎬佸湴鍐沖畾錛屽緢闅句簨鍏堢‘瀹氭瘡涓涓簨鍔¤灝侀攣鍝簺瀵硅薄錛屽洜姝や篃灝卞緢闅炬寜瑙勫畾鐨?/pre>
欏哄簭鍘繪柦鍔犲皝閿併?
 
鍙錛屽湪鎿嶄綔緋葷粺涓箍涓洪噰鐢ㄧ殑棰勯槻姝婚攣鐨勭瓥鐣ュ茍涓嶅緢閫傚悎鏁版嵁搴撶殑鐗圭偣錛屽洜姝BMS鍦?/pre>
瑙e喅姝婚攣鐨勯棶棰樹笂鏅亶閲囩敤鐨勬槸璇婃柇騫惰В闄ゆ閿佺殑鏂規(guī)硶銆?

 2. 姝婚攣鐨勮瘖鏂笌瑙i櫎
 
① 瓚呮椂娉?

 濡傛灉涓涓簨鍔$殑絳夊緟鏃墮棿瓚呰繃浜?jiǎn)瑙勫畾鐨勬棄櫃愬Q屽氨璁や負(fù)鍙戠敓浜?jiǎn)姝婚攣銆傝秴鏃舵硶瀹炵幇綆鍗曪紝浣?/pre>
鍏朵笉瓚充篃寰堟槑鏄俱備竴鏄湁鍙兘璇垽姝婚攣錛屼簨鍔″洜涓哄叾浠栧師鍥犱嬌絳夊緟鏃墮棿瓚呰繃鏃墮檺錛岀郴緇熶細(xì)
璇涓哄彂鐢熶簡(jiǎn)姝婚攣銆備簩鏄椂闄愯嫢璁劇疆寰楀お闀匡紝姝婚攣鍙戠敓鍚庝笉鑳藉強(qiáng)鏃跺彂鐜般?
 
② 絳夊緟鍥炬硶
 
浜嬪姟絳夊緟鍥炬槸涓涓湁鍚戝浘G=(T,U)銆?T涓虹粨鐐圭殑闆嗗悎錛屾瘡涓粨鐐硅〃紺烘榪愯鐨勪簨鍔★紱U涓?/pre>
杈圭殑闆嗗悎錛屾瘡鏉¤竟琛ㄧず浜嬪姟絳夊緟鐨勬儏鍐點(diǎn)傝嫢T1絳夊緟T2,鍒橳1銆乀2涔嬮棿鍒掍竴鏉℃湁鍚戣竟錛屼粠T1
鎸囧悜T2銆備簨鍔$瓑寰呭浘鍔ㄦ佸湴鍙嶆槧浜?jiǎn)鎵鏈変簨鍔$殑絳夊緟鎯呭喌銆傚茍鍙戞帶鍒跺瓙緋葷粺鍛ㄦ湡鎬у湴錛堟瘮濡?/pre>
姣忛殧1鍒嗛挓錛夋嫻嬩簨鍔$瓑寰呭浘錛屽鏋滃彂鐜板浘涓瓨鍦ㄥ洖璺紝鍒欒〃紺虹郴緇熶腑鍑虹幇浜?jiǎn)姝婚攣銆?
 
DBMS鐨勫茍鍙戞帶鍒跺瓙緋葷粺涓鏃︽嫻嬪埌緋葷粺涓瓨鍦ㄦ閿侊紝灝辮璁炬硶瑙i櫎銆傞氬父閲囩敤鐨勬柟娉曟槸閫夋嫨
涓涓鐞嗘閿佷唬浠鋒渶灝忕殑浜嬪姟錛屽皢鍏舵挙娑堬紝閲婃斁姝や簨鍔℃寔鏈夌殑鎵鏈夌殑閿侊紝浣垮叾瀹冧簨鍔″緱浠ョ戶緇?/pre>
榪愯涓嬪幓銆傚綋鐒?dòng)灱屽?guī)挙娑堢殑浜嬪姟鎵鎵ц鐨勬暟鎹慨鏀規(guī)搷浣滃繀欏誨姞浠ユ仮澶嶃?/pre>

chatler 2012-06-08 17:15 鍙戣〃璇勮
]]>涓?C/C++ 欏圭洰鏋勫緩鎮(zhèn)ㄨ嚜宸辯殑鍐呭瓨綆$悊鍣?lt;forward>http://www.shnenglu.com/beautykingdom/archive/2012/05/26/176285.htmlchatlerchatlerSat, 26 May 2012 14:41:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2012/05/26/176285.htmlhttp://www.shnenglu.com/beautykingdom/comments/176285.htmlhttp://www.shnenglu.com/beautykingdom/archive/2012/05/26/176285.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/176285.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/176285.html闃呰鍏ㄦ枃

chatler 2012-05-26 22:41 鍙戣〃璇勮
]]>
TCMalloc : Thread-Caching Mallochttp://www.shnenglu.com/beautykingdom/archive/2012/04/04/170077.htmlchatlerchatlerWed, 04 Apr 2012 13:15:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2012/04/04/170077.htmlhttp://www.shnenglu.com/beautykingdom/comments/170077.htmlhttp://www.shnenglu.com/beautykingdom/archive/2012/04/04/170077.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/170077.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/170077.htmlfrom: 

http://goog-perftools.sourceforge.net/doc/tcmalloc.html

 

TCMalloc : Thread-Caching Malloc

Sanjay Ghemawat, Paul Menage <opensource@google.com>

Motivation

TCMalloc is faster than the glibc 2.3 malloc (available as a separate library called ptmalloc2) and other mallocs that I have tested. ptmalloc2 takes approximately 300 nanoseconds to execute a malloc/free pair on a 2.8 GHz P4 (for small objects). The TCMalloc implementation takes approximately 50 nanoseconds for the same operation pair. Speed is important for a malloc implementation because if malloc is not fast enough, application writers are inclined to write their own custom free lists on top of malloc. This can lead to extra complexity, and more memory usage unless the application writer is very careful to appropriately size the free lists and scavenge idle objects out of the free list

TCMalloc also reduces lock contention for multi-threaded programs. For small objects, there is virtually zero contention. For large objects, TCMalloc tries to use fine grained and efficient spinlocks. ptmalloc2 also reduces lock contention by using per-thread arenas but there is a big problem with ptmalloc2's use of per-thread arenas. In ptmalloc2 memory can never move from one arena to another. This can lead to huge amounts of wasted space. For example, in one Google application, the first phase would allocate approximately 300MB of memory for its data structures. When the first phase finished, a second phase would be started in the same address space. If this second phase was assigned a different arena than the one used by the first phase, this phase would not reuse any of the memory left after the first phase and would add another 300MB to the address space. Similar memory blowup problems were also noticed in other applications.

Another benefit of TCMalloc is space-efficient representation of small objects. For example, N 8-byte objects can be allocated while using space approximately 8N * 1.01 bytes. I.e., a one-percent space overhead. ptmalloc2 uses a four-byte header for each object and (I think) rounds up the size to a multiple of 8 bytes and ends up using 16N bytes.

Usage

To use TCmalloc, just link tcmalloc into your application via the "-ltcmalloc" linker flag.

You can use tcmalloc in applications you didn't compile yourself, by using LD_PRELOAD:

   $ LD_PRELOAD="/usr/lib/libtcmalloc.so" 

LD_PRELOAD is tricky, and we don't necessarily recommend this mode of usage.

TCMalloc includes a heap checker and heap profiler as well.

If you'd rather link in a version of TCMalloc that does not include the heap profiler and checker (perhaps to reduce binary size for a static binary), you can link in libtcmalloc_minimal instead.

Overview

TCMalloc assigns each thread a thread-local cache. Small allocations are satisfied from the thread-local cache. Objects are moved from central data structures into a thread-local cache as needed, and periodic garbage collections are used to migrate memory back from a thread-local cache into the central data structures.

TCMalloc treates objects with size <= 32K ("small" objects) differently from larger objects. Large objects are allocated directly from the central heap using a page-level allocator (a page is a 4K aligned region of memory). I.e., a large object is always page-aligned and occupies an integral number of pages.

A run of pages can be carved up into a sequence of small objects, each equally sized. For example a run of one page (4K) can be carved up into 32 objects of size 128 bytes each.

Small Object Allocation

Each small object size maps to one of approximately 170 allocatable size-classes. For example, all allocations in the range 961 to 1024 bytes are rounded up to 1024. The size-classes are spaced so that small sizes are separated by 8 bytes, larger sizes by 16 bytes, even larger sizes by 32 bytes, and so forth. The maximal spacing (for sizes >= ~2K) is 256 bytes.

A thread cache contains a singly linked list of free objects per size-class.

When allocating a small object: (1) We map its size to the corresponding size-class. (2) Look in the corresponding free list in the thread cache for the current thread. (3) If the free list is not empty, we remove the first object from the list and return it. When following this fast path, TCMalloc acquires no locks at all. This helps speed-up allocation significantly because a lock/unlock pair takes approximately 100 nanoseconds on a 2.8 GHz Xeon.

If the free list is empty: (1) We fetch a bunch of objects from a central free list for this size-class (the central free list is shared by all threads). (2) Place them in the thread-local free list. (3) Return one of the newly fetched objects to the applications.

If the central free list is also empty: (1) We allocate a run of pages from the central page allocator. (2) Split the run into a set of objects of this size-class. (3) Place the new objects on the central free list. (4) As before, move some of these objects to the thread-local free list.

Large Object Allocation

A large object size (> 32K) is rounded up to a page size (4K) and is handled by a central page heap. The central page heap is again an array of free lists. For i < 256, the kth entry is a free list of runs that consist of k pages. The 256th entry is a free list of runs that have length >= 256 pages:

An allocation for k pages is satisfied by looking in the kth free list. If that free list is empty, we look in the next free list, and so forth. Eventually, we look in the last free list if necessary. If that fails, we fetch memory from the system (using sbrk, mmap, or by mapping in portions of /dev/mem).

If an allocation for k pages is satisfied by a run of pages of length > k, the remainder of the run is re-inserted back into the appropriate free list in the page heap.

Spans

The heap managed by TCMalloc consists of a set of pages. A run of contiguous pages is represented by a Span object. A span can either be allocated, or free. If free, the span is one of the entries in a page heap linked-list. If allocated, it is either a large object that has been handed off to the application, or a run of pages that have been split up into a sequence of small objects. If split into small objects, the size-class of the objects is recorded in the span.

A central array indexed by page number can be used to find the span to which a page belongs. For example, span a below occupies 2 pages, span b occupies 1 page, span c occupies 5 pages and span d occupies 3 pages.

A 32-bit address space can fit 2^20 4K pages, so this central array takes 4MB of space, which seems acceptable. On 64-bit machines, we use a 3-level radix tree instead of an array to map from a page number to the corresponding span pointer.

Deallocation

When an object is deallocated, we compute its page number and look it up in the central array to find the corresponding span object. The span tells us whether or not the object is small, and its size-class if it is small. If the object is small, we insert it into the appropriate free list in the current thread's thread cache. If the thread cache now exceeds a predetermined size (2MB by default), we run a garbage collector that moves unused objects from the thread cache into central free lists.

If the object is large, the span tells us the range of pages covered by the object. Suppose this range is [p,q]. We also lookup the spans for pages p-1 andq+1. If either of these neighboring spans are free, we coalesce them with the [p,q] span. The resulting span is inserted into the appropriate free list in the page heap.

Central Free Lists for Small Objects

As mentioned before, we keep a central free list for each size-class. Each central free list is organized as a two-level data structure: a set of spans, and a linked list of free objects per span.

An object is allocated from a central free list by removing the first entry from the linked list of some span. (If all spans have empty linked lists, a suitably sized span is first allocated from the central page heap.)

An object is returned to a central free list by adding it to the linked list of its containing span. If the linked list length now equals the total number of small objects in the span, this span is now completely free and is returned to the page heap.

Garbage Collection of Thread Caches

A thread cache is garbage collected when the combined size of all objects in the cache exceeds 2MB. The garbage collection threshold is automatically decreased as the number of threads increases so that we don't waste an inordinate amount of memory in a program with lots of threads.

We walk over all free lists in the cache and move some number of objects from the free list to the corresponding central list.

The number of objects to be moved from a free list is determined using a per-list low-water-mark L. L records the minimum length of the list since the last garbage collection. Note that we could have shortened the list by L objects at the last garbage collection without requiring any extra accesses to the central list. We use this past history as a predictor of future accesses and move L/2 objects from the thread cache free list to the corresponding central free list. This algorithm has the nice property that if a thread stops using a particular size, all objects of that size will quickly move from the thread cache to the central free list where they can be used by other threads.

Performance Notes

PTMalloc2 unittest

The PTMalloc2 package (now part of glibc) contains a unittest program t-test1.c. This forks a number of threads and performs a series of allocations and deallocations in each thread; the threads do not communicate other than by synchronization in the memory allocator.

t-test1 (included in google-perftools/tests/tcmalloc, and compiled as ptmalloc_unittest1) was run with a varying numbers of threads (1-20) and maximum allocation sizes (64 bytes - 32Kbytes). These tests were run on a 2.4GHz dual Xeon system with hyper-threading enabled, using Linux glibc-2.3.2 from RedHat 9, with one million operations per thread in each test. In each case, the test was run once normally, and once with LD_PRELOAD=libtcmalloc.so.

The graphs below show the performance of TCMalloc vs PTMalloc2 for several different metrics. Firstly, total operations (millions) per elapsed second vs max allocation size, for varying numbers of threads. The raw data used to generate these graphs (the output of the "time" utility) is available in t-test1.times.txt.

  • TCMalloc is much more consistently scalable than PTMalloc2 - for all thread counts >1 it achieves ~7-9 million ops/sec for small allocations, falling to ~2 million ops/sec for larger allocations. The single-thread case is an obvious outlier, since it is only able to keep a single processor busy and hence can achieve fewer ops/sec. PTMalloc2 has a much higher variance on operations/sec - peaking somewhere around 4 million ops/sec for small allocations and falling to <1 million ops/sec for larger allocations.
  • TCMalloc is faster than PTMalloc2 in the vast majority of cases, and particularly for small allocations. Contention between threads is less of a problem in TCMalloc.
  • TCMalloc's performance drops off as the allocation size increases. This is because the per-thread cache is garbage-collected when it hits a threshold (defaulting to 2MB). With larger allocation sizes, fewer objects can be stored in the cache before it is garbage-collected.
  • There is a noticeably drop in the TCMalloc performance at ~32K maximum allocation size; at larger sizes performance drops less quickly. This is due to the 32K maximum size of objects in the per-thread caches; for objects larger than this tcmalloc allocates from the central page heap.

Next, operations (millions) per second of CPU time vs number of threads, for max allocation size 64 bytes - 128 Kbytes.

Here we see again that TCMalloc is both more consistent and more efficient than PTMalloc2. For max allocation sizes <32K, TCMalloc typically achieves ~2-2.5 million ops per second of CPU time with a large number of threads, whereas PTMalloc achieves generally 0.5-1 million ops per second of CPU time, with a lot of cases achieving much less than this figure. Above 32K max allocation size, TCMalloc drops to 1-1.5 million ops per second of CPU time, and PTMalloc drops almost to zero for large numbers of threads (i.e. with PTMalloc, lots of CPU time is being burned spinning waiting for locks in the heavily multi-threaded case).

Caveats

For some systems, TCMalloc may not work correctly on with applications that aren't linked against libpthread.so (or the equivalent on your OS). It should work on Linux using glibc 2.3, but other OS/libc combinations have not been tested.

TCMalloc may be somewhat more memory hungry than other mallocs, though it tends not to have the huge blowups that can happen with other mallocs. In particular, at startup TCMalloc allocates approximately 6 MB of memory. It would be easy to roll a specialized version that trades a little bit of speed for more space efficiency.

TCMalloc currently does not return any memory to the system.

Don't try to load TCMalloc into a running binary (e.g., using JNI in Java programs). The binary will have allocated some objects using the system malloc, and may try to pass them to TCMalloc for deallocation. TCMalloc will not be able to handle such objects.



chatler 2012-04-04 21:15 鍙戣〃璇勮
]]>
How does the DMA workhttp://www.shnenglu.com/beautykingdom/archive/2010/11/14/133602.htmlchatlerchatlerSun, 14 Nov 2010 11:23:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2010/11/14/133602.htmlhttp://www.shnenglu.com/beautykingdom/comments/133602.htmlhttp://www.shnenglu.com/beautykingdom/archive/2010/11/14/133602.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/133602.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/133602.html

The DMA is another two chips on your motherboard (usually is an Intel 8237A-5 chips) that allow you (the programmer) to offload data transfers between I/O boards. DMA actually stands for 'Direct Memory Access'.

DMA can work: memory->I/O, I/O->memory. The memory->memory transfer doesn't work. It doesn't matter because ISA DMA is slow as hell and thus is unusable. Futhermore, using DMA for zeroing out memory would massacre the contents of memory caches.

What about caches and DMA? L1 and L2 caches work absolutely transparently. When DMA writes to memory, caches autmatically load or least invalidate the data that go into the memory. When DMA reads memory, caches supply the unwritten bytes so not old but new values are tranferred to the peripheral.

There are signals DACK, DRQ, and TC. When a peripheral wants to move a byte or 2 bytes into memory (is dependent on whether 8 bit or 16 bit DMA channel is in use -- 0,1,2,3 are 8-bit, 5,6,7 are 16-bit), it issues DRQ. DMA controller chats with CPU and after some time DMA controller issues DACK. Seeing DACK, the peripheral puts it's byte on data bus, DMA controller takes it and puts it in memory. If it was the last byte/word to move, DMA controller sets up also TC during the DACK. When peripheral sees TC, it is possible it will not want any more movements,

In the other direction, everything is the same, but first the byte/word is fetched from the memory and then DACK is generated and the peripheral takes the data.

DMA controller has only 8-bit address counter inside. There is external ALS573 counter for each chip so it makes programmer see it as DMA controller had 16 bits of address counter per channel inside. There are more 8 bits of address per channel of so called page register in LS612 that unfortunately do not increment as those in ALS573. All these 24 bits can address 16777216 of distict addresses.

Recapitulation: for each channel, independently, you see 16 bits of auto-incrementing counter, and 8 bits of page register which doesn't increment.

The difference between 16-bit DMA channels and 8-bit DMA channels is that the address bits for 16-bit channels are wired one bit left to the address bus so every address is 2 times bigger. The lowest bit is 0. The highest bit of page register would fit into bit 24 which is not on ISA so that it is left unconnected. The bus control logic is wired for 16-bit channels in a manner every single DMA transfer, a 16-bit cycle is generated, so ISA device puts 16 bits onto the bus at the time. I don't know what happens if you use 16-bit DMA channel with XT peripheral. I guess it could work but only be slower.

8-bit DMA: increments by 1, cycles inside 65536 bytes, addresses 16MB, moves 8 bits a time.

16-bit DMA: increments by 2, goes only over even addresses, cycles inside 131072 bytes, addresses 16MB, moves 16 bits a time. Uses 16-bit ISA I/O cycle so it takes less ticks to make one move that the 8-bit DMA.

An example of DMA usage would be the Sound Blaster's ability to play samples in the background. The CPU sets up the sound card and the DMA. When the DMA is told to 'go', it simply shovels the data from RAM to the card. Since this is done off-CPU, the CPU can do other things while the data is being transferred.

Enough basics. Here's how you program the DMA chip.


When you want to start a DMA transfer, you need to know several things:

  • Number of DMA channel you want to use
  • What page to use
  • The offset in the page
  • The length
  • How to tell you peripheral to ask for DMA

  • You cannot transfer more than 64K or 128K of data in one shot, and
  • You cannot cross a page boundary. If you cross it, the lower 16 or 17 bits of address will simply wrap and you only suddenly jump 65536 or 131072 bytes lower that where you expected. It will be absolutely OK and no screw up will be performed. If you will take it in account in your program you can use it.

Restriction #1 is rather easy to get around. Simply transfer the first block, and when the transfer is done, send the next block.

For those of you not familiar with pages, I'll try to explain.

Picture the first 16MB region of memory in your system. It is divided into 256 pages of 64K or 128 pages of 128K. Every page starts at a multiple of 65536 or 131072. They are numbered from 0 to 255 or from 0 to 127.

In plain English, the page is the highest 8 bits or 7 bits of the absolute 24 bit address of our memory location. The offset is the lower 16 or 17 bits of the absolute 24 bit address.

Now that we know where our data is, we need to find the length.

The DMA has a little quirk on length. The true length sent to the DMA is actually length + 1. So if you send a zero length to the DMA, it actually transfers one byte or word, whereas if you send 0xFFFF, it transfers 64K or 128K. I guess they made it this way because it would be pretty senseless to program the DMA to do nothing (a length of zero), and in doing it this way, it allowed a full 64K or 128K span of data to be transferred.

Now that you know what to send to the DMA, how do you actually start it? This enters us into the different DMA channels.

The following chart will describe each channel and it's corresponding port number:

DMA ChannelPageAddressCount
087h0h1h
183h2h3h
281h4h5h
382h6h7h
48FhC0hC2h
58BhC4hC6h
689hC8hCAh
78AhCChCEh

DMA 4. Doesn't exist. DMA 4 is used to cascade the two 8237A chips. When first 8237A wants to DMA, it issues "HRQ" to second chip's DRQ 4. The second chip thinks DMA 4 is wanna be made so issues DRQ 4 to the first chip's HLDA. First chip makes it's own DMA 0-3, then sends to the second "OK second chip, my DMA 4 is complete" and second chip knows it's free on the bus. If this mechanism would not work, the two chips could peck each other on the BUS and the PC would screw up. :+)

from:


chatler 2010-11-14 19:23 鍙戣〃璇勮
]]>
鏈夊叧寮傛璇誨啓銆侀氫俊 http://www.shnenglu.com/beautykingdom/archive/2010/09/06/126028.htmlchatlerchatlerMon, 06 Sep 2010 09:33:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2010/09/06/126028.htmlhttp://www.shnenglu.com/beautykingdom/comments/126028.htmlhttp://www.shnenglu.com/beautykingdom/archive/2010/09/06/126028.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/126028.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/126028.html 綆浠?/div>

涓鑸潵璇達(dá)紝綆鍗曠殑寮傛錛圓synchronous錛?璋冪敤鏄繖鏍蜂竴縐嶈皟鐢ㄦ柟寮忥細(xì)鍙戣搗鑰呰姹備竴涓紓姝ヨ皟鐢紝閫氱煡鎵ц鑰咃紝鐒跺悗澶勭悊鍏朵粬宸ヤ綔錛屽湪鏌愪竴涓悓姝ョ偣絳夊緟鎵ц鑰呯殑瀹屾垚錛涙墽琛岃呮墽琛岃皟鐢ㄧ殑瀹為檯鎿嶄綔錛屽畬鎴愬悗閫?鐭ュ彂璧瘋(gè)呫傚彲浠ョ湅鍑猴紝鍦ㄥ紓姝ヨ皟鐢ㄤ腑鏈変袱縐嶈鑹詫細(xì)鍙戣搗鑰呭拰鎵ц鑰咃紝瀹冧滑閮芥槸鑳戒富鍔ㄨ繍琛岀殑瀵硅薄錛屾垜浠О涓轟富鍔ㄥ璞★紝鍚屾椂榪樻湁涓涓悓姝ョ偣錛屼富鍔ㄥ璞″湪鍚屾鐐瑰崗璋?鍚屾銆傚湪鏈枃涓紝鎴戜滑璁ㄨ涓昏鏄氱敤璁$畻鏈恒佸榪涚▼澶氱嚎紼嬬殑鍒嗘椂鎿嶄綔緋葷粺涓婄殑寮傛璋冪敤銆傚湪鎿嶄綔緋葷粺鐨勮搴︿笂鏉ョ湅錛屼富鍔ㄥ璞″寘鎷簡(jiǎn)榪涚▼銆佺嚎紼嬪拰紜歡涓婄殑IC絳夛紝鑷充簬涓柇錛屽彲浠ョ湅浣滄繪槸鍦ㄦ煇涓繘紼嬫垨鑰呯嚎紼嬬殑涓婁笅鏂囧熺敤涓涓婥PU銆傝屽悓姝ユ搷浣滃彲浠ラ氳繃鎿嶄綔緋葷粺寰楀悇縐嶅悓姝ユ満鍒訛細(xì)浜掓枼閿侊紝淇″彿鐏瓑絳夋潵瀹屾垚銆?/div>

鎴戜滑鍙互鍏堢湅鐪嬪紓姝ヨ皟鐢ㄥ湪Windows(鏈枃涓竴鑸笉鍔犳寚鍑虹殑璇濓紝閮芥槸鐗規(guī)寚NT/2000)璇誨啓鏂囦歡涓殑搴旂敤銆俉indows涓殑ReadFile鍜學(xué)riteFile閮芥彁渚涗簡(jiǎn)寮傛鐨勬帴鍙c備互ReadFile涓轟緥錛?/div>

BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);

濡傛灉鏈鍚庝竴涓弬鏁發(fā)pOverlapped涓嶄負(fù)NULL錛屽茍涓旀枃浠朵互FILE_FLAG_OVERLAPPED 鏍囧織鎵撳紑錛岄偅涔堣繖涓皟鐢ㄥ氨鏄紓姝ョ殑錛歊eadFile浼?xì)绔嬪埢杩斿洖锛屽鏋滄搷浣滄病鏈夌珛鍒诲畬鎴愬Q堣繑鍥濬ALSE騫朵笖GetLastError()榪斿洖 ERROR_IO_PENDING錛夛紝閭d箞璋冪敤鑰呭彲浠ュ湪鏌愪釜鏃跺埢閫氳繃WaitForSingleObject絳夊嚱鏁版潵絳夊緟涓殑hEvent鏉ョ瓑寰呮搷浣滃畬鎴?錛堝彲鑳藉凡緇忓畬鎴愶級(jí)榪涜鍚屾錛屽綋鎿嶄綔瀹屾垚浠ュ悗錛屽彲浠ヨ皟鐢℅etOverlappedResult鑰呰幏寰楁搷浣滅殑緇撴灉錛屾瘮濡傛槸鍚︽垚鍔燂紝璇誨彇浜?jiǎn)澶殲畱瀛楄妭绛壗{夈傝繖閲?鐨勫彂璧瘋(gè)呭氨鏄簲鐢ㄧ▼搴忥紝鑰屾墽琛岃呭氨鏄搷浣滅郴緇熸湰韜紝鑷充簬鎵ц鑰呮槸鎬庝箞鎵ц鐨勶紝鎴戜滑浼?xì)鍦ㄥ悗闈㈢殑绡囼q呰璁恒傝屼袱鑰呯殑鍚屾灝辨槸閫氳繃涓涓猈indows Event鏉ュ畬鎴愩?/div>

鎶婅繖涓紓姝ヨ皟鐢ㄧ殑榪囩▼鍐嶆娊璞″拰鎵╁睍涓浜涳紝鎴戜滑鍙互鎶婂紓姝ヨ皟鐢ㄩ渶瑕佽В鍐崇殑闂褰掔粨涓轟袱涓細(xì)涓涓槸鎵ц鐨勫姩鍔涳紝鍙︿竴涓槸涓誨姩瀵硅薄鐨勮皟搴︺傜畝鍗曟潵璇達(dá)紝鍓嶈呮槸鍚勪釜涓誨姩瀵硅薄錛堢嚎紼嬨佽繘紼嬫垨鑰呬竴浜涗唬鐮侊級(jí)鏄浣曡幏寰桟PU錛屽悗鑰呮槸鍚勪釜涓誨姩瀵硅薄濡備綍鍗忓悓宸ヤ綔錛?淇濊瘉鎿嶄綔鐨勬祦紼嬫槸鍗忚皟姝g‘鐨勩備竴鑸潵璇達(dá)紝榪涚▼鍜岀嚎紼嬮兘鍙互鐢辨搷浣滅郴緇熺洿鎺ヨ皟搴﹁岃幏寰桟PU錛岃屾洿緇嗙矑搴︾殑錛屾瘮濡備竴浜涗唬鐮佺殑璋冨害錛屽線寰灝遍渶瑕佷竴涓洿澶嶆潅鐨勬ā 鍨嬶紙姣斿鍦ㄦ搷浣滅郴緇熷唴閮ㄧ殑瀹炵幇錛岃繖鏃跺欑嚎紼嬬殑綺掑害澶矖浜?jiǎn)锛?jí)銆傝屼富鍔ㄥ璞$殑璋冨害錛屽綋鍙備笌鑰呰緝?yōu)畱鐨勬椂鍊欙紝鍙互閫氳繃鍩烘湰鐨勫悓姝ユ満鍒舵潵瀹屾垚錛屽湪鏇村鏉傜殑鎯呭喌涓嬶紝鍙?鑳介氳繃涓涓猻chedule鏈哄埗鏉ュ仛浼?xì)鏇村疄闄呬竴浜涖?/div>

鍔ㄥ姏鍜岃皟搴?/div>

濡傚墠鎵榪幫紝寮傛璋冪敤涓昏闇瑕佽В鍐充袱 涓棶棰橈細(xì)鎵ц鐨勫姩鍔涘拰鎵ц鐨勮皟搴︺傛渶鏅亶鐨勬儏鍐靛氨鏄紝涓涓富瀵兼祦紼嬬殑璋冪敤鑰呰繘紼嬶紙綰跨▼錛夛紝涓涓垨澶氫釜宸ヤ綔鑰呰繘紼嬶紙綰跨▼錛夛紝閫氳繃鎿嶄綔緋葷粺鎻愪緵鐨勫悓姝ユ満鍒舵潵瀹?鎴愬紓姝ヨ皟鐢ㄣ傝繖涓悓姝ユ満鍒跺湪鎵╁睍鍖栫殑鎯呭艦涓嬶紝鏄竴涓垨澶氫釜鏍呮爮Barrier錛屽搴斾簬姣忎釜鍚屾鐨勬墽琛岀偣銆傛墍鏈夐渶瑕佸湪榪欎釜鎵ц鐐瑰悓姝ョ殑涓誨姩瀵硅薄浼?xì)绛夊緟鐩稿簲鐨?Barrier錛岀洿鍒版墍鏈夊璞¢兘瀹屾垚銆傚湪涓浜涚畝鍖栫殑鎯呭艦錛屾瘮濡傝宸ヤ綔鑰呭茍涓嶅叧蹇?jī)璋冪敤鑰呯殑鍚屾錛岄偅涔堣繖涓狟arrier鍙互綆鍖栨垚淇″彿鐏紝鍦ㄥ彧鏈変竴涓伐浣滆?鐨勬儏鍐典笅錛屽彲浠ョ畝鍖栨垚涓涓猈indows浜嬩歡Event鎴栬呮潯浠跺彉閲?Condition Variable銆?/div>

鐜板湪鏉ヨ冭檻澶嶆潅鐨勬儏 褰€傚亣璁炬垜浠敤涓浜涚嚎紼嬫潵鍗忎綔瀹屾垚涓欏瑰伐浣滐紝鍚勪釜綰跨▼鐨勬墽琛屼箣闂存湁鍏堝悗欏哄簭涓婄殑闄愬埗錛岃屾搷浣滅郴緇熷氨鏄繖欏瑰伐浣滅殑璋冨害鑰咃紝璐熻矗鍦ㄩ傚綋鐨勬椂鍊欒皟搴﹂傚綋鐨勭嚎紼嬫潵 鑾峰緱CPU銆傛樉鐒?dòng)灱岒q跺彂鎵ц涓殑涓涓嚎紼嬪浜庡彟澶栦竴涓嚎紼嬫潵璇達(dá)紝鏈川涓婂氨鏄紓姝ョ殑錛屽亣濡傚畠浠箣闂存湁璋冪敤鍏崇郴錛岄偅涔熷氨鏄竴涓紓姝ヨ皟鐢ㄣ傝屾搷浣滅郴緇熷彲浠ラ氳繃鍩?鏈殑鍚屾鏈哄埗浣垮緱鍚堥傜殑綰跨▼鎵嶈璋冨害錛屽叾浠栨湭瀹屾垚鐨勭嚎紼嬪垯澶勪簬絳夊緟鐘舵併備婦渚嬭錛屾垜浠湁4涓嚎紼婣,B,C,D鏉ュ畬鎴愪竴欏瑰伐浣滐紝鍏朵腑鐨勯『搴忛檺鍒舵槸 A>B;C>D錛?#8220;>”琛ㄧず宸﹁竟鐨勭嚎紼嬪畬鎴愬繀欏誨厛浜庡彸杈圭殑綰跨▼鎵ц錛岃?#8220;;”琛ㄧず涓や釜綰跨▼鍙互鍚屾椂榪涜銆傚悓鏃跺亣璁綛鐨勪竴涓搷浣滈渶瑕佽皟鐢?C鏉ュ畬鎴愶紝鏄捐屾槗瑙侊紝榪欐椂鍊欒繖涓搷浣滃氨鏄竴涓紓姝ヨ皟鐢ㄣ傛垜浠彲浠ュ湪姣忎釜“>”鐨勪綅緗瀹氫竴涓悓姝ョ偣錛岀劧鍚庨氳繃涓涓俊鍙風(fēng)伅鏉ュ畬鎴愬悓姝ャ傜嚎紼婤錛孋絳夊緟 絎竴涓俊鍙風(fēng)伅錛岃孌浼?xì)绛夊緟绗簩涓俊鍙风伅銆傝繖涓緥瀛愮殑鍔ㄥ姏鍜岃皟搴﹂兘鏄氳繃鎿嶄綔緋葷粺鐨勫熀鏈満鍒訛紙綰跨▼璋冨害鍜屽悓姝ユ満鍒訛級(jí)鏉ュ畬鎴愩?/div>

鎶婅繖涓繃紼嬫娊璞′竴涓嬶紝鍙互鎻忚堪涓猴細(xì)鑻ュ共涓富鍔ㄥ璞★紙鍖呮嫭浠g爜錛夊崗璋冩潵瀹屾垚涓欏瑰伐浣滐紝閫氳繃涓涓皟搴﹀櫒鏉ヨ皟搴︼紝瀹為檯涓婏紝榪欎釜璋冨害鍣ㄥ彲鑳藉彧鏄竴浜涜皟搴﹁鍒欍傛樉 鐒?dòng)灱寴q涚▼鎴栬呯嚎紼嬪彧瑕佽璋冨害灝辮兘鑾峰緱CPU錛屾墍浠ユ垜浠富瑕佽冭檻浠g爜錛堟瘮濡備竴涓嚱鏁幫級(jí)鎬庝箞鏍鋒墠鑳借幏寰楁墽琛屻傜敤宸ヤ綔鑰呯嚎紼嬫潵璋冪敤榪欎釜鍑芥暟鏄劇劧鏄洿瑙傚拰閫氱敤鐨勪竴 涓柟妗堛備簨瀹炰笂錛屽湪鐢ㄦ埛絀洪棿(user space)鎴栬呯敤鎴鋒?user mode)錛岃繖涓柟娉曟槸寰堝父鐢ㄧ殑銆傝屽湪鍐呮牳鎬?kernel mode)錛屽垯鍙互閫氳繃涓柇鏉ヨ幏寰桟PU錛岃繖涓氳繃娉ㄥ唽IDT鍏?鍙e拰瑙﹀彂杞腑鏂氨鍙互瀹屾垚銆傜‖浠惰澶囦笂鐨処C鏄彟涓涓姩鍔涗箣婧愩傝屼富鍔ㄥ璞$殑璋冨害錛屾渶鍩烘湰鐨勪篃鏄墠闈㈣鐨勫悇縐嶅悓姝ユ満鍒躲傚彟涓涓父鐢ㄧ殑鏈哄埗灝辨槸鍥炶皟鍑芥暟錛岄渶 瑕佹敞鎰忕殑鏄紝鍥炶皟鍑芥暟涓鑸細(xì)鍙戠敓鍦ㄨ窡璋冪敤鑰呬笉涓鏍風(fēng)殑涓婁笅鏂囷紝姣斿璇村悓涓涓繘紼嬬殑涓嶅悓綰跨▼錛岃繖涓樊鍒細(xì)甯︽潵涓浜涢檺鍒躲傚鏋滈渶瑕佸洖璋冨彂鐢熷湪璋冪敤鑰呯殑榪涚▼錛堢嚎 紼嬶級(jí)涓婁笅鏂囷紝鍒欓渶瑕佷竴浜涚被浼糢nix涓嬬殑signal鎴栬匴indows涓嬬殑APC鏈哄埗錛岃繖涓鐐規(guī)垜浠湪鍚庨潰浼?xì)鏈夋墍闃愯堪銆傞偅涔堝湪鍥炶皟鍑芥暟閲岄潰涓鑸綔浜涗粈涔堜簨鎯呭憿錛熸渶甯哥敤鐨勶紝 璺熷悓姝ユ満鍒剁粨鍚堝湪涓璧鳳紝褰撶劧灝辨槸閲婃斁涓涓簰鏂ラ攣錛屼俊鍙風(fēng)伅鎴栬匴indows Event錛圲nix鐨勬潯浠跺彉閲忥級(jí)絳夌瓑錛屼粠鑰屼嬌寰楃瓑寰呭悓姝ョ殑鍏朵粬瀵硅薄鍙互寰楀埌璋冨害鑰岄噸鏂版墽琛岋紝瀹為檯涓婏紝涔熷彲浠ョ湅浣滄槸閫氱煡璋冨害鍣紙鎿嶄綔緋葷粺錛夋煇浜涗富鍔ㄥ璞?錛堢瓑寰呭悓姝ョ殑錛夊彲浠ラ噸鏂拌璋冨害浜?jiǎn)锛屼粠鑰岃皟搴﹀櫒閲嶆柊璋冨害銆備絾鏄浜庡彟澶栦竴浜涜皟搴﹀櫒錛屽湪榪欎釜榪囩▼涓彲鑳戒笉闇瑕佸悓姝ュ璞$殑鍙備笌銆傚湪涓浜涙瀬绔竴浜涚殑渚嬪瓙閲岋紝璋冨害鐢?鑷充笉瑕佹眰涓ユ牸鏈夊簭鐨勩?/div>

鍦ㄥ疄闄呭簲鐢ㄤ腑錛屾牴鎹幆澧冪殑闄愬埗錛屽紓姝ヨ皟鐢ㄧ殑鍔ㄥ姏鍜岃皟搴︾殑瀹炵幇鏂瑰紡鍙互鏈夊緢澶у樊鍒傛垜浠細(xì)鍦ㄥ悗闈㈢殑渚嬪瓙閲屽姞浠ヨ鏄庛?鎿嶄綔緋葷粺涓殑寮傛錛歐indows鐨勫紓姝/O銆?/div>

Windows NT/2000鏄竴涓姠鍗犲紡鐨勫垎鏃舵搷浣滅郴緇熴俉indows鐨勮皟搴﹀崟浣嶆槸綰跨▼錛屽畠鐨?I/O鏋舵瀯鏄畬鍏ㄥ紓姝ョ殑錛屼篃灝辨槸璇村悓姝ョ殑I/O瀹為檯涓婇兘鍩轟簬寮傛I/O鏉ュ畬鎴愩備竴涓敤鎴鋒佺殑綰跨▼璇鋒眰涓涓狪/O鐨勬椂鍊欎細(xì)瀵艱嚧涓涓繍琛岀姸鎬佷粠user mode鍒発ernel mode鐨勮漿鍙橈紙鎿嶄綔緋葷粺鎶婂唴鏍告槧灝勫埌姣忎釜榪涚▼鐨?G-4G鐨勫湴鍧涓婏紝瀵逛簬姣忎釜榪涚▼閮芥槸涓鏍風(fēng)殑錛夈傝繖涓繃紼嬫槸閫氳繃涓柇璋冪敤鍐呮牳杈撳嚭鐨勪竴浜汼ystem Service鏉ュ畬鎴愶紝姣斿璇碦eadFile瀹為檯涓婁細(xì)鎵цNtReadFile錛圸wReadFile錛夛紝闇瑕佹敞鎰忕殑鏄紝榪愯涓婁笅鏂囦粛鐒舵槸褰撳墠綰跨▼銆?NtReadFile鐨勫疄鐜板垯鍩轟簬Windows鍐呮牳鐨勫紓姝/O妗嗘灦錛屽湪I/O Manager鐨勫崗鍔╀笅瀹屾垚銆傞渶瑕佹寚鍑虹殑鏄紝I/O Manager鍙槸鐢辮嫢騫睞PI鏋勬垚鐨勪竴涓娊璞℃蹇碉紝騫舵病鏈変竴涓湡姝g殑I/O Manager綰跨▼鍦ㄨ繍琛屻?/div>

Windows鐨処/O椹卞姩紼嬪簭鏄眰嬈″爢縐殑銆傛瘡涓┍鍔ㄧ▼搴忎細(xì)鎻愪緵涓鑷寸殑鎺ュ彛浠ヤ緵鍒濆鍖栥佹竻鐞嗗拰鍔熻兘璋冪敤銆傞┍鍔ㄧ▼搴忕殑璋冪敤鍩轟簬I/O璇鋒眰鍖咃紙I/O Request Packet, IRP錛夛紝鑰屼笉鏄儚鏅氱殑鍑芥暟璋冪敤閭f牱浣跨敤鏍堟潵浼犻掑弬鏁般傛搷浣滅郴緇熷拰PnP綆$悊鍣ㄦ牴鎹敞鍐岃〃鍦?閫傚綋鐨勬椂鏈哄垵濮嬪寲鍜屾竻鐞嗙浉搴旂殑椹卞姩紼嬪簭銆傚湪涓鑸殑鍔熻兘璋冪敤鐨勬椂鍊欙紝IRP閲岄潰浼?xì)鎸囧畾鍔熻兘璋冪敤鍙风爜浠ュ強(qiáng)鐩稿簲鐨勪笂涓嬫枃鎴栬呭弬鏁幫紙I/O stack location錛夈備竴涓┍鍔ㄧ▼搴忓彲鑳借皟鐢ㄥ埆鐨勯┍鍔ㄧ▼搴忥紝榪欎釜榪囩▼鍙兘鏄悓姝ョ殑錛堢嚎紼嬩笂涓嬫枃涓嶆敼鍙?錛屼篃鍙兘鏄紓姝ョ殑銆侼tReadFile鐨勫疄鐜幫紝澶ц嚧 鏄悜鏈涓婂眰鐨勯┍鍔ㄧ▼搴忓彂鍑轟竴涓垨澶氫釜IRP錛岀劧鍚庣瓑寰呯浉搴斾簨浠剁殑瀹屾垚錛堝悓姝ョ殑鎯呭喌錛夛紝鎴栬呯洿鎺ヨ繑鍥烇紙甯verlapped鐨勬儏鍐碉級(jí)錛岃繖浜涢兘鍦ㄥ彂璧瘋姹傜殑 綰跨▼鎵ц銆?/div>

褰撻┍鍔ㄧ▼搴忓鐞咺RP鐨勬椂鍊欙紝瀹冨彲鑳界珛鍒誨畬鎴愶紝涔熷彲鑳藉湪涓柇閲屾墠鑳藉畬鎴愶紝姣斿璇達(dá)紝寰紜歡璁懼鍙戝嚭涓涓姹傦紙閫氬父鍙互鏄啓 I/O port錛夛紝褰撹澶囧畬鎴愭搷浣滅殑鏃跺欎細(xì)瑙﹀彂涓涓腑鏂紝鐒跺悗鍦ㄤ腑鏂鐞嗗嚱鏁伴噷寰楀埌鎿嶄綔緇撴灉銆俉indows鏈変袱綾諱腑鏂紝紜歡璁懼鐨勪腑鏂拰杞腑鏂紝鍒嗘垚鑻ュ共涓笉 鍚岀殑浼樺厛綰э紙IRQL錛夈傝蔣涓柇涓昏鏈変袱縐嶏細(xì)DPC(Delayed Procedure Call)鍜孉PC(Asynchronous Procedure Call)錛岄兘澶勪簬杈冧綆鐨勪紭鍏堢駭銆傞┍鍔ㄧ▼搴忓彲浠ヤ負(fù)紜歡涓柇娉ㄥ唽ISR(Interrupt Service Routine)錛屼竴鑸氨鏄慨鏀笽DT鏌愪釜鏉$洰鐨勫叆鍙c傚悓鏍鳳紝鎿嶄綔緋葷粺涔熶細(xì)涓篋PC鍜孉PC娉ㄥ唽閫傚綋鐨勪腑鏂鐞嗕緥紼嬶紙涔熸槸鍦↖DT涓級(jí)銆?/div>

鍊煎緱鎸囧嚭鐨勬槸錛孌PC鏄窡澶勭悊鍣ㄧ浉鍏崇殑錛屾瘡涓鐞嗗櫒浼?xì)鏈変竴涓狣PC闃熷垪錛岃孉PC鏄窡綰跨▼鐩稿叧鐨勶紝姣忎釜綰跨▼浼?xì)鏈夊畠鐨凙PC闃熷垪錛堝疄闄呬笂鍖呮嫭涓涓?Kernel APC闃熷垪鍜孶ser APC闃熷垪錛屽畠浠殑璋冨害絳栫暐鏈夋墍鍖哄埆錛夛紝鍙互鎯寵薄錛孉PC騫朵笉綆椾弗鏍兼剰涔変笂鐨勪腑鏂紝鍥犱負(fù)涓柇鍙兘鍙戠敓鍦ㄤ換浣曚竴涓嚎紼嬬殑涓婁笅鏂囦腑錛屽畠琚О涓轟腑鏂紝涓昏鏄洜涓?IRQL鐨勬彁鍗囷紙浠嶱ASSIVE鍒癆PC錛夛紝APC鐨勮皟搴︿竴鑸湪綰跨▼鍒囨崲絳夌瓑鎯呭艦涓嬭繘琛屻傚綋涓柇鍙戠敓鐨勬椂鍊欙紝鎿嶄綔緋葷粺浼?xì)璋冪敤涓柇澶勭悊渚嫿E嬶紝瀵逛簬紜歡璁懼 鐨処SR錛屼竴鑸鐞嗘槸鍏寵澶囦腑鏂紝鍙戝嚭涓涓狣PC璇鋒眰錛岀劧鍚庤繑鍥炪備笉鍦ㄨ澶囩殑涓柇澶勭悊涓嬌鐢ㄥお澶氱殑CPU鏃墮棿錛屼富瑕佽冭檻鏄惁鍒欏彲鑳戒涪澶卞埆 鐨勪腑鏂傜敱浜庣‖浠惰澶囦腑鏂殑IRQL姣擠PC涓柇鐨勯珮錛屾墍浠ュ湪ISR閲岄潰DPC浼?xì)闃诲锛岀洿鍒癐SR榪斿洖IRQL鍥炲埌杈冧綆鐨勬按騫籌紝鎵嶄細(xì)瑙﹀彂DPC涓柇錛屽湪 DPC涓柇閲屾墽琛屼粠紜歡璁懼璇誨彇鏁版嵁浠ュ強(qiáng)閲嶆柊璇鋒眰銆佸紑涓柇絳夋搷浣溿侷SR鎴栬匘PC鍙兘鍦ㄤ換浣曡涓柇鐨勭嚎紼嬩笂涓嬫枃錛坅rbitrary thread context錛夋墽琛岋紝浜嬪疄涓婄嚎紼嬬殑涓婁笅鏂囨槸涓嶅彲瑙佺殑錛屽彲浠ヨ涓烘槸緋葷粺鍊熺敤涓涓嬫椂闂寸墖鑰屽凡銆?/div>

鎬葷殑鏉ヨ錛學(xué)indows鐨勫紓姝/O鏋?鏋勪腑錛屼富瑕佹湁涓ょ鍔ㄥ姏錛屼竴鏄彂璧瘋姹傜殑綰跨▼錛屼竴閮ㄥ垎鍐呮牳浠g爜浼?xì)鍦q欎釜綰跨▼涓婁笅鏂囨墽琛岋紝浜屾槸ISR鍜孌PC錛岃繖閮ㄥ垎鍐呮牳浠g爜浼?xì)鍦ㄤ腑鏂噷瀹屾垚锛屽彲鑳戒娇鐢ㄤ恢M綍涓 涓嚎紼嬬殑涓婁笅鏂囥傝岃皟搴﹀父瑙佷嬌鐢ㄥ洖璋冨拰浜嬩歡錛圞EVENT錛夛紝姣斿璇村湪寰涓嬩竴灞傜殑椹卞姩紼嬪簭鍙戝嚭璇鋒眰鐨勬椂鍊欙紝鍙互鎸囧畾涓涓畬鎴愪緥紼婥ompletion Routine錛屽綋涓嬪眰鐨勯┍鍔ㄥ畬鎴愯繖涓姹傜殑鏃跺欎細(xì)璋冪敤榪欎釜渚嬬▼錛岃屽線寰鍦ㄨ繖涓緥紼嬮噷錛屽氨鏄畝鍗曠殑瑙﹀彂涓涓嬩竴涓簨浠躲?鍙﹀鍙互欏轟究鎻愪竴涓婰inux銆侺inux 2.6涔熸湁綾諱技鐨勪腑鏂満鍒訛紝瀹冩湁鏇村鐨勮蔣涓柇浼樺厛綰э紝鍗充笉鍚屼紭鍏堢駭鐨剆oftirq錛岃岀被浼間簬DPC錛孡inux涔熸彁渚涗簡(jiǎn)涓撻棬鐨勮蔣涓柇錛屽搴擠PC鐨勫氨鏄?tasklet銆侺inux娌℃湁涓涓儚windows榪欎箞涓鑷寸殑灞傛椹卞姩紼嬪簭鏋舵瀯錛屾墍浠ュ畠鐨勫紓姝/O紼嶅井綺楃硻涓浜涳紝涓昏鏄氳繃浠ュ墠鐨勪竴浜涢樆濉炵偣錛岀幇鍦ㄧ洿 鎺ヨ繑鍥?EIOCBRETRY錛岃岃璋冪敤鑰呭湪鍚堥傜殑鏃舵満緇х畫閲嶈瘯銆傚湪榪欎釜鏂規(guī)硶涓紝鍙互璁や負(fù)鏁翠釜鎿嶄綔鐢變竴涓嚱鏁板畬鎴愶紝姣忔鎿嶄綔鏈夎繘灞曟椂錛岄兘鎶婅繖涓嚱鏁頒粠澶存墽 琛屼竴閬嶏紝褰撶劧宸茬粡瀹屾垚鐨勯儴鍒嗗氨涓嶄細(xì)鍐嶆湁瀹為檯鐨処/O銆傝繖鏍風(fēng)殑鏈澶уソ澶勬槸鍘熸湁鐨勬枃浠剁郴緇熷拰椹卞姩紼嬪簭涓嶇敤瀹屽叏閲嶅啓銆傝屽浜庡悓姝ヨ皟鐢紝鍙闃誨灝卞彲浠ヤ簡(jiǎn)錛岃繖鏍峰 緋葷粺鐨勪慨鏀硅緝?yōu)畯銆傝繖鏃跺欙紝瑕佹彁渚汸OSIX aio鐨勮涔夛紝灝卞彲鑳介渶瑕佹彁渚涗竴浜涚敤鎴風(fēng)嚎紼嬫潵瀹屾垚閲嶈瘯鐨勮繃紼嬩簡(jiǎn)錛堝洖鎯砏indows鍙互閫氳繃涓柇鍜孌PC瀹屾垚鐨勶級(jí)銆傝屽浜嶴olaris錛屼篃鏄被浼肩殑澶勭悊錛屽鏋滆澶囨敮鎸佸紓姝/O錛岄偅灝遍氳繃涓柇鍙互瀹屾垚錛屽惁鍒欏氨浣跨敤鍐呴儴鐨凩WP鏉ユā鎷熴?/div>
搴旂敤紼嬪簭錛氫竴涓紓姝ョ殑HTTP鏈嶅姟鍣ㄧ殑璁捐

鍋囪鎴戜滑瑕佽璁′竴涓狧TTP鏈嶅姟鍣紝瀹冪殑璁捐鐩爣鍖呮嫭錛氶珮騫跺彂鎬с佺簿綆 錛堥儴鍒嗘敮鎸丠TTP/1.1錛夈佹敮鎸乸lug-in緇撴瀯銆傚湪涓嶅皯鍦哄悎鍙兘閮芥湁榪欎釜闇姹傘傛諱綋涓婃潵璇達(dá)紝HTTP鏈嶅姟鍣ㄥ彲浠ョ被姣旀垚涓涓熀浜庡綰跨▼鐨勬搷浣滅郴 緇燂細(xì)OS璋冨害姣忎釜宸ヤ綔綰跨▼鍦ㄩ傚綋鐨勬椂鍊欒幏寰楁墽琛岋紝鑰屽伐浣滅嚎紼嬫彁渚涙湇鍔★紙涔熷氨鏄鐞咹TTP璇鋒眰錛夈傚湪榪欎釜鍩虹涓婏紝涓昏鐨勮冭檻灝辨槸璋冨害綺掑害鐨勫ぇ灝忥紝綺掑害澶ぇ 鐨勬椂鍊欏茍鍙戞т細(xì)闄嶄綆錛岃岀矑搴﹀お灝忓張鍙兘鍥犱負(fù)浠誨姟鍒囨崲錛堣冭檻OS鐨凜ontext Switching錛夎屽鑷存晥鐜囬檷浣庯紝鎵浠ヨ繖鍙堟槸涓涓姌琛風(fēng)殑緇撴灉銆傜被浼間簬Apache錛堜互鍙?qiáng)鍏朵粬鐨凥TTP鏈嶅姟鍣級(jí)錛屾垜浠彲浠ユ妸涓涓狧TTP澶勭悊榪囩▼鍒嗕負(fù) 鑻ュ共涓姸鎬侊紝鍩轟簬榪欎簺鐘舵佸彲浠ユ瀯閫犲嚭涓涓狧TTP澶勭悊鐨勭姸鎬佹満銆傝繖縐嶆儏鍐典笅錛屾垜浠氨鍙互鎶婃瘡涓姸鎬佺殑澶勭悊浣滀負(fù)璋冨害鐨勭矑搴︺備竴涓皟搴﹁繃紼嬪氨鏄細(xì)涓涓伐浣滅嚎紼?浠庡叏灞鐨勪換鍔¢槦鍒楅噷鍙栧嚭涓涓狧TTP_Context緇撴瀯錛涙牴鎹綋鍓嶇殑鐘舵佸畬鎴愮浉搴斿鐞嗭紱鐒跺悗鏍規(guī)嵁鐘舵佹満璁劇疆涓嬩竴涓姸鎬侊紱鍐嶆斁鍥炲埌鍏ㄥ眬鐨勪換鍔¢槦鍒楅噷銆傝繖鏍?瀛愶紝鑻ュ共涓狧TTP鐘舵佸氨鍙互閫氳繃榪欎釜璋冨害絳栫暐鏋勬垚涓涓畬鏁碒TTP澶勭悊榪囩▼銆傛樉鑰屾槗瑙侊紝涓涓姸鎬佸浜庝笅涓涓姸鎬佸鐞嗙殑璋冪敤閮藉彲浠ヨ涓烘槸寮傛鐨勩備竴涓?HTTP鐘舵佹満鐨勮璁″涓嬪浘鎵紺恒?/div>

銆銆銆銆
鍥?. HTTP鐘舵佹満

宸ヤ綔綰跨▼鐨勫嚱鏁板叾瀹炲氨鏄袱涓搷浣滐細(xì)浠庣姸鎬侀槦鍒楅噷鍙栧嚭涓涓狧TTP_Context錛岃皟鐢℉TTP_Context鐨剆ervice()鍑芥暟錛屽懆鑰屽姝ゃ?鍦ㄨ繖涓灦鏋勪笂錛屽氨寰堝鏄撳紩鍏ュ紓姝/O鍜孭lug-in鐨勬満鍒朵簡(jiǎn)銆備簨瀹炰笂鎴戜滑涔熷彲浠ヤ嬌鐢ㄥ熀浜庝簨浠訛紙渚嬪select/poll錛夌殑I/O絳栫暐鏉ユā鎷熷紓姝 /O錛屽疄鐜頒腑浣跨敤涓涓敤鎴風(fēng)嚎紼嬪氨鍙互浜?jiǎn)銆?/div>

瀵逛簬寮傛I/O鍜孭lug-in鐨勮皟鐢紝鎴戜滑涔熸槸閲囩敤綾諱技浜嶭inux 2.6閲岄潰aio鐨勯噸璇曟柟妗堬紝鑰屽紓姝ュ畬鎴愮殑鏃跺欓噰鐢ㄥ洖璋冨嚱鏁般傚湪鏌愪釜鐘舵佷笂錛屽鏋滅郴緇熼渶瑕両/O鎿嶄綔錛坮ecv鎴栬卻end錛夛紝鍒欎細(xì)璇鋒眰涓涓紓姝 /O錛堟搷浣滅郴緇熸彁渚涚殑寮傛I/O鎴栬呯敱鐢ㄦ埛綰跨▼妯℃嫙鐨勫紓姝/O錛夛紝榪欐椂鍊欑浉搴旂殑HTTP_Context涓嶄細(xì)閲嶆柊鍥炲埌鐘舵侀槦鍒楅噷錛岃屽湪I/O瀹屾垚鐨勫洖璋?鍑芥暟閲岄潰鎵嶄細(xì)閲嶆柊鏀懼洖鍒扮姸鎬侀槦鍒楋紝寰楀埌閲嶆柊璋冨害鐨勬満浼?xì)銆侶TTP_Context寰楀埌閲嶆柊璋冨害鐨勬椂鍊欎細(xì)媯(gè)鏌/O鐘舵侊紙榪欎釜鍙互閫氳繃涓浜涙爣蹇椾綅鏉ュ畬鎴愶級(jí)錛?濡傛灉宸茬粡瀹屾垚錛屽垯澶勭悊鐒跺悗璁劇疆涓嬩竴鐘舵侊紝閲嶆柊璋冨害錛屽惁鍒欏彲浠ラ噸鏂拌姹備竴涓柊鐨処/O璇鋒眰銆侾lug-in涔熷彲浠ヤ嬌鐢ㄧ被浼肩殑鏂規(guī)錛屾瘮濡傝涓涓狿lug-in 瑕佽窡澶栭儴鐨勪竴涓湇鍔″櫒閫氫俊錛岃繖鏃跺欏氨鍙互鍦ㄩ氫俊瀹屾垚鐨勬椂鍊欐墠鎶奌TTP_Context閲嶆柊鏀懼洖鍒扮姸鎬侀槦鍒椼傛樉鐒?dòng)灱孭lug-in璺烪TTP鐘舵佹槸澶氬澶?鐨勫叧緋伙紝涓涓狿lug-in鍙互鍦ㄨ嫢騫蹭釜鍏沖績(jī)鐨勭姸鎬佹敞鍐岃嚜韜紝鍚屾椂榪樺彲浠ヨ緗竴浜泂hort-path鏉ユ彁楂樺鐞嗙殑鏁堢巼銆?/div>

緇撹

鎬葷殑鏉ヨ錛屽紓姝ヨ皟鐢ㄧ殑璁捐鍜屽簲鐢ㄥ綊鏍圭粨搴曞氨鏄澶氫釜涓誨姩瀵硅薄鐨勭鐞嗛棶棰橈細(xì)濡備綍鎻愪緵鎵ц鐨勫姩鍔涗互鍙?qiáng)濡備綍淇濊瘉鎵ц鐨剻搴搴忛昏緫銆備富瑕佽冭檻鐨勯棶棰樻槸涓誨姩瀵硅薄鐨勭矑 搴︿互鍙?qiáng)鎵ц鏂瑰紡锛屽悓姝ユ垨鑰呭洖璋冩潵瀹屾垚欏哄簭鐨勮皟搴︼紝鎴栬呬嬌鐢ㄨ繎浼肩殑璋冨害鑰屽姞涓浜涢瞾媯掔殑閿欒澶勭悊鏈哄埗鏉ヤ繚璇佽涔夌殑姝g‘銆傚悗鑰呭彲浠ヨ冭檻鍦ㄤ嬌鐢ㄥ熀浜庝簨浠剁殑 socket鐨勬椂鍊欙紝readable浜嬩歡鐨勯氱煡鍙互鏄啑浣欑殑錛屾垨鑰呰鍙互姣斿疄闄呬腑鍙戠敓鐨剅eadable浜嬩歡鏇村錛岃繖涓椂鍊欎嬌鐢ㄩ潪闃誨鐨剆ocket, 鏈変簺read()錛堟垨鑰卹ecv()錛変細(xì)鐩存帴榪斿洖EWOULDBLOCK錛岀郴緇熷彧瑕佽冭檻澶勭悊榪欑鎯呭喌錛堜嬌鐢╪on blocking socket鑰屼笉鏄痓locking socket錛夛紝褰撲緥澶栫殑鎯呭喌涓嶅鐨勬椂鍊欐槸鍙互鎺ュ彈鐨勩傝繖鏃跺欏彲浠ヨ浜嬩歡鐨勬姤鍛婂氨鍙槸榪戜技鐨勩?/div>
from:



chatler 2010-09-06 17:33 鍙戣〃璇勮
]]>semaphore and spinlockhttp://www.shnenglu.com/beautykingdom/archive/2010/04/01/111257.htmlchatlerchatlerThu, 01 Apr 2010 03:50:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2010/04/01/111257.htmlhttp://www.shnenglu.com/beautykingdom/comments/111257.htmlhttp://www.shnenglu.com/beautykingdom/archive/2010/04/01/111257.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/111257.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/111257.html鍐呮牳鍚屾鎺柦

    涓轟簡(jiǎn)閬垮厤騫跺彂錛岄槻姝㈢珵浜夈傚唴鏍告彁渚涗簡(jiǎn)涓緇勫悓姝ユ柟娉曟潵鎻愪緵瀵瑰叡浜暟鎹殑淇濇姢銆? 鎴戜滑鐨勯噸鐐逛笉鏄粙緇嶈繖浜涙柟娉曠殑璇︾粏鐢ㄦ硶錛岃屾槸寮鴻皟涓轟粈涔堜嬌鐢ㄨ繖浜涙柟娉曞拰瀹冧滑涔嬮棿鐨勫樊鍒?br>     Linux 浣跨敤鐨勫悓姝ユ満鍒跺彲浠ヨ浠?.0鍒?.6浠ユ潵涓嶆柇鍙戝睍瀹屽杽銆備粠鏈鍒濈殑鍘熷瓙鎿嶄綔錛屽埌鍚庢潵鐨勪俊鍙烽噺錛屼粠澶у唴鏍擱攣鍒頒粖澶╃殑鑷棆閿併傝繖浜涘悓姝ユ満鍒剁殑鍙戝睍浼撮殢 Linux浠庡崟澶勭悊鍣ㄥ埌瀵圭О澶氬鐞嗗櫒鐨勮繃搴︼紱浼撮殢鐫浠庨潪鎶㈠崰鍐呮牳鍒版姠鍗犲唴鏍哥殑榪囧害銆傞攣鏈哄埗瓚婃潵瓚婃湁鏁堬紝涔熻秺鏉ヨ秺澶嶆潅銆?br>     鐩墠鏉ヨ鍐呮牳涓師瀛愭搷浣滃鐢ㄦ潵鍋氳鏁頒嬌鐢紝鍏跺畠鎯呭喌鏈甯哥敤鐨勬槸涓ょ閿佷互鍙?qiáng)瀹冧滑鐨勫彉绉?涓涓槸鑷棆閿侊紝鍙︿竴涓槸淇″彿閲忋傛垜浠笅闈㈠氨鏉ョ潃閲嶄粙緇嶄竴涓嬭繖涓ょ閿佹満 鍒躲?br>

鑷棆閿?br> ------------------------------------------------------
    鑷棆閿佹槸涓撲負(fù)闃叉澶氬鐞嗗櫒騫跺彂鑰屽紩鍏ョ殑涓縐嶉攣錛屽畠鍦ㄥ唴鏍鎬腑澶ч噺搴旂敤浜庝腑鏂鐞嗙瓑閮ㄥ垎(瀵逛簬鍗曞鐞嗗櫒鏉ヨ錛岄槻姝腑鏂鐞嗕腑鐨勫茍鍙戝彲綆鍗曢噰鐢ㄥ叧闂腑鏂殑鏂瑰紡錛? 涓嶉渶瑕佽嚜鏃嬮攣)銆?br>     鑷棆閿佹渶澶氬彧鑳借涓涓唴鏍鎬換鍔℃寔鏈夛紝濡傛灉涓涓唴鏍鎬換鍔¤瘯鍥捐姹備竴涓凡琚簤鐢?宸茬粡琚寔鏈?鐨勮嚜鏃嬮攣錛岄偅涔堣繖涓換鍔″氨浼?xì)涓鐩磋繘琛屽繖寰幆鈥斺旀棆杞斺旂瓑寰呴攣閲? 鏂板彲鐢ㄣ?/strong>瑕佹槸閿佹湭琚簤鐢紝璇鋒眰瀹冪殑鍐呮牳浠誨姟渚胯兘绔嬪埢寰楀埌瀹冨茍涓旂戶緇繘琛屻傝嚜鏃嬮攣鍙互鍦ㄤ換浣曟椂鍒婚槻姝㈠浜庝竴涓殑鍐呮牳浠誨姟鍚? 鏃惰繘鍏ヤ復(fù)鐣屽尯錛屽洜姝よ繖縐嶉攣鍙湁鏁堝湴閬垮厤澶氬鐞嗗櫒涓婂茍鍙戣繍琛岀殑鍐呮牳浠誨姟绔炰簤鍏變韓璧勬簮銆?br>     浜嬪疄涓婏紝鑷棆閿佺殑鍒濊》灝辨槸錛氬湪鐭湡闂村唴榪涜杞婚噺綰х殑閿佸畾銆備竴涓浜夌敤鐨勮嚜鏃嬮攣浣垮緱璇鋒眰瀹冪殑綰跨▼鍦ㄧ瓑寰呴攣閲嶆柊鍙敤鐨勬湡闂磋繘琛岃嚜鏃?鐗瑰埆嫻垂澶勭悊鍣ㄦ椂闂?錛屾墍 浠ヨ嚜鏃嬮攣涓嶅簲璇ヨ鎸佹湁鏃墮棿榪囬暱銆傚鏋滈渶瑕侀暱鏃墮棿閿佸畾鐨勮瘽, 鏈濂戒嬌鐢ㄤ俊鍙烽噺銆?br> 鑷棆閿佺殑鍩烘湰褰㈠紡濡備笅錛?br>     spin_lock(&mr_lock);
    //涓寸晫鍖?br>     spin_unlock(&mr_lock);

    鍥犱負(fù)鑷棆閿佸湪鍚屼竴鏃跺埢鍙兘琚渶澶氫竴涓唴鏍鎬換鍔℃寔鏈夛紝鎵浠ヤ竴涓椂鍒誨彧鏈変竴涓嚎紼嬪厑璁稿瓨鍦ㄤ簬涓寸晫鍖轟腑銆傝繖鐐瑰緢濂藉湴婊¤凍浜?jiǎn)瀵箍U板澶勭悊鏈哄櫒闇瑕佺殑閿佸畾鏈嶅姟銆傚湪鍗曞 鐞嗗櫒涓婏紝鑷棆閿佷粎浠呭綋浣滀竴涓緗唴鏍告姠鍗犵殑寮鍏熾傚鏋滃唴鏍告姠鍗犱篃涓嶅瓨鍦紝閭d箞鑷棆閿佷細(xì)鍦ㄧ紪璇戞椂琚畬鍏ㄥ墧闄ゅ嚭鍐呮牳銆?br>     綆鍗曠殑璇達(dá)紝鑷棆閿佸湪鍐呮牳涓富瑕佺敤鏉ラ槻姝㈠澶勭悊鍣ㄤ腑騫跺彂璁塊棶涓寸晫鍖猴紝闃叉 鍐呮牳鎶㈠崰閫犳垚鐨勭珵浜夈?/strong>鍙﹀鑷棆閿佷笉鍏佽浠誨姟鐫$湢(鎸佹湁鑷棆閿佺殑浠誨姟鐫$湢浼?xì)閫犳垚鑷閿佲斺斿洜涓虹潯鐪犳湁鍙兘閫犳垚鎸佹湁閿佺殑鍐呮牳 浠誨姟琚噸鏂拌皟搴︼紝鑰屽啀嬈$敵璇瘋嚜宸卞凡鎸佹湁鐨勯攣)錛?font color="#800000">瀹冭兘澶熷湪涓柇涓婁笅鏂囦腑浣跨敤銆?br>     姝婚攣錛氬亣璁炬湁涓涓垨澶氫釜鍐呮牳浠誨姟鍜屼竴涓垨澶氫釜璧勬簮錛屾瘡涓唴鏍擱兘鍦ㄧ瓑寰呭叾涓殑涓涓祫婧愶紝浣嗘墍鏈夌殑璧勬簮閮藉凡緇忚鍗犵敤浜?jiǎn)銆傝繖渚夸細(xì)鍙戠敓鎵鏈夊唴鏍鎬換鍔¢兘鍦ㄧ浉浜掔瓑寰咃紝 浣嗗畠浠案榪滀笉浼?xì)閲婃攽址簿l忓崰鏈夌殑璧勬簮錛屼簬鏄換浣曞唴鏍鎬換鍔¢兘鏃犳硶鑾峰緱鎵闇瑕佺殑璧勬簮錛屾棤娉曠戶緇繍琛岋紝榪欎究鎰忓懗鐫姝婚攣鍙戠敓浜?jiǎn)銆傝嚜姝葷悙鏄鑷繁鍗犳湁浜?jiǎn)鏌愪釜璧勬簮锛岀劧鍚? 鑷繁鍙堢敵璇瘋嚜宸卞凡鍗犳湁鐨勮祫婧愶紝鏄劇劧涓嶅彲鑳藉啀鑾峰緱璇ヨ祫婧愶紝鍥犳灝辮嚜緙氭墜鑴氫簡(jiǎn)銆?br>

淇″彿閲?br> ------------------------------------------------------
    Linux涓殑淇″彿閲忔槸涓縐嶇潯鐪犻攣銆傚鏋滄湁涓涓換鍔¤瘯鍥捐幏寰椾竴涓凡琚寔鏈夌殑淇″彿閲忔椂錛屼俊鍙烽噺浼?xì)灏嗗叾鎺ㄥ叆绛夊緟闃熷垪锛岀劧鍚庤鍏剁潯鐪犮傝繖鏃跺鐞嗗櫒鑾峰緱鑷敱鍘繪墽琛? 鍏跺畠浠g爜銆傚綋鎸佹湁淇″彿閲忕殑榪涚▼灝嗕俊鍙烽噺閲婃斁鍚庯紝鍦ㄧ瓑寰呴槦鍒椾腑鐨勪竴涓換鍔″皢琚敜閱掞紝浠庤屼究鍙互鑾峰緱榪欎釜淇″彿閲忋?br>     淇″彿閲忕殑鐫$湢鐗規(guī)э紝浣垮緱淇″彿閲忛傜敤浜庨攣浼?xì)琚暱鏃堕棿鎸佹湁鐨勬儏鍐靛Q涘彧鑳藉湪榪涚▼涓婁笅鏂囦腑浣跨敤錛屽洜涓轟腑鏂笂涓嬫枃涓槸涓嶈兘琚皟搴︾殑錛涘彟澶栧綋浠g爜鎸佹湁淇″彿閲忔椂錛屼笉鍙互 鍐嶆寔鏈夎嚜鏃嬮攣銆?br>
淇″彿閲忓熀鏈嬌鐢ㄥ艦寮忎負(fù)錛?br> static DECLARE_MUTEX(mr_sem);//澹版槑浜掓枼淇″彿閲?br> if(down_interruptible(&mr_sem))
    //鍙涓柇鐨勭潯鐪狅紝褰撲俊鍙鋒潵鍒幫紝鐫$湢鐨勪換鍔¤鍞ら啋
    //涓寸晫鍖?br> up(&mr_sem);


淇″彿閲忓拰鑷棆閿佸尯鍒?br> ------------------------------------------------------
    铏界劧鍚搗鏉ヤ袱鑰呬箣闂寸殑浣跨敤鏉′歡澶嶆潅錛屽叾瀹炲湪瀹為檯浣跨敤涓俊鍙烽噺鍜岃嚜鏃嬮攣騫朵笉鏄撴販娣嗐傛敞鎰忎互涓嬪師鍒?
    濡傛灉浠g爜闇瑕佺潯鐪犫斺旇繖寰寰鏄彂鐢熷湪鍜岀敤鎴風(fēng)┖闂村悓姝ユ椂鈥斺斾嬌鐢ㄤ俊鍙烽噺鏄敮涓鐨勯夋嫨銆傜敱浜庝笉鍙楃潯鐪犵殑闄愬埗錛屼嬌鐢ㄤ俊鍙烽噺閫氬父鏉ヨ鏇村姞綆鍗曚竴浜涖傚鏋滈渶瑕佸湪鑷棆 閿佸拰淇″彿閲忎腑浣滈夋嫨錛屽簲璇ュ彇鍐充簬閿佽鎸佹湁鐨勬椂闂撮暱鐭傜悊鎯蟲儏鍐墊槸鎵鏈夌殑閿侀兘搴旇灝藉彲鑳界煭鐨勮鎸佹湁錛屼絾鏄鏋滈攣鐨勬寔鏈夋椂闂磋緝闀跨殑璇濓紝浣跨敤淇″彿閲忔槸鏇村ソ鐨勯? 鎷┿傚彟澶栵紝淇″彿閲忎笉鍚屼簬鑷棆閿侊紝瀹冧笉浼?xì)鍏抽棴鍐呮牳鎶㈠崰锛屾墍浠ユ寔鏈変俊鍙烽噺鐨勪唬鐮佸彲浠ヨ鎶㈠崰銆傝繖鎰忓懗鑰呬俊鍙烽噺涓嶄細(xì)瀵瑰獎(jiǎng)鍝嶈皟搴﹀弽搴旀椂闂村甫鏉ヨ礋闈㈠獎(jiǎng)鍝嶃?br>

鑷棆閿佸淇″彿閲?br> ------------------------------------------------------
闇姹?                    寤鴻鐨勫姞閿佹柟娉?br>
浣庡紑閿鍔犻攣               浼樺厛浣跨敤鑷棆閿?br> 鐭湡閿佸畾                 浼樺厛浣跨敤鑷棆閿?br> 闀挎湡鍔犻攣                 浼樺厛浣跨敤淇″彿閲?br> 涓柇涓婁笅鏂囦腑鍔犻攣          浣跨敤鑷棆閿?br> 鎸佹湁閿佹槸闇瑕佺潯鐪犮佽皟搴?nbsp;    浣跨敤淇″彿閲?br>
from:
http://blog.chinaunix.net/u1/38576/showart_367985.html



chatler 2010-04-01 11:50 鍙戣〃璇勮
]]>
An In-Depth Look into the Win32 Portable Executable File Formathttp://www.shnenglu.com/beautykingdom/archive/2010/03/25/110504.htmlchatlerchatlerThu, 25 Mar 2010 07:17:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2010/03/25/110504.htmlhttp://www.shnenglu.com/beautykingdom/comments/110504.htmlhttp://www.shnenglu.com/beautykingdom/archive/2010/03/25/110504.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/110504.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/110504.html闃呰鍏ㄦ枃

chatler 2010-03-25 15:17 鍙戣〃璇勮
]]>
IA-32 淇濇姢妯″紡鍐呭瓨綆$悊http://www.shnenglu.com/beautykingdom/archive/2010/03/25/110503.htmlchatlerchatlerThu, 25 Mar 2010 07:03:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2010/03/25/110503.htmlhttp://www.shnenglu.com/beautykingdom/comments/110503.htmlhttp://www.shnenglu.com/beautykingdom/archive/2010/03/25/110503.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/110503.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/110503.html CHAPTER 3 PROTECTED-MODE MEMORY MANAGEMENT

3.1. MEMORY MANAGEMENT OVERVIEW

The memory management facilities of the IA-32 architecture are divided into two parts: segmentation and paging. Segmentation provides a mechanism of isolating individual code, data, and stack modules so that multiple programs (or tasks) can run on the same processor without interfering with one another. Paging provides a mechanism for implementing a conventional demand-paged, virtual-memory system where sections of a program’s execution environment are mapped into physical memory as needed. Paging can also be used to provide isolation between multiple tasks. When operating in protected mode, some form of segmentation must be used. There is no mode bit to disable segmentation. The use of paging, however, is optional.

IA-32鏋舵瀯鐨勫唴瀛樼鐞嗘満鏋勶紙facilities錛夊彲鍒掑垎涓轟袱涓儴鍒嗭細(xì)鍒嗘錛坰egmentation錛夊拰鍒嗛〉錛坧aging錛夈傚垎孌靛姛鑳芥彁渚涗簡(jiǎn)鍒嗛殧 浠g爜銆佹暟鎹拰鍫嗘爤鐨勬満鍒訛紝浠庤屼嬌澶氫釜榪涚▼榪愯鍦ㄥ悓涓涓狢PU鐗╃悊鍦板潃絀洪棿鍐呰屼簰涓嶅獎(jiǎng)鍝嶏紱鍒嗛〉鍙敤鏉ュ疄鐜頒竴縐?#8220;璇鋒眰欏靛紡錛坉emand-paged錛?#8221;鐨勮櫄 鎷熷唴瀛樻満鍒訛紝浠庤岄〉鍖栫▼搴忔墽琛岀幆澧冿紝鍦ㄧ▼搴忚繍琛屾椂鍙皢鎵闇瑕佺殑欏墊槧灝勫埌鐗╃悊鍐呭瓨銆傚垎欏墊満鍒朵篃鍙敤浣滈殧紱誨榪涚▼浠誨姟銆傚垎孌靛姛鑳芥槸CPU淇濇姢妯″紡蹇呴』鐨勶紝娌℃湁 璁劇疆浣嶅彲浠ュ睆钄藉唴瀛樺垎孌碉紱涓嶈繃鍐呭瓨鍒嗛〉鍒欐槸鍙夌殑銆?/p>

These two mechanisms (segmentation and paging) can be configured to support simple single-program (or single-task) systems, multitasking systems, or multiple-processor systems that used shared memory.

As shown in Figure 3-1, segmentation provides a mechanism for dividing the processor’s addressable memory space (called the linear address space) into smaller protected address spaces called segments. Segments can be used to hold the code, data, and stack for a program or to hold system data structures (such as a TSS or LDT). If more than one program (or task) is running on a processor, each program can be assigned its own set of segments. The processor then enforces the boundaries between these segments and insures that one program does not interfere with the execution of another program by writing into the other program’s segments.

鍒嗘鍜屽垎欏墊満鍒惰閰嶇疆鎴愭敮鎸佸崟浠誨姟緋葷粺銆佸浠誨姟緋葷粺鎴栧澶勭悊鍣ㄧ郴緇熴?/span>

濡傚浘3-1錛屽唴瀛樺垎孌靛皢CPU鐨勫彲瀵誨潃絀洪棿錛堢О涓虹嚎鎬у湴鍧絀洪棿錛夊垝鍒嗘洿灝忕殑鍙椾繚鎶ょ殑鍐呭瓨孌碉紝榪欎簺孌靛瓨鏀劇▼搴忕殑鏁版嵁錛堜唬鐮併佹暟鎹拰鍫嗘爤錛夊拰緋葷粺鐨勬暟鎹粨鏋? 錛堝儚TSS 鎴?LDT錛夈傚鏋滃鐞嗗櫒榪愯鐫澶氫釜浠誨姟錛岄偅涔堟瘡涓換鍔¢兘鏈変竴闆嗚嚜宸辯嫭绔嬬殑鍐呭瓨孌點(diǎn)?/p>

The segmentation mechanism also allows typing of segments so that the operations that may be performed on a particular type of segment can be restricted.

All the segments in a system are contained in the processor’s linear address space. To locate a byte in a particular segment, a logical address (also called a far pointer) must be provided. A logical address consists of a segment selector and an offset. The segment selector is a unique identifier for a segment. Among other things it provides an offset into a descriptor table (such as the global descriptor table, GDT) to a data structure called a segment descriptor. Each segment has a segment descriptor, which specifies the size of the segment, the access rights and privilege level for the segment, the segment type, and the location of the first byte of the segment in the linear address space (called the base address of the segment). The offset part of the logical address is added to the base address for the segment to locate a byte within the segment. The base address plus the offset thus forms a linear address in the processor’s linear address space.

榪涚▼鐨勫悇涓閮藉繀欏諱綅浜嶤PU鐨勭嚎鎬х┖闂翠箣鍐咃紝榪涚▼瑕佽闂煇孌電殑涓涓瓧鑺傦紝蹇呴』緇欏嚭璇ュ瓧鑺? 鐨勯昏緫鍦板潃錛堜篃鍙繙鎸囬拡錛夈傞昏緫鍦板潃鐢辨閫夋嫨瀛愶紙segment selector 錛夊拰鍋忕Щ鍊肩粍鎴愩傛閫夋嫨瀛愭槸孌電殑鍞竴鏍囪瘑錛屾寚鍚戜竴涓彨孌墊弿榪扮鐨勬暟鎹粨鏋勶紱孌墊弿榪扮浣嶄簬涓涓彨鎻忚堪琛ㄤ箣鍐咃紙濡傚叏灞鎻忚堪琛℅DT錛? 姣忎釜孌靛繀欏婚兘鏈夌浉搴旂殑孌墊弿榪扮錛岀敤浠ユ寚瀹氭澶у皬銆佽闂潈闄愬拰孌電殑鐗規(guī)潈綰у埆錛坧rivilege level錛夈佹綾誨瀷鍜屾鐨勯鍦板潃鍦ㄧ嚎鎬у湴鍧絀洪棿鐨勪綅緗紙鍙鐨勫熀鍦板潃錛夈傞昏緫鍦板潃閫氳繃鍩哄湴鍧鍔犱笂孌靛唴鍋忕Щ寰楀埌銆?/span>

If paging is not used, the linear address space of the processor is mapped directly into the physical address space of processor. The physical address space is defined as the range of addresses that the processor can generate on its address bus.

Because multitasking computing systems commonly define a linear address space much larger than it is economically feasible to contain all at once in physical memory, some method of “virtualizing” the linear address space is needed. This virtualization of the linear address space is handled through the processor’s paging mechanism.

濡傛灉涓嶇敤鍒嗛〉鍔熻兘錛屽鐞嗗櫒鐨刐綰挎у湴鍧絀洪棿]灝變細(xì)鐩存帴鏄犲皠鍒癧鐗╃悊鍦板潃絀洪棿]銆俒鐗╃悊鍦板潃絀洪棿]鐨勫ぇ灝忓氨鏄鐞嗗櫒鑳介氳繃鍦板潃鎬葷嚎浜х敓鐨勫湴鍧鑼冨洿銆備負(fù)浜?jiǎn)鐩存? 浣跨敤綰挎у湴鍧絀洪棿浠庤岀畝鍖栫紪紼嬪拰瀹炵幇澶氳繘紼嬭屾彁楂樺唴瀛樼殑鍒╃敤鐜囷紝闇瑕佸疄鐜版煇縐嶅綰挎у湴鍧絀洪棿榪涜“铏氭嫙鍖栵紙virtualizing錛?#8221;錛孋PU鐨勫垎欏墊満 鍒跺疄鐜頒簡(jiǎn)榪欑铏氭嫙鍖栥?/p>

Paging supports a “virtual memory” environment where a large linear address space is simulated with a small amount of physical memory (RAM and ROM) and some disk storage. When using paging, each segment is divided into pages (typically 4 KBytes each in size), which are stored either in physical memory or on the disk. The operating system or executive maintains a page directory and a set of page tables to keep track of the pages. When a program (or task) attempts to access an address location in the linear address space, the processor uses the page directory and page tables to translate the linear address into a physical address and then performs the requested operation (read or write) on the memory location. If the page being accessed is not currently in physical memory, the processor interrupts execution of the program (by generating a page-fault exception). The operating system or executive then reads the page into physical memory from the disk and continues executing the program.

“铏氭嫙鍐呭瓨”灝辨槸鍒╃敤鐗╃悊鍐呭瓨鍜岀鐩樻潵瀵笴PU鐨勭嚎鎬у湴鍧榪涜妯℃嫙錛坘emin:楂樼駭璇█婧愮爜鎸囧畾鐨勬槸絎﹀彿鍦板潃錛屾槸铏氱殑錛屾湁浜?jiǎn)铏氭嫙鍐呭瓨鍗充究鏄敤姹嚲~栨寚瀹氫竴 鍥哄畾鍦板潃涔熸槸铏氱殑銆傞棶棰樻槸榪欎簺铏氬瓨鏄庝箞綆$悊鐨勶級(jí)銆傚綋浣跨敤鍒嗛〉鏃訛紝榪涚▼鐨勬瘡涓閮戒細(xì)琚垎鎴愬ぇ灝忓浐瀹氱殑欏碉紝榪欎簺欏靛彲鑳藉湪鍐呭瓨涓紝涔熷彲鑳藉湪紓佺洏銆傛搷浣滅郴緇熺敤浜? 涓寮犻〉鐩綍錛坧age directory錛夊拰澶氬紶欏佃〃鏉ョ鐞嗚繖浜涢〉銆傚綋榪涚▼璇曞浘璁塊棶綰挎у湴鍧絀洪棿鐨勬煇涓綅緗紝澶勭悊鍣ㄤ細(xì)閫氳繃欏電洰褰曞拰欏佃〃鍏堝皢綰挎у湴鍧杞崲鎴愮墿鐞嗗湴鍧錛岀劧鍚庡啀璁塊棶 錛堣鎴栧啓錛夛紙kemin錛氳漿鎹㈢粏鑺傛病鏈夎錛夈傚鏋滆璁塊棶鐨勯〉褰撳墠涓嶅湪鍐呭瓨錛屽鐞嗗氨浼?xì)涓柇杩浗E嬬殑榪愯錛堥氳繃浜х敓緙洪〉寮傚父涓柇錛夛紙kemin:鎬庝箞鍒ゆ柇鏌愰〉涓? 鍦ㄥ唴瀛橈紵錛夈傛搷浣滅郴緇熻礋璐d粠紓佺洏璇誨叆璇ラ〉騫剁戶緇墽琛岃榪涚▼錛坘emin:欏佃鍏ョ殑鍓嶅墠鍚庡悗娌℃湁璁詫級(jí)銆?/p>

When paging is implemented properly in the operating-system or executive, the swapping of pages between physical memory and the disk is transparent to the correct execution of a program. Even programs written for 16-bit IA-32 processors can be paged (transparently) when they are run in virtual-8086 mode.


from:

http://blog.csdn.net/keminlau/archive/2008/10/19/3090337.aspx



chatler 2010-03-25 15:03 鍙戣〃璇勮
]]>
Context Switch Definitionhttp://www.shnenglu.com/beautykingdom/archive/2010/02/25/108457.htmlchatlerchatlerThu, 25 Feb 2010 15:09:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2010/02/25/108457.htmlhttp://www.shnenglu.com/beautykingdom/comments/108457.htmlhttp://www.shnenglu.com/beautykingdom/archive/2010/02/25/108457.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/108457.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/108457.htmlA context switch (also sometimes referred to as a process switch or a task switch ) is the switching of the CPU (central processing unit) from one process or thread to another.

A process (also sometimes referred to as a task ) is an executing (i.e., running) instance of a program. In Linux, threads are lightweight processes that can run in parallel and share an address space (i.e., a range of memory locations) and other resources with their parent processes (i.e., the processes that created them).

A context is the contents of a CPU's registers and program counter at any point in time. A register is a small amount of very fast memory inside of a CPU (as opposed to the slower RAM main memory outside of the CPU) that is used to speed the execution of computer programs by providing quick access to commonly used values, generally those in the midst of a calculation. A program counter is a specialized register that indicates the position of the CPU in its instruction sequence and which holds either the address of the instruction being executed or the address of the next instruction to be executed, depending on the specific system.

Context switching can be described in slightly more detail as the kernel operating system) performing the following activities with regard to processes (including threads) on the CPU: (1) suspending the progression of one process and storing the CPU's state (i.e., the context) for that process somewhere in memory, (2) retrieving the context of the next process from memory and restoring it in the CPU's registers and (3) returning to the location indicated by the program counter (i.e., returning to the line of code at which the process was interrupted) in order to resume the process. (i.e., the core of the

A context switch is sometimes described as the kernel suspending execution of one process on the CPU and resuming execution of some other process that had previously been suspended. Although this wording can help clarify the concept, it can be confusing in itself because a process is , by definition, an executing instance of a program. Thus the wording suspending progression of a process might be preferable.

Context Switches and Mode Switches

Context switches can occur only in kernel mode . Kernel mode is a privileged mode of the CPU in which only the kernel runs and which provides access to all memory locations and all other system resources. Other programs, including applications, initially operate in user mode , but they can run portions of the kernel code via system calls . A system call is a request in a Unix-like operating system by an active process (i.e., a process currently progressing in the CPU) for a service performed by the kernel, such as input/output (I/O) or process creation (i.e., creation of a new process). I/O can be defined as any movement of information to or from the combination of the CPU and main memory (i.e. RAM), that is, communication between this combination and the computer's users (e.g., via the keyboard or mouse), its storage devices (e.g., disk or tape drives), or other computers.

The existence of these two modes in Unix-like operating systems means that a similar, but simpler, operation is necessary when a system call causes the CPU to shift to kernel mode. This is referred to as a mode switch rather than a context switch, because it does not change the current process.

Context switching is an essential feature of multitasking operating systems. A multitasking operating system is one in which multiple processes execute on a single CPU seemingly simultaneously and without interfering with each other. This illusion of concurrency is achieved by means of context switches that are occurring in rapid succession (tens or hundreds of times per second). These context switches occur as a result of processes voluntarily relinquishing their time in the CPU or as a result of the scheduler making the switch when a process has used up its CPU time slice .

A context switch can also occur as a result of a hardware interrupt , which is a signal from a hardware device (such as a keyboard, mouse, modem or system clock) to the kernel that an event (e.g., a key press, mouse movement or arrival of data from a network connection) has occurred.

Intel 80386 and higher CPUs contain hardware support for context switches. However, most modern operating systems perform software context switching , which can be used on any CPU, rather than hardware context switching in an attempt to obtain improved performance. Software context switching was first implemented in Linux for Intel-compatible processors with the 2.4 kernel.

One major advantage claimed for software context switching is that, whereas the hardware mechanism saves almost all of the CPU state, software can be more selective and save only that portion that actually needs to be saved and reloaded. However, there is some question as to how important this really is in increasing the efficiency of context switching. Its advocates also claim that software context switching allows for the possibility of improving the switching code, thereby further enhancing efficiency, and that it permits better control over the validity of the data that is being loaded.

The Cost of Context Switching

Context switching is generally computationally intensive. That is, it requires considerable processor time, which can be on the order of nanoseconds for each of the tens or hundreds of switches per second. Thus, context switching represents a substantial cost to the system in terms of CPU time and can, in fact, be the most costly operation on an operating system.

Consequently, a major focus in the design of operating systems has been to avoid unnecessary context switching to the extent possible. However, this has not been easy to accomplish in practice. In fact, although the cost of context switching has been declining when measured in terms of the absolute amount of CPU time consumed, this appears to be due mainly to increases in CPU clock speeds rather than to improvements in the efficiency of context switching itself.

One of the many advantages claimed for Linux as compared with other operating systems, including some other Unix-like systems, is its extremely low cost of context switching and mode switching.


from:
http://blog.csdn.net/wave_1102/archive/2007/09/04/1771745.aspx



chatler 2010-02-25 23:09 鍙戣〃璇勮
]]>
鍫嗗拰鏍堢殑鍖哄埆http://www.shnenglu.com/beautykingdom/archive/2009/12/11/103023.htmlchatlerchatlerFri, 11 Dec 2009 15:53:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/12/11/103023.htmlhttp://www.shnenglu.com/beautykingdom/comments/103023.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/12/11/103023.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/103023.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/103023.html鍫嗭細(xì)銆鏄ぇ瀹跺叡鏈夌殑絀洪棿錛屽垎鍏ㄥ眬鍫嗗拰灞閮ㄥ爢銆傚叏灞鍫嗗氨鏄墍鏈夋病鏈夊垎閰嶇殑絀洪棿錛屽眬閮ㄥ爢灝辨槸鐢ㄦ埛鍒嗛厤鐨勭┖闂淬傚爢鍦ㄦ搷浣滅郴緇熷榪涚▼鍒濆鍖栫殑鏃跺欏垎閰嶏紝榪愯榪囩▼涓篃鍙互鍚戠郴緇熻棰濆鐨勫爢錛屼絾鏄寰楃敤瀹屼簡(jiǎn)瑕佽繕緇欐搷浣滅郴緇燂紝瑕佷笉鐒跺氨鏄唴瀛樻硠婕忋?/p>

鏍堬細(xì)鏄釜綰跨▼鐙湁鐨勶紝淇濆瓨鍏惰繍琛岀姸鎬佸拰灞閮ㄨ嚜鍔ㄥ彉閲忕殑銆傛爤鍦ㄧ嚎紼嬪紑濮嬬殑鏃跺欏垵濮嬪寲錛屾瘡涓嚎紼嬬殑鏍堜簰鐩哥嫭绔嬶紝鍥犳錛屾爤鏄thread safe鐨勩傛瘡涓跡銆錛嬶紜瀵硅薄鐨勬暟鎹垚鍛樹篃瀛樺湪鍦ㄦ爤涓紝姣忎釜鍑芥暟閮芥湁鑷繁鐨勬爤錛屾爤琚敤鏉ュ湪鍑芥暟涔嬮棿浼犻掑弬鏁般傛搷浣滅郴緇熷湪鍒囨崲綰跨▼鐨勬椂鍊欎細(xì)鑷姩鐨勫垏鎹㈡爤錛屽氨鏄垏鎹€錛籌汲錛忥譏錛籌及瀵勫瓨鍣ㄣ傛爤絀洪棿涓嶉渶瑕佸湪楂樼駭璇█閲岄潰鏄懼紡鐨勫垎閰嶅拰閲婃斁銆?/p>

鍫嗗拰鏍堢殑鍖哄埆

涓銆侀澶囩煡璇嗏旂▼搴忕殑鍐呭瓨鍒嗛厤
涓涓敱c/C++緙栬瘧鐨勭▼搴忓崰鐢ㄧ殑鍐呭瓨鍒嗕負(fù)浠ヤ笅鍑犱釜閮ㄥ垎錛?br>1銆佹爤鍖猴紙stack錛夆?鐢辯紪璇戝櫒鑷姩鍒嗛厤閲婃斁錛屽瓨鏀懼嚱鏁扮殑鍙傛暟鍊鹼紝灞閮ㄥ彉閲忕殑鍊肩瓑銆傚叾鎿嶄綔鏂瑰紡綾諱技浜庢暟鎹粨鏋勪腑鐨勬爤銆?/p>

2銆佸爢鍖猴紙heap錛?鈥?涓鑸敱紼嬪簭鍛樺垎閰嶉噴鏀撅紝鑻ョ▼搴忓憳涓嶉噴鏀撅紝紼嬪簭緇撴潫鏃跺彲鑳界敱O(jiān)S鍥炴敹銆傛敞鎰忓畠涓庢暟鎹粨鏋勪腑鐨勫爢鏄袱鍥炰簨錛屽垎閰嶆柟寮忓掓槸綾諱技浜庨摼琛ㄣ?/p>

3銆佸叏灞鍖猴紙闈?rùn)鎬佸尯錛夛紙static錛夆旓紝鍏ㄥ眬鍙橀噺鍜岄潤(rùn)鎬佸彉閲忕殑瀛樺偍鏄斁鍦ㄤ竴鍧楃殑錛屽垵濮嬪寲鐨勫叏灞鍙橀噺鍜岄潤(rùn)鎬佸彉閲忓湪涓鍧楀尯鍩燂紝鏈垵濮嬪寲鐨勫叏灞鍙橀噺鍜屾湭鍒濆鍖栫殑闈?rùn)鎬佸彉閲忓湪鐩擱偦鐨勫彟涓鍧楀尯鍩熴?- 紼嬪簭緇撴潫鍚庣敱緋葷粺閲婃斁銆?/p>

4銆佹枃瀛楀父閲忓尯   鈥斿父閲忓瓧絎︿覆灝辨槸鏀懼湪榪欓噷鐨勩傜▼搴忕粨鏉熷悗鐢辯郴緇熼噴鏀俱?/p>

5銆佺▼搴忎唬鐮佸尯鈥斿瓨鏀懼嚱鏁頒綋鐨勪簩榪涘埗浠g爜銆?/p>

浜屻佷緥瀛愮▼搴?br>//main.cpp
int a = 0; 鍏ㄥ眬鍒濆鍖栧尯
char *p1; 鍏ㄥ眬鏈垵濮嬪寲鍖?br>main()
{
int b; 鏍?br>char s[] = "abc"; 鏍?br>char *p2; 鏍?br>char *p3 = "123456"; 123456\0鍦ㄥ父閲忓尯錛宲3鍦ㄦ爤涓娿?br>static int c =0錛涘叏灞錛堥潤(rùn)鎬侊級(jí)鍒濆鍖栧尯
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);
鍒嗛厤寰楁潵寰?0鍜?0瀛楄妭鐨勫尯鍩熷氨鍦ㄥ爢鍖恒?br>strcpy(p1, "123456"); 123456\0鏀懼湪甯擱噺鍖猴紝緙栬瘧鍣ㄥ彲鑳戒細(xì)灝嗗畠涓巔3鎵鎸囧悜鐨?123456"浼樺寲鎴愪竴涓湴鏂廣?br>}
浜屻佸爢鍜屾爤鐨勭悊璁虹煡璇?br>2.1鐢寵鏂瑰紡
stack:
鐢辯郴緇熻嚜鍔ㄥ垎閰嶃備緥濡傦紝澹版槑鍦ㄥ嚱鏁頒腑涓涓眬閮ㄥ彉閲?int b; 緋葷粺鑷姩鍦ㄦ爤涓負(fù)b寮杈熺┖闂?br>heap:
闇瑕佺▼搴忓憳鑷繁鐢寵錛屽茍鎸囨槑澶у皬錛屽湪c涓璵alloc鍑芥暟
濡俻1 = (char *)malloc(10);
鍦–++涓敤new榪愮畻絎?br>濡俻2 = (char *)malloc(10);
浣嗘槸娉ㄦ剰p1銆乸2鏈韓鏄湪鏍堜腑鐨勩?/p>


2.2
鐢寵鍚庣郴緇熺殑鍝嶅簲
鏍堬細(xì)鍙鏍堢殑鍓╀綑絀洪棿澶т簬鎵鐢寵絀洪棿錛岀郴緇熷皢涓虹▼搴忔彁渚涘唴瀛橈紝鍚﹀垯灝嗘姤寮傚父鎻愮ず鏍堟孩鍑恒?br>鍫嗭細(xì) 棣栧厛搴旇鐭ラ亾鎿嶄綔緋葷粺鏈変竴涓褰曠┖闂插唴瀛樺湴鍧鐨勯摼琛紝褰撶郴緇熸敹鍒扮▼搴忕殑鐢寵鏃訛紝浼?xì)閬嶅巻璇ラ摼琛ㄥQ屽鎵劇涓涓┖闂村ぇ浜庢墍鐢寵絀洪棿鐨勫爢緇撶偣錛岀劧鍚庡皢璇ョ粨鐐逛粠絀洪棽 緇撶偣閾捐〃涓垹闄わ紝騫跺皢璇ョ粨鐐圭殑絀洪棿鍒嗛厤緇欑▼搴忥紝鍙﹀錛屽浜庡ぇ澶氭暟緋葷粺錛屼細(xì)鍦ㄨ繖鍧楀唴瀛樼┖闂翠腑鐨勯鍦板潃澶勮褰曟湰嬈″垎閰嶇殑澶у皬錛岃繖鏍鳳紝浠g爜涓殑delete璇彞鎵嶈兘姝g‘鐨勯噴鏀炬湰鍐呭瓨絀洪棿銆傚彟澶栵紝鐢變簬鎵懼埌鐨勫爢緇撶偣鐨勫ぇ灝忎笉涓瀹氭濂界瓑浜庣敵璇風(fēng)殑澶у皬錛岀郴緇熶細(xì)鑷姩鐨勫皢澶氫綑鐨勯偅閮ㄥ垎閲嶆柊鏀懼叆絀洪棽閾捐〃涓?/p>

2.3鐢寵澶у皬鐨勯檺鍒?br>鏍堬細(xì)鍦╓indows涓?鏍堟槸鍚戜綆鍦板潃鎵╁睍鐨勬暟鎹粨鏋勶紝鏄竴鍧楄繛緇殑鍐呭瓨鐨勫尯鍩熴傝繖鍙ヨ瘽鐨勬剰鎬濇槸鏍堥《鐨勫湴鍧鍜屾爤鐨勬渶澶у閲忔槸緋葷粺棰勫厛瑙勫畾濂界殑錛屽湪WINDOWS涓嬶紝鏍堢殑澶у皬鏄?M錛堜篃鍙兘鏄?M錛屽畠鏄竴涓紪璇戞椂灝辯‘瀹氱殑甯告暟錛夛紝濡傛灉鐢寵鐨勭┖闂磋秴榪囨爤鐨勫墿浣欑┖闂存椂錛屽皢鎻愮ずoverflow銆傚洜姝わ紝鑳戒粠鏍堣幏寰楃殑絀洪棿杈冨皬
銆?br>鍫嗭細(xì)鍫嗘槸鍚戦珮鍦板潃鎵╁睍鐨勬暟鎹粨鏋勶紝鏄笉榪炵畫鐨勫唴瀛樺尯鍩熴傝繖鏄敱浜庣郴緇熸槸鐢ㄩ摼琛ㄦ潵瀛樺偍鐨勭┖闂插唴瀛樺湴鍧鐨勶紝鑷劧鏄笉榪炵畫鐨勶紝鑰岄摼琛ㄧ殑閬嶅巻鏂瑰悜鏄敱浣庡湴鍧鍚戦珮鍦板潃銆傚爢鐨勫ぇ灝忓彈闄愪簬璁$畻鏈虹郴緇熶腑鏈夋晥鐨勮櫄鎷熷唴瀛樸傜敱姝ゅ彲瑙侊紝鍫嗚幏寰楃殑絀洪棿姣旇緝鐏墊椿錛屼篃姣旇緝澶с?/p>


2.4鐢寵鏁堢巼鐨勬瘮杈冿細(xì)
鏍堢敱緋葷粺鑷姩鍒嗛厤錛岄熷害杈冨揩銆備絾紼嬪簭鍛樻槸鏃犳硶鎺у埗鐨勩?br>鍫嗘槸鐢眓ew鍒嗛厤鐨勫唴瀛橈紝涓鑸熷害姣旇緝鎱紝鑰屼笖瀹規(guī)槗浜х敓鍐呭瓨紕庣墖,涓嶈繃鐢ㄨ搗鏉ユ渶鏂逛究.
鍙﹀錛屽湪WINDOWS涓嬶紝鏈濂界殑鏂瑰紡鏄敤VirtualAlloc鍒嗛厤鍐呭瓨錛屼粬涓嶆槸鍦ㄥ爢錛屼篃涓嶆槸鍦ㄦ爤鏄洿鎺ュ湪榪涚▼鐨勫湴鍧絀洪棿涓繚鐣欎竴蹇唴瀛橈紝铏界劧鐢ㄨ搗鏉ユ渶涓嶆柟渚褲備絾鏄熷害蹇紝涔熸渶鐏墊椿銆?/p>

2.5鍫嗗拰鏍堜腑鐨勫瓨鍌ㄥ唴瀹?br>鏍堬細(xì)鍦ㄥ嚱鏁拌皟鐢ㄦ椂錛岀涓涓繘鏍堢殑鏄富鍑芥暟涓悗鐨勪笅涓鏉℃寚浠わ紙鍑芥暟璋冪敤璇彞鐨勪笅涓鏉″彲鎵ц璇彞錛夌殑鍦板潃錛岀劧鍚庢槸鍑芥暟鐨勫悇涓弬鏁幫紝鍦ㄥぇ澶氭暟鐨凜緙栬瘧鍣ㄤ腑錛屽弬鏁版槸鐢卞彸寰宸﹀叆鏍堢殑錛岀劧鍚庢槸鍑芥暟涓殑灞閮ㄥ彉閲忋傛敞鎰忛潤(rùn)鎬佸彉閲忔槸涓嶅叆鏍堢殑銆?br>褰撴湰嬈″嚱鏁拌皟鐢ㄧ粨鏉熷悗錛屽眬閮ㄥ彉閲忓厛鍑烘爤錛岀劧鍚庢槸鍙傛暟錛屾渶鍚庢爤欏舵寚閽堟寚鍚戞渶寮濮嬪瓨鐨勫湴鍧錛屼篃灝辨槸涓誨嚱鏁頒腑鐨勪笅涓鏉℃寚浠わ紝紼嬪簭鐢辮鐐圭戶緇繍琛屻?br>鍫嗭細(xì)涓鑸槸鍦ㄥ爢鐨勫ご閮ㄧ敤涓涓瓧鑺傚瓨鏀懼爢鐨勫ぇ灝忋傚爢涓殑鍏蜂綋鍐呭鏈夌▼搴忓憳瀹夋帓銆?/p>

2.6瀛樺彇鏁堢巼鐨勬瘮杈?/p>

char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
aaaaaaaaaaa鏄湪榪愯鏃跺埢璧嬪肩殑錛?br>鑰宐bbbbbbbbbb鏄湪緙栬瘧鏃跺氨紜畾鐨勶紱
浣嗘槸錛屽湪浠ュ悗鐨勫瓨鍙栦腑錛屽湪鏍堜笂鐨勬暟緇勬瘮鎸囬拡鎵鎸囧悜鐨勫瓧絎︿覆(渚嬪鍫?蹇?br>姣斿錛?br>void main()
{
char a = 1;
char c[] = "1234567890";
char *p ="1234567890";
a = c[1];
a = p[1];
return;
}
瀵瑰簲鐨勬眹緙栦唬鐮?br>10: a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4],cl
11: a = p[1];
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]
00401070 8A 42 01 mov al,byte ptr [edx+1]
00401073 88 45 FC mov byte ptr [ebp-4],al
絎竴縐嶅湪璇誨彇鏃剁洿鎺ュ氨鎶婂瓧絎︿覆涓殑鍏冪礌璇誨埌瀵勫瓨鍣╟l涓紝鑰岀浜岀鍒欒鍏堟妸鎸囬拡鍊艱鍒癳dx涓紝鍦ㄦ牴鎹?br>edx璇誨彇瀛楃錛屾樉鐒舵參浜?jiǎn)銆?/p>


2.7灝忕粨錛?br>鍫嗗拰鏍堢殑鍖哄埆鍙互鐢ㄥ涓嬬殑姣斿柣鏉ョ湅鍑猴細(xì)
浣跨敤鏍堝氨璞℃垜浠幓楗閲屽悆楗紝鍙鐐硅彍錛堝彂鍑虹敵璇鳳級(jí)銆佷粯閽便佸拰鍚冿紙浣跨敤錛夛紝鍚冮ケ浜?jiǎn)灏辫祹图屼笉蹇呯悊浼?xì)鍒囪彍銆佹礂鑿滅瓑鍑嗗宸ヤ綔鍜屾礂紕椼佸埛閿呯瓑鎵熬宸ヤ綔錛屼粬鐨勫ソ澶勬槸蹇嵎錛屼絾鏄嚜鐢卞害灝忋?br>浣跨敤鍫嗗氨璞℃槸鑷繁鍔ㄦ墜鍋氬枩嬈㈠悆鐨勮彍鑲達(dá)紝姣旇緝楹葷儲(chǔ)錛屼絾鏄瘮杈冪鍚堣嚜宸辯殑鍙e懗錛岃屼笖鑷敱搴﹀ぇ銆?br>


涓嬮潰鏄彟涓綃囷紝鎬葷粨鐨勬瘮涓婇潰濂斤細(xì)

鍫嗗拰鏍堢殑鑱旂郴涓庡尯鍒玠d

      鍦╞bs涓婏紝鍫嗕笌鏍堢殑鍖哄垎闂錛屼技涔庢槸涓涓案鎭掔殑璇濋錛岀敱姝ゅ彲瑙侊紝鍒濆鑰呭姝ゅ線寰鏄販娣嗕笉娓呯殑錛屾墍浠ユ垜鍐沖畾鎷夸粬絎竴涓紑鍒銆?/p>

       棣栧厛錛屾垜浠婦涓涓緥瀛愶細(xì)

       void f() { int* p=new int[5]; }

       榪欐潯鐭煭鐨勪竴鍙ヨ瘽灝卞寘鍚簡(jiǎn)鍫嗕笌鏍堬紝鐪嬪埌new錛屾垜浠鍏堝氨搴旇鎯沖埌錛屾垜浠垎閰嶄簡(jiǎn)涓鍧楀爢鍐呭瓨錛岄偅涔堟寚閽坧鍛紵浠栧垎閰嶇殑鏄竴鍧楁爤鍐呭瓨錛屾墍浠ヨ繖鍙ヨ瘽鐨勬剰鎬濆氨鏄細(xì) 鍦ㄦ爤鍐呭瓨涓瓨鏀句簡(jiǎn)涓涓寚鍚戜竴鍧楀爢鍐呭瓨鐨勬寚閽坧銆傚湪紼嬪簭浼?xì)鍏埣嫯瀹氬湪鍫嗕腑鍒嗛厤鍐呭瓨鐨勫ぇ灏忓Q岀劧鍚庤皟鐢╫perator new鍒嗛厤鍐呭瓨錛岀劧鍚庤繑鍥炶繖鍧楀唴瀛樼殑棣栧湴鍧錛屾斁鍏ユ爤涓紝浠栧湪VC6涓嬬殑姹囩紪浠g爜濡備笅錛?/p>

       00401028      push           14h

       0040102A      call           operator new (00401060)

       0040102F      add            esp,4

       00401032      mov            dword ptr [ebp-8],eax

       00401035      mov            eax,dword ptr [ebp-8]

       00401038      mov            dword ptr [ebp-4],eax

       榪欓噷錛屾垜浠負(fù)浜?jiǎn)绠鍗曞茍娌℃湁閲婃斁鍐呭瓨錛岄偅涔堣鎬庝箞鍘婚噴鏀懼憿錛熸槸delete p涔堬紵婢籌紝閿欎簡(jiǎn)錛屽簲璇ユ槸delete []p錛岃繖鏄負(fù)浜?jiǎn)鍛婅瘔缂栬瘧鍣ㄥQ氭垜鍒犻櫎鐨勬槸涓涓暟緇勶紝VC6灝變細(xì)鏍規(guī)嵁鐩稿簲鐨凜ookie淇℃伅鍘昏繘琛岄噴鏀懼唴瀛樼殑宸ヤ綔銆?/p>

       濂戒簡(jiǎn)錛屾垜浠洖鍒版垜浠殑涓婚錛氬爢鍜屾爤絀剁珶鏈変粈涔堝尯鍒紵

       涓昏鐨勫尯鍒敱浠ヤ笅鍑犵偣錛?/p>

       1銆佺鐞嗘柟寮忎笉鍚岋紱

       2銆佺┖闂村ぇ灝忎笉鍚岋紱

       3銆佽兘鍚︿駭鐢熺鐗囦笉鍚岋紱

       4銆佺敓闀挎柟鍚戜笉鍚岋紱

       5銆佸垎閰嶆柟寮忎笉鍚岋紱

       6銆佸垎閰嶆晥鐜囦笉鍚岋紱

       綆$悊鏂瑰紡錛氬浜庢爤鏉ヨ錛屾槸鐢辯紪璇戝櫒鑷姩綆$悊錛屾棤闇鎴戜滑鎵嬪伐鎺у埗錛涘浜庡爢鏉ヨ錛岄噴鏀懼伐浣滅敱紼嬪簭鍛樻帶鍒訛紝瀹規(guī)槗浜х敓memory leak銆?/p>

       絀洪棿澶у皬錛氫竴鑸潵璁插湪32浣嶇郴緇熶笅錛屽爢鍐呭瓨鍙互杈懼埌4G鐨勭┖闂達(dá)紝浠庤繖涓搴︽潵鐪嬪爢鍐呭瓨鍑犱箮鏄病鏈変粈涔堥檺鍒剁殑銆備絾鏄浜庢爤鏉ヨ錛屼竴鑸兘鏄湁涓瀹氱殑絀洪棿澶у皬 鐨勶紝渚嬪錛屽湪VC6涓嬮潰錛岄粯璁ょ殑鏍堢┖闂村ぇ灝忔槸1M錛堝ソ鍍忔槸錛岃涓嶆竻妤氫簡(jiǎn)錛夈傚綋鐒?dòng)灱屾垜浠彲浠ヤ慨鏀瑰Q?nbsp;   

       鎵撳紑宸ョ▼錛屼緷嬈℃搷浣滆彍鍗曞涓嬶細(xì)Project->Setting->Link錛屽湪Category 涓変腑Output錛岀劧鍚庡湪Reserve涓瀹氬爢鏍堢殑鏈澶у煎拰commit銆?/p>

娉ㄦ剰錛歳eserve鏈灝忓間負(fù)4Byte錛沜ommit鏄繚鐣欏湪铏氭嫙鍐呭瓨鐨勯〉鏂囦歡閲岄潰錛屽畠璁劇疆鐨勮緝澶т細(xì)浣挎爤寮杈熻緝澶х殑鍊鹼紝 鍙兘澧炲姞鍐呭瓨鐨勫紑閿鍜屽惎鍔ㄦ椂闂淬?/p>

       紕庣墖闂錛氬浜庡爢鏉ヨ錛岄綣佺殑new/delete鍔垮繀浼?xì)閫犳垚鍐呭瓨絀洪棿鐨勪笉榪炵畫錛屼粠鑰岄犳垚澶ч噺鐨勭鐗囷紝浣跨▼搴忔晥鐜囬檷浣庛傚浜庢爤鏉ヨ錛屽垯涓嶄細(xì)瀛樺湪榪欎釜闂錛? 鍥犱負(fù)鏍堟槸鍏堣繘鍚庡嚭鐨勯槦鍒楋紝浠栦滑鏄姝ょ殑涓涓瀵瑰簲錛屼互鑷充簬姘歌繙閮戒笉鍙兘鏈変竴涓唴瀛樺潡浠庢爤涓棿寮瑰嚭錛屽湪浠栧脊鍑轟箣鍓嶏紝鍦ㄤ粬涓婇潰鐨勫悗榪涚殑鏍堝唴瀹瑰凡緇忚寮瑰嚭錛岃緇嗙殑 鍙互鍙傝冩暟鎹粨鏋勶紝榪欓噷鎴戜滑灝變笉鍐嶄竴涓璁ㄨ浜?jiǎn)銆?/p>

       鐢熼暱鏂瑰悜錛氬浜庡爢鏉ヨ錛岀敓闀挎柟鍚戞槸鍚戜笂鐨勶紝涔熷氨鏄悜鐫鍐呭瓨鍦板潃澧炲姞鐨勬柟鍚戯紱瀵逛簬鏍堟潵璁詫紝瀹冪殑鐢熼暱鏂瑰悜鏄悜涓嬬殑錛屾槸鍚戠潃鍐呭瓨鍦板潃鍑忓皬鐨勬柟鍚戝闀褲?/p>

       鍒嗛厤鏂瑰紡錛氬爢閮芥槸鍔ㄦ佸垎閰嶇殑錛屾病鏈夐潤(rùn)鎬佸垎閰嶇殑鍫嗐傛爤鏈?縐嶅垎閰嶆柟寮忥細(xì)闈?rùn)鎬佸垎閰嶅拰鍔ㄦ佸垎閰嶃傞潤(rùn)鎬佸垎閰嶆槸緙栬瘧鍣ㄥ畬鎴愮殑錛屾瘮濡傚眬閮ㄥ彉閲忕殑鍒嗛厤銆傚姩鎬佸垎閰嶇敱 alloca鍑芥暟榪涜鍒嗛厤錛屼絾鏄爤鐨勫姩鎬佸垎閰嶅拰鍫嗘槸涓嶅悓鐨勶紝浠栫殑鍔ㄦ佸垎閰嶆槸鐢辯紪璇戝櫒榪涜閲婃斁錛屾棤闇鎴戜滑鎵嬪伐瀹炵幇銆?/p>

       鍒嗛厤鏁堢巼錛氭爤鏄満鍣ㄧ郴緇熸彁渚涚殑鏁版嵁緇撴瀯錛岃綆楁満浼?xì)鍦ㄥ簳灞傚?guī)爤鎻愪緵鏀寔錛氬垎閰嶄笓闂ㄧ殑瀵勫瓨鍣ㄥ瓨鏀炬爤鐨勫湴鍧錛屽帇鏍堝嚭鏍堥兘鏈変笓闂ㄧ殑鎸囦護(hù)鎵ц錛岃繖灝卞喅瀹氫簡(jiǎn)鏍堢殑鏁堢巼姣? 杈冮珮銆傚爢鍒欐槸C/C++鍑芥暟搴撴彁渚涚殑錛屽畠鐨勬満鍒舵槸寰堝鏉傜殑錛屼緥濡備負(fù)浜?jiǎn)鍒嗛厤涓鍧楀唴瀛橈紝搴撳嚱鏁頒細(xì)鎸夌収涓瀹氱殑綆楁硶錛堝叿浣撶殑綆楁硶鍙互鍙傝冩暟鎹粨鏋?鎿嶄綔緋葷粺錛夊湪鍫? 鍐呭瓨涓悳绱㈠彲鐢ㄧ殑瓚沖澶у皬鐨勭┖闂達(dá)紝濡傛灉娌℃湁瓚沖澶у皬鐨勭┖闂達(dá)紙鍙兘鏄敱浜庡唴瀛樼鐗囧お澶氾級(jí)錛屽氨鏈夊彲鑳借皟鐢ㄧ郴緇熷姛鑳藉幓澧炲姞紼嬪簭鏁版嵁孌電殑鍐呭瓨絀洪棿錛岃繖鏍峰氨鏈夋満浼?xì)鍒? 鍒拌凍澶熷ぇ灝忕殑鍐呭瓨錛岀劧鍚庤繘琛岃繑鍥炪傛樉鐒?dòng)灱屽爢鐨勬晥鐜囨瘮鏍堣浣庡緱澶氥?/p>

       浠庤繖閲屾垜浠彲浠ョ湅鍒幫紝鍫嗗拰鏍堢浉姣旓紝鐢變簬澶ч噺new/delete鐨勪嬌鐢紝瀹規(guī)槗閫犳垚澶ч噺鐨勫唴瀛樼鐗囷紱鐢變簬娌℃湁涓撻棬鐨勭郴緇熸敮鎸侊紝鏁堢巼寰堜綆錛涚敱浜庡彲鑳藉紩鍙戠敤鎴鋒? 鍜屾牳蹇?jī)鎬佺殑鍒囨崲錛屽唴瀛樼殑鐢寵錛屼唬浠峰彉寰楁洿鍔犳槀璐點(diǎn)傛墍浠ユ爤鍦ㄧ▼搴忎腑鏄簲鐢ㄦ渶騫挎硾鐨勶紝灝辯畻鏄嚱鏁扮殑璋冪敤涔熷埄鐢ㄦ爤鍘誨畬鎴愶紝鍑芥暟璋冪敤榪囩▼涓殑鍙傛暟錛岃繑鍥炲湴 鍧錛孍BP鍜屽眬閮ㄥ彉閲忛兘閲囩敤鏍堢殑鏂瑰紡瀛樻斁銆傛墍浠ワ紝鎴戜滑鎺ㄨ崘澶у灝介噺鐢ㄦ爤錛岃屼笉鏄敤鍫嗐?/p>

       铏界劧鏍堟湁濡傛浼楀鐨勫ソ澶勶紝浣嗘槸鐢變簬鍜屽爢鐩告瘮涓嶆槸閭d箞鐏墊椿錛屾湁鏃跺欏垎閰嶅ぇ閲忕殑鍐呭瓨絀洪棿錛岃繕鏄敤鍫嗗ソ涓浜涖?/p>

       鏃犺鏄爢榪樻槸鏍堬紝閮借闃叉瓚婄晫鐜拌薄鐨勫彂鐢燂紙闄ら潪浣犳槸鏁呮剰浣垮叾瓚婄晫錛夛紝鍥犱負(fù)瓚婄晫鐨勭粨鏋滆涔堟槸紼嬪簭宕╂簝錛岃涔堟槸鎽ф瘉紼嬪簭鐨勫爢銆佹爤緇撴瀯錛屼駭鐢熶互鎯充笉鍒扮殑緇撴灉,灝? 綆楁槸鍦ㄤ綘鐨勭▼搴忚繍琛岃繃紼嬩腑錛屾病鏈夊彂鐢熶笂闈㈢殑闂錛屼綘榪樻槸瑕佸皬蹇?jī)锛岃涓嶅畾浠涔堟椂鍊欏氨宕╂帀錛岄偅鏃跺檇ebug鍙槸鐩稿綋鍥伴毦鐨?:)    瀵逛簡(jiǎn)錛岃繕鏈変竴浠朵簨錛屽鏋滄湁浜烘妸鍫嗘爤鍚堣搗鏉ヨ錛岄偅瀹冪殑鎰忔濇槸鏍堬紝鍙笉鏄爢錛屽懙鍛? 娓呮浜?jiǎn)锛?/p>


from錛?br>http://blog.chinaunix.net/u2/76292/showart_1327414.html

http://hi.baidu.com/54wangjun/blog/item/d1b4a74424d5934f510ffedd.html



chatler 2009-12-11 23:53 鍙戣〃璇勮
]]>
鍚屾銆佸紓姝ャ侀樆濉炲拰闈為樆濉炵殑姒傚康http://www.shnenglu.com/beautykingdom/archive/2009/12/05/102631.htmlchatlerchatlerSat, 05 Dec 2009 15:10:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/12/05/102631.htmlhttp://www.shnenglu.com/beautykingdom/comments/102631.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/12/05/102631.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/102631.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/102631.html

chatler 2009-12-05 23:10 鍙戣〃璇勮
]]>
涓柇銆丏MA銆侀氶亾http://www.shnenglu.com/beautykingdom/archive/2009/11/10/100672.htmlchatlerchatlerTue, 10 Nov 2009 15:34:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/11/10/100672.htmlhttp://www.shnenglu.com/beautykingdom/comments/100672.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/11/10/100672.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/100672.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/100672.html涓銆佽疆璇㈡柟寮?/strong>
瀵笽/O璁懼鐨勭▼搴忚疆璇㈢殑鏂瑰紡錛屾槸鏃╂湡鐨勮綆楁満緋葷粺瀵笽/O璁懼鐨勪竴縐嶇鐞嗘柟寮忋傚畠瀹氭椂瀵瑰悇縐嶈澶囪疆嫻佽闂竴閬嶆湁鏃犲鐞嗚姹傘傝疆嫻佽闂箣鍚庯紝鏈夎姹傜殑錛屽垯鍔犱互澶勭悊銆傚湪澶勭悊I/O璁懼鐨勮姹備箣鍚庯紝澶勭悊鏈鴻繑鍥炵戶緇伐浣溿?br>灝界杞闇瑕佹椂闂達(dá)紝浣嗚疆璇笉姣擨/O璁懼鐨勯熷害瑕佸揩寰楀錛屾墍浠ヤ竴鑸笉浼?xì)鍙戠敓涓嶈兘鍙?qiáng)鏃跺鐞嗙殑闂銆?
褰撶劧錛屽啀蹇殑澶勭悊鏈猴紝鑳藉鐞嗙殑杈撳叆杈撳嚭璁懼鐨勬暟閲忎篃鏄湁涓瀹氶檺搴︾殑銆傝屼笖錛岀▼搴忚疆璇㈡瘯绔熷崰鎹簡(jiǎn)CPU鐩稿綋涓閮ㄥ垎澶勭悊鏃墮棿錛屽洜姝ょ▼搴忚疆璇㈡槸涓縐嶆晥鐜囪緝浣庣殑鏂瑰紡錛屽湪鐜頒唬璁$畻鏈虹郴緇熶腑宸插緢灝戝簲鐢ㄣ?/font>

浜屻佷腑鏂柟寮?
澶勭悊鍣ㄧ殑楂橀熷拰杈撳叆杈撳嚭璁懼鐨勪綆閫熸槸涓瀵圭煕鐩撅紝鏄澶囩鐞嗚瑙e喅鐨勪竴涓噸瑕侀棶棰樸備負(fù)浜?jiǎn)鎻愰珮鏁翠綋鏁堢巼锛屽噺灏戝湪绋嬪簭鐩存帴鎺у埗鏂瑰紡涓瑿PU涔嬮棿鐨勬暟鎹紶閫侊紝鏄緢蹇呰鐨勩?
鍦↖/O璁懼涓柇鏂瑰紡涓嬶紝涓ぎ澶勭悊鍣ㄤ笌I/O璁懼涔嬮棿鏁版嵁鐨勪紶杈撴楠ゅ涓嬶細(xì)
鈶村湪鏌愪釜榪涚▼闇瑕佹暟鎹椂錛屽彂鍑烘寚浠ゅ惎鍔ㄨ緭鍏ヨ緭鍑?guó)櫘惧鍑嗗鏁版?
鈶靛湪榪涚▼鍙戝嚭鎸囦護(hù)鍚姩璁懼涔嬪悗錛岃榪涚▼鏀懼純澶勭悊鍣紝絳夊緟鐩稿叧I/O鎿嶄綔瀹屾垚銆傛鏃訛紝榪涚▼璋冨害紼嬪簭浼?xì)璋冨害鍏朵粬灏本l繘紼嬩嬌鐢ㄥ鐞嗗櫒銆?
鈶跺綋I/O鎿嶄綔瀹屾垚鏃訛紝杈撳叆杈撳嚭璁懼鎺у埗鍣ㄩ氳繃涓柇璇鋒眰綰垮悜澶勭悊鍣ㄥ彂鍑轟腑鏂俊鍙鳳紝澶勭悊鍣ㄦ敹鍒頒腑鏂俊鍙蜂箣鍚庯紝杞悜棰勫厛璁捐濂界殑涓柇澶勭悊紼嬪簭錛屽鏁版嵁浼犻佸伐浣滆繘琛岀浉搴旂殑澶勭悊銆?
鈶峰緱鍒頒簡(jiǎn)鏁版嵁鐨勮繘紼嬶紝杞叆灝辯華鐘舵併傚湪闅忓悗鐨勬煇涓椂鍒伙紝榪涚▼璋冨害紼嬪簭浼?xì)閫変腑璇ヨ繘紼嬬戶緇伐浣溿?
涓柇鏂瑰紡鐨勪紭緙虹偣
I/O璁懼涓柇鏂瑰紡浣垮鐞嗗櫒鐨勫埄鐢ㄧ巼鎻愰珮錛屼笖鑳芥敮鎸佸閬撶▼搴忓拰I/O璁懼鐨勫茍琛屾搷浣溿?
涓嶈繃錛屼腑鏂柟寮忎粛鐒跺瓨鍦ㄤ竴浜涢棶棰樸傞鍏堬紝鐜頒唬璁$畻鏈虹郴緇熼氬父閰嶇疆鏈夊悇縐嶅悇鏍風(fēng)殑杈撳叆杈撳嚭璁懼銆傚鏋滆繖浜汭/O璁懼閮藉悓榪囦腑鏂鐞嗘柟寮忚繘琛屽茍琛屾搷浣滐紝閭d箞涓柇嬈℃暟鐨勬ュ墽澧炲姞浼?xì)閫犳垚CPU鏃犳硶鍝嶅簲涓柇鍜屽嚭鐜版暟鎹涪澶辯幇璞°?
鍏舵錛屽鏋淚/O鎺у埗鍣ㄧ殑鏁版嵁緙撳啿鍖烘瘮杈冨皬錛屽湪緙撳啿鍖鴻婊℃暟鎹箣鍚庡皢浼?xì)鍙戠敓涓柇銆傞偅涔堬紝鍦ㄦ暟鎹紶閫佽繃紼嬩腑錛屽彂鐢熶腑鏂殑鏈轟細(xì)杈冨錛岃繖灝嗚楀幓澶ч噺鐨凜PU澶勭悊鏃墮棿銆?/font>

涓夈佺洿鎺ュ唴瀛樺瓨鍙栵紙DMA錛夋柟寮?
鐩存帴鍐呭瓨瀛樺彇鎶鏈槸鎸囷紝鏁版嵁鍦ㄥ唴瀛樹笌I/O璁懼闂寸洿鎺ヨ繘琛屾垚鍧椾紶杈撱?
DMA鎶鏈壒寰?
DMA鏈変袱涓妧鏈壒寰侊紝棣栧厛鏄洿鎺ヤ紶閫侊紝鍏舵鏄潡浼犻併?
鎵璋撶洿鎺ヤ紶閫侊紝鍗沖湪鍐呭瓨涓嶪O璁懼闂翠紶閫佷竴涓暟鎹潡鐨勮繃紼嬩腑錛屼笉闇瑕丆PU鐨勪換浣曚腑闂村共娑夛紝鍙渶瑕丆PU鍦ㄨ繃紼嬪紑濮嬫椂鍚戣澶囧彂鍑?#8220;浼犻佸潡鏁版嵁”鐨勫懡浠わ紝鐒跺悗閫氳繃涓柇鏉ュ緱鐭ヨ繃紼嬫槸鍚︾粨鏉熷拰涓嬫鎿嶄綔鏄惁鍑嗗灝辯華銆?
DMA宸ヤ綔榪囩▼
鈶村綋榪涚▼瑕佹眰璁懼杈撳叆鏁版嵁鏃訛紝CPU鎶婂噯澶囧瓨鏀捐緭鍏ユ暟鎹殑鍐呭瓨璧峰鍦板潃浠ュ強(qiáng)瑕佷紶閫佺殑瀛楄妭鏁板垎鍒佸叆DMA鎺у埗鍣ㄤ腑鐨勫唴瀛樺湴鍧瀵勫瓨鍣ㄥ拰浼犻佸瓧鑺傝鏁板櫒銆?br>鈶靛彂鍑烘暟鎹紶杈撹姹傜殑榪涜榪涘叆絳夊緟鐘舵併傛鏃舵鍦ㄦ墽琛岀殑CPU鎸囦護(hù)琚殏鏃舵寕璧楓傝繘紼嬭皟搴︾▼搴忚皟搴﹀叾浠栬繘紼嬪崰鎹瓹PU銆?br>鈶惰緭鍏ヨ澶囦笉鏂湴紿冨彇CPU宸ヤ綔鍛ㄦ湡錛屽皢鏁版嵁緙撳啿瀵勫瓨鍣ㄤ腑鐨勬暟鎹簮婧愪笉鏂湴鍐欏叆鍐呭瓨錛岀洿鍒版墍瑕佹眰鐨勫瓧鑺傚叏閮ㄤ紶閫佸畬姣曘?br>鈶稤MA鎺у埗鍣ㄥ湪浼犻佸畬鎵鏈夊瓧鑺傛椂錛岄氳繃涓柇璇鋒眰綰垮彂鍑轟腑鏂俊鍙楓侰PU鍦ㄦ帴鏀跺埌涓柇淇″彿鍚庯紝杞叆涓柇澶勭悊紼嬪簭榪涜鍚庣畫澶勭悊銆?br>鈶鎬腑鏂鐞嗙粨鏉熷悗錛孋PU榪斿洖鍒拌涓柇鐨勮繘紼嬩腑錛屾垨鍒囨崲鍒版柊鐨勮繘紼嬩笂涓嬫枃鐜涓紝緇х畫鎵ц銆?strong>
銆銆DMA涓庝腑鏂殑鍖哄埆
鈶翠腑鏂柟寮忔槸鍦ㄦ暟鎹紦鍐插瘎瀛樺櫒婊′箣鍚庡彂鍑轟腑鏂紝瑕佹眰CPU榪涜涓柇澶勭悊錛岃孌MA鏂瑰紡鍒欐槸鍦ㄦ墍瑕佹眰浼犻佺殑鏁版嵁鍧楀叏閮ㄤ紶閫佺粨鏉熸椂瑕佹眰CPU 榪涜涓柇澶勭悊銆傝繖灝卞ぇ澶у噺灝戜簡(jiǎn)CPU榪涜涓柇澶勭悊鐨勬鏁般?br>鈶典腑鏂柟寮忕殑鏁版嵁浼犻佹槸鍦ㄤ腑鏂鐞嗘椂鐢盋PU鎺у埗瀹屾垚鐨勶紝鑰孌MA鏂瑰紡鍒欐槸鍦―MA鎺у埗鍣ㄧ殑鎺у埗涓嬶紝涓嶇粡榪嘋PU鎺у埗瀹屾垚鐨勩傝繖灝辨帓闄や簡(jiǎn)CPU鍥犲茍琛岃澶囪繃澶氳屾潵涓嶅強(qiáng)澶勭悊浠ュ強(qiáng)鍥犻熷害涓嶅尮閰嶈岄犳垚鏁版嵁涓㈠け絳夌幇璞°?strong>
銆銆DMA鏂瑰紡鐨勪紭緙虹偣
鍦―MA鏂瑰紡涓紝鐢變簬I/O璁懼鐩存帴鍚屽唴瀛樺彂鐢熸垚鍧楃殑鏁版嵁浜ゆ崲錛屽洜姝/O鏁堢巼姣旇緝楂樸傜敱浜嶥MA鎶鏈彲浠ユ彁楂業(yè)/O鏁堢巼錛屽洜姝ゅ湪鐜頒唬璁$畻鏈虹郴緇熶腑錛?寰楀埌浜?jiǎn)骞挎硾鐨勫簲鐢ㄣ傝澶氳緭鍏ヨ緭鍑?guó)櫘惧鐨勬帶鍒跺櫒锛岀壒鍒槸鍧楄畱证囩殑鎺у埗鍣ㄥQ岄兘鏀寔DMA鏂瑰紡銆?br>閫氳繃涓婅堪鍒嗘瀽鍙互鐪嬪嚭錛孌MA鎺у埗鍣ㄥ姛鑳界殑寮哄急錛屾槸鍐沖畾DMA鏁堢巼鐨勫叧閿洜绱犮侱MA鎺у埗鍣ㄩ渶瑕佷負(fù)姣忔鏁版嵁浼犻佸仛澶ч噺鐨勫伐浣滐紝鏁版嵁浼犻佸崟浣嶇殑澧炲ぇ鎰忓懗鐫浼犻佹鏁扮殑鍑忓皯銆傚彟澶栵紝DMA鏂瑰紡紿冨彇浜?jiǎn)濮嬀l堝懆鏈燂紝CPU澶勭悊鏁堢巼闄嶄綆浜?jiǎn)锛岃鎯碁閲彏畱鍦敖H冨彇濮嬬粓鍛ㄦ湡錛屽氨瑕佽娉曟彁楂楧MA鎺у埗鍣ㄧ殑鎬ц兘錛岃繖鏍峰彲浠ヨ緝?yōu)畱鍦板奖鍝岰PU鍑虹悊鏁堢巼銆?/font>

鍥涖侀氶亾鏂瑰紡
杈撳叆/杈撳嚭閫氶亾鏄竴涓嫭绔嬩簬CPU鐨勶紝涓撻棬綆$悊I/O鐨勫鐞嗘満錛屽畠鎺у埗璁懼涓庡唴瀛樼洿鎺ヨ繘琛屾暟鎹氦鎹€傚畠鏈夎嚜宸辯殑閫氶亾鎸囦護(hù)錛岃繖浜涢氶亾鎸囦護(hù)鐢盋PU鍚姩錛屽茍鍦ㄦ搷浣滅粨鏉熸椂鍚慍PU鍙戝嚭涓柇淇″彿錛岃鍥?-3銆?br>杈撳叆/杈撳嚭閫氶亾鎺у埗鏄竴縐嶄互鍐呭瓨涓轟腑蹇?jī)锛屽疄鐜拌畱证囧拰鍐呭弬鍐呯洿鎺ヤ氦鎹㈡暟鎹殑鎺у埗鏂瑰紡銆傚湪閫氶亾鏂瑰紡涓紝鏁版嵁鐨勪紶閫佹柟鍚戙佸瓨鏀炬暟鎹殑鍐呭瓨璧峰鍦板潃浠ュ強(qiáng)浼犻佺殑鏁版嵁鍧楅暱搴︾瓑閮界敱閫氶亾鏉ヨ繘琛屾帶鍒躲?br>鍙﹀錛岄氶亾鎺у埗鏂瑰紡鍙互鍋氬埌涓涓氶亾鎺у埗澶氬彴璁懼涓庡唴瀛樿繘琛屾暟鎹氦鎹€傚洜鑰岋紝閫氶亾鏂瑰紡榪涗竴姝ュ噺杞諱簡(jiǎn)CPU鐨勫伐浣滆礋鎷咃紝澧炲姞浜?jiǎn)璁〗帡鏈壕p葷粺鐨勫茍琛屽伐浣滅▼搴︺?strong>
銆銆杈撳叆/杈撳嚭閫氶亾鍒嗙被
鎸夌収淇℃伅浜ゆ崲鏂瑰紡鍜屾墍榪炴帴鐨勮澶囩綾諱笉鍚岋紝閫氶亾鍙互鍒嗕負(fù)浠ヤ笅涓夌綾誨瀷錛?br>鈶村瓧鑺傚璺氶亾
瀹冮傜敤浜庤繛鎺ユ墦鍗版満銆佺粓绔瓑浣庨熸垨涓熺殑I/O璁懼銆傝繖縐嶉氶亾浠ュ瓧鑺備負(fù)鍗曚綅浜ゅ弶宸ヤ綔錛氬綋涓轟竴鍙拌澶囦紶閫佷竴涓瓧鑺傚悗錛岀珛鍗寵漿鍘諱負(fù)鍙︿竴瀹冭澶囦紶閫佷竴涓瓧鑺傘?br>鈶甸夋嫨閫氶亾
瀹冮傜敤浜庤繛鎺ョ鐩樸佺甯︾瓑楂橀熻澶囥傝繖縐嶉氶亾浠?#8220;緇勬柟寮?#8221;宸ヤ綔錛屾瘡嬈′紶閫佷竴鎵規(guī)暟鎹紝浼犻侀熺巼寰堥珮錛屼絾鍦ㄤ竴孌墊椂闂村彧鑳戒負(fù)涓鍙拌澶囨湇鍔°傛瘡褰撲竴涓狪/O璇鋒眰澶勭悊瀹屼箣鍚庯紝灝遍夋嫨鍙︿竴鍙拌澶囧茍涓哄叾鏈嶅姟銆?br>鈶舵垚緇勫璺氶亾
榪欑閫氶亾緇煎悎浜?jiǎn)瀛楄妭澶氳矾閫氶亾鍒嗘椂宸ヤ綔鍜岄夋嫨閫氶亾浼犺緭閫熺巼楂樼殑鐗圭偣錛屽叾瀹炶川鏄細(xì)瀵歸氶亾紼嬪簭閲囩敤澶氶亾紼嬪簭璁捐鎶鏈紝浣垮緱涓庨氶亾榪炴帴鐨勮澶囧彲浠ュ茍琛屽伐浣溿?strong>
銆銆閫氶亾宸ヤ綔鍘熺悊
鍦ㄩ氶亾鎺у埗鏂瑰紡涓紝I/O璁懼鎺у埗鍣紙甯哥畝縐頒負(fù)I/O鎺у埗鍣級(jí)涓病鏈変紶閫佸瓧鑺傝鏁板櫒鍜屽唴瀛樺湴鍧瀵勫瓨鍣紝浣嗗浜?jiǎn)閫氶亾璁懼鎺у埗鍣ㄥ拰鎸囦護(hù)鎵ц閮ㄤ歡銆侰PU鍙渶鍙戝嚭鍚姩鎸囦護(hù)錛屾寚鍑洪氶亾鐩稿簲鐨勬搷浣滃拰I/O璁懼錛岃鎸囦護(hù)灝卞彲鍚姩閫氶亾騫朵嬌璇ラ氶亾浠庡唴瀛樹腑璋冨嚭鐩稿簲鐨勯氶亾鎸囦護(hù)鎵ц銆?br>涓鏃PU鍙戝嚭鍚姩閫氶亾鐨勬寚浠わ紝閫氶亾灝卞紑濮嬪伐浣溿侷/O閫氶亾鎺у埗I/O鎺у埗鍣ㄥ伐浣滐紝I/O鎺у埗鍣ㄥ張鎺у埗I/O璁懼銆傝繖鏍鳳紝涓涓氶亾鍙互榪炴帴澶氫釜I/O鎺у埗鍣紝鑰屼竴涓狪/O鎺у埗鍣ㄥ張鍙互榪炴帴鑻ュ共鍙板悓綾誨瀷鐨勫閮ㄨ澶囥?strong>
銆銆閫氶亾鐨勮繛鎺?/strong>
鐢變簬閫氶亾鍜屾帶鍒跺櫒鐨勬暟閲忎竴鑸瘮璁懼鏁伴噺瑕佸皯錛屽洜姝わ紝濡傛灉榪炴帴涓嶅綋錛屽線寰浼?xì)瀵艰嚧鍑虹?#8220;鐡墮”銆傛晠涓鑸澶囩殑榪炴帴閲囩敤浜ゅ弶榪炴帴錛岃繖鏍峰仛鐨勫ソ澶勬槸錛?br>鈶?nbsp; 鎻愰珮緋葷粺鐨勫彲闈犳э細(xì)褰撴煇鏉¢氳礬鍥犳帶鍒跺櫒鎴栭氶亾鏁呴殰鑰屾柇寮鏃訛紝鍙嬌鐢ㄥ叾浠栭氳礬銆?br>鈶?nbsp;  鎻愰珮璁懼鐨勫茍琛屾э細(xì)瀵逛簬鍚屼竴涓澶囷紝褰撲笌瀹冪浉榪炵殑鏌愪竴鏉¢氳礬涓殑鎺у埗鍣ㄦ垨閫氶亾琚崰鐢ㄦ椂錛屽彲浠ラ夋嫨鍙︿竴鏉$┖闂查氳礬錛屽噺灝戜簡(jiǎn)璁懼鍥犵瓑寰呴氳礬鎵闇瑕佽姳璐圭殑鏃墮棿銆?strong>
銆銆閫氶亾澶勭悊鏈?/strong>
閫氶亾鐩稿綋浜庝竴涓姛鑳藉崟綰殑澶勭悊鏈猴紝瀹冨叿鏈夎嚜宸辯殑鎸囦護(hù)緋葷粺錛屽寘鎷銆佸啓銆佹帶鍒躲佽漿縐匯佺粨鏉熶互鍙?qiáng)绌烘搷浣溄{夋寚浠わ紝騫跺彲浠ユ墽琛岀敱榪欎簺鎸囦護(hù)緙栧啓鐨勯氶亾紼嬪簭銆?br>閫氶亾鐨勮繍綆楁帶鍒墮儴浠跺寘鎷細(xì)
鈶?閫氶亾鍦板潃瀛楋紙CAW錛夛細(xì)璁板綍涓嬩竴鏉¢氶亾鎸囦護(hù)瀛樻斁鐨勫湴鍧錛屽叾鍔熻兘綾諱技浜庝腑澶鐞嗘満鐨勬寚浠ゅ瘎瀛樺櫒銆?br>鈶?閫氶亾鍛戒護(hù)瀛楋紙CCW錛夛細(xì)璁板綍姝e湪鎵ц鐨勯氶亾鎸囦護(hù)錛屽叾浣滅敤鐩稿綋浜庝腑澶鐞嗘満鐨勬寚浠ゅ瘎瀛樺櫒銆?br>鈶?nbsp; 閫氶亾鐘舵佸瓧錛圕SW錛夛細(xì)璁板綍閫氶亾銆佹帶鍒跺櫒銆佽澶囩殑鐘舵侊紝鍖呮嫭I/O浼犺緭瀹屾垚淇℃伅銆佸嚭閿欎俊鎭侀噸澶嶆墽琛屾鏁扮瓑銆?strong>
銆銆閫氶亾瀵逛富鏈虹殑璁塊棶
閫氶亾涓鑸渶瑕佷笌涓繪満鍏變韓鍚屼竴涓唴瀛橈紝浠ヤ繚瀛橀氶亾紼嬪簭鍜屼氦鎹㈡暟鎹傞氶亾璁塊棶鍐呭瓨閲囩敤“鍛ㄦ湡紿冪敤”鏂瑰紡銆?br>閲囩敤閫氶亾鏂瑰紡鍚庯紝杈撳叆/杈撳嚭鐨勬墽琛岃繃紼嬪涓嬶細(xì)
CPU鍦ㄦ墽琛岀敤鎴風(fēng)▼搴忔椂閬囧埌I/O璇鋒眰錛屾牴鎹敤鎴風(fēng)殑I/O璇鋒眰鐢熸垚閫氶亾紼嬪簭錛堜篃鍙互鏄簨鍏堢紪濂界殑錛夈傛斁鍒板唴瀛樹腑錛屽茍鎶婅閫氶亾紼嬪簭棣栧湴鍧鏀懼叆CAW涓?br>鐒跺悗錛孋PU鎵ц“鍚姩I/O”鎸囦護(hù)錛屽惎鍔ㄩ氶亾宸ヤ綔銆傞氶亾鎺ユ敹“鍚姩I/O”鎸囦護(hù)淇″彿錛屼粠CAW涓彇鍑洪氶亾紼嬪簭棣栧湴鍧錛屽茍鏍規(guī)嵁姝ゅ湴鍧鍙栧嚭閫氶亾紼嬪簭鐨勭涓鏉℃寚浠わ紝鏀懼叆CCW涓紱鍚屾椂鍚慍U鍙戝洖絳斾俊鍙鳳紝閫氱煡“鍚姩I/O”鎸囦護(hù)瀹屾垚瀹屾瘯錛孋PU鍙戶緇墽琛屻?br>閫氶亾寮濮嬫墽琛岄氶亾紼嬪簭錛岃繘琛岀墿鐞咺/O鎿嶄綔銆傚綋鎵ц瀹屼竴鏉℃寚浠ゅ悗錛屽鏋滆繕鏈変笅涓鏉℃寚浠ゅ垯緇х畫鎵ц錛涘惁鍒欒〃紺轟紶杈撳畬鎴愶紝鍚屾椂鑷鍋滄錛岄氱煡CPU杞幓澶勭悊閫氶亾緇撴潫浜嬩歡錛屽茍浠嶤CW涓緱鍒版湁鍏抽氶亾鐘舵併?br>鎬諱箣錛屽湪閫氶亾涓紝I/O榪愮敤涓撶敤鐨勮緟鍔╁鐞嗗櫒澶勭悊I/O鎿嶄綔錛屼粠鑰屽壀寰勪簡(jiǎn)涓誨鐞嗗櫒澶勭悊I/O鐨勮礋鎷呫備富澶勭悊鍣ㄥ彧瑕佸彂鍑轟竴涓狪/O鎿嶄綔鍛戒護(hù)錛屽墿涓嬬殑宸ヤ綔瀹屽叏鐢遍氶亾璐熻矗銆侷/O鎿嶄綔緇撴潫鍚庯紝I/O閫氶亾浼?xì)鍙戝囖Z竴涓腑鏂姹傦紝琛ㄧず鐩稿簲鎿嶄綔宸插畬鎴愩?strong>
銆銆閫氶亾鐨勫彂灞?/strong>
閫氶亾鐨勬濇兂鏄粠鏃╂湡鐨勫ぇ鍨嬭綆楁満緋葷粺涓彂灞曡搗鏉ョ殑銆傚湪鏃╂湡鐨勫ぇ鍨嬭綆楁満緋葷粺涓紝涓鑸厤鏈夊ぇ閲忕殑I/O璁懼銆備負(fù)浜?jiǎn)鎶婂I/O璁懼鐨勭鐞嗕粠璁$畻鏈轟富鏈轟腑鍒嗙鍑烘潵錛屽艦鎴愪簡(jiǎn)I/O閫氶亾鐨勬蹇碉紝騫朵笓闂ㄨ璁″嚭I/O閫氶亾澶勭悊鏈恒?br>I/O閫氶亾鍦ㄨ綆楁満緋葷粺涓槸涓涓潪甯擱噸瑕佺殑閮ㄤ歡錛屽畠瀵圭郴緇熸暣浣撴ц兘鐨勬彁楂樿搗浜?jiǎn)鐩稿綋閲嶈鐨勪綔鐢ㄣ備笉榪囷紝闅忕潃鎶鏈笉鏂殑鍙戝睍錛屽鐞嗘満鍜孖/O璁懼鎬ц兘鐨勪笉鏂彁楂橈紝涓撶敤鐨勩佺嫭绔婭/O閫氶亾澶勭悊鏈哄凡涓嶅鏄撹鍒般備絾鏄氶亾鐨勬濇兂鍙堣瀺鍏ヤ簡(jiǎn)璁稿鏂扮殑鎶鏈紝鎵浠ヤ粛鍦ㄥ箍娉涘湴搴旂敤鐫銆傜敱浜庡厜綰ら氶亾鎶鏈叿鏈夋暟鎹紶杈撻熺巼楂樸佹暟鎹紶杈撹窛紱昏繙浠ュ強(qiáng)鍙畝鍖栧ぇ鍨嬪瓨鍌ㄧ郴緇熻璁$殑浼樼偣錛屾柊鐨勯氱敤鍏夌氦閫氶亾鎶鏈鍦ㄥ揩閫熷彂灞曘傝繖縐嶉氱敤鍏夌氦閫氶亾鍙互鍦ㄤ竴涓氶亾涓婂綰沖杈?27涓殑澶у閲忕‖鐩橀┍鍔ㄥ櫒銆傛樉鐒?dòng)灱屽湪澶у畾w噺楂橀熷瓨鍌ㄥ簲鐢ㄩ鍩燂紝閫氱敤鍏夌氦閫氶亾鏈夌潃騫挎硾鐨勫簲鐢ㄥ墠鏅?br>
杞嚜錛?br>http://blog.chinaunix.net/u2/67780/showart_2063742.html


chatler 2009-11-10 23:34 鍙戣〃璇勮
]]>callback function from wikipediahttp://www.shnenglu.com/beautykingdom/archive/2009/05/12/82747.htmlchatlerchatlerTue, 12 May 2009 15:28:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/05/12/82747.htmlhttp://www.shnenglu.com/beautykingdom/comments/82747.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/05/12/82747.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/82747.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/82747.htmlCallback (computer science)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

A callback is often back on the level of the original caller.

However, while technically accurate, this might not be the most illustrative explanation. Think of it as an "In case of fire, break glass" subroutine. Many computer programs tend to be written such that they expect a certain set of possibilities at any given moment. If "Thing That Was Expected", then "Do something", otherwise, "Do something else." is a common theme. However, there are many situations in which events (such as fire) could happen at any time. Rather than checking for them at each possible step ("Thing that was expected OR Things are on fire"), it is easier to have a system which detects a number of events, and will call the appropriate function upon said event (this also keeps us from having to write programs like "Thing that was expected OR Things are on fire OR Nuclear meltdown OR alien invasion OR the dead rising from the grave OR...etc., etc.) Instead, a callback routine is a sort of insurance policy. If zombies attack, call this function. If the user moves their mouse over an icon, call HighlightIcon, and so forth.

Usually, there is a framework in which a series of events (some condition is met) in which the running framework (be it a generic library or unique to the program) will call a registered chunk of code based on some pre-registered function (typically, a handle or a function pointer) The events may be anything from user input (such as mouse or keyboard input), network activity (callbacks are frequently used as message handlers for new network sessions) or an internal operating system event (such as a POSIX-style signal) The concept is to develop a piece of code that can be registered within some framework (be it a GUI toolkit, network library, etc.) that will serve as the handler upon the condition stated at registration. How the flow of control is passed between the underlying framework and the registered callback function is specific to the framework itself.

In another common scenario, the callback is first registered and later called asynchronously.

Contents

[hide]

[edit] Motivation

To understand the motivation for using callbacks, consider the problem of a network server. At any given point in time, it may have an internal state machine that is currently at a point in which it is dealing with one very specific communication session, not necessarily expecting new participants. As a host, it could be dealing with all the name exchange and handshakes and pleasantries, but no real way of dealing with the next dinner party guest that walks through the door. One way to deal with this is for this server to live by a state machine in which it rejects new connections until the current one is dealt with...not very robust (What if the other end goes away unexpectedly?) and not very scalable (Would you really want to make other clients wait (or more likely, keep retrying to connect) until it's their turn?) Instead, it's easier to have some sort of management process that spins off a new thread (or process) to deal with the new connection. Rather than writing programs that keep dealing with all of the possible resource contention problems that could come of this, or all of the details involved in socket code (your desired platform may be more straight-forward than others, but one of your design goals may be cross-platform compatibility), many have opted to use more generic frameworks that will handle such details in exchange for providing a reference such that the underlying framework can call it if the registered event occurs.

[edit] Example

The following code in C demonstrates the use of callbacks for the specific case of dealing with a POSIX-style signal (in this case SIGUSR1).

#include <stdio.h>
#include <signal.h>
void * sig(int signum)
{
printf("Received signal number %d!\n", signum);
}
int main(int argc, char *argv[])
{
signal(SIGUSR1,&sig);
while(1){};
return 0;
}

The while loop will keep this example from doing anything interesting, but it will give you plenty of time to send a signal to this process. (If you're on a unix-like system, try a "kill -USR1 <pid>" to the process ID associated with this sample program. No matter how or when you send it, the callback should respond.)

[edit] Implementation

The form of a callback varies among programming languages.

  • C and C++ allow function pointers as arguments to other functions.
  • Several programming languages (though especially functional programming languages such as Scheme or ML) allow closures, a generalization of function pointers, as arguments to other functions.
  • Several programming languages, especially interpreted languages, allow one to pass the name of a function A as a parameter to a function B and have B call A by means of eval.
  • In object-oriented programming languages, a call can accept an object that implements some abstract interface, without specifying in detail how the object should do so. The programmer who implements that object may use the interface's methods exclusively for application-specific code. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate. They are useful in implementing various design patterns like Visitor, Observer, and Strategy.
  • C++ allows objects to provide their own implementation of the function call operation. The Standard Template Library accepts these objects (called functors), as well as function pointers, as parameters to various polymorphic algorithms
  • C# .NET Framework provides a type-safe encapsulating reference, a 'delegate', to manage function pointers. These can be used for callback operations.
  • Perl supports subroutine references.[1][2]
  • Some systems have built-in programming languages to support extension and adaptation. These languages provide callbacks without the need for separate software development tools.

[edit] Special cases

Callback functions are also frequently used as a means to handle exceptions arising within the low level function, as a way to enable side-effects in response to some condition, or as a way to gather operational statistics in the course of a larger computation. Interrupt handlers in an operating system respond to hardware conditions, signal handlers of a process are triggered by the operating system, and event handlers process the asynchronous input a program receives.

A pure callback function is one which is purely functional (always returns the same value given the same inputs) and free of observable side-effects. Some uses of callbacks require pure callback functions to operate correctly.

A special case of a callback is called a predicate callback, or just predicate for short. This is a pure callback function which accepts a single input value and returns a Boolean value. These types of callbacks are useful for filtering collections of values by some condition.

[edit] See also

[edit] External links

[edit] References



chatler 2009-05-12 23:28 鍙戣〃璇勮
]]>
Critical Section<from wikipedia>http://www.shnenglu.com/beautykingdom/archive/2009/05/11/82547.htmlchatlerchatlerMon, 11 May 2009 03:48:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/05/11/82547.htmlhttp://www.shnenglu.com/beautykingdom/comments/82547.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/05/11/82547.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/82547.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/82547.htmlCritical section

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In concurrent programming a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task or process will only have to wait a fixed time to enter it (i.e. bounded waiting). Some synchronization mechanism is required at the entry and exit of the critical section to ensure exclusive use, for example a semaphore.

By carefully controlling which variables are modified inside and outside the critical section (usually, by accessing important state only from within), concurrent access to that state is prevented. A critical section is typically used when a multithreaded program must update multiple related variables without a separate thread making conflicting changes to that data. In a related situation, a critical section may be used to ensure a shared resource, for example a printer, can only be accessed by one process at a time.

How critical sections are implemented varies among operating systems.

The simplest method is to prevent any change of processor control inside the critical section. On uni-processor systems, this can be done by disabling interrupts on entry into the critical section, avoiding system calls that can cause a context switch while inside the section and restoring interrupts to their previous state on exit. Any thread of execution entering any critical section anywhere in the system will, with this implementation, prevent any other thread, including an interrupt, from getting the CPU and therefore from entering any other critical section or, indeed, any code whatsoever, until the original thread leaves its critical section.

This brute-force approach can be improved upon by using semaphores. To enter a critical section, a thread must obtain a semaphore, which it releases on leaving the section. Other threads are prevented from entering the critical section at the same time as the original thread, but are free to gain control of the CPU and execute other code, including other critical sections that are protected by different semaphores.

Some confusion exists in the literature about the relationship between different critical sections in the same program.[citation needed] In general, a resource that must be protected from concurrent access may be accessed by several pieces of code. Each piece must be guarded by a common semaphore. Is each piece now a critical section or are all the pieces guarded by the same semaphore in aggregate a single critical section? This confusion is evident in definitions of a critical section such as "... a piece of code that can only be executed by one process or thread at a time". This only works if all access to a protected resource is contained in one "piece of code", which requires either the definition of a piece of code or the code itself to be somewhat contrived.

Contents

[hide]

[edit] Application Level Critical Sections

Application-level critical sections reside in the memory range of the process and are usually modifiable by the process itself. This is called a user-space object because the program run by the user (as opposed to the kernel) can modify and interact with the object. However the functions called may jump to kernel-space code to register the user-space object with the kernel.

Example Code For Critical Sections with POSIX pthread library

/* Sample C/C++, Unix/Linux */
#include <pthread.h>

/* This is the critical section object (statically allocated). */
static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER;

void f()
{
/* Enter the critical section -- other threads are locked out */
pthread_mutex_lock( &cs_mutex );

/* Do some thread-safe processing! */

/*Leave the critical section -- other threads can now pthread_mutex_lock() */
pthread_mutex_unlock( &cs_mutex );
}

Example Code For Critical Sections with Win32 API

/* Sample C/C++, Windows, link to kernel32.dll */
#include <windows.h>

static CRITICAL_SECTION cs; /* This is the critical section object -- once initialized,
it cannot be moved in memory */

/* If you program in OOP, declare this in your class */

/* Initialize the critical section before entering multi-threaded context. */
InitializeCriticalSection(&cs);

void f()
{
/* Enter the critical section -- other threads are locked out */
EnterCriticalSection(&cs);

/* Do some thread-safe processing! */

/* Leave the critical section -- other threads can now EnterCriticalSection() */
LeaveCriticalSection(&cs);
}

/* Release system object when all finished -- usually at the end of the cleanup code */
DeleteCriticalSection(&cs);

Note that on Windows NT (not 9x/ME), the function TryEnterCriticalSection() can be used to attempt to enter the critical section. This function returns immediately so that the thread can do other things if it fails to enter the critical section (usually due to another thread having locked it). With the pthreads library, the equivalent function is pthread_mutex_trylock(). Note that the use of a CriticalSection is not the same as a Win32 Mutex, which is an object used for inter-process synchronization. A Win32 CriticalSection is for intra-process synchronization (and is much faster as far as lock times), however it cannot be shared across processes.

[edit] Kernel Level Critical Sections

Typically, critical sections prevent process and thread migration between processors and the preemption of processes and threads by interrupts and other processes and threads.

Critical sections often allow nesting. Nesting allows multiple critical sections to be entered and exited at little cost.

If the scheduler interrupts the current process or thread in a critical section, the scheduler will either allow the process or thread to run to completion of the critical section, or it will schedule the process or thread for another complete quantum. The scheduler will not migrate the process or thread to another processor, and it will not schedule another process or thread to run while the current process or thread is in a critical section.

Similarly, if an interrupt occurs in a critical section, the interrupt's information is recorded for future processing, and execution is returned to the process or thread in the critical section. Once the critical section is exited, and in some cases the scheduled quantum completes, the pending interrupt will be executed.

Since critical sections may execute only on the processor on which they are entered, synchronization is only required within the executing processor. This allows critical sections to be entered and exited at almost zero cost. No interprocessor synchronization is required, only instruction stream synchronization. Most processors provide the required amount of synchronization by the simple act of interrupting the current execution state. This allows critical sections in most cases to be nothing more than a per processor count of critical sections entered.

Performance enhancements include executing pending interrupts at the exit of all critical sections and allowing the scheduler to run at the exit of all critical sections. Furthermore, pending interrupts may be transferred to other processors for execution.

Critical sections should not be used as a long-lived locking primitive. They should be short enough that the critical section will be entered, executed, and exited without any interrupts occurring, from neither hardware much less the scheduler.

Kernel Level Critical Sections are the base of the software lockout issue.

[edit] See also

[edit] External links

Critical Section documentation on the MSDN Library homepage: http://msdn2.microsoft.com/en-us/library/ms682530.aspx



chatler 2009-05-11 11:48 鍙戣〃璇勮
]]>
榪涚▼鍜岀嚎紼嬬紪紼?lt;杞?gt;http://www.shnenglu.com/beautykingdom/archive/2009/03/26/77949.htmlchatlerchatlerThu, 26 Mar 2009 07:56:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/03/26/77949.htmlhttp://www.shnenglu.com/beautykingdom/comments/77949.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/03/26/77949.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/77949.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/77949.html闃呰鍏ㄦ枃

chatler 2009-03-26 15:56 鍙戣〃璇勮
]]>
OS FAQhttp://www.shnenglu.com/beautykingdom/archive/2009/03/15/76637.htmlchatlerchatlerSun, 15 Mar 2009 05:20:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/03/15/76637.htmlhttp://www.shnenglu.com/beautykingdom/comments/76637.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/03/15/76637.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/76637.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/76637.htmlV6:::::::::

1.3 What is the main advantage of multiprogramming?
Answer: Multiprogramming makes efficient use of the CPU by overlapping the demands for the CPU and its I/O devices from various users. It attempts to increase CPU utilization by always having something for the CPU to execute.

1.5 In a multiprogramming and time-sharing environment, several users share the system simultaneously. This situation can result in various security problems.
a. What are two such problems?
b. Can we ensure the same degree of security in a time-shared machine as we have in a
dedicated machine? Explain your answer.
Answer:
a. Stealing or copying one’s programs or data; using system resources (CPU, memory, disk space, peripherals) without proper accounting.
b. Probably not, since any protection scheme devised by humans can inevitably be broken by a human, and the more complex the scheme, the more difficult it is to feel
confident of its correct implementation.

1.9 Describe the differences between symmetric and asymmetric multiprocessing. What are three advantages and one disadvantage of multiprocessor systems?
Answer: Symmetric multiprocessing treats all processors as equals, and I/O can be processed on any CPU. Asymmetric multiprocessing has one master CPU and the remainder CPUs are slaves. The master distributes tasks among the slaves, and I/O is usually done by themaster only. Multiprocessors can savemoney by not duplicating power supplies, housings, and peripherals. They can execute programs more quickly and can have increased reliability. They are also more complex in both hardware and software than uniprocessor systems.

1.10 What is the main difficulty that a programmer must overcome in writing an operating system for a real-time environment?
Answer: The main difficulty is keeping the operating system within the fixed time constraints of a real-time system. If the system does not complete a task in a certain time
frame, it may cause a breakdown of the entire system it is running. Therefore when writing an operating system for a real-time system, the writer must be sure that his scheduling schemes don’t allow response time to exceed the time constraint.
 
2.1 Prefetching is a method of overlapping the I/O of a job with that job’s own computation.
The idea is simple. After a read operation completes and the job is about to start operating on the data, the input device is instructed to begin the next read immediately. The CPU and input device are then both busy. With luck, by the time the job is ready for the next data item, the input device will have finished reading that data item. The CPU can then begin processing the newly read data, while the input device starts to read the following data.
A similar idea can be used for output. In this case, the job creates data that are put into a buffer until an output device can accept them. Compare the prefetching scheme with the spooling scheme, where the CPU overlaps the input of one job with the computation and output of other jobs.
Answer: Prefetching is a user-based activity, while spooling is a system-based activity.
Spooling is a much more effective way of overlapping I/O and CPU operations.

2.3 What are the differences between a trap and an interrupt? What is the use of each function?
   An interrupt is a hardware-generated change-of-flow within the system. An interrupt handler is summoned to deal with the cause of the interrupt; control is then re-turned to the interrupted context and instruction. 
   A trap is a software-generated interrupt.
   An interrupt can be used to signal the completion of an I/O to obviate the need for device polling. 
   A trap can be used to call operating system routines or to catch arithmetic errors.

V7::::::::

19.3 The Linux 2.6 kernel can be built with no virtual memory system. Explain how this feature may appeal to designers of real-time systems.
Answer: By disabling the virtual memory system, processes are guaranteed to have portions of its address space resident in physical memory.
This results in a system that does not suffer from page faults and therefore does not have to deal with unanticipated costs corresponding to paging the address space.
 The resulting system is appealing to designers of real-time systems who prefer to avoid variability in performance.






chatler 2009-03-15 13:20 鍙戣〃璇勮
]]>
鍒涘緩綰跨▼鍑芥暟鐨勪嬌鐢?/title><link>http://www.shnenglu.com/beautykingdom/archive/2008/09/26/62790.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Fri, 26 Sep 2008 01:07:00 GMT</pubDate><guid>http://www.shnenglu.com/beautykingdom/archive/2008/09/26/62790.html</guid><wfw:comment>http://www.shnenglu.com/beautykingdom/comments/62790.html</wfw:comment><comments>http://www.shnenglu.com/beautykingdom/archive/2008/09/26/62790.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/beautykingdom/comments/commentRss/62790.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/beautykingdom/services/trackbacks/62790.html</trackback:ping><description><![CDATA[<p>搴旂敤鍦烘櫙錛?br>        鍋氫簡(jiǎn)涓涓猚lient錛屽幓鍜孧essage Middleware閫氫俊錛屽疄鏃惰幏鍙栨秷鎭腑闂翠歡浠opic鏂瑰紡(涓嶆槸Queue錛屽Message Middleware鏉ヨ錛孮ueue鏄彂閫佷竴涓猟estination錛宼opic鍙互鍙戝涓?銆?/p> <p>        浠庡疄鏃惰幏鍙栫殑瑙掑害鏉ヨ錛岄渶瑕佸惎涓涓嚎紼嬶紝鎺ユ敹Message Middleware娑堟伅錛岀劧鍚庡仛鍦烘櫙闇瑕佺殑澶勭悊銆傚垱寤虹嚎紼嬬殑鍑芥暟濡備笅鎵紺猴細(xì)<br> // for compilers which have it, we should use C RTL function for thread<br>// creation instead of Win32 API one because otherwise we will have memory<br>// leaks if the thread uses C RTL (and most threads do)<br>#if defined(__VISUALC__) || \<br>    (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \<br>    (defined(__GNUG__) && defined(__MSVCRT__))<br>    typedef unsigned (__stdcall *RtlThreadStart)(void *);</p> <p>    m_hThread = (HANDLE)_beginthreadex(NULL, 0,<br>                                       (RtlThreadStart)<br>                                       wxThreadInternal::WinThreadStart,<br>                                       thread, CREATE_SUSPENDED,<br>                                       (unsigned int *)&m_tid);<br>#else // compiler doesn't have _beginthreadex<br>    m_hThread = ::CreateThread<br>                  (<br>                    NULL,                               // default security<br>                    0,                                  // default stack size<br>                    (LPTHREAD_START_ROUTINE)            // thread entry point<br>                    wxThreadInternal::WinThreadStart,   // the function that runs under thread<br>                    (LPVOID)thread,                     // parameter<br>                    CREATE_SUSPENDED,                   // flags<br>                    &m_tid                              // [out] thread id<br>                  );<br>#endif // _beginthreadex/CreateThread<br>note: there should be a function definition before these lines.eg:<br> DWORD wxThreadInternal::WinThreadStart(wxThread *thread)</p> <img src ="http://www.shnenglu.com/beautykingdom/aggbug/62790.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/beautykingdom/" target="_blank">chatler</a> 2008-09-26 09:07 <a href="http://www.shnenglu.com/beautykingdom/archive/2008/09/26/62790.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>感谢您访问我们的网站,您可能还对以下资源感兴趣:</p> <a href="http://www.shnenglu.com/" title="精品视频久久久久">精品视频久久久久</a> <div class="friend-links"> </div> </div> </footer> <a href="http://www.vnxz.cn" target="_blank">久久99国产精品久久99</a>| <a href="http://www.leaow.cn" target="_blank">99久久婷婷国产综合精品草原</a>| <a href="http://www.newcr.cn" target="_blank">久久99热这里只有精品66</a>| <a href="http://www.goodleg.cn" target="_blank">日韩电影久久久被窝网</a>| <a href="http://www.pyspiderking.cn" target="_blank">国内精品久久久久久久coent</a>| <a href="http://www.21kexue.cn" target="_blank">91精品国产91热久久久久福利 </a>| <a href="http://www.zingl.cn" target="_blank">久久精品国产亚洲av麻豆图片 </a>| <a href="http://www.ut100.cn" target="_blank">99久久超碰中文字幕伊人 </a>| <a href="http://www.rereyy.cn" target="_blank">亚洲午夜久久久久久久久电影网</a>| <a href="http://www.taotaoduoduo.cn" target="_blank">久久久久久国产精品美女</a>| <a href="http://www.mpyx.net.cn" target="_blank">亚洲va中文字幕无码久久不卡</a>| <a href="http://www.my163.net.cn" target="_blank">久久久久亚洲Av无码专</a>| <a href="http://www.wxpk10.cn" target="_blank">久久国产精品久久</a>| <a href="http://www.xwgqzp.cn" target="_blank">久久综合久久伊人</a>| <a href="http://www.liangfang013.cn" target="_blank">久久久精品人妻一区二区三区蜜桃 </a>| <a href="http://www.td16yf8g.cn" target="_blank">精品国产日韩久久亚洲</a>| <a href="http://www.beiju123.cn" target="_blank">亚洲人成伊人成综合网久久久</a>| <a href="http://www.jingxi.jx.cn" target="_blank">久久大香香蕉国产</a>| <a href="http://www.jy06.cn" target="_blank">yellow中文字幕久久网</a>| <a href="http://www.igdl.cn" target="_blank">亚洲伊人久久综合影院</a>| <a href="http://www.tehmd.cn" target="_blank">精品久久久久久国产潘金莲</a>| <a href="http://www.jxjkyt.cn" target="_blank">99久久国产综合精品五月天喷水</a>| <a href="http://www.hzskc.cn" target="_blank">伊人久久大香线蕉AV一区二区 </a>| <a href="http://www.njyy888.com.cn" target="_blank">国产精品久久久天天影视</a>| <a href="http://www.donghuitong.cn" target="_blank">国产精品狼人久久久久影院</a>| <a href="http://www.hnxxzw.cn" target="_blank">三级韩国一区久久二区综合 </a>| <a href="http://www.iteethle.cn" target="_blank">国产精品免费久久久久久久久</a>| <a href="http://www.lepinw.cn" target="_blank">日本久久中文字幕</a>| <a href="http://www.chenxipeng.cn" target="_blank">久久久久亚洲av无码专区</a>| <a href="http://www.qp8978.cn" target="_blank">久久精品国产99久久香蕉</a>| <a href="http://www.xiangxiangren.cn" target="_blank">日本一区精品久久久久影院</a>| <a href="http://www.y447.cn" target="_blank">亚洲国产成人久久综合一区77</a>| <a href="http://www.ykezn.cn" target="_blank">久久久久久亚洲AV无码专区</a>| <a href="http://www.jsjingkou.cn" target="_blank">狠狠色综合网站久久久久久久</a>| <a href="http://www.yizhu888.cn" target="_blank">久久www免费人成看片</a>| <a href="http://www.pingpangq.cn" target="_blank">久久成人国产精品二三区</a>| <a href="http://www.csmfy.cn" target="_blank">狠狠色丁香婷婷久久综合五月</a>| <a href="http://www.cnsei.cn" target="_blank">国产精品久久99</a>| <a href="http://www.bttzc.cn" target="_blank">久久午夜夜伦鲁鲁片免费无码影视</a>| <a href="http://www.yzx777.cn" target="_blank">国产精品一区二区久久精品</a>| <a href="http://www.qhsy217.cn" target="_blank">久久精品国产免费观看</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>