实验一: 进程的创建与同步.doc_第1页
实验一: 进程的创建与同步.doc_第2页
实验一: 进程的创建与同步.doc_第3页
实验一: 进程的创建与同步.doc_第4页
实验一: 进程的创建与同步.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

实验一: 进程的创建与同步1实验目的(1) 熟悉Windows系统提供的进程创建与同步。(2) 掌握Windows系统环境下进程的创建与撤销方法。2 进程创建2.1结构体模拟进程PCBtypedef struct pcb int num; struct pcb *next; int priority; int timeneed; int state;pcb;/*用此结构体来模拟一个进程*/2.2 随机产生进程运行的优先级与运行时间srand(unsigned int)time(NULL);for(i=1;inext=q; q-num=i; q-next=NULL; q-priority=1+(10*rand()/(RAND_MAX+1.0);/*随机产生优先级*/ q-timeneed=1+(100*rand()/(RAND_MAX+1.0);/*随机产生运行时间*/ q-state=WAIT; p=q; 3 进程的执行和调度void control()/*此函数是用来控制各个进程的执行和调度*/if(run-timeneed10) /*进程需要时间减少*/run-timeneed=run-timeneed-run-timeneed/10;elserun-timeneed-;run-priority=run-priority-1;/*进程优先级减1*/if(run-prioritynext-priority)/*如果当前运行的进程的优先级低于队首进程的优先级*/run-state=WAIT;run=getmaxpriority(head);/*则从进程队列中挑选一个优先级最大的进程来运行*/run-state=RUN;4 进程删除void delect(struct pcb *head,struct pcb *run)/*此函数用来将运行完成的进程删除出进程队列*/5实验总结6源程序框架#includestdio.h#includestdlib.h#include string.h#include ctime#include #include#define WAIT 1#define RUN 2#define FINISH 3using namespace std;typedef struct pcb int num; struct pcb *next; int priority; int timeneed; int state;pcb;/*用此结构体来模拟一个进程*/struct pcb *head;struct pcb *run;pcb *jccreat(int n)/*此函数用于创建进程队列*/ int i=1; pcb *head,*p,*q; head=(pcb *)malloc(sizeof(pcb);/*创建一个空表头*/ p=head; srand(unsigned int)time(NULL);/ for(i=1;inext=q; q-num=i; q-next=NULL; q-priority=1+(10*rand()/(RAND_MAX+1.0);/*随机产生优先级*/ q-timeneed=1+(100*rand()/(RAND_MAX+1.0);/*随机产生运行时间*/ q-state=WAIT; p=q; return head;/*返回表头指针*/pcb *getmaxpriority(struct pcb *head)/*此函数用来挑选一个优先级最大的进程来执行*/ struct pcb *p,*q; int max; p=head-next; max=p-priority;/*初始max为队首结点的优先级*/ q=p; while(p) if(p-prioritymax)/*逐一比较,选出优先级最大的结点*/ max=p-priority; q=p;/? p=p-next; return q;void delete1(struct pcb *head,struct pcb *run)/*此函数用来将运行完的进程删除出进程队列*/struct pcb *q=head; while(q-next)/*扫描进程队列,找到执行完了的进程*/if(q-next-num=run-num)/*判断是不是已完成的进程*/if(run-next!=NULL)q-next=run-next;else q-next=NULL;free(run);/*释放申请的空间*/return;q=q-next; int i=0;void control()/*此函数是用来控制各个进程的执行和调度*/struct pcb *p;run=head-next;/*初始让第一个进程运行*/run-state=RUN;while(run)if(run-timeneed0)/*如果当前run指针指向的进程所需时间不为零,状态为运行状态,就让这个进程运行*/if(run-state=RUN)printf(pcb %d is running.n,run-num);printf(Waiting list:);/*显示整个等待队列*/p=head-next;while(p)if(p!=run)printf(pcb%d ,p-num);p=p-next;printf(n);Sleep(500);/*模拟进程运行*/if(run-timeneed10)run-timeneed=run-timeneed-run-timeneed/10;/*进程需要时间减少*/elserun-timeneed-;run-priority=run-priority-1;/*进程优先级减1*/couttimeneed;coutt;coutpriority;couttimeneed!=0) if(run!=getmaxpriority(head)/*如果当前运行进程的优先级不等于队列中最大的进程优先级*/ if(run-prioritypriority)/作用不同run-state=WAIT;run=getmaxpriority(head);/*则从进程队列中挑选一个优先级最大的进程来运行*/run-state=RUN;else printf(pcb %d is finished.n,run-num); Sleep(500); delete1(head,run);/*删除该结点*/ if(head-next!=NULL)/*判断进程队列是不是为空*/ run=head-next; run-state=RUN; else printf(All progresses are done.n); return; main()int n;int flag=1;printf(Enter the number of the progresses:);scanf(%d,&n);/*输入要创建的进程的数量*/head=jccreat(n);/*创建进程队列,将链表的表头赋给head指针*/run=head-next;/*run指针指向正在运行的进程的pcb*/while(run)printf(num: %d ,priority: %d ,timeneeds: %d n,run-num,run-priority,run-timeneed); run=run-next; /*将刚创建的进程队列打印出来*/while(flag)/*由fla

温馨提示

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

评论

0/150

提交评论