




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、操作系统实验报告实验三一个进程启动另一个程序的执行【实验目的】在Linux环境系统中,execve系统调用用于执行一个程序(可执行二进 制文件或脚本)。exec 函数家族,包括 execl> execlp> execle execv> execvp, 是execve系统调用的前端。本实验要求学生学习在一个进程中启动另一个程序 执行的基本方法,了解execve系统调用和exec函数家族的使用方法。【实验内容】(-)初步认识“在一个进程中启动另一个程序的执行”。1 >编辑一个源程序dummy.c,并编译为可执行程序dummy0/ dunimy.c#include <s
2、tdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>int main(int argc, char *argv)(int result;printf(nnYou are now in a running program "%s". argvOJ);printf(MMy PID is %d. My parent's PID is %d.n", getpid(), getppid();printf(HPlease input an int
3、eger (0-255), which will be returned to my parent process:'。");scanf(H%d, &result);printf(nGoodbye.nnn);return (result &0377);1)2、再编辑一个源程序exec_test.c,并编译为可执行程序exec_testo/ exec_test.c#inckide <stdio.h>#inckide <stdlib.h>#include <error.h>#include <sys/types.h>
4、#include <unistd.h>#include <sys/wait.h>int main(int argc, char *argv)(int result;result = fork();if (result < 0)perror(nFailed to create child");exit(l);)else if (result = 0)/ Child 1char *cmd = "./dummy"printf("Child process*s PID is %d. My parent's PID is %d
5、.n", getpid(), getppid();printf(HChild process is about to execute n%sHnnH, cmd);result = execlp(cmd, cmd, NULL);if (result = - 1)perror(nIn child process, failed to exec a program");)exit(O);)else(/ parentint status;printf(HParent process's PID is %d.n, getpidQ);printf(MParent process
6、 is waiting . nn);wait(&status);printf(HIn parent process, status = Ox%x, WEXITSTATUS(status) = %d (i.e. Ox%x)n' status, WEXITSTATUS(status), WEXITSTATUS(status);)return (EXIT_SUCCESS);)3、先执行dummy,观察、分析执行结果;然后执行程序exejtest,观察、 分析执行结果。(注意,两个可执行程序都在当前目录下)(二)实现一个简单的命令解释外壳(Shell)。1、基本功能:(1)从标准输入读
7、取一行字符串,其中包含欲执行的命令和它的命令行参 数(如果有的话)。提示:需要将输入的一行字符串进行拆分,以空格、制表符 (t)作为分隔符,分解为命令、命令行参数(零个或多个)。如果用户输入的命 令是“quit”,则退出执行。(2)创建一个子进程。(3)在子进程中,执行在(1)读入的命令,如果有命令行参数,也要传递。(4)在父进程中,等待子进程结束,然后打印子进程的返回值。(5)在父进程中,控制转移至(Do【实验要求】按照要求编写程序,放在相应的目录中,编译成功后执行,并按照要求分析执行结果,并写出实验报告。【实验设计】dummy程序:Iliyuxin221liyuxin221 test35
8、./dunwyYou are now in a running program M./dumry M.My pid is 14758. My parent's PID is 14449.Please input an integer (0-255)r which will be returned to my parent process: 233Goodbye.Iliyuxin22WXiyuxin221 test35 I结果显示了该进程的PID和父进程的PID,并且将一个值返回给了父进程。exec_test 程序:In parent process, status = 0x2a00rW
9、EXITSTATUS(status) = 42 (i.e. 0x2a) Iiyuxin221liyuxin221 test3$ ,/exec test.outParent process's PID is 14769.Parent process is waitingChild process's PID is 14770. My parent's PID is 14769.Child process is about to execute 11 ./dumy"you are now in a running program "./durmy M.M
10、y PID is 14770. My parent's PID is 14769.Please input an integer (0-255), which will be returned to i)y parent process: 19Goodbye.in parent process, status = 9xi309rWEXiTSTATUS(status) = 19 (i.e. 0x13) Liyuxin22lliyuxin221 test33 |在子进程中键入的数值返回了父进程,并显示了出来(19).实现一个简单的命令解释外壳(Shell) 源代码:/example#inc
11、lude<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>#i nc 1 u de<errno. h>#include<sys/wait.h>#include<string.h>int main()int pid;int rm; 子进程的返回值int exec_errorno;char command256;char * p;char * q;char *c20;int i=0J=0;while(l)/从终端读取要执行的命令pri
12、ntf(">H);command 0=,01;p= fgets(command,256,stdin);if (p= NULL)perror(MError in fgets().H);exit(-l);)/Delete the last char (new line) in the string returned by fgets() commandstrlen(command)-1 = '0'; p=command;q二p;/Quit if user inputs HquitHif (!strcmp(command,HquitH) break;)/Create
13、a child process to execute command pid = fork();if (pid<0) perror(MFailed while calling fork.”); exit(-l);)else if(pid = 0)/子进程执行此命令for(;)ci=strtok(p/f H); if(ci=NULL) break;)i+; p=NULL;1P=c01;for(j=0;j<i;j+)printf("sn:cj); ) exec_errorno = execvp(p,c);如臬exec函数返回,表明没有正常执行命令只有在这种情况下,才会执行下面
14、的打印错误信息perror(command);exit(exec_errorno);)else父进程,等待子进程结束,并打印子进程的返回值wait(&rtn);printf(Hn Value returned from child process, rtn =%dnH,rtn);printf("WEXITSTATUS(rtn) = %dii",WEXITSTATUS(rtn);)return 0;)【实验测试结果及分析】此程序通过对exec,p()函数的调用,使得通过输入命令被执行。输入的命令用fgets()存入command字符串中,然后反复调用strtok。函数
15、, 分割所得字符用,达到将命令分割的目的,使得多命令行参数得以实现。【运行结果】又件(F) 融(E) S#(V)籍端(T)帮助(H)>quitIiyuxin221liyuxin221 test3$ ./final2.out>ls -alIsal皂用星 152jrwxrwxr-x.2liyuxin221Xiyuxin221469618月231G:45,jrwxrwxr-x.5Uyuxin221liyuxin221469618月2820:35,-rwxrwxr-x.1liyuxin221liyuxin221536418月2821:34dunwny-rwxrwxr-x.1liyuxm22
16、1liyuxin221536418月2319:43dummy30- rwxrw-rw-,1Uyuxin221liyuxin22145218月2821:34dummy.c- ni'xrwxr-x.1liyuxin221liyuxin221523818月2821:03dummy.out- rwxErw.1Liyuxm22Lliyuxin221162418月2310:24example.c- rwxrwxr-x. 1Uyuxin221liyuxin221635418月2310:25example.outrwxrwrw.1liyuxin221Hyuxin22110018月2120:58exec
17、l.c-rwxrwxr-x.1Iiyuxm221liyuxin221474818 月2120:58exec I. out-rwxrw-rw-.1liyuxm221liyuxin22192818月2820:54exec_test.c-rwxrwxr-x.1Uyuxin221Hyuxin221598618月2629:54exec-test.outrwxrwrw-.1liyuxin221liyuxin221161918月2310:22finall.c-rwxrwxr-x.1liyuxin221liyuxin221633318月2310:22fmall.out- rwxrw-rw-.1Uyuxin22
18、1Iiyuxin221164718月2310:42final?.crxrwxrx.1liyuxin221liyuxin221630518月2310:42final2.ouT- pR-xr-xr-x.1Iiyuxin221liyuxin221633318月1619:57gnome-terminal.desktop- rwxrwxr-x.1liyuxin221liyuxin22164G718月2120:48shell- rwxrwrw.1 liyuxin221 Hyuxin221 1928 18月 21 20:47 shell.c- rwxrw-rw-. 1liyuxin221liyuxin221
19、30118月2310:45strtokl.c- rwxrwxr-x. 1liyuxm221liyuxin221496518月2310:45strtokl.out- rwxrw-rw-, 1Uyuxin221liyuxin22141818月2121:24strtok.c- rwxrw-rw-. 1liyuxin221liyuxin221565211月62812 text3【收获及体会】 实验指导书在实验调试和分析的过程中,我越来越懂得自学能力和自 我独立解决问题的重要性。在以后的实验和学习中,我会不断的加强 对这方面的训练。【参考资料】实验指导书实验五 线程间的互斥与同步【实验目的】理解POSI
20、X线程(Pthread)互斥锁和POSIX信号量机制,学习它们的使用方 法;编写程序,实现多个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是
21、消费者,从缓冲区读取数据然后作不同的计算(加和乘运算)。使用互斥锁和信号量控制这些线程的同步。不限制线程C和D从缓冲区得到的数据来自 哪个文件。(2)在生产者线程中,确保从文件读出数据以后,再去“生产”。在开始设计和实现之前,务必认真阅读下列内容:课本6.8.4节;讲义(课堂PPT)中关于“生产者-消费者问题”的部分;课本第6章后面的编程项目一一生产者-消费者问题。【实验要求】按照要求编写程序,放在相应的目录中,编译成功后执行,并按照要求分析执行结果,并写出实验报告。【实验设计】#include <stdio.h>#include <pthread.h>#inckide
22、 <semaphore.h>#inckide <stdlib.h>#include <unistd.h>#define NUM THREADS 5int buffer5;int in = 0; 写入标识int out = 0; 读取标识pthread_mutex_t niutex2in, mutex2out;sem_t wri, rea;void *writel(void *arg)(FILE *fpl = fopen(ndatal.txtH, Mrn);int wl;int il = 0;for(i 1 =0;il < 10;il+)(/p (写资源
23、) sem_wait(&wri);从文件中读取一个文件if(fscanf(fpl/,%d, &wl)9 else (printf(Tm the pthread write 1J can't read a num from datal.txt.nn); exit(l);)/放入缓存对应位置加in锁pthread_mutex_lock(&mutex2in);bufferin = wl;in = (in + 1) % NUM_THREADS;解in锁pthread_mutex_unlock(&mutex2in);v(读环源) sem_post(&rea
24、);)线程结束fclose(fpl);pthread_exit(NULL);) void *write2(void *arg)int w2;FILE *fp2 = fopen("data2.txt", "r");int i2 = 0;for (i2 = 0; i2< 10; i2+)(HP (写资源)sem_wait(&wri);从文件中读取一个文件if (fscanf(fp2, "%d", &w2)else(printf(Tm the pthread write 1J can't read a num
25、from datal.txt.nn); exit(l);)/放入缓存对应位置加in锁pthread_mutexjock(&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)(加进程int al, bl, cl;int p = 0;for (p = 0; p < 5; p+)(p(读资
26、源)sem_wait(&rea);从缓冲中读取一个文件加out锁pthread_mutexjock(&mutex2out);al = bufferout;out = (out + 1) % NUM_THREADS;解out锁pthread_mutex_unlock(&mutex2out);/V (写资源)sem_post(&wri);p(读资源)sem_wait(&rea);加out锁pthread_mutexjock(&mutex2out);从缓而中读最一个文件bl = bufferout;out = (out+ 1)% NUM_THREADS
27、;解out锁pthread_mutex_unlock(&mutex2out);cl = al + bl;计算求和并且输出到屏幕printf(nrm the pthread of pkisingjhis is my result:t %d + %d = %d. n", al, bl, cl);/V (写资源)sem_post(&wri);)线程结束pthread_exit(NULL);)void *mult(void *arg)(乘进程int a2, b2, c2;int p = 0;for (p = 0; p < 5; p+)P(读资源) sem_wait(&a
28、mp;rea);/加out锁pthread_mutexjock(&mutex2out);从缓而中读最一个文件a2 = bufferout;out = (out + 1) % NUM_THREADS;解out锁pthread_mutex_unlock(&mutex2out);/V (写资源) sem_post(&wri);p(读资源) sem_wait(&rea);加out锁pthread_mutexjock(&mutex2out);从缓而中读最一个文件b2 = bufferout;out = (out+ 1)% NUM_THREADS;解out锁pthr
29、ead_mutex_unlock(&mutex2out); c2 = a2 * b2;计算求积并且输出到屏幕printf(the pthread of multiplyingjhis is my result:t %d * %d = %d. n”, a2, b2, c2);/V (写资源) sem_post(&wri);)线程结束pthread_exit(NULL);)int main()(互斥锁初始化pthread_mutex_init(&mutex2in, NULL);pthread_mutex_init(&mutex2out, NULL);pthreadA
30、, B, C, D;int err;信号量初始化sem_init(&wri, 0, 5);sem_init(&rea, 0, 0);创建线程1err = pthread_create(&A, NULL, write 1, NULL);if (err !=0)printf("can't create myThreadl :%snH, strerror(err);1创建线程2err = pthread_create(&B, NULL, write2, NULL);if (err != 0)(printf(Hcanrt create myThread
31、2:%snH, strerror(err);1稍微延迟sleep(O.l);创建线程3err = pthread_create(&C, NULL, plus, NULL);if (err !=0)printf(Hcanrt create myThread3:%sn,r, strerror(err);)创建线程4err = pthread_create(&D, NULL, mult, NULL);if (err !=0)printf(Hcan,t create myThread4:%snH, strerror(err);)pthreadJoin(A, NULL);pthreadJ
32、oin(B, NULL);pthread-join(C, NULL);pthread-join(D, NULL);return 0;> Fedo»» x I丘栅月也受嵇Q匚一上dataX.txt I - /*1打开土Iiyuxin221>liyuxin221:*/tc5tztcst5震件IF)网t第E:,»«(V)胡&(T>帚劝(川datd2.txt x39427384952637485920n the pthread of plusing,this is try result:n the pthread b the pihre
33、ad n the pthread n the pthread a the pthread n the pthread 口 the pthread fi the pthread n the pthread a the pthreadn the pthread fi the pthread n the pthread a the pthread fi the pthread n the pihread n the pthreac n the pthread n the pthread n the pthreadof of of of of of ofofof : of of of of : of7
34、 oftest5)$ ./tests.2.outpluslngrtftls is ny result:wjltiplyingrthis is wy wjltiplying,this is try »jltiplylng,thls is try wltiplying.this is wypluslngrthls plusingrthi5 pluming,thi5 plusing,th"is ny result: is ny result: 15 fry result: is ny result:result: result: result: result:test5)$ ,/
35、tcstJ.Z.outwjltlplylng,thls is try result : pl”ing,vn$ is ny result: plusing,this is fry result: plusingrthis is rry result: plusing,thi$ is ny result: wltiplying,this U ny result : plusingjnis is ny result: »jltiplying,thi5 is vy result: wltiplying,this Is uy result:wltiplying.this is ny resul
36、t :ultiplying.this is «y result:31=60.39 - 42 =179.73 4 2637 - 4859 201 M 31 48.70. =159. -117.10381898 1776.1180.31.953759139 , 42 -179. =63.-107.- 21.73 « 2 : 36.46 - 6576 8995 22=1638.146.=2998.-6230. =2898文件(门珈2S(V)搜方S) T*<TI_ jfij打畀v 2也存日datal.at x31 4G65708993 2斗data2.txt (-/test/
37、test5) - geditIJI nJ |_xj .一')搜索工具(T)文档(D)砌(H)画Iiyuxin221liyuxin221:-/test/test5LJ 回文件(F)编辑(日查看(V)终端(T)帮助(H)I'm the pthread of plusing,this is my result: 48 + 20 = 68. liyuxin221(3Hyuxin221 test5$ ./test5.2.outI'm the pthread of multiplying,this is my result:39 42 = 1638.I'm the pthread of plusing,this is my result: 84 + 95 = 179.I'm thepthreadofmultiplying,thisismyresult:73*26=1898.I'm thepthreadofmultiplying,thisismyresult:37*48=1776.I'm thepthreadofmultiplying,thisismyresult:59*20=1180.m thepthreadofmultiplying,this
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 客户停送电管理制度
- 宣传部统一管理制度
- 家具送货单管理制度
- 个人学习远程培训总结-1
- 彩钢厂安全管理制度
- 循环水使用管理制度
- 心理检测科管理制度
- 快递员业务管理制度
- 总分包安全管理制度
- 总裁班培训管理制度
- 2025年全国低压电工作业证(复审)考试练习题库(600题)附答案
- 混凝土预制构件项目可行性研究报告参考范文
- 2025漳浦县国企招聘考试题目及答案
- 知识产权相关的国际法的试题及答案
- 低压电工复审培训
- 钢结构墙板拆除施工方案
- 2025年养老护理员专业知识测试卷:养老护理员护理技能操作试题集
- 新能源汽车充电系统故障诊断与维修技术研究
- 护理典型案例分享
- VDA6.3-2023版培训教材课件
- 2025年GCP(药物临床试验质量管理规范)相关知识考试题与答案
评论
0/150
提交评论