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

            版權(quán)聲明:轉(zhuǎn)載時(shí)請(qǐng)以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明
            http://xufish.blogbus.com/logs/40537374.html

            012號(hào)與013 號(hào)程序,分別是關(guān)于消息隊(duì)列和共享內(nèi)存的

            /*********************程序相關(guān)信 息*********************
            程序編號(hào):012
            程序編寫(xiě)起始日期:2008.11.1
            程序編寫(xiě)完成日期:2008.11.1
            程序修改日 期:                                   修改備注:
            程序目的:學(xué)習(xí)linux消息隊(duì)列通信
            所用主要函 數(shù):msgget(),msgsnd(),msgrcv(),msgctl()
            程序 存疑:
            程序完成地點(diǎn): 宿舍內(nèi)
            *********************程序相關(guān)信息*********************/
            #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;//后者為消息隊(duì)列識(shí)別代號(hào)
                struct msgbuf
                {
                    long mtype;//消息類型
                    char mtext[20];//消息內(nèi)容
                }send_buf,receive_buf;
                if((msqid=msgget(IPC_PRIVATE,0700))<0)//建立消息隊(duì)列
                {
                    printf("msgget建立消息隊(duì)列失敗。\n");
                    exit(1);
                }
                else
                    printf("msgget建立消息隊(duì)列成功,該消息隊(duì)列識(shí)別代號(hào)為%d。\n",msqid);
                if((pid=fork())<0)
                {
                    printf("fork()函數(shù)調(diào)用失敗!\n");
                    exit(2);
                }
                else if(pid>0)//父進(jìn)程,發(fā)送消息到消息隊(duì)列
                {
                    send_buf.mtype=1;
                    strcpy(send_buf.mtext,"My test information");
                    printf("發(fā)送到消息隊(duì)列的信息內(nèi)容為:%s\n",send_buf.mtext);
                    if(msgsnd(msqid,&send_buf,20,IPC_NOWAIT)<0)//發(fā)送send_buf中的信息到msqid 對(duì)應(yīng)的消息隊(duì)列
                    {
                        printf("msgsnd消息發(fā)送失敗。\n");
                        exit(3);
                    }
                    else
                        printf("msgsnd消息發(fā)送成功。\n");
                    sleep(2);
                    exit(0);
                }
                else//子進(jìn)程,從消息隊(duì)列中接收消息]
                {
                    sleep(2);//等待父進(jìn)程發(fā)送消息完成
                    int infolen;//讀到的信息數(shù)據(jù)長(zhǎng)度
                    if((infolen=msgrcv(msqid,&receive_buf,20,0,IPC_NOWAIT))<0)//自消息隊(duì)列 接收信息
                    {
                        printf("msgrcv讀取信息錯(cuò)誤。\n");
                        exit(4);
                    }
                    else
                        printf("msgrcv讀取信息成功。\n");
                    printf("自消息隊(duì)列讀取到的內(nèi)容為%s,共讀取%d個(gè)字節(jié)。\n",receive_buf.mtext,infolen);
                    if((msgctl(msqid,IPC_RMID,NULL))<0)//刪除msqid對(duì)應(yīng)的消息隊(duì)列
                    {
                        printf("msgctl函數(shù)調(diào)用出現(xiàn)錯(cuò)誤。\n");
                        exit(5);
                    }
                    else
                    {
                        printf("識(shí)別代號(hào)為%d的消息隊(duì)列已經(jīng)被成功刪除。\n",msqid);
                        exit(0);
                    }
                }
            }
            /*********************程序運(yùn)行結(jié) 果*********************
            [root@localhost temp]# ./msg
            msgget建立消息隊(duì)列成功,該消息隊(duì)列識(shí)別代號(hào)為 98304。
            發(fā)送到消息隊(duì)列的信息內(nèi)容為:My test information
            msgsnd消息發(fā)送成功。
            msgrcv讀取信息成功。
            自消息隊(duì)列讀取到的內(nèi)容為My test information,共讀取20個(gè)字節(jié)。
            識(shí)別代號(hào)為98304的消息 隊(duì)列已經(jīng)被成功刪除。
            ***********************************************************/

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

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

            FeedBack:
            # re: linux的消息隊(duì)列與共享內(nèi)存編程
            2011-05-27 11:01 | 朱志超
            內(nèi)容選擇得很好,謝謝  回復(fù)  更多評(píng)論
              
            <2009年11月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺(jué)這個(gè)博客還是不錯(cuò),雖然做的東西和我不大相關(guān),覺(jué)得看看還是有好處的

            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

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久精品日日躁夜夜躁欧美| 国产精品伦理久久久久久| 亚洲综合精品香蕉久久网| 亚洲精品高清国产一线久久| 97久久久精品综合88久久| 国产亚洲美女精品久久久| 久久天天躁夜夜躁狠狠| 久久精品亚洲中文字幕无码麻豆| 91精品国产综合久久久久久| 久久久久亚洲精品无码网址| 久久精品国产99国产精品导航| 7777久久亚洲中文字幕| 久久婷婷色综合一区二区| 一本一本久久a久久综合精品蜜桃| 国产精品欧美久久久天天影视| 理论片午午伦夜理片久久| 久久精品午夜一区二区福利| 婷婷久久综合| 九九99精品久久久久久| 国产精品99久久久精品无码 | 2021久久精品国产99国产精品| 久久青草国产精品一区| 思思久久99热只有频精品66| 色综合久久88色综合天天| 无码任你躁久久久久久老妇App| 久久99国产精品一区二区| 国产69精品久久久久9999APGF| 99热热久久这里只有精品68| 久久久久亚洲精品无码蜜桃 | av无码久久久久不卡免费网站| 色综合久久久久综合99| 久久久精品免费国产四虎| 亚洲精品乱码久久久久久蜜桃图片| 久久精品无码一区二区三区免费| 国内精品久久人妻互换| 久久精品国产日本波多野结衣 | 精品综合久久久久久888蜜芽| 久久人搡人人玩人妻精品首页 | 好久久免费视频高清| 亚洲AV日韩精品久久久久久| 一本久久免费视频|