|
2012 級嵌入式上考試參考程序之一
/*注:部分程序代碼來自于互聯網,本人將此整理,僅用于學習交流,請下載之后于 24 小時內刪除詳見附件*/
/*實驗內容:
(1) 設計一個具有一個整形參數的子函數,取出參數的低 16 位并且把低 8 位置一,將結果返回;
(2) f2.c:設計一個具有兩個參數的 子函數,子函數將 2 參數值互換;
(3)實現主函數,主函數中創建 2 個進程,進程 1 定義一個整形變量,調用文件 f1 中定義的函數求取結果并將結果和進程 ID 輸出;進程 2 定義 2 個整形變量,調用文件 f2 中定義的函數將兩變量的值進行互換并將結果和進程 ID 輸出;*/
//主函數 maain.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include"f1.h"
#include"f2.h"
int main(int argc ,char**argv)
{
unsigned int FinalNumber,p=0xffff5689;
unsigned int p1 = 30,p2 = 50;
char i = 0;
char pid;
pid = fork();
if(pid == -1)
{
printf("Fork error\n");
}
else if(pid == 0)
{
FinalNumber = NumberExtract(p);
printf("before extract the number is %x \n" ,p);
printf("after extract the numberis %x \n",FinalNumber);
printf("I am child1 process ,my pid is %d.\n",getpid());
}
else
{
printf("I'm father process,my ppid is %d.\n ",getppid());
pid = fork();
if(pid == 0)
{
printf("I am child2 process ,my pid is %d.\n",getpid());
printf("before exchange The Number p1,p2 is %d,%d.\n",p1,p2);
ExchangeNumber(&p1,&p2);
printf("after exchange The Number p1,p2 is %d,%d.\n",p1,p2);
}
}
}
//子函數 f1.c
#include<stdio.h>
#include<stdlib.h>
#include"f1.h"
unsigned int NumberExtract(unsigned int parg)
{
unsigned int FinalValue;
FinalValue=(parg&0x0000ffff)|0x00ff;
return FinalValue;
}
//頭文件 f1.h
unsigned int NumberExtract(unsigned int parg);
//子函數 f2.c
#include<stdio.h>
#include<stdlib.h>
#include"f2.h"
void ExchangeNumber(unsigned int*parg1,unsigned
int*parg2)
{
unsigned int parg;
parg = *parg1;
*parg1 = *parg2;
*parg2 = parg;
}
//頭文件 f2.h
void ExchangeNumber(unsigned int*parg1,unsigned
int*parg2);
Makefile
Love: maain.o f1.o f2.o
gcc -o Love maain.o f1.o f2.o
maain.o: maain.c f1.h f2.h
gcc -c maain.c
f1.o: f1.c f1.h
gcc -c f1.c
f2.o: f2.c f2.h
文件夾具體文件構成
編譯時圖片
后續還會再上傳,僅供參考。
|
-
-
嵌入式上機考試題之一.zip
2016-4-21 17:28 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
260.84 KB, 下載次數: 2, 下載積分: 黑幣 -5
嵌入式程序
評分
-
查看全部評分
|