嵌入式系统核心设计与项目实战.docx
- 文档编号:23263683
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:118
- 大小:3.87MB
嵌入式系统核心设计与项目实战.docx
《嵌入式系统核心设计与项目实战.docx》由会员分享,可在线阅读,更多相关《嵌入式系统核心设计与项目实战.docx(118页珍藏版)》请在冰豆网上搜索。
嵌入式系统核心设计与项目实战
课设报告
名称嵌入式系统核心设计与项目实战
指导教师
学院自动化学院控制工程系
专业自动化
学生姓名Luke
班级/学号
成绩
项目一Linux操作命令和文本编辑器vi,编译器GCC使用
1.实验目的
熟悉vi编辑器环境
通过一个简单的程序练习vi常用命令
2.程序代码
#include
/*****打印hello,world!
*****/
intmain()
{
printf("hello,world!
\n");
exit(0);
}
3.实验步骤
(1)点击图标
打开软件。
显示如下界面:
软件界面
(2)打开虚拟机linux
点击
,打开D:
\虚拟机\培训班虚拟机20110718\RedHatEnterpriseLinux4.vmx
进入等待页面,并登陆,如下
等待界面1
等待界面2
等待界面3
等待界面4
登陆输入用户名
登陆输入密码
欢迎界面
进入后可通过设置->方案改变颜色方案,方便观看,如下图:
编辑界面
(3)进入实验目录/mnt/hgfs/share/01_hello
输入命令cd/mnt/hgfs/share/01_hello
图10进入01_hello文件夹
(4)进入vi编辑器,创建hello.c文件
输入命令vihello.c
(5)进入插入模式
输入命令a或者o
(6)编辑程序,输入源程序代码,如下图:
图11输入源程序
(7)完成后,单击“Esc”,进入命令模式,
输入命令:
wq保存退出
(8)编译连接程序
输入命令gcc–ohellohello.c
输入命令:
ls可以查看此时01_hello文件夹下有hello.c和hello文件,如下图
生成可执行文件hello
(9)执行程序
输入命令./hello
(10)查看实验结果
实验结果:
运行程序之后,出现“Helloworld!
”
项目二GDB调试器的使用
一实验目的
掌握gdb调试工具的使用和常用的调试命令
二实验内容
利用vi编辑器编写c语言程序,使用gcc编译,利用gdb调试器调试
实验程序:
#include
intdisplay1(char*string);
intdisplay2(char*string);
intmain()
{
charstring[]="abcd";
display1(string);
display2(string);
}
intdisplay1(char*string)
{
printf("theorginalstringis%s\n",string);
}
intdisplay2(char*string1)
{
char*string2;
intsize,i;
size=strlen(string1);
string2=(char*)malloc(size+1);
for(i=0;i string2[size-i-1]=string1[i]; string2[size+1]='\0'; printf("thechangedstringis%s\n",string2); } 实验结果: 初始字符串“abcd”,通过运行程序改变字符串显示顺序,变为“dcba”。 项目三Makefile编写 一实验目的 1了解Makefile的基本概念和基本结构 2初步掌握编写简单的Makefile的方法 3了解递归Make的编译过程 4初步掌握利用GNUMake编译应用程序的方法 二实验内容 编写hello.c文件,利用GNUmake编译应用程序 实验程序: #include #include"hello2.h" voidtest() { printf("Iamthetestfunctiondefinedinhello2\n"); return0; } 项目四Linux系统下文件编程—文件创建 一实验目的 学会创建文件,并制定文件属性 二实验内容 编写一个应用程序,创建可读可写的文件 实验程序: #include"stdio.h" #include"stdlib.h" #include"sys/types.h" #include"sys/stat.h" #include"fcntl.h" voidfile_creat(char*filename) { if(creat(filename,0755)<0) { printf("creatfile%sfailure\n",filename); exit(EXIT_FAILURE); } else { printf("creatfile%ssuccess! \n",filename); } } intmain(intargc,char*argv[]){ inti; if(argc<2){ perror("youhaven'tinputthefilename,pleasetryagain\n"); exit(EXIT_FAILURE); } for(i=1;i file_creat(argv[i]); } //printf("%d,%s\n",*argv[0],argv[0]); //printf("%d,%s\n",*argv[1],argv[1]); //printf("%d\n",*argv[2]); exit(EXIT_SUCCESS); } 实验结果: 创建”filecreat”文件 项目五Linux系统下文件综合设计 一实验目的 1理解文件系统中文件的打开和关闭实现过程 2理解文件系统中文件读和写的实现过程 3利用文件系统调用函数实现文件的基本操作过程 4理解缓冲区在文件系统中的作用 二实验内容 1理解open函数和close函数的调用过程 2理解read函数和write函数的调用过程 3利用上述函数实现文件的复制过程,并利用缓冲区来减少系统调用次数 实验程序 #include #include #include #include #include #include #defineBUFFER_SIZE1024 intmain(intargc,char*argv[]) { intfrom_fd,to_fd; intbytes_read,bytes_write; charbuffer[BUFFER_SIZE]; char*ptr; if(argc! =3) { printf("file_copyerror,pleaseinput2filename\n"); exit (1); } /*打开源文件*/ if((from_fd=open(argv[1],O_RDONLY))==-1) { printf("openfile1error\n"); exit (1); } /*创建目的文件*/ if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1) { printf("creatfile2error\nn"); exit (1); } /*以下代码是一个经典的靠背文件的代码*/ while(bytes_read=read(from_fd,buffer,BUFFER_SIZE)) { if((bytes_read==-1)&&(errno! =EINTR))break; elseif(bytes_read>0) { ptr=buffer; while(bytes_write=write(to_fd,ptr,bytes_read)) { if((bytes_write==-1)&&(errno! =EINTR))break; elseif(bytes_write==bytes_read)break; elseif(bytes_write>0) { ptr+=bytes_write; bytes_read-=bytes_write; } } if(bytes_write==-1)break; } } close(from_fd); close(to_fd); exit(0); } 项目六fork创建子进程 一实验目的 学会使用linux系统调用fork函数 二实验要求 编写一个应用程序,在程序中创建一个子进程,分别在父进程和子进程中打印进程ID 实验程序 #include #include #include intmain() { pid_tpid; intcount=0,i=0; printf("i=%d\n",i); pid=fork(); count++; printf("count=%d\n",count); if(pid<0) printf("error\n"); elseif(pid==0) printf("I'mthechildfork,ID=%d\n",getpid()); else printf("I'mtheparentfork,ID=%d\n",getpid()); } #include #include #include #include #include #include #include intmain(void) { pid_tchild; intcount=0; child=vfork(); count++; if(child==0)//子进程 { sleep (1);//子进程睡眠一秒 printf("Iamthechild: %d\n",getpid()); printf("%d\n",count); exit(0); } else//父进程 { printf("Iamthefather: %d\n",getpid()); printf("%d\n",count); exit(0); } } 项目七vfock创建子进程 一实验目的 学会实验linux的系统调用函数vfock,理解fock和vfock函数的区别 二实验要求 实验vfock创建一个子进程,分别在父进程和子进程中打印进程id,观察父进程和子进程的运行程序 #include #include #include #include #include #include #include intmain(void) { pid_tchild; if((child=vfork())==-1) { printf("vforkerror\n"); exit (1); } else if(child==0)//子进程 { sleep(3);//子进程睡眠一秒 printf("Iamthechild: %d\n",getpid()); exit(0); } else//父进程 { printf("Iamthefather: %d\n",getpid()); exit(0); } } #include #include #include #include #include #include #include intmain(void) { pid_tchild; intcount=0; child=vfork(); count++; if(child==0)//子进程 { sleep (1);//子进程睡眠一秒 printf("Iamthechild: %d\n",getpid()); printf("%d\n",count); exit(0); } else//父进程 { printf("Iamthefather: %d\n",getpid()); printf("%d\n",count); exit(0); } } 项目八进程等待 一、实验目的 学会使用linux系统调试wait函数 二、实验要求 编写一个应用程序,在程序中创建一个子进程,父进程需等待子进程运行结束后才能执行 实验程序: #include #include #include #include #include intmain() { pid_tpc,pr; pc=fork();//pc是fock的返回值,返回值是0代表子进程,若非0代表父进程 if(pc==0) { printf("Iamchildprocesswithpid: %d\n",getpid()); sleep(10); } else { pr=wait(NULL);//等待子进程结束 printf("Icaptureachildprocesswithpid: %d\n",pr); } exit(0); } 项目九有名管道和无名管道 一、实验目的 (1)理解进程通信机制中有名管道和无名管道的实现原理 (2)掌握管道的建立、读、写等函数的使用 (3)掌握有名管道和无名管道实现进程之间的通信 二、实验内容 编写程序: (1) 编写管道通信程序: 要求主进程创建子进程后,在子进程中调用exec函数执行一个新程 序前,通过管道给即将执行的程序传递命令行参数,包括: exit,子进程退出;getpid,获取进程号等。 (2)编写有名管道程序: 实现写进程获得从键盘输入的数据,读进程获得从写进程端读的数据 并显示在屏幕中。 实验程序: #include #include #include #include intmain() { intpipe_fd[2]; pid_tpid; charbuf_r[100]; char*p_wbuf; intr_num; memset(buf_r,0,sizeof(buf_r));//buf_r数组清零 /*创建管道*/ if(pipe(pipe_fd)<0) { printf("pipecreaterror\n"); return-1; } /*创建子进程*/ if((pid=fork())==0) { printf("\n"); close(pipe_fd[1]);//子进程不需要写管道,可以关掉 sleep(8);//睡眠两秒,让父进程去写管道 if(r_num=read(pipe_fd[0],buf_r,100)>0)//从管道中读100个字节到buf_r中 { printf("Childprocessreadspipesuccessfully! \n"); } close(pipe_fd[0]); exit(0); } elseif(pid>0) {close(pipe_fd[0]);//父进程不需要读管道,可以关掉 if(write(pipe_fd[1],"hello",5)>0)//向管道中写hello printf("Thisisfirstwritingoftheparentpipe! \n"); if(write(pipe_fd[1],"pipe",4)>0)//向管道中写pipe printf("Thisissecondwritingoftheparentpipe! \n"); close(pipe_fd[1]); sleep(8);//睡眠3s,让子进程读管道 waitpid(pid,NULL,0); exit(0); } } #include #include #include #include intmain() { intpipe_fd[2]; pid_tpid; charbuf_r[100]; char*p_wbuf; intr_num; memset(buf_r,0,sizeof(buf_r));//buf_r数组清零 /*创建管道*/ if(pipe(pipe_fd)<0) { printf("pipecreaterror\n"); return-1; } /*创建子进程*/ if((pid=fork())==0) { printf("\n"); close(pipe_fd[1]);//子进程不需要写管道,可以关掉 sleep(8);//睡眠两秒,让父进程去写管道 if(r_num=read(pipe_fd[0],buf_r,100)>0)//从管道中读100个字节到buf_r中 { printf("Childprocessreadspipesuccessfully! \n"); printf("%s\n",buf_r); } close(pipe_fd[0]); exit(0); } elseif(pid>0) {close(pipe_fd[0]);//父进程不需要读管道,可以关掉 if(write(pipe_fd[1],"hello",6)>0)//向管道中写hello printf("Thisisfirstwritingoftheparentpipe! \n"); if(write(pipe_fd[1],"pipe",4)>0)//向管道中写pipe printf("Thisissecondwritingoftheparentpipe! \n"); close(pipe_fd[1]); sleep(8);//睡眠3s,让子进程读管道 waitpid(pid,NULL,0); exit(0); } } 实验总结 : 通过本次实验理解了进程通信机制中有名管道和无名管道的实现原理,有名管道和无名管道实现通信的区别,以及文件描述符重定向和流重定向;掌握了管道的建立、读、写等函数的使用,掌握了有名管道和无名管道实现进程之间的通信。 项目十进程之间的通信 一、实验目的 为了理解和掌握UNIX和Linux进程通信系统调用的功能,这里给出了进程通信实现机制中使用的系统调用命令的格式和如何利用系统调用命令进行进程通信编程,以便通过学习,提高学生对进程通信系统调用的编程能力。 二、实验要求 编写一个有名管道程序。 一个(客户)进程从键盘循环读一系列字符,将这些字符和发送者的pid发给服务器进程,让其统计输入的是字符还是数字,分别为多少个,完成后再向客户进程发回服务的结果,由客户进程输出。 实验程序: Shell1 #include #include #include #include #include #include #include #defineFIFO"/tmp/myfifo" intmain() { charbuf_r[100]; intfd; intnread; if(mkfifo(FIFO,O_CREAT|O_EXCL)<0&&(errno! =EEXIST)) printf("cannotcreatfifoserver\n"); printf("Preparingforreadingbytes...\n"); memset(buf_r,0,sizeof(buf_r)); fd=open(FIFO,O_RDONLY|O_NONBLOCK,0); if(fd==1) { perror("open"); exit (1); } while (1) { memset(buf_r,0,sizeof(buf_r)); if((nread=read(fd,buf_r,100)==-1)); { if(errno==EAGAIN) printf("nodatayet\n"); } printf("read%sfromFIFO\n",buf_r); sleep (1); } pause(); } Shell2 #include #include #include #include #include #include #include #defineFIFO_SERVER"/tmp/myfifo" intmain(intargc,char*argv[]) { intfd; charw_buf[100]; intnwrite; fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0); if(argc==1) {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 嵌入式 系统 核心 设计 项目 实战