重庆大学操作系统实验3报告书.doc_第1页
重庆大学操作系统实验3报告书.doc_第2页
重庆大学操作系统实验3报告书.doc_第3页
重庆大学操作系统实验3报告书.doc_第4页
重庆大学操作系统实验3报告书.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

操作系统实验报告年级、专业、班级姓名实验题目多线程和多进程的管理实验时间实验地点实验成绩 实验性质验证性 设计性 综合性教师评价:算法/实验过程正确;源程序/实验内容提交 程序结构/实验步骤合理;实验结果正确; 语法、语义正确; 报告规范; 其他: 评价教师签名:一、实验目的了解操作系统中常见的进程调度算法了解在linux中利用多线程模拟实现FCFS,SJF,RR的调度过程。了解进程同步的特点,掌握利用信号量实现进程间同步的的方法。了解哲学家问题中进程之间的相互制约关系,能够合理的设置信号量。了解Linux系统下创建多线程的原理及使用方法,模拟哲学家问题的实现。二、实验项目内容一:本实验利用线程模拟进程,实现进程间的调度算法。主要有以下内容:创建主线程,主线程的工作包括:创建子线程,保存子线程的虚拟PCB ;并负责子线程的调度。调度的基本时间单位为1s。主线程创建20个子线程,分别实现FCFS调度、SJF调度、RR调度、优先级调度和多级队列调度,并且计算每个调度的平均等待时间。(其中优先级调度和多级队列调度为选做)。对于每个子线程,在其运行期间,输出其占用的时间标号(例如,第3个线程占用了第10秒的CPU时间,输出为:“Thread3:10”)。二:1 根据哲学家进程间的相互制约关系,设置合理的信号量及信号量初值。2 创建5个线程,分别模拟5个哲学家进程。3 在哲学家线程之间通过对信号量的P,V操作,实现线程之间的同步关系。三、 实验过程或算法(源程序)FCFS.c:#include#include#include#include#include#define N 20struct PCBpthread_t threadId;int threadNum;int arrivalTime;int burstTime;int priority;struct PCB pN;int runTime=0;int waitTimeListN=0;void* thread(void* in)int i,num;for(i=0;iN;i+)if(pi.threadId=pthread_self()num=i;break;waitTimeListnum=runTime;runTime+=pnum.burstTime;printf(Thread%d:%dn,pnum.threadNum,runTime);pthread_exit(void*)0);int main()srand(unsigned)time(NULL);int i,j;for(i=0;iN;i+)pi.arrivalTime=rand()%201;pi.burstTime=rand()%11+1;pi.priority=rand()%6+1;pi.threadNum=i;for(i=0;iN;i+)printf(Thread%d:arrival time:%d,burst time:%dn,pi.threadNum,pi.arrivalTime,pi.burstTime);struct PCB temp;for(i=0;ii;j-)if(pj.arrivalTimepj-1.arrivalTime)temp=pj;pj=pj-1;pj-1=temp;for(i=0;iN;i+)pthread_create(&pi.threadId,NULL,(void*)thread,NULL);usleep(N);for(i=0;iN;i+)pthread_join(pi.threadId,NULL);int sum=0;for(i=0;iN;i+)sum+=waitTimeListi;printf(Average wait time=%dn,sum/N);return 0;SJF.c:#include#include#include#include#include#define N 20struct PCBpthread_t threadId;int threadNum;int arrivalTime;int burstTime;int priority;struct PCB pN;int runTime=0;int waitTimeListN=0;void* thread(void* in)int i,num;for(i=0;iN;i+)if(pi.threadId=pthread_self()num=i;break;waitTimeListnum=runTime;runTime+=pnum.burstTime;printf(Thread%d:%dn,pnum.threadNum,runTime);pthread_exit(void*)0);int main()srand(unsigned)time(NULL);int i,j;for(i=0;iN;i+)pi.arrivalTime=rand()%201;pi.burstTime=rand()%11+1;pi.priority=rand()%6+1;pi.threadNum=i;for(i=0;iN;i+)printf(Thread%d:arrival time:%d,burst time:%dn,pi.threadNum,pi.arrivalTime,pi.burstTime);struct PCB temp;for(i=0;ii;j-)if(pj.burstTimepj-1.burstTime)temp=pj;pj=pj-1;pj-1=temp;for(i=0;iN;i+)pthread_create(&pi.threadId,NULL,(void*)thread,NULL);usleep(N);for(i=0;iN;i+)pthread_join(pi.threadId,NULL);int sum=0;for(i=0;iN;i+)sum+=waitTimeListi;printf(Average wait time=%dn,sum/N);return 0;RR.c:#include#include#include#include#include#define N 20struct PCBpthread_t threadId;int threadNum;int arrivalTime;int burstTime;int priority;struct PCB pN;int runTime=0;int waitTimeListN=0;void* thread(void* in)int i,num;for(i=0;iN;i+)if(pi.threadId=pthread_self()num=i;break;while(pnum.burstTime!=0)pnum.burstTime-;waitTimeListnum=runTime;runTime+;printf(Thread%d:%dn,pnum.threadNum,runTime);usleep(N*20);pthread_exit(void*)0);int main()srand(unsigned)time(NULL);int i,j;for(i=0;iN;i+)pi.arrivalTime=rand()%201;pi.burstTime=rand()%11+1;pi.priority=rand()%6+1;pi.threadNum=i;for(i=0;iN;i+)printf(Thread%d:arrival time:%d,burst time:%dn,pi.threadNum,pi.arrivalTime,pi.burstTime);struct PCB temp;for(i=0;ii;j-)if(pj.arrivalTimepj-1.arrivalTime)temp=pj;pj=pj-1;pj-1=temp;for(i=0;iN;i+)pthread_create(&pi.threadId,NULL,(void*)thread,NULL);usleep(3);for(i=0;iN;i+)pthread_join(pi.threadId,NULL);int sum=0;for(i=0;iN;i+)sum+=waitTimeListi;printf(Average wait time=%dn,sum/N);return 0;sy3b.c:#include#include#include#include#define N 5pthread_t philosopherIDN;/存放哲学家线程IDpthread_mutex_t chopsticksN;/信号量int room=0;/请求用餐的哲学家人数int sw(pthread_t id)if(id=philosopherID0)return 0;else if(id=philosopherID1)return 1;else if(id=philosopherID2)return 2;else if(id=philosopherID3)return 3;else if(id=philosopherID4)return 4;return -1;void* eat_think(void* in)int left,right,num,op;op=sw(pthread_self();switch(op)case 0:num=0;left=0;right=4;break;case 1:num=1;left=1;right=0;break;case 2:num=2;left=2;right=1;break;case 3:num=3;left=3;right=2;break;case 4:num=4;left=4;right=3;break;while(1)printf(philosopher %d is thinking.n,num);usleep(N);pthread_mutex_lock(&chopsticksleft);if(pthread_mutex_trylock(&chopsticksright)!=0)pthread_mutex_unlock(&chopsticksleft);room+;/printf(%d philosopher is waitingn,room);continue;printf(philosopher %d is eating.n,num);if(room0)room-;usleep(N);pthread_mutex_unlock(&chopsticksright);pthread_mutex_unlock(&chopsticksleft);printf(philosopher %d is thinking.n,num);pthread_exit(void*)0);int main()int i;for(i=0;iN;i+)pthread_mutex_init(&chopsticksi,NULL);for(i=0;iN;i+)pthread_create(&philosopherIDi,NULL,eat_think,NULL);while(1)sleep(N);return 0;四、 实验结果及分析和(或)源程序调试过程结果截图:FCFS:SJF:RR:sy3b:(部分截图)遇到的问题:1.进程调度实验通过定义一个虚拟PCB保存线程的各种信息,具体信息以随机数的方式存储,对FCFS与SJF调度,主线程用冒泡排序以线程的虚拟PCB中的arrivalTime与burstTime为对象排序了虚拟PCB并以此决定先运行哪

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论