物联网工程操作系统实验报告.docx

上传人:夺命阿水 文档编号:851548 上传时间:2023-12-27 格式:DOCX 页数:17 大小:106.76KB
返回 下载 相关 举报
物联网工程操作系统实验报告.docx_第1页
第1页 / 共17页
物联网工程操作系统实验报告.docx_第2页
第2页 / 共17页
物联网工程操作系统实验报告.docx_第3页
第3页 / 共17页
物联网工程操作系统实验报告.docx_第4页
第4页 / 共17页
物联网工程操作系统实验报告.docx_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《物联网工程操作系统实验报告.docx》由会员分享,可在线阅读,更多相关《物联网工程操作系统实验报告.docx(17页珍藏版)》请在课桌文档上搜索。

1、操作系统实验报告专业:物联网工程实验三一个进程启动另一个程序的执行【实验目的】在Linux环境系统中,execve系统调用用于执行一个程序(可执行二进制文件或脚本)。exec函数家族,包括execl、execlpexecleexecvexecvp,是execve系统调用的前端。本实验要求学生学习在一个进程中启动另一个程序执行的基本方法,了解execve系统调用和exec函数家族的使用方法。【实验内容】(一)初步认识“在一个进程中启动另一个程序的执行二1编辑一个源程序dummy.c,并编译为可执行程序dummy。/dummy.c#include#include#include#includein

2、tmain(intargc,char*argv)(intresult;printf(,nYouarenowinarunningprogram,%s.n,argv0);printf(,MyPIDis%d.MyparentsPlDis%d.n,getpid(),getppid();printf(nPleaseinputaninteger(0-255),whichwillbereturnedtomyparentprocessn);scanf(,%d,&result);printf(,Goodbye.nn);return(result&0377);)2、再编辑一个源程序exec_test.c,并编译为

3、可执行程序exec_testo/exec_test.c#include#include#include#include#include#includeintmain(intargc,char*argv)(intresult;result=fork();if(result0)(perror(,Failedtocreatechild*);exit(l);)elseif(result=0)(/Child1char*cmd=./dummy;printf(,Childprocess,sPIDis%d.MyparentsPIDis%d.n,getpid(),getppid();printf(Childpr

4、ocessisabouttoexecute,%sunn,cmd);result=execlp(cmd,cmd,NULL);if(result=-1)perror(Inchildprocess,failedtoexecaprogram1);)exit(0);)else(/parentintstatus;printf(,ParentprocesssPIDis%d.n,getpid();printf(Parentprocessiswaiting.n);wait(&status);printf(nInparentprocess,status=0x%x,WEXITSTATUS(status)=%d(i.

5、e.0x%x)nn,status,WExiTSTATUS(Status),WEXITSTATUS(status);)return(EXIT.SUCCESS);)3、先执行dummy,观察、分析执行结果;然后执行程序exec_test,观察、分析执行结果。(注意,两个可执行程序都在当前目录下)(二)实现一个简单的命令解释外壳(Shell)o1基本功能:(I)从标准输入读取一行字符串,其中包含欲执行的命令和它的命令行参数(如果有的话)。提示:需要将输入的一行字符串进行拆分,以空格、制表符(t)作为分隔符,分解为命令、命令行参数(零个或多个)。如果用户输入的命令是“quit”,则退出执行。(2)创建

6、一个子进程。(3)在子进程中,执行在(1)读入的命令,如果有命令行参数,也要传递。(4)在父进程中,等待子进程结束,然后打印子进程的返回值。(5)在父进程中,控制转移至(I)o【实验要求】按照要求编写程序,放在相应的目录中,编译成功后执行,并按照要求分析执行结果,并写出实验报告。【实验设计】dummy程序:HIiyuxin221liyuxin221test3$./dummyHyouarenowinarunningprogram./dummy. MyPIDis14758.MyparentsPIDis14449.HPleaseinputaninteger(-255),whichwillberetu

7、rnedtomyparentprocess.233 Goodbye.I,lyg11221QlIyUXln221test33结果显示了该进程的PID和父进程的PID,并且将一个值返回给了父进程。exec_testSJ?5:Inparentprocess,status=2a0,WEXITSTATUS(status)=42(i.e.x2a)Iiyuxin221liyuxin221test3S./exectest.outParentprocesssPIDis14769.ParentprocessiswaitingChildprocesssPIDis1477.MyparentsPIDis14769.Ch

8、ildprocessisabouttoexecute./dummyYouarenowinarunningprogram./dummy.MyPIDis14770.MyparentsPIDis14769.Pleaseinputaninteger(-255),whichwillbereturnedtomyparentprocess:19Goodbye.Inparentprocess,status=0x13,WEXITSTATUS(status)=19(i.e.13)Iiyuxin221(aiiyuxin221test3S在子进程中键入的数值返回了父进程,并显示了出来(19).实现一个简单的命令解释外

9、壳(Shell)源代码:/example#include#include#include#include#include#include#includeintmain()intpid;intrtn;子进程的返回值intexec_errorno;charcommand256;char*p;char*q;char*c20;inti=0,j=0;while(l)/从终端读取要执行的命令printf();command0=,0,;p=fgets(command,256,stdin);if(p=NULL)perror(Errorinfgets().n);exit(-l);)/Deletethelastc

10、har(newline)inthestringreturnedbyfgets()Commandfstrlen(Command)-1=,0,;p=command;q=p;/Quitifuserinputsquitif(!strcmp(command,quit,)break;)/Createachildprocesstoexecutecommandpid=fork();if(pidO)perror(HFailedwhilecallingfork.*);exit(-l);1elseif(pid=0)子进程执行此命令for(;)ci=strtok(p,M,);if(ci=NULL)break;1i+;

11、p=NULL;)p=c0;for(j=0;jquitIiyuxin221liyuxi221 test3 fial2.outls -alIs -al 总用量152 dxrwxr-x dnxnxr-x -xrwxr-x -rwxrwxr-x -rwxn-n- -rwxrwxr-x -nxn-rw- rwxrwxr-x -xn-n- -nxrfxr-x -nxn-n- -rwxnxr-x -nxn-n- -rwxrwxr-x -rwxn-rv- rwxrwxr-x -rwxr-xr-x -nxrwxr-x -xnn -rwxrw-n- -rwxnxr-x -rwxn-n- -nxrw-rw-2 51

12、 1111111 11111111 11111Uyuxin221 liyuxin221 liyuxi221 liyui221 liyin221 Uyuxin221 liyuxin221 Uyuxin221 liyuxin221 liyuxi221 liyuxin221 liyui221 liyin221 Xiyuxin221 Uyuxin221 Uyuxin221 liyuxin221 liyuxin221 liyuxi221 liyin221 liyuin221 liyuxin221 liyuin221liyuxin221 liyuxin221 liyuxi221 liyuxin221 li

13、yuxin221 liyuxin221 Uyuxin221 liyuxin221 liyuxin221 liyuxin221 liyuxi221 liyuxin221 liyuxin221 liyuxin221 Uyuxin221 liyuxin221 liyuxin221 liyuxin221 liyuxi221 liyuxin221liyuxi221 Uyuxin221 Uyuxin221496 496 5364 5364452 5238 1624 6354l 474892 5986 1619 6333 1647635 6333 647 19283l 4965418 5652IG月 1月

14、l月 1月 1月 l月 l月 Ie月 Ie月 IG月 Ie月 Ie月 1月 l月 l月 IG月 1。月 1月 1。月1月 1月 10月 11月23 10:45 .2 20:35 .2 21:34 dummy23 10:43 dummy32 21:34 dummy.C2 21:03 dummy.out23 10:24 example.c23 10:25 example.out21 20:58 execl.c21 20:58 execl.out2 20:54 exec test.c2 20:54 exec test.out23 18:22 finall.c23 10:22 finall.out23

15、 16:42 final2.c23 10:42 final2.out16 19:57 gnome - te rminal. desktop21 20:48 shell21 20:47 shell.c23 10:45 Strtokl.C23 10:45 StrtOkl-OUt21 21:24 strtok.c6 212 text3【收获及体会】实验指导书在实验调试和分析的过程中,我越来越懂得自学能力和自我独立解决问题的重要性。在以后的实验和学习中,我会不断的加强对这方面的训练。【参考资料】实验指导书实验五线程间的互斥与同步【实验目的】理解POSlX线程(Pthread)互斥锁和POSlX信号量机

16、制,学习它们的使用方法;编写程序,实现多个POSIX线程的同步控制。【实验内容】创建4个POSIX线程。其中2个线程(A和B)分别从2个数据文件(datal.txt和data2.txt)读取10个整数.线程A和B把从文件中读取的逐一整数放入一个缓冲池.缓冲池由n个缓冲区构成(n=5,并可以方便地调整为其他值),每个缓冲区可以存放一个整数。另外2个线程,C和D,各从缓冲池读取10数据。线程C、D每读出2个数据,分别求出它们的和或乘积,并打印输出。提示:(1)在创建4个线程当中,A和B是生产者,负责从文件读取数据到公共的缓冲区,C和D是消费者,从缓冲区读取数据然后作不同的计算(加和乘运算)。使用互

17、斥锁和信号量控制这些线程的同步。不限制线程C和D从缓冲区得到的数据来自哪个文件。(2)在生产者线程中,确保从文件读出数据以后,再去“生产二在开始设计和实现之前,务必认真阅读下列内容:课本6.8.4节;讲义(课堂PPT)中关于“生产者.消费者问题”的部分;课本第6章后面的编程项目一一生产者-消费者问题。【实验要求】按照要求编写程序,放在相应的目录中,编译成功后执行,并按照要求分析执行结果,并写出实验报告。【实验设计】#include#include#include#include#inciude#defineNUMTHREADS5intbuffer5;intin=O;写入标识intout=0;读

18、取标识pthread_mutex_tmutex2in,mutex2out;sem_twri,rea;void*writel(void*arg)(FILE*fpl=fopen(datal.txt,V);intwl;intil=0;for(il=0;il10;il+)/P(写资源)sem_wait(&wri);从文件中读取一个文件if(fscanf(fpl,%dn,&wl)else(printf(mthepthreadwritel,Ican,treadanumfromdata1.txt.n);exit(l);)放入缓存对应位置加in锁Pthread_mutexOCk(&mutex2in);buff

19、erin=wl;in=(in+1)%NUM_THREADS;解in锁pthread_mutex_unlock(&mutex2in);V(读底源)sem_post(&rea);)线程结束fclose(fpl);pthread-exit(NULL);void*write2(void*arg)intw2;FILE*fp2=fopen(data2.txt,r);inti2=0;for(i2=0;i210;i2+)/P(写资源)sem_wait(&wri);从文件中读取一个文件if(fscanf(fp2,%dt,&w2)else(printf(mthepthreadwritel,Ican,treadan

20、umfromdata1.txt.n);exit(l);)放入缓存对应位置加in锁Pthread_mutexOCk(&mutex2in);bufferin=w2;in=(in+1)%NUM_THREADS;解in锁pthread_mutex_unlock(&mutex2in);V(读底源)sem_post(&rea);)线程结束fclose(fp2);pthread_exit(NULL);)void*plus(void*arg)(/加进程intal,bl,cl;intp=0;for(p=0;p5;p+)P(读资源)sem_wait(&rea);从缓冲中读取一个文件加OUt锁pthread_mut

21、ex_lock(&mutex2out);al=bufferfout;out=(out+1)%NUM_THREADS;解OUt锁pthread_mutex_unlock(&mutex2out);/V(写资源)sem_post(&wri);/P(读资源)sem_wait(&rea);加OUt锁pthread_mutex_lock(&mutex2out);从缓相中读取一个文件bl=bufferout;out=(out+1)%NUM_THREADS;解OUt锁pthread_mutex_unlock(&mutex2out);cl=al+bl;计算求和并且输出到屏幕printf(mthepthreado

22、fplusing,thisismyresultt%d+%d=%d.n,al,bl,cl);/V(写资源)sem_post(&wri);)线程结束pthread_exit(NULL);)void*mult(void*arg)(/乘进程inta2,b2,c2;intp=0;for(p=0;p5;p+)P(读资源)sem_wait(&rea);加OUt锁pthread_mutex_lock(&mutex2out);从缓相中读取一个文件a2=bufferout;out=(out+1)%NUM_THREADS;解OUt锁pthread_mutex_unlock(&mutex2out);/V(写资源)se

23、m_post(&wri);P(读资源)sem_wait(&rea);加OUt锁pthread_mutex_lock(&mutex2out);从缓相中读取一个文件b2=bufferout;out=(out+1)%NUM_THREADS;解OUt锁pthread_mutex_unlock(&mutex2out);c2=a2*b2;计算求积并且输出到屏幕printf(mthepthreadofmultiplying,thisismyresult:t%d*%d=%d.n,a2,b2,c2);/V(写资源)sem_post(&wri);)线程结束pthread_exit(NULL);)intmain()

24、(互斥锁初始化pthread_mutex_init(&mutex2in,NULL);pthread_mutex_init(&mutex2out,NULL);pthread_tA,B,C,D;interr;信号量初始化sem_init(&wri,O,5);sem_init(&rea,O,O);创建线程1err=pthread_create(&A,NULL,write1,NULL);if(err!=0)(printf(,cantcreatemyThreadl:%snu,strerror(err);)创建线程2err=pthread_create(&B,NULL,write2,NULL);if(er

25、r!=0)(printf(,cantcreatemyThread2%snn,strerror(err);)稍微延迟sleep(0.1);创建线程3err=pthread_create(&C,NULL,plus,NULL);if(err!=0)(printf(cantcreatemyThread3%snu,strerror(err);)创建线程4err=pthread_create(&D,NULL,mult,NULL);if(err!=0)(printf(,can,tcreatemyThread4%sn,strerror(err);)PthreadOin(A,NULL);pthreadJoin(

26、B,NULL);pthreadJoin(C,NULL);pthreadJoin(D,NULL);returnO;data2.txt(*tesVtst5)-gdit:-;文件(F)Mtl(E)K(V)JJffl(三)IR(T)文档(D)MIft(三)匚R打开土Hyuxln2219lyuxin22I:-AesVtestSBTl文件(F)网(E)M(V)(T)M脉H)Jdata2.txtX39427384952637485920ImthepthreadofPIUSing.thisisnyresult:482868.Iiyuxin221liyuxin221test5S.test5.2.outmthep

27、threadofIiuUlplylngrthis15nyresult:3942=1638.mthepthreadofPlUSing,thisisnyresult:84+95-179.mthepthreadofIlUltiPIying,thisisnyresult:7326=1898.mthepthreadofIiuUlplylngfthis1$nyresult:3748=1776.Imthepthreadofnultiplyingrthisisnyresult:592118.mthepthreadofIuJItiPIying,thisisnyresult:13131.mthepthreadof

28、PIUnng,this15yresult:246=48.mthepthreadofplusingrthisisnyresult:565三7e.I,mthepthreadofplusing,thisisnyresult:789159.mthepthreadofplusing,thisIsnyresult:9522=117.(Uyuxi2219Uyuin221test5)S./tests.2.outI,mthepthreadofIHJltiPlying,thisisnyresult:39421638.mthepthreadofPlUSing,thisisnyresult:8495=179.Imth

29、epthreadofPlUSing,this1$nyresult:2637三63.I,mthepthreadofPlUSing,thisisnyresult:4859167.mthepthreadofPIUSingJhiSisnyresult:21=21.Imthepthreadofnultplyingrthlsisnyresult:732=146.Imthepthreadofplusingrthisisnyresult:31536.mthepthreadofIiultiplyingrthisisyresult:4665=2998.ImthepthreadofaultlplyigrthlsIs

30、nyresult:7089=6239.ImthepthreadofIuJItiPlying,thisisnyresult:952229.IIiyUXin2210IiyUXin221test5SQ四乂小V跳格JML8v行6,列3息人I应厢i序121无datal,txt文件(F)Mti(E)1*(V投军工HE匚Ia打开V土保存日c-datal.txtX.用程序位置系统.文件(F)编辑(E)查看(VU与打开11月IOB囱dat文件(F)编辑(E)查看(V)搜索(data2.txt427384952637485920匚Ja打开V土保存Ildatal.txt1659521234567892目回凶data

31、2.txt(testytest5)-geditIiyuxin221liyuxin221:*/test/test5文件(F)编鬼(E)查看(V)终端(T)帮助(三)Im the pthread of plusingrthis is my result: Iiyuxin221l.iyuxin221 test5S ./tests.2.out48 +20 = 68.I,m the Im the Im the Im the Im the Im the Im the Im the Im the Im thepthread pthread pthread pthread pthread pthread pt

32、hread pthread pthread pthreadof of of of of of of of of ofmultiplying,this is my result: PIUSing,this is my result: 84 +multiplying,this is my multiplying,this is my multiplying,this is my multiplying,this is myresult: result: result: result:plusing,this PlUSing,this plusingfthis plusing,thisis my r

33、esult: is my result: is my result: is my result:39 42 = 95 = 179.73 * 26 =37 48 =59 * 2 =1 * 31 =: 46 = 48.5 + 65 = 7.7 +95 +89 = 159.22 = 117.1638.1898.1776. :118. 31.Iiyuxin22101iyuxi221 test5$ .test5.2.outIm the Im the Im the Im the Im the Im the Im the Im the Im the Im thepthread pthread pthread

34、 pthread pthread pthread pthread pthread pthread pthreadof of of of of of of of of ofmultiplying,this is my result:plusingrthis is my PlUSing,this is my plusing,this is my PlUSing,this is myresult: result: result: result:84 +26 +48 +20 +39 42 = 1638.95 179.37 = 63.5917.multiplying,this is my result:

35、 PlUSing,this is my result: 31 multiplying,this is my result: multiplying,this is my result:multiplying,this is my result: Iiyuxin221liyuxin221 test5S 1 = 21.73 5 = 36.46 * 7 * 95 2 = 146.65 = 299.89623.22 = 290.【收获及体会】在完成上次实验时,我在网上找到了一片相关文章,里面稍有提及互斥锁的知识,当时我粗略看了一下。没想到这次试验的主体内容就是使用互斥锁和信号量。简而言之,互斥锁是对互斥资源进行限制访问,以防止不同线程(进程)同时对某个资源进行访问,而导致资源的状态不明,出乎意料,用来实现互斥的。进而在使用共有资源时,例如本题中的标识量in,out时候,就需要加锁和解锁,以达到在访问in,out时候,只有某一个线程访问此资源,就不会导致资源的状态混乱。而信号量是用于实现进程同步的,一般是为了使若干线程(进程)之间运行先后有序。本例中,读取线程一定要在计算线程之前,因而使用信号量,来控制线程之间的先后顺序。通过本次实验,我也复习了之前学过的一些C语言知识。包括文件的读取与写入,线程的创建与结束等。这次实验很有意义,我收获很多。【参考资料】实验指导书

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号