#include<stdio.h>
#include<time.h>
#include<stdlib.h>
typedef struct prc
{
int ch;
int l;
int s;
struct prc *last;
struct prc *next;
}pr;
pr *get(pr *p);
int n=0;
int main()
{
int i,B[100],q1,j=0,k,flag=0,k1,ji1=0,ji2=0;
int A[100],ch;
tm *t_m1,*t_m2;
pr *p,*q,*head;
time_t a,t;
long fa1,fa2;
printf(“input the number of the process that you want to run:\n”);
scanf(“%d”,&n);
printf(“input the time sheet(cpu时间片长度):\n”);
scanf(“%d”,&q1);
printf(“input the name and the time of processes:\n press enter to STOP\n”);
for(i=0;i<n;i++)
{
printf(“进程编号,运行时长—”);
scanf(“%d”,&A[i]);
scanf(“%d”,&B[i]);
}
if(j==0)
{
p=(pr *)malloc(sizeof(pr));
head=p;
p->ch=A[0];
p->l=B[0];
p->s=0;
p->last=NULL;
p->next=NULL;
q=p;
j++;
}
while(j>0&&n>1)
{
p=(pr *)malloc(sizeof(pr));
q->next=p;
p->last=q;
p->s=0;
p->ch=A[j];
p->l=B[j];
q=p;
j++;
if(j==n)break;
}
p->next=head;
head->last=p;
system(“cls”);
/*
p=head;
while(head->last!=p)
{
printf(“%d\n”,head->ch);
p=p->next;
}
*/
p=head;
while(p->next!=p)
{
time(&a);
fa1=a;
t_m1=localtime(&a);
ji1=t_m1->tm_sec;
printf(“procc %d is runing!\n”,p->ch/*,fa1,p->s*/);
k=0;
k1=0;
for(;;)
{ flag=0;
time(&t);
fa2=t;
t_m2=localtime(&t);
ji2=t_m2->tm_sec;
// printf(“fa2 %d\n”,fa2);
if(fa2==fa1+1+k1){(p->s)++;k++;k1++;/*printf(” p->s %d p->l %d\n”,p->s,p->l);*/}
if(k==q1){flag=1;/*printf(“k–q1 %d\n”,k);*/time(&t);fa2=t;/*printf(“ fa2 %d\n”,fa2);*/}
if(p->s==p->l)
{
printf(“process %d over\n”,p->ch);
p=get(p);
break;
}
if(flag==1)break;
}
system(“cls”);
p=p->next;
}
time(&a);
t_m1=localtime(&a);
ji1=t_m1->tm_sec;
printf(“procc %d is runing! time–%d\n”,p->ch,fa1);
k=0;
for(;;)
{ flag=0;
time(&t);
t_m2=localtime(&t);
ji2=t_m2->tm_sec;
if(ji2==ji1+1){(p->s)++;k++;printf(” p->s %d p->l %d\n”,p->s,p->l);}
if(k==q1){flag=1;/*time(&t);fa2=t;printf(“ fa2 %d\n”,fa2);*/}
if(p->s==p->l)
{
printf(“process %d over\n”,p->ch);
break;
}
if(flag==1)break;
}
// system(“cls”);
return 0;
}
pr *get(pr *p)
{
pr *q;
q=p->next;
p->last->next=p->next;
p->last->next->last=p->last;
free(p);
return q;
}
>> 本文固定链接: http://www.vcgood.com/archives/2022
>> 转载请注明: jinfan1009 2007年12月16日 于 C语言帝国 发表