国产毛片a精品毛-国产毛片黄片-国产毛片久久国产-国产毛片久久精品-青娱乐极品在线-青娱乐精品

嵌入式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://m.qingdxww.cn/thread-123767-1-1.html     【打印本頁】

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

廠商推薦

  • Microchip視頻專區
  • Dev Tool Bits——使用MPLAB® Discover瀏覽資源
  • Dev Tool Bits——使用條件軟件斷點宏來節省時間和空間
  • Dev Tool Bits——使用DVRT協議查看項目中的數據
  • Dev Tool Bits——使用MPLAB® Data Visualizer進行功率監視
  • 貿澤電子(Mouser)專區

相關視頻

關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产一区二区免费不卡在线播放 | 狠狠色丁香久久综合婷婷 | 在线www 天堂网在线 | 国产在线观看高清精品 | 中国美女大战黑人国产 | 欧美在线区 | 精品91麻豆免费免费国产在线 | 日韩 欧美 中文字幕 不卡 | 国产青青在线视频 | 21款禁用app软件下载 | 清纯唯美综合亚洲第一页 | 久久99这里只有精品国产 | 国产精品成人扳一级aa毛片 | 九九热观看视频 | 女性爽爽影院免费观看麻豆 | 日本在线视频网站www色下载 | 国产情侣真实露脸在线最新 | 春意影院免费入口 | 高清欧美在线三级视频 | 免费看国产一级片 | 好男人好手机视频在线影视 | 日本三级黄网站 | 1024国产欧美日韩精品 | 亚洲欧美国产另类 | 免费看91 | 欧美一区二区在线免费观看 | 国产一区二区三区手机在线观看 | 欧美第八页| 国产麻豆免费观看 | 四虎国产在线观看 | 日本xxxⅹ69xxxx护士 | 99久久久久国产 | 亚洲国产欧美国产第一区二区三区 | 在线免费观看毛片网站 | 天堂最新版资源在线下载 | 久久久久久一级毛片免费野外 | 欧美草比| 国产麻豆福利a v在线播放 | 天堂看动漫| 男女爱爱视频免费看 | 日韩国产一区二区 |