<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    嵌入式Linux系統編程常見問題解答(二)

    發布時間:2013-11-26 11:24    發布者:edu118gct
    關鍵詞: 編程

    接上層嵌入式Linux系統編程常見問題解答(一)
    6:假設上一題中編譯生成可執行文件名為tme。若在另一進程中調用system("./tme"),也會運行顯示時間的程序。要求根據system函數的內部實現過程,用其它學過的函數實現與system("./tme")同樣的功能。
    答案:
    #include
    #include
    extern char **environ;
    int main()
    {
        pid_t pid = fork();
        if(pid > 0)
        {
            waitpid(pid, NULL, 0);
        }
        else
        {
            char *buf[] = {"./tme", NULL};
            execve("./tme", buf, environ);
        }
        return 0;
    }

    7:調用函數創建一個子進程,父進程從鍵盤讀入一個整數,然后子進程把這個整數輸出來。要求:信號是進程間通信的一種方式,本題要求利用信號知識實現,不能通過寫入文件或管道等其它方式實現。
    答案:
    /*提示:任何整數都是由0-9這幾個位組成的,可以把它的每個位都以信號的方式發給子進程,子進程接受到它的每個位后再計算出值。但有些信號比較特殊不能捕捉處理,故這里用信號10代表數字9,信號12代表數字0,信號11代表通信結束。其它的數字與其信號值相同。因為使用到了數學庫函數pow,編譯的時候最后帶上-lm選項*/
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    void fun(int n);
    int main()
    {
            int a = fork();
            if(a > 0)
            {        
                    int num;
                    printf("Input a number:\n");
                    scanf("%d", &num);
                    sleep(1);
                    /*從低到高以信號的形式向子進程發送該整數的每個位*/
                    int num1 = num;
                    do{
                            if(num1%10 == 9)
                                    kill(a, 10);
                            else if(num1%10 == 0)
                                    kill(a, 12);
                            else
                                    kill(a, num1%10);        
                            num1 = num1/10;
                            sleep(1);
                    }while(num1 != 0);
                    kill(a, 11);

                    waitpid(a, 0, 0);
            }

            else
            {
                    /*對相關信號注冊捕捉處理函數*/
                    signal(1, fun);
                    signal(2, fun);
                    signal(3, fun);
                    signal(4, fun);
                    signal(5, fun);
                    signal(6, fun);
                    signal(7, fun);
                    signal(8, fun);
                    signal(10, fun);
                    signal(11, fun);
                    signal(12, fun);
                    while(1)
                            sleep(1);
            }

            return 0;
    }
    /*子進程收到相關信號會調用此函數,此函數用來根據各個位計算出整數的值*/
    void fun(int n)
    {
            static int m, i;
            
            if(n == 10)
            {        
                    printf("第%d位是 9\n", i + 1);
                    m = m + 9 * pow(10, i);
                    i++;
            }
            else if(n == 12)
            {        
                    printf("第%d位是 0\n", i + 1);
                    //m = m + 0 * pow(10, i);
                    i++;
            }
            else if(n == 11)
            {        
                    printf("The number is %d\n", m);
                    sleep(1);
                    exit(0);
            }
            else
            {
                    printf("第%d位是 %d\n", i + 1,  n);
                    m = m + n * pow(10, i);
                    i++;
            }
    }

    8:不用網絡編程,用其它的IPC方式寫一個模擬聊天過程的小程序。A進程終端下輸入消息內容,按Enter后發送消息給B進程,B進程收到消息后在終端下顯示消息內容。
    B進程也能實現同樣的功能。要顯示出接收消息的時間。要能循環發送接收,如果輸入QUIT就退出。
    答案:
    /*QQA.c*/
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #define BUFFER 255
    #define SIZE sizeof(struct msgbuf)
    void showime();
    int msgid;
    struct msgbuf{
            long mtype;
            char mtext[BUFFER + 1];
    };
    void* recv(void *p);
    void* send(void *p);

    int main()
    {
            msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
            if(msgid == -1)
            {
                    perror("msgget error!");
                    exit(-1);
            }
            pthread_t pthid_send, pthid_recv;
            pthread_create(&pthid_send, NULL, send, NULL);
            pthread_create(&pthid_recv, NULL, recv, NULL);
            pthread_join(pthid_send, NULL);
            pthread_join(pthid_recv, NULL);

            msgctl(msgid, IPC_RMID, NULL);
            return 0;
    }

    void showtime()
    {
            time_t tt = time(NULL);
            struct tm *pTm;
            pTm = localtime(&tt);
            printf("%02d-%02d",(1 + pTm->tm_mon), (pTm->tm_mday));
            printf(" %02d:%02d:%02d\n",(pTm->tm_hour), (pTm->tm_min), (pTm->tm_sec));
            printf(" ");
    }
    void* send(void *p)
    {
            struct msgbuf msgsend;
            while(1)
            {

                    printf(" I say  ");
                    showtime();
                    memset(&msgsend, 0, SIZE);
                    msgsend.mtype = 1;
                    scanf("%s", msgsend.mtext);
                    if(strcmp(msgsend.mtext, "QUIT") == 0)
                            exit(0);
                    else
                            msgsnd(msgid, &msgsend, SIZE, 0);
            }
    }

    void* recv(void * p)
    {
            struct msgbuf msgrecv;
            while(1)
            {
                    memset(&msgrecv, 0, SIZE);
                    msgrcv(msgid, &msgrecv, SIZE, 2,0);
                    if(strcmp(msgrecv.mtext, "QUIT") == 0)
                            exit(0);
                    else
                    {
                            printf(" Jim say ");
                            showtime();   
                            printf("%s\n", msgrecv.mtext);
                    }
            }
    }

    /*QQB.c*/
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #define BUFFER 255
    #define SIZE sizeof(struct msgbuf)
    void showime();
    int msgid;
    struct msgbuf{
            long mtype;
            char mtext[BUFFER + 1];
    };
    void* recv(void *p);
    void* send(void *p);

    int main()
    {
            msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
            if(msgid == -1)
            {
                    perror("msgget error!");
                    exit(-1);
            }

            pthread_t pthid_send, pthid_recv;

            pthread_create(&pthid_send, NULL, send, NULL);
            pthread_create(&pthid_recv, NULL, recv, NULL);
            pthread_join(pthid_send, NULL);
            pthread_join(pthid_recv, NULL);

            msgctl(msgid, IPC_RMID, NULL);
            return 0;
    }

    void showtime()
    {
            time_t tt = time(NULL);
            struct tm *pTm;
            pTm = localtime(&tt);
            printf("%02d-%02d",(1 + pTm->tm_mon), (pTm->tm_mday));
            printf(" %02d:%02d:%02d\n",(pTm->tm_hour), (pTm->tm_min), (pTm->tm_sec));
            printf(" ");
    }
    void* send(void *p)
    {
            struct msgbuf msgsend;
            while(1)
            {
                    printf(" I say  ");
                    showtime();
                    memset(&msgsend, 0, SIZE);
                    msgsend.mtype = 2;
                    scanf("%s", msgsend.mtext);
                    if(strcmp(msgsend.mtext, "QUIT") == 0)
                            exit(0);
                    else
                            msgsnd(msgid, &msgsend, SIZE, 0);
            }
    }

    void* recv(void * p)
    {
            struct msgbuf msgrecv;
            while(1)
            {
                    memset(&msgrecv, 0, SIZE);
                    msgrcv(msgid, &msgrecv, SIZE, 1,0);
                    if(strcmp(msgrecv.mtext, "QUIT") == 0)
                            exit(0);
                    else
                    {
                            printf(" Lucy say ");
                            showtime();
                            printf("%s\n", msgrecv.mtext);
                    }
            }
    }深圳專業嵌入式實訓。咨詢Q754634522


    本文地址:http://www.portaltwn.com/thread-123767-1-1.html     【打印本頁】

    本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
    上網去溜溜 發表于 2013-12-11 10:00:59
    廣告。。!
    edu118gct 發表于 2013-12-12 11:38:23
    學習了
    您需要登錄后才可以發表評論 登錄 | 立即注冊

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區

    相關視頻

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷