实验一:进程的创建与同步
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操作实现同步;
●使用信号量使两个进程实现互斥运行。