| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 516 人关注过本帖
标题:[求助]看看这个
只看楼主 加入收藏
平晓飞
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-10-22
收藏
 问题点数:0 回复次数:1 
[求助]看看这个
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define Max 20
typedef struct Process//保存初始化创建的十个进程,所有的数据成员都是随机生成的,有三个状态,0是立刻进入运行的进程,1是等待的进程,2是就绪的进程
{
int pid;
int prior;
int state; /* 0 - running,1 - waiting,2 - ready */
int time;
struct Process *next;
};
struct Process *head = NULL;
struct Process *end = NULL;
struct Running//保存运行的进程
{
int pid;
int prior;
int time;
struct Running *next;
};
struct Running *rhead = NULL;
struct Running *rend = NULL;
struct Waiting//保存等待的进程
{
int pid;
int prior;
int time;
int state; /* 0 - io, 1 - read, 2 - break */
struct Waiting *next;
};
struct Waiting *whead = NULL;
struct Waiting *wend = NULL;
struct Ready//保存就绪的进程
{
int pid;
int prior;
int time;
struct Ready *next;
};
struct Ready *rehead = NULL;
struct Ready *reend = NULL;
void create()//创建进程,每调用一次就创建一个进程,保存在struct Process中
{
struct Process *ps;
ps = (struct Process *)malloc(sizeof(struct Process));
if (head == NULL)
head = end = ps;
else
end->next = ps;
end = ps;
end->next = NULL;
ps->pid = rand()%10000;
/*printf("input process's name:\n");
scanf("%s",ps->name);
ps->name = "Process";*/
ps->prior = rand()%100;
/*printf("input process's state:\n");
printf("0 - running, 1 - waiting, 2 - ready\n");
scanf("%d",&ps->state);*/
ps->state = rand()%3;
ps->time = rand()%20;
ps->prior = ps->prior + ps->time;
}
void fenlei()//将create()创建的进程分配到Running,Waiting,Ready中
{
struct Process *p;
struct Running *ps1;
struct Waiting *ps2;
struct Ready *ps3;
p = head;
while(p)
{
if (p->state == 0 )
{
ps1 = (struct Running *)malloc(sizeof(struct Running));
if (rhead == NULL)
rhead = rend =ps1;
else
rend->next = ps1;
rend = ps1;
rend->next = NULL;
ps1->pid = p->pid;
ps1->prior = p->prior;
ps1->time = p->time;
}
if (p->state == 1)
{
ps2 = (struct Waiting *)malloc(sizeof(struct Waiting));
if (whead == NULL)
whead = wend =ps2;
else
wend->next = ps2;
wend = ps2;
wend->next = NULL;
ps2->pid = p->pid;
ps2->prior = p->prior;
ps2->state = rand()%3;
ps2->time = p->time;
}
if (p->state == 2 )
{
ps3 = (struct Ready *)malloc(sizeof(struct Ready));
if (rehead == NULL)
rehead = reend =ps3;
else
reend->next = ps3;
reend = ps3;
reend->next = NULL;
ps3->pid = p->pid;
ps3->prior = p->prior;
ps3->time = p->time;
}
p = p->next;
}
}
void delay1s()//延时函数
{
unsigned char h, i, j, k;
for(h = 5; h > 0; h--)
for(i = 4; i > 0; i--)
for(j = 116; j > 0; j--)
for(k = 214; k > 0; k--);
}
void runsort()//用冒泡排序法对Running排序
{
struct Running *p, *q;
int tempprior, temptime, temppid;
p = rhead;
while(p)
{
q = p->next;
while(q)
{
if (p->prior < q->prior)
{
tempprior = p->prior;
p->prior = q->prior;
q->prior = tempprior;
temptime = p->time;
p->time = q->time;
q->time = temptime;
temppid = p->pid;
p->pid = q->pid;
q->pid = temppid;
}
q = q->next;
}
p = p->next;
}
}
void waitsort()//用冒泡排序法对Waiting排序
{
struct Waiting *p, *q;
int tempprior, temptime, temppid, tempstate;
p = whead;
while(p)
{
q = p->next;
while(q)
{
if (p->prior < q->prior)
{
tempprior = p->prior;
p->prior = q->prior;
q->prior = tempprior;
temptime = p->time;
p->time = q->time;
q->time = temptime;
temppid = p->pid;
p->pid = q->pid;
q->pid = temppid;
tempstate = p->state;
p->state = q->state;
q->state = tempstate;
}
q = q->next;
}
p = p->next;
}
}
void readysort()//用冒泡排序法对Ready排序
{
struct Ready *p, *q;
int tempprior, temptime, temppid;
p = rehead;
while(p)
{
q = p->next;
while(q)
{
if (p->prior < q->prior)
{
tempprior = p->prior;
p->prior = q->prior;
q->prior = tempprior;
temptime = p->time;
p->time = q->time;
q->time = temptime;
temppid = p->pid;
p->pid = q->pid;
q->pid = temppid;
}
q = q->next;
}
p = p->next;
}
}
void showprocess()//将Running,Waiting,Ready的成员全部列出来
{
struct Running *p1;
struct Waiting *p2;
struct Ready *p3;
runsort();
p1 = rhead;
printf("Running Process:\n\n");
printf(" Pid Prior Time\n");
while(p1)
{
printf("%6d%6d%6d\n", p1->pid, p1->prior, p1->time);
p1 = p1->next;
}
printf("\n\n");
printf("Waiting Process: 0 -- IO 1 -- Read 2 -- Break\n\n");
printf(" Pid Prior state Time\n");
waitsort();
p2 = whead;
while(p2)
{
printf("%6d%6d%6d%6d\n", p2->pid, p2->prior, p2->state, p2->time);
p2 = p2->next;
}
printf("\n\n");
printf("Ready Process:\n\n");
printf(" Pid Prior Time\n");
readysort();
p3 = rehead;
while(p3)
{
printf("%6d%6d%6d\n", p3->pid, p3->prior, p3->time);
p3 = p3->next;
}
}
void updaterun()//时间和优先权减一
{
struct Running *p;
p = rhead;
while(p)
{
p->prior--;
p->time--;
p = p->next;
}
}
void updatewait()//时间和优先权减一
{
struct Waiting *p;
p = whead;
while(p)
{
p->prior--;
p->time--;
p = p->next;
}
}
void updateready()//时间和优先权减一
{
struct Ready *p;
p = rehead;
while(p)
{
p->prior--;
p->time--;
p = p->next;
}
}
void checkrun()//检查Running的进程时间是否为0,为0就转入到Waiting
{
struct Running *p, *temp, *tp;
struct Waiting *q, *s;
p = rhead;
while(p)
{
temp = rhead;
s = whead;
if (p->time <= 0)
{
p->prior = rand()%100;
p->time = rand()%20;
q = (struct Waiting *)malloc(sizeof(struct Waiting));
q->pid = p->pid;
q->prior = p->prior;
q->time = p->time;
q->state = rand()%3;
q->prior = q->prior + q->time;
if (s != NULL)
{
while(s->next)
{
s = s->next;
}
s->next = q;
}
else
{
whead = q;
}
q->next = NULL;
if (p == rhead)
{
rhead = p->next;
/*delete p;*/
/*p = rhead;*/
if (rhead == NULL)
return; /*p->next = NULL;*/
}
while(temp)
{
if (temp->next == p)
{
tp = temp->next;
temp->next = tp->next;
/*p->next = temp
//delete p;
//p = temp; */
}
temp = temp->next;
}
}
p = p->next;
}
}
void checkwait()//检查Waiting的进程时间是否为0,为0就转入到Ready
{
struct Waiting *p, *temp, *tp;
struct Ready *q, *s;
p = whead;
while(p)
{
temp = whead;
s = rehead;
if (p->time <= 0)
{
p->prior = rand()%100;
p->time = rand()%20;
q = (struct Ready *)malloc(sizeof(struct Ready));
q->pid = p->pid;
q->prior = p->prior;
q->time = p->time;
q->prior = q->prior + q->time; /*q->state = rand()%3;*/
if (s != NULL)
{
while(s->next)
{
s = s->next;
}
s->next = q;
}
else
{
rehead = q;
}
q->next = NULL;
if (p == whead)
{
whead = p->next;
if (whead == NULL)
return;
/*delete p;
//p = whead; */
}
while(temp)
{
if (temp->next == p)
{
tp = temp->next;
temp->next =tp->next;
/*delete p;
//p = temp; */
}
temp = temp->next;
}
}
p = p->next;
}
}
void checkready()//检查Ready的进程时间是否为0,为0就转入到Running
{
struct Ready *p, *temp, *tp;
struct Running *q, *s;
p = rehead;
while(p)
{
temp = rehead;
s = rhead;
if (p->time <= 0)
{
p->prior = rand()%100;
p->time = rand()%20;
q = (struct Running *)malloc(sizeof(struct Running));
q->pid = p->pid;
q->prior = p->prior;
q->time = p->time;
q->prior = q->prior + q->time; /*q->state = rand()%3; */
if (s != NULL)
{
while(s->next)
{
s = s->next;
}
s->next = q;
}
else
{
rhead = q;
}
q->next = NULL;
if (p == rehead)
{
rehead = p->next;
if (rehead == NULL)
return;
/*delete p;
//p = rhead;
//p->next = NULL;*/
}
while(temp)
{
if (temp->next == p)
{
tp = temp->next;
temp->next = tp->next;
/*p->next = temp
//delete p;
//p = temp; */
}
temp = temp->next;
}
}
p = p->next;
}
}
void main()
{
/*Process *p;
//Running *prun;
//Waiting *pwait;
//Ready *pready; */
int i;
srand(time(NULL));
for (i = 0; i < 10; i++)
create();
fenlei();
while(1)
{
system("cls");
showprocess();
checkrun();
checkwait();
checkready();
updaterun();
updatewait();
updateready();
for (i = 0; i < 1000; i++)
delay1s();
}
}
搜索更多相关主题的帖子: head next Running running waiting 
2006-12-29 15:25
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

这个是题目.

[求助]怎样实现进程并发操作系统程序作业题目:
实验二:

编写一个C语言程序,模拟正在运行的进程从运行状态转为等待状态。等待队列有3个(等待输入输出的,等待读写硬盘的,等待中断完成的)
要求:
1。数据结构自己定义
2。最后效果:能看出来正在运行的进程进入等待队列

实验三:
把等待状态的进程转化为就绪状态
要求:就绪队列只有一个,等待队列是第二次实验的等待序列

实验四:
把就绪状态的进程转化为运行状态
要求:和前两个实验,组成一个完整的程序,要有流程图和实验报告


倚天照海花无数,流水高山心自知。
2006-12-29 15:33
快速回复:[求助]看看这个
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017732 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved