• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              32 隨筆 :: 0 文章 :: 25 評(píng)論 :: 0 Trackbacks
            進(jìn)程一將數(shù)據(jù)寫(xiě)入共享內(nèi)存,進(jìn)程二從共享內(nèi)存讀取數(shù)據(jù)

            #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 閱讀(552) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Linux
            国产高潮国产高潮久久久91 | 久久国产视屏| 中文字幕精品久久久久人妻| 99久久精品免费看国产一区二区三区 | 精品无码久久久久国产动漫3d| 精品久久久久中文字幕日本| 久久久久久国产精品无码下载| 久久99精品国产麻豆不卡| 国内精品久久久久影院日本| 久久国产精品无码一区二区三区| 天天做夜夜做久久做狠狠| 国内精品久久久久伊人av| 国产亚洲美女精品久久久 | 久久精品天天中文字幕人妻| 国产高潮久久免费观看| 久久综合给合久久国产免费| 久久久SS麻豆欧美国产日韩| 99久久777色| 国产成人无码精品久久久久免费| 久久无码中文字幕东京热 | 2022年国产精品久久久久| 午夜视频久久久久一区| 久久99国产精品一区二区| 久久99国产精品久久99果冻传媒 | 狠狠色综合久久久久尤物| 午夜不卡久久精品无码免费| 亚洲AV无码久久精品狠狠爱浪潮 | 99久久99这里只有免费费精品| 久久本道久久综合伊人| 久久99国产精品久久99| 99精品国产在热久久| 狠狠色噜噜狠狠狠狠狠色综合久久| 亚洲欧美一级久久精品| 亚洲国产日韩综合久久精品| 人妻无码精品久久亚瑟影视| 久久艹国产| 中文字幕无码久久精品青草 | 国产欧美一区二区久久| 国产成人久久激情91| 国产欧美一区二区久久| 91精品婷婷国产综合久久|