接上層嵌入式Linux系統(tǒng)編程常見問題解答(一) 6:假設(shè)上一題中編譯生成可執(zhí)行文件名為tme。若在另一進(jìn)程中調(diào)用system("./tme"),也會運(yùn)行顯示時(shí)間的程序。要求根據(jù)system函數(shù)的內(nèi)部實(shí)現(xiàn)過程,用其它學(xué)過的函數(shù)實(shí)現(xiàn)與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:調(diào)用函數(shù)創(chuàng)建一個(gè)子進(jìn)程,父進(jìn)程從鍵盤讀入一個(gè)整數(shù),然后子進(jìn)程把這個(gè)整數(shù)輸出來。要求:信號是進(jìn)程間通信的一種方式,本題要求利用信號知識實(shí)現(xiàn),不能通過寫入文件或管道等其它方式實(shí)現(xiàn)。 答案: /*提示:任何整數(shù)都是由0-9這幾個(gè)位組成的,可以把它的每個(gè)位都以信號的方式發(fā)給子進(jìn)程,子進(jìn)程接受到它的每個(gè)位后再計(jì)算出值。但有些信號比較特殊不能捕捉處理,故這里用信號10代表數(shù)字9,信號12代表數(shù)字0,信號11代表通信結(jié)束。其它的數(shù)字與其信號值相同。因?yàn)槭褂玫搅藬?shù)學(xué)庫函數(shù)pow,編譯的時(shí)候最后帶上-lm選項(xiàng)*/ #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); /*從低到高以信號的形式向子進(jìn)程發(fā)送該整數(shù)的每個(gè)位*/ 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 { /*對相關(guān)信號注冊捕捉處理函數(shù)*/ 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; } /*子進(jìn)程收到相關(guān)信號會調(diào)用此函數(shù),此函數(shù)用來根據(jù)各個(gè)位計(jì)算出整數(shù)的值*/ 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:不用網(wǎng)絡(luò)編程,用其它的IPC方式寫一個(gè)模擬聊天過程的小程序。A進(jìn)程終端下輸入消息內(nèi)容,按Enter后發(fā)送消息給B進(jìn)程,B進(jìn)程收到消息后在終端下顯示消息內(nèi)容。 B進(jìn)程也能實(shí)現(xiàn)同樣的功能。要顯示出接收消息的時(shí)間。要能循環(huán)發(fā)送接收,如果輸入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); } } }深圳專業(yè)嵌入式實(shí)訓(xùn)。咨詢Q754634522 |