首页 实验一: 进程的创建与同步

实验一: 进程的创建与同步

举报
开通vip

实验一: 进程的创建与同步实验一:进程的创建与同步 1实验目的 (1) 熟悉Windows系统提供的进程创建与同步。 (2) 掌握Windows系统环境下进程的创建与撤销方法。 2 进程创建 2.1结构体模拟进程PCB typedef struct pcb { int num; struct pcb *next; int priority; int timeneed; int state; }pcb;/*用此结构体来模拟一个进程*/ 2.2 随机产生进程运行的优先级与运行时间 srand((unsigned ...

实验一: 进程的创建与同步
实验一:进程的创建与同步 1实验目的 (1) 熟悉Windows系统提供的进程创建与同步。 (2) 掌握Windows系统环境下进程的创建与撤销方法。 2 进程创建 2.1结构体模拟进程PCB typedef struct pcb { int num; struct pcb *next; int priority; int timeneed; int state; }pcb;/*用此结构体来模拟一个进程*/ 2.2 随机产生进程运行的优先级与运行时间 srand((unsigned int)time(NULL)); for(i=1;i<=n;i++)/*用循环来创建指定个结点*/ { // q=(pcb *)malloc(sizeof(pcb)); p->next=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->timeneed>10) /*进程需要时间减少*/ run->timeneed=run->timeneed-run->timeneed/10; else run->timeneed--; run->priority=run->priority-1;/*进程优先级减1*/ if(run->priority<=head->next->priority)/*如果当前运行的进程的优先级低于队首进程的优先级*/ { run->state=WAIT; run=getmaxpriority(head);/*则从进程队列中挑选一个优先级最大的进程来运行*/ run->state=RUN; } 4 进程删除 void delect(struct pcb *head,struct pcb *run)/*此函数用来将运行完成的进程删除出进程队列*/ 5实验总结 …… 6源程序框架 #include"stdio.h" #include"stdlib.h" #include "string.h" #include "ctime" #include #include #define WAIT 1 #define RUN 2 #define FINISH 3 using 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));/*创建一个空 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 头*/ p=head; srand((unsigned int)time(NULL));// for(i=1;i<=n;i++)/*用循环来创建指定个结点*/ { q=(pcb *)malloc(sizeof(pcb)); p->next=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->priority>max)/*逐一比较,选出优先级最大的结点*/ {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->timeneed>0)/*如果当前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->timeneed>10) run->timeneed=run->timeneed-run->timeneed/10;/*进程需要时间减少*/ else run->timeneed--; run->priority=run->priority-1;/*进程优先级减1*/ cout<timeneed; cout<<"\t"; cout<priority; cout<<"\n"; if(i==5) { i=0; getchar();} else i++; } if(run->timeneed!=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)/*由flag的值判断是否继续执行control()函数*/ { if(head->next)/*判断进程是否完成*/ control(); else flag=0; } getchar(); } 7 实验内容改进: ●在进程结构中增加进程到达时间; ●增加信号量机制,设置不同的进程的运行时序关系,通过PV操作实现同步; ●使用信号量使两个进程实现互斥运行。
本文档为【实验一: 进程的创建与同步】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_083599
暂无简介~
格式:doc
大小:27KB
软件:Word
页数:0
分类:理学
上传时间:2020-03-08
浏览量:2