2022年操作系统实践分析报告.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年操作系统实践分析报告.pdf》由会员分享,可在线阅读,更多相关《2022年操作系统实践分析报告.pdf(41页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、页眉内容页脚内容操作系统实践报告精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 1 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容多进程题目sh1.c: 实现 shell 程序,要求具备如下功能支持命令参数$ echo arg1 arg2 arg3 $ ls /bin /usr/bin /home 实现内置命令cd、pwd 、exit $ cd /bin $ pwd /bin 思路:说明:首先设置一个死循环模仿shell 终端,读取用户的输入,并且根据空格将输入拆分成字符串数组
2、,然后调用excute这个子函数进行处理。1.echo 根据数组第一个元素来判断命令是什么,判断出是ehco 后, fork 一个新的进程,将其后的内容一个个输出出来,并且父进程等待子进程退出后再执行,确保输出在屏幕上时不被打断。2.ls 读取用户输入并且根据拆分的数组判断出是ls 命令后, fork 一个新的进程,调用execlp函数将/bin/ls下的 ls 程序装入子进程并将拆分的数组参数部分传递给ls 即可,同样的,父进程等待子进程退出,确保输出在屏幕上不被打断。3.cd 同样是根据输入并拆分成数组后判断出是cd 命令后, fork 一个新的进程,然后调用chdir 并将拆分数组的参数
3、部分传递给chdir作为实参即可。4.pwd 同样是根据输入并拆分成数组后判断出是pwd 命令后, fork一个新的进程,然后调用system(pwd)即可,此命令也可以用来验证上面的cd 命令是否正确执行。5.exit 根据用户输入逼格拆分的数组判断出是exit 命令后,excute 子函数返回 -1 , 在循环中检测excute的返回值,如果是-1 则直接 return ,退出模仿的shell 终端。精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 41 页 - - - - - - - -
4、- - 页眉内容页脚内容sh2.c: 实现 shell 程序,要求在第 1 版的基础上,添加如下功能实现文件重定向$ echo hello log $ cat log Hello 思路:接 sh1.c 的描述,若判断出是echo 命令后,要再次判断拆分的字符串数组中有无“ ”出现,如果有,则把“ ”之前、 echo 之后的内容作为输出,把“ ”之后到“ ”之后的第一个空白字符作为文件名, fopen创建文件并fwrite将输出内容输出到该文件中,并关闭文件。sh1.c 和 sh2.c 的源代码:#include #include #include #include #include #incl
5、ude #include #define LEN 256 #define WIDTH 256 #define HEIGHT 10 void split(char source,char destHEIGHTWIDTH) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 3 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容char *p; p=strsep(&source, ); int i=0; for(i=0;pi!=0;i+) dest0i=pi; dest0i=0; int
6、j=1; while(p) p=strsep(&source, ); if(p) for(i=0;pi!=0;i+) destji=pi; destji=0; j+; int execute(char commHEIGHTWIDTH) if(strcmp(comm0,echo)=0) int pid=fork(); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 4 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容if(pid=0) int i=0; int is=0; for(
7、i=1;commi0!=0;i+) if(commi0=) is=1; break; if(is=1) puts(commi+1); FILE *fp=fopen(commi+1,w+); int j=0; for(j=1;j); gets(command); split(command,splitArray); int i=0; if(-1=execute(splitArray) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 7 页,共 41 页 - - - - - - - - - - 页眉内容页脚内
8、容return 0; sh3.c: 实现 shell 程序,要求在第 2 版的基础上,添加如下功能实现管道$ cat /etc/passwd | wc -l 实现管道和文件重定向$ cat input.txt 3 2 1 3 2 1 $ cat output.txt $ cat output.txt 1 2 3 思路:首先读取用户输入,以“|”为分隔符将输入分割成字符串数组,然后在一个while循环中依次执行下面的动作:代码中通过pipe() 函数来创建管道,创建之后父进程和子进程一个只能向管道写内容,一个只能向管道读内容。然后利用dup() 函数来把进程的输入流或者输出流重定向到管道里,这样
9、就能实现管道的操作。 实现的时候注意可以使用多个“|” 来迭代进行管道操作,需要使用一个循环来处理。用 system 执行每一条命令,同时还要注意最后一个操作的输出流是标准输出(即屏幕),不需要重定向到管道里,需要特殊处理一下。精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 8 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容源代码:#include #include #include #include #include #include #include #define LEN
10、 256 #define WIDTH 256 #define HEIGHT 10 void split(char source,char destHEIGHTWIDTH) char *p; p=strsep(&source,|); int i=0; for(i=0;pi!=0;i+) dest0i=pi; dest0i=0; int j=1; while(p) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 9 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容p=strsep(&
11、source,|); if(p) for(i=0;pi!=0;i+) destji=pi; destji=0; j+; main() char commandLEN; char splitArrayHEIGHTWIDTH=0; printf(%s,); gets(command); split(command,splitArray); int i=0; for(i=0;splitArrayi0!=0;i+) puts(splitArrayi); int p2; pipe(p); int j=0; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 -
12、 - - - - - - - - -第 10 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容for(j=0;splitArrayj+10!=0;j+) if (fork() = 0) / Child process close(0); close(p0) close(p1); dup(p0); system(splitArrayj); else /Parent process close(1); close(p0) close(p1); dup(p1); system(splitArrayj+1); 多线程题目pi1.c: 使用 2 个线程根据莱布尼兹级数计算PI
13、莱布尼兹级数公式: 1 - 1/3 + 1/5 - 1/7 + 1/9 - . = PI/4 主线程创建 1 个辅助线程主线程计算级数的前半部分精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 11 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容辅助线程计算级数的后半部分主线程等待辅助线程运行結束后,将前半部分和后半部分相加思路:计算公式前1000 项,主线程计算前5000 项,子线程计算后5000 项,主进程等待子进程结束,通过 pthread_join(sub,(void *
14、)&result);的 result 参数获取子进程计算结果再相加即可。源代码:#include #include #include #include #define LEN 10000 struct result float sum; ; void *subThread() int i; float j; struct result *result; float sum1=0,sum2=0,sum=0; for(i=LEN/2+1;isum=sum; return result; int main() 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师
15、归纳 - - - - - - - - - -第 13 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容int i; float j; float sum1=0,sum2=0,sum=0; for(i=1;isum; printf(%fn,sum); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 14 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容return 0; pi2.c: 使用 N 个线程根据莱布尼兹级数计算PI 与上一题类似,但本题更加通用化
16、,能适应N 个核心,需要使用线程参数来实现主线程创建 N 个辅助线程每个辅助线程计算一部分任务,并将结果返回主线程等待 N 个辅助线程运行结束,将所有辅助线程的结果累加思路:设计算公式前1000 项,读取用户输入的线程数目N ,通过pthread_create(&workersi-1,NULL,compute,myparam);产生 N 个线程, 并且通过 myparam设置每一个线程计算的起始项和终止项,通过 pthread_join(workersj,(void *)&myresult);等待每个线程结束并通过result 获取结果,将结果相加即可。源代码:#include #includ
17、e #include #include #define LEN 10000 #define MAX_WORKERS 100 struct param int start; int end; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 15 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容; struct result float sum; ; void *compute(void *arg) int i; float j; struct param *myparam; myp
18、aram=(struct param *)arg; int start=myparam-start; int end=myparam-end; struct result *myresult; float sum1=0,sum2=0,sum3=0; for(i=start;isum=sum2-sum1; return myresult; int main() int thread_num=1; struct param myparamsMAX_WORKERS+1; pthread_t workersMAX_WORKERS; printf(please input thread number:)
19、; scanf(%d,&thread_num); int i; myparams0.start=0; myparams0.end=0; for(i=1;istart=myparamsi-1.end+1; myparam-end=myparamsi.start+(LEN/thread_num)-1; pthread_create(&workersi-1,NULL,compute,myparam); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 17 页,共 41 页 - - - - - - - - - -
20、 页眉内容页脚内容myparamsthread_num.start=myparamsthread_num-1.end+1; myparamsthread_num.end=LEN; pthread_create(&workersthread_num-1,NULL,compute,&myparamsthread_num); int j; float sum=0; for(j=0;jsum; free(myresult); printf(%fn,sum); return 0 ; sort.c: 多线程排序主线程创建一个辅助线程主线程使用选择排序算法对数组的前半部分排序辅助线程使用选择排序算法对数组的
21、后半部分排序主线程等待辅助线程运行結束后,使用归并排序算法归并数组的前半部分和后半部分思路:精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 18 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容主线程排序数组的前半部分,辅助线程排序后半部分,pthread_create(&worker_id,NULL,&sort,&pa);中 pa 传递的是数组的首地址,主线程等辅助线程结束后,再调用merge将数组合并为有序。源代码:#include #include #include #in
22、clude #define LEN 10 int arrayLEN=0,3,8,6,2,9,5,4,1,7; struct param int *arr; ; void *sort(void *arg) struct param *mypa; mypa=(struct param *)arg; int i=0; int j=0; int min=0; int temp=0; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 19 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容fo
23、r(i=LEN/2;iLEN-1;i+) min=i; for(j=i;jarrminmypa-arrj)min=j; temp=mypa-arrmin; mypa-arrmin=mypa-arri; mypa-arri=temp; void merge() int i=0; int aLEN/2; int bLEN/2; for(i=0;iLEN/2;i+) ai=arrayi; bi=arrayi+LEN/2; /* for(i=0;iLEN/2;i+) printf(%dn,ai); */ 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 -
24、 - - - - - - - - -第 20 页,共 41 页 - - - - - - - - - - 页眉内容页脚内容int tm=0; int ti=0,tj=0; while(tiLEN/2&tjLEN/2) if(atibtj) arraytm=ati; ti+; else arraytm=btj; tj+; tm+; int main() struct param pa; pa.arr=array; int ti=0,tj=0,tmin=0; for(ti=0;tiLEN/2-1;ti+) tmin=ti; for(tj=ti;tjarraytj)tmin=tj; int temp=
25、arraytmin; arraytmin=arrayti; arrayti=temp; pthread_t worker_id; pthread_create(&worker_id,NULL,&sort,&pa); pthread_join(worker_id,NULL); merge(); int i=0; for(i=0;iLEN;i+) printf(%dn,arrayi); pc1.c: 使用条件变量解决生产者、计算者、消费者问题系统中有 3 个线程:生产者、计算者、消费者系统中有 2 个容量为 4 的缓冲区: buffer1、buffer2 生产者生产 a 、b 、c 、 d 、e、
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022 操作系统 实践 分析 报告
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内