版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、南 京 林 业 大 学 本 科课程设计报告2010 2011 学年 第 二 学期课程名称:操 作 系 统 任课老师: 夏 霖 学科专业:计算机科学与技术学 号: 090801124 姓 名: 谢 阳 2011 年 6 月 28 号目录设计目的:2设计目的:18设计目的:24实验总结设计一 进程管理设计目的:(1)加深对进程概念的理解,明确进程和程序的区别;(2)进一步认识并发执行的实质;(3)实现犁linux。(4)了解Linux系统中进程通信的基本原理。设计内容: 1、【题目】进程的创建和控制 【任务1】编写一段程序,使用系统调用fork()创建两个子程序。当此程序运行时,在系统中有一个父进
2、程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符a;子进程分别显示字符b和c。记录屏幕上的显示结果,并分析原因。修改以前编写的程序,将每个进程输出一个字符改为每个进程输出一句话。【详细设计】 #include<stdio.h> main() int p1,p2; while(p1=fork()=-1); if(p1=0) putchar('b'); else while(p2=fork()=-1); if(p2=0) putchar('c'); else putchar('a'); 【运行结果】lenovoleno
3、vo-desktop:$ gcc -o 1-1-1.out 1-1-1.clenovolenovo-desktop:$ ./1-1-1.outbca修改后程序:#include<stdio.h>main() int p1,p2; while(p1=fork()=-1); if(p1=0) printf("你好中国n"); else while(p2=fork()=-1); if(p2=0) printf("我叫谢阳n"); else printf("我就读于南林n"); 运行结果:lenovolenovo-desktop:
4、$ gcc -o 1-1-0.out 1-1-0.clenovolenovo-desktop:$ ./1-1-0.out你好中国我叫谢阳我就读于南林【任务2】【详细设计】#include<stdio.h>void main() int p1,p2,i; while(p1=fork()=-1); if(p1=0) for(i=0;i<10;i+) printf("child%dn",i); else while(p2=fork()=-1); if(p2=0) for(i=0;i<10;i+) printf("son%dn",i);
5、else for(i=0;i<10;i+) printf("daughter%dn",i); 【运行结果】lenovolenovo-desktop:$ gcc -o 1-1-2.out 1-1-2.c1-1-2.c: 在函数main中:1-1-2.c:3: 警告: main的返回类型不是intlenovolenovo-desktop:$ ./1-1-2.outchild0child1child2child3child4child5child6child7child8child9son0son1son2son3son4son5son6son7son8son9daught
6、er0daughter1daughter2daughter3daughter4daughter5daughter6daughter7daughter8daughter9【任务3】【详细设计】#include<stdio.h>main() int p1,p2; int i; while(p1=fork()=-1); if(p1=0) lockf(1,1,0); for(i=0;i<10;i+) printf("child %dn",i); lockf(1,0,0); else while(p2=fork()=-1); if(p2=0) lockf(1,1,0
7、); for(i=0;i<10;i+) printf("son %dn",i); lockf(1,0,0); else lockf(1,1,0); for(i=0;i<10;i+) printf("daughter %dn",i); lockf(1,0,0); 【运行结果】lenovolenovo-desktop:$ gcc -o 1-1-3.out 1-1-3.clenovolenovo-desktop:$ ./1-1-3.outchild 0child 1child 2child 3child 4child 5child 6child 7
8、child 8child 9son 0son 1son 2son 3son 4son 5son 6son 7son 8son 9daughter 0daughter 1daughter 2daughter 3daughter 4daughter 5daughter 6daughter 7daughter 8daughter 92、【题目】软中断通信 【任务1】 【详细设计】#include<stdio.h>#include<signal.h>#include<unistd.h> int wait_mark,SIGNAL;void waiting(),stop
9、();void waiting() while(wait_mark!=1);void stop() wait_mark=0;main() int p1,p2,stdout; while(p1=fork()=-1); if(p1>0) while(p2=fork()=-1); if(p2>0) wait_mark=1; signal(SIGNAL,stop); waiting(0); kill(p1,16); kill(p2,17); wait(0); wait(0); printf("parent process is killed!n"); exit(0);
10、else wait_mark=1; signal(17,stop); waiting(); lockf(stdout,1,0); printf("child process 2 is killed by parent!n"); exit(0); else while(p2=fork()=-1); if(p2>0) wait_mark=1; signal(16,stop); waiting(); lockf(stdout,1,0); printf("child process 1 is killed by parent!n"); lockf(stdo
11、ut,0,0); exit(0); 【运行结果】lenovolenovo-desktop:$ gcc -o 1-2-1.out 1-2-1.c1-2-1.c: 在函数main中:1-2-1.c:30: 警告: 隐式声明与内建函数exit不兼容1-2-1.c:38: 警告: 隐式声明与内建函数exit不兼容1-2-1.c:50: 警告: 隐式声明与内建函数exit不兼容lenovolenovo-desktop:$ ./1-2-1.outchild process 1 is killed by parent!child process 2 is killed by parent!parent pr
12、ocess is killed!【任务2】【详细设计】#include<unistd.h>#include<signal.h>#include<stdio.h>int pid1=0,pid2=0;int SIG,IGN,pid;int EndFlag=0;void IntDelete() kill(pid1,16); kill(pid2,17); EndFlag=1;void Int1() printf("child process 1 is killed!by parentn"); exit(0);void Int2() printf(
13、"child process 2 is killed!by parentn"); exit(0);main() int exitpid; signal(SIGINT,SIG-IGN); signal(SIGQUIT,SIG-IGN); while(pid1=fork()=-1); if(pid=0) signal(SIGINT,Int1); signal(SIGQUIT,SIG-IGN); pause(); exit(0);else while(pid2=fork()=-1); if(pid2=0) signal(SIGINT,Int2); signal(SIGQUIT,I
14、ntDelete); pause(); exit(0); else signal(SIGINT,IntDelete); waitpid(-1,&exitpid,0); printf("parent process is killedn"); exit(0);【运行结果】lenovolenovo-desktop:$ gcc -o 1-2-2.out 1-2-2.c1-2-2.c: 在函数Int1中:1-2-2.c:14: 警告: 隐式声明与内建函数exit不兼容1-2-2.c: 在函数Int2中:1-2-2.c:18: 警告: 隐式声明与内建函数exit不兼容1-2-
15、2.c: 在函数main中:1-2-2.c:22: 警告: 传递signal的第 2 个参数时将整数赋给指针,未作类型转换1-2-2.c:23: 警告: 传递signal的第 2 个参数时将整数赋给指针,未作类型转换1-2-2.c:27: 警告: 传递signal的第 2 个参数时将整数赋给指针,未作类型转换1-2-2.c:29: 警告: 隐式声明与内建函数exit不兼容1-2-2.c:37: 警告: 隐式声明与内建函数exit不兼容1-2-2.c:43: 警告: 隐式声明与内建函数exit不兼容lenovolenovo-desktop:$ ./1-2-2.out你好我来自计算机一班我的学号是
16、0908011243、【题目】进程的管理通信【任务】【详细设计】#include <unistd.h>#include <signal.h>#include <stdio.h>int pid1,pid2;main() int fd2; char OutPipe100,InPipe100; pipe(fd); while(pid1=fork()=-1); if(pid1=0) lockf(fd1,1,0); sprintf(OutPipe,"child 1 process is sending message!"); write(fd1,O
17、utPipe,50); sleep(1); lockf(fd1,0,0); exit(0); else while(pid2=fork()=-1); if(pid2=0) lockf(fd1,1,0); sprintf(OutPipe,"child 2 process is sending message!"); write(fd1,OutPipe,50); sleep(1); lockf(fd1,0,0); exit(0); else wait(0); read(fd0,InPipe,50); printf("%sn",InPipe); wait(0)
18、; read(fd0,InPipe,50); printf("%sn",InPipe); exit(0); 【运行结果】lenovolenovo-desktop:$ gcc -o 1-3.out 1-3.c1-3.c: 在函数main中:1-3.c:18: 警告: 隐式声明与内建函数exit不兼容1-3.c:30: 警告: 隐式声明与内建函数exit不兼容1-3.c:40: 警告: 隐式声明与内建函数exit不兼容lenovolenovo-desktop:$ ./1-3.outchild 1 process is sending message!child 2 proces
19、s is sending message!设计二 进程间通信设计目的:设计预备内容设计内容1、【题目】消息的创建,发送和接收【任务】【详细设计】#include<stdio.h>#include<sys/types.h>#include<sys/msg.h>#include<sys/ipc.h>#define MSGKEY 75struct msgform long mtype; char mtext1030;msg;int msqid,i;void CLIENT() int i; msqid=msgget(MSGKEY,0777); for(i
20、=10;i>=1;i-) msg.mtype=i; printf("(client)sentn"); msgsnd(msqid,&msg,1024,0); exit(0);void SERVER() msqid=msgget(MSGKEY,0777|IPC_CREAT); do msgrcv(msqid,&msg,1030,0,0); printf("(server)receivedn"); while(msg.mtype!=1); msgctl(msqid,IPC_RMID,0); exit(0);void main() whil
21、e(i=fork()=-1) if(!i) SERVER(); while(i=fork()=-1) if(!i) CLIENT(); wait(0); wait(0);【运行结果】lenovolenovo-desktop:$ gcc -o 2-1.out 2-1.c2-1.c: 在函数CLIENT中:2-1.c:25: 警告: 隐式声明与内建函数exit不兼容2-1.c: 在函数SERVER中:2-1.c:37: 警告: 隐式声明与内建函数exit不兼容2-1.c: 在函数main中:2-1.c:41: 警告: main的返回类型不是intlenovolenovo-desktop:$ ./2
22、-1.out(client)sent (client)sent (client)sent (client)sent (client)sent (client)sent (client)sent (client)sent (client)sent (server)received (server)received (server)received (server)received (server)received (server)received (server)received (server)received (server)received (client)sent (server)rec
23、eived useruser-desktop:$ gcc -o bb.out 1-21.c useruser-desktop:$ ./bb.out (client)sent (client)sent (client)sent (server)received (server)received (client)sent (server)received (client)sent (server)received (client)sent (server)received (client)sent (server)received (client)sent (server)received (cl
24、ient)sent (server)received (client)sent (server)received (server)received 2、【题目】消息的创建,发送和接收【任务】【详细设计】#include<stdio.h>#include<sys/types.h>#include<sys/msg.h>#include<sys/ipc.h>#define MSGKEY 75int shmid,i;int *addr;int SHMKEY;int shmaid;void CLIENT() int i; shmid=shmget(SHMK
25、EY,1024,0777); addr=shmat(shmid,0,0); for(i=9;i>0;i-) while(*addr!=-1); printf("(client)sentn"); *addr=i; exit(0);void SERVER() shmaid=shmget(SHMKEY,1024,0777|IPC_CREAT); addr=shmat(shmid,0,0); do *addr=-1; while(*addr=-1); printf("(server)recevidn"); while(*addr); shmctl(shmi
26、d,IPC_RMID,0); exit(0);void main() int i; while(i=fork()=-1); if(!i) SERVER(); while(i=fork()=-1); if(!i) CLIENT(); wait(0); wait(0);【运行结果】设计三 存储管理设计目的:设计内容【任务】【详细设计】#define TRUE 1 #define FALSE 0 #define INVALID -1 #define NULL 0 #define total_instruction 320 #define total_vp 32 #define clear_perio
27、d 50 #include <stdio.h> typedef struct int pn,pfn,counter,time; pl_type; pl_type pltotal_vp; struct pfc_struct int pn,pfn; struct pfc_struct *next; ; typedef struct pfc_struct pfc_type; pfc_type pfctotal_vp,*freepf_head,*busypf_head, *busypf_tail; int diseffect, atotal_instruction; int pagetot
28、al_instruction, offsettotal_instruction; void initialize(); void FIFO(); void LRU(); void OPT(); void LFU(); void NUR(); main() int S,i,j; srand(getpid()*10); S=319*rand()/32767+1; for(i=0;i<total_instruction;i+=4) ai=S; ai+1=ai+1; ai+2=ai*rand()/32767; ai+3=ai+2+1; S=rand()*(318-ai+2)/32767+ai+2
29、+2; for(i=0;i<total_instruction;i+) pagei=ai%32; offseti=ai%10; for(i=4;i<=32;i+) printf("%2d page frames",i); FIFO(i); LRU(i); OPT(i); LFU(i); NUR(i); printf("n"); void FIFO(total_pf) int total_pf; int i,j; pfc_type *p,*t; initialize(total_pf); busypf_head=busypf_tail=NULL
30、; for(i=0;i<total_instruction;i+) if(plpagei.pfn=INVALID) diseffect+=1; if(freepf_head=NULL) p=busypf_head->next; plbusypf_head->pn.pfn=INVALID; freepf_head=busypf_head; freepf_head->next=NULL; busypf_head=p; p=freepf_head->next; freepf_head->next=NULL; freepf_head->pn=pagei; pl
31、pagei.pfn=freepf_head->pfn; if(busypf_tail=NULL) busypf_head=busypf_tail=freepf_head; else busypf_tail->next=freepf_head; busypf_tail=freepf_head; freepf_head=p; printf("FIFO:%6.4f",1-(float)diseffect/320); void LRU(total_pf) int total_pf; int min,minj,i,j,present_time; initialize(to
32、tal_pf);present_time=0; for(i=0;i<total_instruction;i+) if(plpagei.pfn=INVALID) diseffect+; if(freepf_head=NULL) min=32767; for(j=0;j<total_vp;j+) if(min>plj.time&&plj.pfn!=INVALID) min=plj.time;minj=j; freepf_head=&pfcplminj.pfn; plminj.pfn=INVALID; plminj.time=-1; freepf_head-
33、>next=NULL; plpagei.pfn=freepf_head->pfn; plpagei.time=present_time; freepf_head=freepf_head->next; else plpagei.time=present_time; present_time+; printf("LRU:%6.4f",1-(float)diseffect/320); void NUR(total_pf) int total_pf; int i,j,dp,cont_flag,old_dp; pfc_type *t; initialize(tota
34、l_pf); dp=0; for(i=0;i<total_instruction;i+) if(plpagei.pfn=INVALID) diseffect+; if(freepf_head=NULL) cont_flag=TRUE;old_dp=dp; while(cont_flag) if(pldp.counter=0&&pldp.pfn!=INVALID) cont_flag=FALSE; else dp+; if(dp=total_vp) dp=0; for(j=0;j<total_vp;j+) plj.counter=0; freepf_head=&
35、;pfcpldp.pfn; pldp.pfn=INVALID; freepf_head->next=NULL; plpagei.pfn=freepf_head->pfn; freepf_head=freepf_head->next; else plpagei.counter=1; if(i%clear_period=0) for(j=0;j<total_vp;j+) plj.counter=0; printf("NUR:%6.4f",1-(float)diseffect/320); void OPT(total_pf) int total_pf; i
36、nt i,j,max,maxpage,d,disttotal_vp; pfc_type *t; initialize(total_pf); for(i=0;i<total_instruction;i+) if(plpagei.pfn=INVALID) diseffect+; if(freepf_head=NULL) for(j=0;j<total_vp;j+) if(plj.pfn!=INVALID) distj=32767; else distj=0; d=1; for(j=i+1;j<total_instruction;j+) if(plpagej.pfn!=INVALI
37、D) distpagej=d; d+; max=-1; for(j=0;j<total_vp;j+) if(max<distj) max=distj; maxpage=j; freepf_head=&pfcplmaxpage.pfn; freepf_head->next=NULL; plmaxpage.pfn=INVALID; plpagei.pfn=freepf_head->pfn; freepf_head=freepf_head->next; printf("OPT:%6.4f",1-(float)diseffect/320); v
38、oid LFU(total_pf) int total_pf; int i,j,min,minpage; pfc_type *t; initialize(total_pf); for(i=0;i<total_instruction;i+) if(plpagei.pfn=INVALID) diseffect+; if(freepf_head=NULL) min=32767; for(j=0;j<total_vp;j+) if(min>plj.counter&&plj.pfn!=INVALID) min=plj.counter; minpage=j; plj.co
39、unter=0; freepf_head=&pfcplminpage.pfn; plminpage.pfn=INVALID; freepf_head->next=NULL; plpagei.pfn=freepf_head->pfn; freepf_head=freepf_head->next; else plpagei.counter+; printf("LFU:%6.4f",1-(float)diseffect/320); void initialize(total_pf) int total_pf; int i; diseffect=0; fo
40、r(i=0;i<total_vp;i+) pli.pn=i; pli.pfn=INVALID; pli.counter=0; pli.time=-1; for(i=1;i<total_pf;i+) pfci-1.next=&pfci; pfci-1.pfn=i-1; pfctotal_pf-1.next=NULL; pfctotal_pf-1.pfn=total_pf-1; freepf_head=&pfc0; 【运行结果】lenovolenovo-desktop:$ ls0.c 1-2-1.out ex2.l 操作系统课程设计 090801124 谢阳.doc0.
41、out 1-2-2.out ex4.l 公共的1-1-0.c 1-3.c ex5.l 模板1-1-0.c 1-3.out ex6.l 视频1-1-0.out 2-1.c examples.desktop 图片1-1-1.c 2-1.out jieguo.c 未命名文件夹1-1-1.out 2-2.c lex.1 文档1-1-2.c 2-2.out lex.yy.c 新文件1-1-2.out 3-1.c lex.yy.o 新文件lexi1-1-3.c 3-1.c regn 音乐1-1-3.out ex1file testfile 桌面1-2-1.c ex1.l testfilelenovolen
42、ovo-desktop:$ gcc -o 3-1.out 3-1.clenovolenovo-desktop:$ ./3-1.out 4 page framesFIFO:0.5719LRU:0.5781OPT:0.6188LFU:0.5813NUR:0.5938 5 page framesFIFO:0.6094LRU:0.6031OPT:0.6375LFU:0.6031NUR:0.5969 6 page framesFIFO:0.6250LRU:0.6281OPT:0.6531LFU:0.6094NUR:0.6281 7 page framesFIFO:0.6312LRU:0.6438OPT:
43、0.6625LFU:0.6219NUR:0.6438 8 page framesFIFO:0.6344LRU:0.6469OPT:0.6719LFU:0.6375NUR:0.6375 9 page framesFIFO:0.6500LRU:0.6531OPT:0.6875LFU:0.6438NUR:0.662510 page framesFIFO:0.6656LRU:0.6594OPT:0.7031LFU:0.6625NUR:0.668711 page framesFIFO:0.6750LRU:0.6750OPT:0.7250LFU:0.6813NUR:0.678112 page frames
44、FIFO:0.6875LRU:0.6813OPT:0.7344LFU:0.6937NUR:0.671913 page framesFIFO:0.7094LRU:0.6969OPT:0.7438LFU:0.7031NUR:0.703114 page framesFIFO:0.7094LRU:0.7000OPT:0.7562LFU:0.7188NUR:0.718815 page framesFIFO:0.7312LRU:0.7031OPT:0.7656LFU:0.7344NUR:0.746916 page framesFIFO:0.7312LRU:0.7094OPT:0.7719LFU:0.7438NUR:0.740617 page framesFIFO:0.7531LRU:0.7281OPT:0.7844LFU:
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 质量保证协议书
- 装修返点协议书
- 自然灾害协议书
- 总承包合同范本
- 屋基调换协议书
- 艺校合作协议书
- 小孩周岁协议书
- 舞团合伙协议书
- 闸机购买合同范本
- 英语短语协议书
- 生化肝功项目解读课件
- 北京林业大学《线性系统理论基础》2025-2026学年第一学期期末试卷
- 2025贵州六盘水市水城区招聘城市社区工作者162人备考考点题库及答案解析
- 2025年山东省检察院书记员考试试题及答案
- 2025天津大学管理岗位集中招聘15人笔试考试参考题库及答案解析
- 外卖运营面试攻略与技巧全解析
- 2025浙江杭州地铁商业经营管理有限公司招聘11人(第四批)笔试历年参考题库附带答案详解
- 2025年人工智能培训项目可行性研究报告及总结分析
- 小班数学课件《挂灯笼》课件
- 安全三日管理制度
- 居间服务费合同(标准版)
评论
0/150
提交评论