• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            doing5552

            記錄每日點(diǎn)滴,不枉人生一世

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              73 Posts :: 0 Stories :: 94 Comments :: 0 Trackbacks

            公告

            常用鏈接

            留言簿(24)

            我參與的團(tuán)隊(duì)

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 454869
            • 排名 - 48

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            昨天在上篇blog里描寫了如何把STL容器放到共享內(nèi)存里去,不過由于好久不寫blog,發(fā)覺詞匯組織能力差了很多,不少想寫的東西寫的很零散,今天剛好翻看自己的書簽,看到一篇挺老的文章,不過從共享內(nèi)存到STL容器講述得蠻全面,還提供了學(xué)習(xí)的實(shí)例,所以順便翻譯過來,并附上原文地址

            共享內(nèi)存(shm)是當(dāng)前主流UNIX系統(tǒng)中的一種IPC方法,它允許多個(gè)進(jìn)程把同一塊物理內(nèi)存段(segment)映射(map)到它們的地址空間中去。既然內(nèi)存段對(duì)于各自附著(attach)的進(jìn)程是共享的,這些進(jìn)程可以很方便的通過這塊共享內(nèi)存上的共有數(shù)據(jù)進(jìn)行通信。因此,顧名思義,共享內(nèi)存就是進(jìn)程之間共享的一組內(nèi)存段。當(dāng)一個(gè)進(jìn)程附著到一塊共享內(nèi)存上后,它得到一個(gè)指向這塊共享內(nèi)存的指針;該進(jìn)程可以像使用其他內(nèi)存一樣使用這塊共享內(nèi)存。當(dāng)然,由于這塊內(nèi)存同樣會(huì)被其他進(jìn)程訪問或?qū)懭耄员仨氁⒁膺M(jìn)程同步問題。

            參考如下代碼,這是UNIX系統(tǒng)上使用共享內(nèi)存的一般方法(注:本文調(diào)用的是POSIX函數(shù)):

            //Get shared memory id
                        //shared memory key
                        const key_t ipckey = 24568;
                        //shared memory permission; can be
                        //read and written by anybody
                        const int perm = 0666;
                        //shared memory segment size
                        size_t shmSize = 4096;
                        //Create shared memory if not
                        //already created with specified
                        //permission
                        int shmId = shmget
                        (ipckey,shmSize,IPC_CREAT|perm);
                        if (shmId ==-1) {
                        //Error
                        }
                         
                        //Attach the shared memory segment
                         
                        void* shmPtr = shmat(shmId,NULL,0);
                         
                        struct commonData* dp =  (struct commonData*)shmPtr;
                         
                        //detach shared memory
                        shmdt(shmPtr);

            存放在共享內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)

            當(dāng)保存數(shù)據(jù)到共享內(nèi)存中時(shí)需要留意,參考如下結(jié)構(gòu):

            struct commonData {
                        int sharedInt;
                        float  sharedFloat;
                        char* name;
                        Struct CommonData* next;
                        };

            進(jìn)程A把數(shù)據(jù)寫入共享內(nèi)存:

            //Attach shared memory
                        struct commonData* dp =
                        (struct commonData*) shmat
                        (shmId,NULL,0);
                         
                        dp->sharedInt = 5;
                        .
                        .
                        dp->name = new char [20];
                        strcpy(dp->name,"My Name");
                         
                        dp->next = new struct commonData();

            稍后,進(jìn)程B把數(shù)據(jù)讀出:

            struct commonData* dp =
                        (struct commonData*) shmat
                        (shmId,NULL,0);
                         
                        //count = 5;
                        int count = dp->sharedInt;
                        //problem
                        printf("name = [%s]\n",dp->name);
                        dp = dp->next;  //problem

            結(jié)構(gòu) commonData 的成員 name 和指向下一個(gè)結(jié)構(gòu)的 next 所指向的內(nèi)存分別從進(jìn)程A的地址空間中的堆上分配,顯然 name 和 next 指向的內(nèi)存也只有進(jìn)程A可以訪問。當(dāng)進(jìn)程B訪問 dp->name 或者 dp->next 時(shí)候,由于它在訪問自己地址空間以外的內(nèi)存空間,所以這將是非法操作(memory violation),它無法正確得到 namenext 所指向的內(nèi)存。因此,所有的共享內(nèi)存中的指針必須同樣指向共享內(nèi)存中的地址。(這也是為什么包含虛函數(shù)繼承的C++類對(duì)象不能放到共享內(nèi)存中的原因——這是另外一個(gè)話題。注:因?yàn)樘摵瘮?shù)的具體實(shí)現(xiàn)可能會(huì)在其他的內(nèi)存空間中)由于這些條件限制,放入共享內(nèi)存中的結(jié)構(gòu)應(yīng)該簡(jiǎn)單簡(jiǎn)單。(注:我覺得最好避免使用指針)

            共享內(nèi)存中的STL容器

            想像一下把STL容器,例如map, vector, list等等,放入共享內(nèi)存中,IPC一旦有了這些強(qiáng)大的通用數(shù)據(jù)結(jié)構(gòu)做輔助,無疑進(jìn)程間通信的能力一下子強(qiáng)大了很多。我們沒必要再為共享內(nèi)存設(shè)計(jì)其他額外的數(shù)據(jù)結(jié)構(gòu),另外,STL的高度可擴(kuò)展性將為IPC所驅(qū)使。STL容器被良好的封裝,默認(rèn)情況下有它們自己的內(nèi)存管理方案。當(dāng)一個(gè)元素被插入到一個(gè)STL列表(list)中時(shí),列表容器自動(dòng)為其分配內(nèi)存,保存數(shù)據(jù)。考慮到要將STL容器放到共享內(nèi)存中,而容器卻自己在堆上分配內(nèi)存。一個(gè)最笨拙的辦法是在堆上構(gòu)造STL容器,然后把容器復(fù)制到共享內(nèi)存,并且確保所有容器的內(nèi)部分配的內(nèi)存指向共享內(nèi)存中的相應(yīng)區(qū)域,這基本是個(gè)不可能完成的任務(wù)。例如下邊進(jìn)程A所做的事情:

            //Attach to shared memory
                        void* rp = (void*)shmat(shmId,NULL,0);
                        //Construct the vector in shared
                        //memory using placement new
                        vector<int>* vpInA = new(rp) vector<int>*;
                        //The vector is allocating internal data
                        //from the heap in process A's address
                        //space to hold the integer value
                        (*vpInA)[0] = 22;

            然后進(jìn)程B希望從共享內(nèi)存中取出數(shù)據(jù):

            vector<int>* vpInB =
                        (vector<int>*) shmat(shmId,NULL,0);
                         
                        //problem - the vector contains internal 
                        //pointers allocated in process A's address 
                        //space and are invalid here 
                        int i = *(vpInB)[0];

            重用STL allocator

            進(jìn)一步考察STL容器,我們發(fā)現(xiàn)它的模板定義中有第二個(gè)默認(rèn)參數(shù),也就是allocator 類,該類實(shí)際是一個(gè)內(nèi)存分配模型。默認(rèn)的allocator是從堆上分配內(nèi)存(注:這就是STL容器的默認(rèn)表現(xiàn),我們甚至可以改造它從一個(gè)網(wǎng)絡(luò)數(shù)據(jù)庫(kù)中分配空間,保存數(shù)據(jù))。下邊是 vector 類的一部分定義:

            template<class T, class A = allocator<T> >
                        class vector {
                        //other stuff
                        };

            考慮如下聲明:

            //User supplied allocator myAlloc
                        vector<int,myAlloc<int> > alocV;

            假設(shè) myAlloc 從共享內(nèi)存上分配內(nèi)存,則 alocV 將完全在共享內(nèi)存上被構(gòu)造,所以進(jìn)程A可以如下:

            //Attach to shared memory
                        void* rp = (void*)shmat(shmId,NULL,0);
                        //Construct the vector in shared memory
                        //using placement new
                        vector<int>* vpInA =
                        new(rp) vector<int,myAlloc<int> >*;
                        //The vector uses myAlloc<int> to allocate
                        //memory for its internal data structure
                        //from shared memory
                        (*v)[0] = 22;

            進(jìn)程B可以如下讀出數(shù)據(jù):

            vector<int>* vpInB =
                        (vector<int,myAlloc<int> >*) shmat
                        (shmId,NULL,0);
                         
                        //Okay since all of the vector is
                        //in shared memory
                        int i = *(vpInB)[0];

            所有附著在共享內(nèi)存上的進(jìn)程都可以安全的使用該vector。在這個(gè)例子中,該類的所有內(nèi)存都在共享內(nèi)存上分配,同時(shí)可以被其他的進(jìn)程訪問。只要提供一個(gè)用戶自定義的allocator,任何STL容器都可以安全的放置到共享內(nèi)存上。

            一個(gè)基于共享內(nèi)存的STL Allocator

            清單 shared_allocator.hh 是一個(gè)STL Allocator的實(shí)現(xiàn),SharedAllocator 是一個(gè)模板類。而 Pool 類完成共享內(nèi)存的分配與回收。

            template<class T>class SharedAllocator {
                        private:
                        Pool pool_;    // pool of elements of sizeof(T)
                        public:
                        typedef T value_type;
                        typedef unsigned int  size_type;
                        typedef ptrdiff_t difference_type;
                        typedef T* pointer;
                        typedef const T* const_pointer;
                        typedef T& reference;
                        typedef const T& const_reference;
                        pointer address(reference r) const { return &r; }
                        const_pointer address(const_reference r) const {return &r;}
                        SharedAllocator() throw():pool_(sizeof(T)) {}
                        template<class U> SharedAllocator
                        (const SharedAllocator<U>& t) throw():
                        pool_(sizeof(T)) {}
                        ~SharedAllocator() throw() {};
                        // space for n Ts
                        pointer allocate(size_t n, const void* hint=0)
                        {
                        return(static_cast<pointer> (pool_.alloc(n)));
                        }
                        // deallocate n Ts, don't destroy
                        void deallocate(pointer p,size_type n)
                        {
                        pool_.free((void*)p,n);
                        return;
                        }
                        // initialize *p by val
                        void construct(pointer p, const T& val) { new(p) T(val); }
                        // destroy *p but don't deallocate
                        void destroy(pointer p) { p->~T(); }
                        size_type max_size() const throw()
                        {
                        pool_.maxSize();
                        }
                        template<class U>
                        // in effect: typedef SharedAllocator<U> other
                        struct rebind { typedef SharedAllocator<U> other; };
                        };
                         
                        template<class T>bool operator==(const SharedAllocator<T>& a,
                        const SharedAllocator<T>& b) throw()
                        {
                        return(a.pool_ == b.pool_);
                        }
                        template<class T>bool operator!=(const SharedAllocator<T>& a,
                        const SharedAllocator<T>& b) throw()
                        {
                        return(!(a.pool_ == b.pool_));
                        }

            清單pool.hh是 Pool 類定義,其中靜態(tài)成員shm_ 是類型 shmPool,保證每個(gè)進(jìn)程只有唯一的一個(gè)shmPool 實(shí)例。shmPool ctor 創(chuàng)建并附著所需大小的內(nèi)存到共享內(nèi)存上。共享內(nèi)存的參數(shù),比如 鍵值、段數(shù)目、段大小,都通過環(huán)境變量傳遞給 shmPool ctor。成員 segs_ 是共享段的數(shù)目,segSize_是每個(gè)共享段的大小,成員path_key_ 用來創(chuàng)建唯一的 ipckeyshmPool 為每個(gè)共享段創(chuàng)建一個(gè)信號(hào)量(semaphore)用于同步。shmPool 還在為每個(gè)共享段構(gòu)造了一個(gè) Chunk 類,一個(gè) Chunk代表一個(gè)共享段。每個(gè)共享段的標(biāo)識(shí)是shmId_, 信號(hào)量 semId_控制該段的訪問許可,一個(gè)指向 Link 結(jié)構(gòu)的指針表明 Chunk類的剩余列表。

            class Pool {
                        private:
                        class shmPool {
                        private:
                        struct Container {
                        containerMap* cont;
                        };
                        class Chunk {
                        public:
                        Chunk()
                        Chunk(Chunk&);
                        ~Chunk() {}
                        void* alloc(size_t size);
                        void free (void* p,size_t size);
                        private:
                        int shmId_;
                        int semId_;
                        int lock_()
                        };
                        int key_;
                        char* path_;
                        Chunk** chunks_;
                        size_t segs_;
                        size_t segSize_;
                        Container* contPtr_;
                        int contSemId_;
                        public:
                        shmPool();
                        ~shmPool();
                        size_t maxSize();
                        void* alloc(size_t size);
                        void free(void* p, size_t size);
                        int shmPool::lockContainer()
                        int unLockContainer()
                        containerMap* getContainer()
                        void shmPool::setContainer(containerMap* container)
                        };
                         
                        private:
                        static shmPool shm_;
                        size_t elemSize_;
                        public:
                        Pool(size_t elemSize);
                        ~Pool() {}
                        size_t maxSize();
                        void* alloc(size_t size);
                        void free(void* p, size_t size);
                        int lockContainer();
                        int unLockContainer();
                        containerMap* getContainer();
                        void setContainer(containerMap* container);
                        };
                        inline bool operator==(const Pool& a,const Pool& b)
                        {
                        return(a.compare(b));
                        }

            把STL容器放入共享內(nèi)存

            假設(shè)進(jìn)程A在共享內(nèi)存中放入了數(shù)個(gè)容器,進(jìn)程B如何找到這些容器呢?一個(gè)方法就是進(jìn)程A把容器放在共享內(nèi)存中的確定地址上(fixed offsets),則進(jìn)程B可以從該已知地址上獲取容器。另外一個(gè)改進(jìn)點(diǎn)的辦法是,進(jìn)程A先在共享內(nèi)存某塊確定地址上放置一個(gè)map容器,然后進(jìn)程A再創(chuàng)建其他容器,然后給其取個(gè)名字和地址一并保存到這個(gè)map容器里。進(jìn)程B知道如何獲取該保存了地址映射的map容器,然后同樣再根據(jù)名字取得其他容器的地址。清單container_factory.hh是一個(gè)容器工廠類。類Pool的方法setContainer把map容器放置在一個(gè)已知地址上,方法getContainer可以重新獲取這個(gè)map。該工廠的方法用來在共享內(nèi)存中創(chuàng)建、獲取和刪除容器。當(dāng)然,傳遞給容器工廠的容器需要以SharedAllocator作為allocator。

            struct keyComp {
                        bool operator()(const char* key1,const char* key2)
                        {
                        return(strcmp(key1,key2) < 0);
                        }
                        };
                        class containerMap: public map<char*,void*,keyComp,SharedAllocator<char* > > {};
                        class containerFactory {
                        public:
                        containerFactory():pool_(sizeof(containerMap)){}
                        ~containerFactory() {}
                        template<class Container> Container* createContainer
                        (char* key,Container* c=NULL);
                        template<class Container> Container* getContainer
                        (char* key,Container* c=NULL);
                        template<class Container> int removeContainer
                        (char* key,Container* c=NULL);
                        private:
                        Pool pool_;
                        int lock_();
                        int unlock_();
                        };

            結(jié)論

            本文描述的方案可以在共享內(nèi)存中創(chuàng)建STL容器,其中的一個(gè)缺陷是,在分配共享內(nèi)存之前,應(yīng)該保證共享內(nèi)存的總大小(segs_* segSize_)大于你要保存STL容器的最大長(zhǎng)度,因?yàn)橐坏╊?code>Pool 超出了共享內(nèi)存的,該類無法再分配新的共享內(nèi)存。

            完整的源代碼可以從這里下載:www.cuj.com/code

            參考文獻(xiàn)

            • Bjarne Stroustrup. The C++ Programming Language, Third Edition (Addison-Wesley, 1997).
            • Matthew H. Austern. Generic Programming and the STL: Using and
              Extending the C++ Standard Template Library (Addison-Wesley, 1999).

            關(guān)于作者

            Grum Ketema has Masters degrees in Electrical Engineering and Computer Science. With 17 years of experience in software development, he has been using C since 1985, C++ since 1988, and Java since 1997. He has worked at AT&T Bell Labs, TASC, Massachusetts Institute of Technology, SWIFT, BEA Systems, and Northrop.

            posted on 2010-07-24 21:14 doing5552 閱讀(8976) 評(píng)論(6)  編輯 收藏 引用

            Feedback

            # re: [翻譯]把STL容器放入共享內(nèi)存 2012-03-01 16:42 弧光
            博主,你好!,首先謝謝你翻譯并轉(zhuǎn)載這篇文章,本人受益匪淺。對(duì)于博文中有一段內(nèi)容不理解,不知道博主是否能解釋或者有實(shí)際的例子代碼?
            以下部分為本人疑惑的地方:
            把STL容器放入共享內(nèi)存
            假設(shè)進(jìn)程A在共享內(nèi)存中放入了數(shù)個(gè)容器,進(jìn)程B如何找到這些容器呢?一個(gè)方法就是進(jìn)程A把容器放在共享內(nèi)存中的確定地址上(fixed offsets),則進(jìn)程B可以從該已知地址上獲取容器。另外一個(gè)改進(jìn)點(diǎn)的辦法是,進(jìn)程A先在共享內(nèi)存某塊確定地址上放置一個(gè)map容器,然后進(jìn)程A再創(chuàng)建其他容器,然后給其取個(gè)名字和地址一并保存到這個(gè)map容器里。進(jìn)程B知道如何獲取該保存了地址映射的map容器,然后同樣再根據(jù)名字取得其他容器的地址。清單container_factory.hh是一個(gè)容器工廠類。類Pool的方法setContainer把map容器放置在一個(gè)已知地址上,方法getContainer可以重新獲取這個(gè)map。該工廠的方法用來在共享內(nèi)存中創(chuàng)建、獲取和刪除容器。當(dāng)然,傳遞給容器工廠的容器需要以SharedAllocator作為allocator。

            本人的聯(lián)系方式:
            msn:duoduono2002@hotmail.com
            qq:93320694
            mail:hewg@szkingdom.com
            以上任何方式均能聯(lián)系到本人,歡迎博主與其他看客來幫忙解決此疑問,謝謝!!!  回復(fù)  更多評(píng)論
              

            # re: [翻譯]把STL容器放入共享內(nèi)存[未登錄] 2014-04-30 10:35 none
            這篇文章最大的問題就在于它假定每個(gè)進(jìn)程attach上這個(gè)共享內(nèi)存時(shí)得到的地址偏移量是相同的。但實(shí)際上往往是不同的,所以這篇文章中給出的方法是根本不能正常使用的。

            當(dāng)兩個(gè)進(jìn)程中同一塊共享內(nèi)存映射的地址偏移量不同的時(shí)候,任何放入共享內(nèi)存的指針都無法直接使用。像STL這種復(fù)雜的容器庫(kù),里面大量用到了指針,如果不做偏移量調(diào)整的話幾乎可以肯定會(huì)出現(xiàn)嚴(yán)重問題。  回復(fù)  更多評(píng)論
              

            # re: [翻譯]把STL容器放入共享內(nèi)存 2014-08-05 19:33 zzyoucan
            今天也在看vector怎么存入共享內(nèi)存,不過boost可以,這個(gè)只是存入單個(gè)vector要是一個(gè)結(jié)構(gòu)體就不太好弄了,還是把vector換成數(shù)組吧。  回復(fù)  更多評(píng)論
              

            # re: [翻譯]把STL容器放入共享內(nèi)存[未登錄] 2014-08-06 14:39 a
            歪門邪道,正途是進(jìn)行序列化操作  回復(fù)  更多評(píng)論
              

            # re: [翻譯]把STL容器放入共享內(nèi)存 2016-01-15 11:50 ABC
            @none
            如果是同樣的映射 偏移怎么可能是不同的呢?如果 按你這種說法 共享內(nèi)存還能用? 你要如何才能算出里面的數(shù)據(jù)? 不說放map 這種了 你放字符串 放 struct 都沒法用。 別誤人 好不~!  回復(fù)  更多評(píng)論
              

            # re: [翻譯]把STL容器放入共享內(nèi)存 2016-06-14 22:56 DDD
            @ABC
            人家是說的映射之后的基地址吧,共享內(nèi)存內(nèi)的 offset 是一樣的,但是在虛擬內(nèi)存中的地址很可能是不一樣的  回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久久久久久久久久| 久久精品亚洲日本波多野结衣| 精品久久久久香蕉网| 伊人色综合九久久天天蜜桃| 久久久99精品成人片中文字幕| 色综合久久综合网观看| 久久99国产精品久久久| 久久久久久亚洲Av无码精品专口 | 久久国产精品99久久久久久老狼| 亚洲中文字幕无码久久综合网| 久久亚洲精品成人无码网站| 尹人香蕉久久99天天拍| 国产精品久久新婚兰兰| 久久久久久久久久久精品尤物 | 91久久精品国产91性色也| 久久精品国产亚洲av高清漫画| 国产69精品久久久久9999APGF| 伊人久久大香线蕉亚洲五月天| 一本色道久久88精品综合| 熟妇人妻久久中文字幕| 99久久99这里只有免费费精品| 国产情侣久久久久aⅴ免费| www.久久热.com| 国产一区二区精品久久凹凸| 久久激情五月丁香伊人| 狠狠色丁香婷婷久久综合| 日韩久久久久久中文人妻| 精品一区二区久久| 理论片午午伦夜理片久久| 国产69精品久久久久APP下载| 色综合久久无码五十路人妻| 久久精品成人国产午夜| 久久久久久久久久久免费精品 | 国产精品99久久99久久久| 亚洲狠狠久久综合一区77777| 精品久久久久久无码中文字幕| 久久青青色综合| 久久天堂电影网| 中文字幕无码精品亚洲资源网久久 | 久久夜色tv网站| 狠狠色丁香久久婷婷综合_中|