• <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>
            posts - 297,  comments - 15,  trackbacks - 0

            版權聲明:轉載時請以超鏈接形式標明文章原始出處和作者信息及本聲明
            http://xufish.blogbus.com/logs/40537374.html

            012號與013 號程序,分別是關于消息隊列和共享內存的

            /*********************程序相關信 息*********************
            程序編號:012
            程序編寫起始日期:2008.11.1
            程序編寫完成日期:2008.11.1
            程序修改日 期:                                   修改備注:
            程序目的:學習linux消息隊列通信
            所用主要函 數:msgget(),msgsnd(),msgrcv(),msgctl()
            程序 存疑:
            程序完成地點: 宿舍內
            *********************程序相關信息*********************/
            #include<sys/types.h>
            #include<sys/ipc.h>
            #include<sys/msg.h>
            #include<stdio.h>
            #include<string.h>
            #include<stdlib.h>
            int main()
            {
                int pid,msqid;//后者為消息隊列識別代號
                struct msgbuf
                {
                    long mtype;//消息類型
                    char mtext[20];//消息內容
                }send_buf,receive_buf;
                if((msqid=msgget(IPC_PRIVATE,0700))<0)//建立消息隊列
                {
                    printf("msgget建立消息隊列失敗。\n");
                    exit(1);
                }
                else
                    printf("msgget建立消息隊列成功,該消息隊列識別代號為%d。\n",msqid);
                if((pid=fork())<0)
                {
                    printf("fork()函數調用失敗!\n");
                    exit(2);
                }
                else if(pid>0)//父進程,發送消息到消息隊列
                {
                    send_buf.mtype=1;
                    strcpy(send_buf.mtext,"My test information");
                    printf("發送到消息隊列的信息內容為:%s\n",send_buf.mtext);
                    if(msgsnd(msqid,&send_buf,20,IPC_NOWAIT)<0)//發送send_buf中的信息到msqid 對應的消息隊列
                    {
                        printf("msgsnd消息發送失敗。\n");
                        exit(3);
                    }
                    else
                        printf("msgsnd消息發送成功。\n");
                    sleep(2);
                    exit(0);
                }
                else//子進程,從消息隊列中接收消息]
                {
                    sleep(2);//等待父進程發送消息完成
                    int infolen;//讀到的信息數據長度
                    if((infolen=msgrcv(msqid,&receive_buf,20,0,IPC_NOWAIT))<0)//自消息隊列 接收信息
                    {
                        printf("msgrcv讀取信息錯誤。\n");
                        exit(4);
                    }
                    else
                        printf("msgrcv讀取信息成功。\n");
                    printf("自消息隊列讀取到的內容為%s,共讀取%d個字節。\n",receive_buf.mtext,infolen);
                    if((msgctl(msqid,IPC_RMID,NULL))<0)//刪除msqid對應的消息隊列
                    {
                        printf("msgctl函數調用出現錯誤。\n");
                        exit(5);
                    }
                    else
                    {
                        printf("識別代號為%d的消息隊列已經被成功刪除。\n",msqid);
                        exit(0);
                    }
                }
            }
            /*********************程序運行結 果*********************
            [root@localhost temp]# ./msg
            msgget建立消息隊列成功,該消息隊列識別代號為 98304。
            發送到消息隊列的信息內容為:My test information
            msgsnd消息發送成功。
            msgrcv讀取信息成功。
            自消息隊列讀取到的內容為My test information,共讀取20個字節。
            識別代號為98304的消息 隊列已經被成功刪除。
            ***********************************************************/

            /********************* 程序相關信息*********************
            程序編號:013
            程序編寫起始日期:2008.11.1
            程序編寫完成日期:2008.11.1
            程序修改日 期:                                   修改備注:
            程序目的:學習linux共享內存
            所用主要函 數:shmget(),shmat(),shmctl(),shmdt()
            程序存 疑:
            程序完成地點: 宿舍內
            *********************程序相關信息*********************/
            #include<sys/ipc.h>
            #include<sys/shm.h>
            #include<stdlib.h>
            #include<string.h>
            #include<stdio.h>
            int main()
            {
                int pid,shmid;//后者為共享內存識別代號
                char *write_address;
                char *read_address;
                struct shmid_ds dsbuf;
                if((shmid=shmget(IPC_PRIVATE,32,0))<0)//分配共享內存
                {
                    printf("shmid共享內存分配出現錯誤。\n");
                    exit(1);
                }
                else
                    printf("shmid共享內存分配成功,共享內存識別代號為:%d。\n",shmid);
                if((pid=fork())<0)
                {
                    printf("fork函數調用出現錯誤!\n");
                    exit(2);
                }
                else if(pid>0)//父進程,向共享內存中寫入數據
                {
                    printf("父進程的ID是:%d\n",getpid());
                    write_address=(char *)shmat(shmid,NULL,0);//連接共享內存
                    if((int)write_address==-1)
                    {
                        printf("shmat連接共享內存錯誤。\n");
                        exit(3);
                    }
                    else
                    {
                        printf("shmat連接共享內存成功。\n");
                        strcpy(write_address,"我是寫入共享內存的測試數據");//將數據寫入共享內存
                        printf("寫入共享內存的信息為“%s”。\n",write_address);
                        if((shmdt((void *)write_address))<0)//斷開與共享內存的連接
                            printf("shmdt共享內存斷開錯誤。\n");
                        else
                            printf("shmdt共享內存斷開成功。\n");
                        sleep(2);
                        return;
                    }
                }
                else//子進程,從共享內存中讀取數據
                {
                    sleep(2);//等待父進程寫入共享內存完畢
                    printf("子進程ID是:%d\n",getpid());
                    if((shmctl(shmid,IPC_STAT,&dsbuf))<0)
                    {
                        printf("shmctl獲取共享內存數據結構出現錯誤。\n");
                        exit(4);
                    }
                    else
                    {
                        printf("shmctl獲取共享內存數據結構成功。\n建立這個共享內存的進程ID是:%d\n",dsbuf.shm_cpid);
                        printf("該共享內存的大小為:%d\n",dsbuf.shm_segsz);
                        if((read_address=(char *)shmat(shmid,0,0))<0)//連接共享內存
                        {
                            printf("shmat連接共享內存出現錯誤。\n");
                            exit(5);
                        }
                        else
                        {
                            printf("自共享內存中讀取的信息為:“%s”。\n",read_address);
                            printf("最后一個操作該共享內存的進程ID是:%d\n",dsbuf.shm_lpid);
                            if((shmdt((void *)read_address))<0)//斷開與共享內存的連接
                            {
                                printf("shmdt共享內存斷開錯誤。\n");
                                exit(6);
                            }
                            else
                                printf("shmdt共享內存斷開成功。\n");
                            if(shmctl(shmid,IPC_RMID,NULL)<0)//刪除共享內存及其數據結構
                            {
                                printf("shmctl刪除共享內存及其數據結構出現錯誤。\n");
                                exit(7);
                            }
                            else
                                printf("shmctl刪除共享內存及其數據結構成功。\n");
                            exit(0);
                        }
                    }    
                }
            }
            /*********************程序運行結 果*********************
            [root@localhost temp]# ./shm
            shmid共享內存分配成功,共享內存識別代號 為:1703947。
            父進程的ID是:7647
            shmat連接共享內存成功。
            寫入共享內存的信息為“我是寫入共享內存的測試數據”。
            shmdt 共享內存斷開成功。
            子進程ID是:7648
            shmctl獲取共享內存數據結構成功。
            建立這個共享內存的進程ID是:7647
            該共享內存的大小 為:32
            自共享內存中讀取的信息為:“我是寫入共享內存的測試數據”。
            最后一個操作該共享內存的進程ID是:7647
            shmdt共享內存斷開成功。
            shmctl刪除共享內存及其數據結構成功。
            ***********************************************************/

            posted on 2010-07-14 10:43 chatler 閱讀(2060) 評論(1)  編輯 收藏 引用 所屬分類: Linux_Coding

            FeedBack:
            # re: linux的消息隊列與共享內存編程
            2011-05-27 11:01 | 朱志超
            內容選擇得很好,謝謝  回復  更多評論
              
            <2008年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久亚洲AV成人网人人网站 | 久久精品人人槡人妻人人玩AV | 亚洲欧美另类日本久久国产真实乱对白| 乱亲女H秽乱长久久久| 伊人久久大香线蕉亚洲五月天| 久久这里有精品| 精品免费久久久久久久| 久久99久久99小草精品免视看| 中文字幕久久欲求不满| 亚洲?V乱码久久精品蜜桃 | 97久久精品国产精品青草| 久久久精品免费国产四虎| 国产精自产拍久久久久久蜜| 亚洲国产精品成人久久蜜臀| 日日躁夜夜躁狠狠久久AV| 国产午夜精品理论片久久| 国产69精品久久久久久人妻精品| 99久久这里只有精品| 亚洲国产精品成人AV无码久久综合影院 | 久久久WWW成人| 亚洲av成人无码久久精品| 91亚洲国产成人久久精品| 亚洲午夜久久久久久久久电影网 | 性高湖久久久久久久久| 国产成人久久精品麻豆一区| 亚洲精品无码专区久久久| 精品久久久久久久久久久久久久久| 久久精品免费一区二区| 久久久噜噜噜久久中文字幕色伊伊| 奇米综合四色77777久久| 亚洲欧洲久久久精品| 9191精品国产免费久久| MM131亚洲国产美女久久| 精品国产日韩久久亚洲| 久久九色综合九色99伊人| 久久精品国产精品国产精品污| 精品久久久无码21p发布| 亚洲精品WWW久久久久久| 国产精品欧美久久久久天天影视| 人妻精品久久久久中文字幕一冢本| 狠狠色丁香久久婷婷综合图片 |