• <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>

            Jiwu Bu

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              32 隨筆 :: 0 文章 :: 25 評論 :: 0 Trackbacks
            進程一將數據寫入共享內存,進程二從共享內存讀取數據

            #include <sys/ipc.h>
            #include 
            <sys/shm.h>
            #include 
            <unistd.h>
            #include 
            <errno.h>
            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <string.h>
            #include 
            <errno.h>
            #include 
            <sys/types.h>

            using namespace std;


            int main(int argc, char* argv[])
            {
                key_t key 
            = ftok("/etc/passwd"'a');
                
            if-1 == key )
                {
                    fprintf(stderr, 
            "Creat Key Error:%s\n", strerror(errno));
                    
            return -1;
                }
                
                
            /*
                 if your IPC are used by processes related bya fork() 
                (parent-children-grandchildren) use IPC_PRIVATE as the key
                
            */
                
            int shm_id=shmget( key, 1024, IPC_CREAT |0660);

                
            if( shm_id == -1 )
                {
                    
            if ( shmctl(shm_id, IPC_RMID, NULL) == -1 )
                    {
                        fprintf(stderr, 
            "shmctl remove shmid error:%s\n", strerror(errno ));
                        
            return -1;
                    }

                    shm_id
            =shmget( key, 1024, IPC_CREAT |0660);
                    
            if( shm_id == -1 )
                    {
                        fprintf(stderr, 
            "shmget create error:%s\n", strerror(errno) );
                        
            return -1;
                    }
                }

                
            char* pMap = (char *)shmat(shm_id, NULL, 0);

                
            if( (int)pMap == -1)
                {
                    fprintf(stderr, 
            "shmat create error:%s\n", strerror(errno) );
                    shmctl(shm_id, IPC_RMID, NULL);
                    
            return -1;
                }

                strcpy( pMap, 
            "Bujiwu Swallow");

                getchar();

                shmdt(pMap);
                shmctl(shm_id, IPC_RMID, NULL);

                
            return 0;
            }

            #include <sys/ipc.h>
            #include 
            <sys/shm.h>
            #include 
            <unistd.h>
            #include 
            <errno.h>
            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <string.h>
            #include 
            <errno.h>
            #include 
            <sys/types.h>

            using namespace std;

            int main(int argc, char* argv[])
            {
                key_t key 
            = ftok("/etc/passwd"'a');
                
            if-1 == key )
                {
                    fprintf(stderr, 
            "Creat Key Error:%s\n", strerror(errno));
                    
            return -1;
                }
                
                
            /*
                 if your IPC are used by processes related bya fork() 
                (parent-children-grandchildren) use IPC_PRIVATE as the key
                
            */
                
            int shm_id=shmget( key, 1024, IPC_CREAT|0660);
                
                
            if( shm_id == -1 )
                {
                    
            if ( shmctl(shm_id, IPC_RMID, NULL) == -1 )
                    {
                        fprintf(stderr, 
            "shmctl remove shmid error:%s\n", strerror(errno ));
                        
            return -1;
                    }

                    shm_id
            =shmget( key, 1024, IPC_CREAT |0660);
                    
            if( shm_id == -1 )
                    {
                        fprintf(stderr, 
            "shmget create error:%s\n", strerror(errno) );
                        
            return -1;
                    }
                }
                
            char* pMap = (char *)shmat(shm_id, NULL, 0);

                
            if( (int)pMap == -1)
                {
                    fprintf(stderr, 
            "shmat create error:%s\n", strerror(errno) );
                    shmctl(shm_id, IPC_RMID, NULL);
                    
            return -1;
                }

                printf(
            "Shared  memory::%s\n", pMap);

                shmdt(pMap);
                shmctl(shm_id, IPC_RMID, NULL);

                
            return 0;
            }

            http://www.shnenglu.com/Files/bujiwu/ShareMemory.rar
            posted on 2009-11-08 09:39 bujiwu 閱讀(551) 評論(0)  編輯 收藏 引用 所屬分類: Linux
            漂亮人妻被中出中文字幕久久| 亚洲国产精品无码久久久不卡| 久久久久亚洲AV无码麻豆| 日韩精品久久无码中文字幕| 狠狠色丁香久久综合五月| 久久国产精品一区| 麻豆亚洲AV永久无码精品久久 | 亚洲中文字幕无码久久2020| 亚洲AV无码久久精品蜜桃| 国产AⅤ精品一区二区三区久久| 2020久久精品亚洲热综合一本 | 欧美久久久久久午夜精品| 精品人妻伦九区久久AAA片69| 久久精品国内一区二区三区| 精品多毛少妇人妻AV免费久久| 亚洲乱亚洲乱淫久久| 亚洲中文字幕久久精品无码APP | 久久国产成人精品麻豆| 亚洲欧洲久久久精品| 国产精品免费久久久久影院 | 久久午夜无码鲁丝片秋霞| 94久久国产乱子伦精品免费| 久久久久亚洲精品天堂| 久久婷婷是五月综合色狠狠| 国产三级观看久久| 99久久人人爽亚洲精品美女| 久久发布国产伦子伦精品| 亚洲综合精品香蕉久久网| 色综合久久夜色精品国产| 理论片午午伦夜理片久久 | 人妻系列无码专区久久五月天| 97超级碰碰碰久久久久| 精品国产VA久久久久久久冰| 色综合久久无码五十路人妻| 久久青青草原精品国产| 久久99国产精品尤物| 99久久精品国产高清一区二区 | 日韩精品久久久久久| 国产激情久久久久影院| 狠狠精品干练久久久无码中文字幕| 日本福利片国产午夜久久|